• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/v8stdint.h"
6 
7 #include "src/preparse-data-format.h"
8 #include "src/preparse-data.h"
9 
10 #include "src/checks.h"
11 #include "src/globals.h"
12 #include "src/hashmap.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 
CompleteParserRecorder()18 CompleteParserRecorder::CompleteParserRecorder()
19     : function_store_(0) {
20   preamble_[PreparseDataConstants::kMagicOffset] =
21       PreparseDataConstants::kMagicNumber;
22   preamble_[PreparseDataConstants::kVersionOffset] =
23       PreparseDataConstants::kCurrentVersion;
24   preamble_[PreparseDataConstants::kHasErrorOffset] = false;
25   preamble_[PreparseDataConstants::kFunctionsSizeOffset] = 0;
26   preamble_[PreparseDataConstants::kSizeOffset] = 0;
27   ASSERT_EQ(5, PreparseDataConstants::kHeaderSize);
28 #ifdef DEBUG
29   prev_start_ = -1;
30 #endif
31 }
32 
33 
LogMessage(int start_pos,int end_pos,const char * message,const char * arg_opt,bool is_reference_error)34 void CompleteParserRecorder::LogMessage(int start_pos,
35                                         int end_pos,
36                                         const char* message,
37                                         const char* arg_opt,
38                                         bool is_reference_error) {
39   if (has_error()) return;
40   preamble_[PreparseDataConstants::kHasErrorOffset] = true;
41   function_store_.Reset();
42   STATIC_ASSERT(PreparseDataConstants::kMessageStartPos == 0);
43   function_store_.Add(start_pos);
44   STATIC_ASSERT(PreparseDataConstants::kMessageEndPos == 1);
45   function_store_.Add(end_pos);
46   STATIC_ASSERT(PreparseDataConstants::kMessageArgCountPos == 2);
47   function_store_.Add((arg_opt == NULL) ? 0 : 1);
48   STATIC_ASSERT(PreparseDataConstants::kIsReferenceErrorPos == 3);
49   function_store_.Add(is_reference_error ? 1 : 0);
50   STATIC_ASSERT(PreparseDataConstants::kMessageTextPos == 4);
51   WriteString(CStrVector(message));
52   if (arg_opt != NULL) WriteString(CStrVector(arg_opt));
53 }
54 
55 
WriteString(Vector<const char> str)56 void CompleteParserRecorder::WriteString(Vector<const char> str) {
57   function_store_.Add(str.length());
58   for (int i = 0; i < str.length(); i++) {
59     function_store_.Add(str[i]);
60   }
61 }
62 
63 
ExtractData()64 Vector<unsigned> CompleteParserRecorder::ExtractData() {
65   int function_size = function_store_.size();
66   int total_size = PreparseDataConstants::kHeaderSize + function_size;
67   Vector<unsigned> data = Vector<unsigned>::New(total_size);
68   preamble_[PreparseDataConstants::kFunctionsSizeOffset] = function_size;
69   MemCopy(data.start(), preamble_, sizeof(preamble_));
70   if (function_size > 0) {
71     function_store_.WriteTo(data.SubVector(PreparseDataConstants::kHeaderSize,
72                                            total_size));
73   }
74   return data;
75 }
76 
77 
78 } }  // namespace v8::internal.
79