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.
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"
16 CompleteParserRecorder::CompleteParserRecorder()
17 : function_store_(0) {
18 preamble_[PreparseDataConstants::kMagicOffset] =
19 PreparseDataConstants::kMagicNumber;
20 preamble_[PreparseDataConstants::kVersionOffset] =
21 PreparseDataConstants::kCurrentVersion;
22 preamble_[PreparseDataConstants::kHasErrorOffset] = false;
23 preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0;
24 preamble_[PreparseDataConstants::kSizeOffset] = 0;
25 DCHECK_EQ(5, PreparseDataConstants::kHeaderSize);
32 void CompleteParserRecorder::LogMessage(int start_pos,
36 bool is_reference_error) {
37 if (HasError()) return;
38 preamble_[PreparseDataConstants::kHasErrorOffset] = true;
39 function_store_.Reset();
40 STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
41 function_store_.Add(start_pos);
42 STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
43 function_store_.Add(end_pos);
44 STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
45 function_store_.Add((arg_opt == NULL) ? 0 : 1);
46 STATIC_ASSERT(PreparseDataConstants::kIsReferenceErrorPos == 3);
47 function_store_.Add(is_reference_error ? 1 : 0);
48 STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 4);
49 WriteString(CStrVector(message));
50 if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
54 void CompleteParserRecorder::WriteString(Vector<const char> str) {
55 function_store_.Add(str.length());
56 for (int i = 0; i < str.length(); i++) {
57 function_store_.Add(str[i]);
62 ScriptData* CompleteParserRecorder::GetScriptData() {
63 int function_size = function_store_.size();
64 int total_size = PreparseDataConstants::kHeaderSize + function_size;
65 unsigned* data = NewArray<unsigned>(total_size);
66 preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
67 MemCopy(data, preamble_, sizeof(preamble_));
68 if (function_size > 0) {
69 function_store_.WriteTo(Vector<unsigned>(
70 data + PreparseDataConstants::kHeaderSize, function_size));
72 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment));
73 ScriptData* result = new ScriptData(reinterpret_cast<byte*>(data),
74 total_size * sizeof(unsigned));
75 result->AcquireDataOwnership();
80 } } // namespace v8::internal.