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/globals.h"
7 #include "src/hashmap.h"
8 #include "src/parsing/parser.h"
9 #include "src/parsing/preparse-data.h"
10 #include "src/parsing/preparse-data-format.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 
CompleteParserRecorder()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 
LogMessage(int start_pos,int end_pos,MessageTemplate::Template message,const char * arg_opt,ParseErrorType error_type)31 void CompleteParserRecorder::LogMessage(int start_pos, int end_pos,
32                                         MessageTemplate::Template message,
33                                         const char* arg_opt,
34                                         ParseErrorType error_type) {
35   if (HasError()) return;
36   preamble_[PreparseDataConstants::kHasErrorOffset] = true;
37   function_store_.Reset();
38   STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
39   function_store_.Add(start_pos);
40   STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
41   function_store_.Add(end_pos);
42   STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
43   function_store_.Add((arg_opt == NULL) ? 0 : 1);
44   STATIC_ASSERT(PreparseDataConstants::kParseErrorTypePos == 3);
45   function_store_.Add(error_type);
46   STATIC_ASSERT(PreparseDataConstants::kMessageTemplatePos == 4);
47   function_store_.Add(static_cast<unsigned>(message));
48   STATIC_ASSERT(PreparseDataConstants::kMessageArgPos == 5);
49   if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
50 }
51 
52 
WriteString(Vector<const char> str)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 
GetScriptData()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 internal
80 }  // namespace v8.
81