blob: c1331d044fcfd6148cd9dbb30846c76e3fc1d8bb [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08004
Ben Murdoch257744e2011-11-30 15:57:28 +00005#ifndef V8_PREPARSE_DATA_H_
6#define V8_PREPARSE_DATA_H_
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/allocation.h"
9#include "src/hashmap.h"
10#include "src/preparse-data-format.h"
11#include "src/utils-inl.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080012
13namespace v8 {
14namespace internal {
15
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016class ScriptData;
17
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080018
19// Abstract interface for preparse data recorder.
20class ParserRecorder {
21 public:
22 ParserRecorder() { }
23 virtual ~ParserRecorder() { }
24
25 // Logs the scope and some details of a function literal in the source.
26 virtual void LogFunction(int start,
27 int end,
28 int literals,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000029 int properties,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 StrictMode strict_mode) = 0;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080031
32 // Logs an error message and marks the log as containing an error.
33 // Further logging will be ignored, and ExtractData will return a vector
34 // representing the error only.
35 virtual void LogMessage(int start,
36 int end,
37 const char* message,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038 const char* argument_opt,
39 bool is_reference_error) = 0;
40 private:
41 DISALLOW_COPY_AND_ASSIGN(ParserRecorder);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080042};
43
44
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045class SingletonLogger : public ParserRecorder {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080046 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 SingletonLogger()
48 : has_error_(false), start_(-1), end_(-1), is_reference_error_(false) {}
49 virtual ~SingletonLogger() {}
50
51 void Reset() { has_error_ = false; }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080052
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000053 virtual void LogFunction(int start,
54 int end,
55 int literals,
56 int properties,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 StrictMode strict_mode) {
58 DCHECK(!has_error_);
59 start_ = start;
60 end_ = end;
61 literals_ = literals;
62 properties_ = properties;
63 strict_mode_ = strict_mode;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080064 }
65
66 // Logs an error message and marks the log as containing an error.
67 // Further logging will be ignored, and ExtractData will return a vector
68 // representing the error only.
69 virtual void LogMessage(int start,
70 int end,
71 const char* message,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 const char* argument_opt,
73 bool is_reference_error) {
74 if (has_error_) return;
75 has_error_ = true;
76 start_ = start;
77 end_ = end;
78 message_ = message;
79 argument_opt_ = argument_opt;
80 is_reference_error_ = is_reference_error;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080081 }
82
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 bool has_error() const { return has_error_; }
84
85 int start() const { return start_; }
86 int end() const { return end_; }
87 int literals() const {
88 DCHECK(!has_error_);
89 return literals_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080090 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 int properties() const {
92 DCHECK(!has_error_);
93 return properties_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080094 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 StrictMode strict_mode() const {
96 DCHECK(!has_error_);
97 return strict_mode_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080098 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 int is_reference_error() const { return is_reference_error_; }
100 const char* message() {
101 DCHECK(has_error_);
102 return message_;
Steve Block9fac8402011-05-12 15:51:54 +0100103 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 const char* argument_opt() const {
105 DCHECK(has_error_);
106 return argument_opt_;
Steve Block9fac8402011-05-12 15:51:54 +0100107 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800108
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800109 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 bool has_error_;
111 int start_;
112 int end_;
113 // For function entries.
114 int literals_;
115 int properties_;
116 StrictMode strict_mode_;
117 // For error messages.
118 const char* message_;
119 const char* argument_opt_;
120 bool is_reference_error_;
121};
122
123
124class CompleteParserRecorder : public ParserRecorder {
125 public:
Steve Block9fac8402011-05-12 15:51:54 +0100126 struct Key {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127 bool is_one_byte;
Steve Block9fac8402011-05-12 15:51:54 +0100128 Vector<const byte> literal_bytes;
129 };
130
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 CompleteParserRecorder();
132 virtual ~CompleteParserRecorder() {}
Steve Block9fac8402011-05-12 15:51:54 +0100133
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 virtual void LogFunction(int start,
135 int end,
136 int literals,
137 int properties,
138 StrictMode strict_mode) {
139 function_store_.Add(start);
140 function_store_.Add(end);
141 function_store_.Add(literals);
142 function_store_.Add(properties);
143 function_store_.Add(strict_mode);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800144 }
145
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 // Logs an error message and marks the log as containing an error.
147 // Further logging will be ignored, and ExtractData will return a vector
148 // representing the error only.
149 virtual void LogMessage(int start,
150 int end,
151 const char* message,
152 const char* argument_opt,
153 bool is_reference_error_);
154 ScriptData* GetScriptData();
155
156 bool HasError() {
157 return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800158 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 Vector<unsigned> ErrorMessageData() {
160 DCHECK(HasError());
161 return function_store_.ToVector();
162 }
163
164 private:
165 void WriteString(Vector<const char> str);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800166
167 // Write a non-negative number to the symbol store.
168 void WriteNumber(int number);
169
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 Collector<unsigned> function_store_;
171 unsigned preamble_[PreparseDataConstants::kHeaderSize];
172
173#ifdef DEBUG
174 int prev_start_;
175#endif
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800176};
177
178
179} } // namespace v8::internal.
180
Ben Murdoch257744e2011-11-30 15:57:28 +0000181#endif // V8_PREPARSE_DATA_H_