a66a1adcf9b6bd7eb6d12735d231843657e70287
[platform/upstream/nodejs.git] / deps / v8 / src / preparse-data.cc
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/base/logging.h"
6 #include "src/compiler.h"
7 #include "src/globals.h"
8 #include "src/hashmap.h"
9 #include "src/preparse-data.h"
10 #include "src/preparse-data-format.h"
11
12 namespace v8 {
13 namespace internal {
14
15
16 CompleteParserRecorder::CompleteParserRecorder() {
17   preamble_[PreparseDataConstants::kMagicOffset] =
18       PreparseDataConstants::kMagicNumber;
19   preamble_[PreparseDataConstants::kVersionOffset] =
20       PreparseDataConstants::kCurrentVersion;
21   preamble_[PreparseDataConstants::kHasErrorOffset] = false;
22   preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0;
23   preamble_[PreparseDataConstants::kSizeOffset] = 0;
24   DCHECK_EQ(5, PreparseDataConstants::kHeaderSize);
25 #ifdef DEBUG
26   prev_start_ = -1;
27 #endif
28 }
29
30
31 void CompleteParserRecorder::LogMessage(int start_pos,
32                                         int end_pos,
33                                         const char* message,
34                                         const char* arg_opt,
35                                         bool is_reference_error) {
36   if (HasError()) return;
37   preamble_[PreparseDataConstants::kHasErrorOffset] = true;
38   function_store_.Reset();
39   STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
40   function_store_.Add(start_pos);
41   STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
42   function_store_.Add(end_pos);
43   STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
44   function_store_.Add((arg_opt == NULL) ? 0 : 1);
45   STATIC_ASSERT(PreparseDataConstants::kIsReferenceErrorPos == 3);
46   function_store_.Add(is_reference_error ? 1 : 0);
47   STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 4);
48   WriteString(CStrVector(message));
49   if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
50 }
51
52
53 void CompleteParserRecorder::WriteString(Vector<const char> str) {
54   function_store_.Add(str.length());
55   for (int i = 0; i < str.length(); i++) {
56     function_store_.Add(str[i]);
57   }
58 }
59
60
61 ScriptData* CompleteParserRecorder::GetScriptData() {
62   int function_size = function_store_.size();
63   int total_size = PreparseDataConstants::kHeaderSize + function_size;
64   unsigned* data = NewArray<unsigned>(total_size);
65   preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
66   MemCopy(data, preamble_, sizeof(preamble_));
67   if (function_size > 0) {
68     function_store_.WriteTo(Vector<unsigned>(
69         data + PreparseDataConstants::kHeaderSize, function_size));
70   }
71   DCHECK(IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment));
72   ScriptData* result = new ScriptData(reinterpret_cast<byte*>(data),
73                                       total_size * sizeof(unsigned));
74   result->AcquireDataOwnership();
75   return result;
76 }
77
78
79 } }  // namespace v8::internal.