1 // Copyright 2011 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 #ifndef V8_PREPARSE_DATA_H_
6 #define V8_PREPARSE_DATA_H_
8 #include "src/allocation.h"
9 #include "src/hashmap.h"
10 #include "src/preparse-data-format.h"
11 #include "src/utils-inl.h"
18 ScriptData(const byte* data, int length);
20 if (owns_data_) DeleteArray(data_);
23 const byte* data() const { return data_; }
24 int length() const { return length_; }
25 bool rejected() const { return rejected_; }
27 void Reject() { rejected_ = true; }
29 void AcquireDataOwnership() {
34 void ReleaseDataOwnership() {
45 DISALLOW_COPY_AND_ASSIGN(ScriptData);
48 // Abstract interface for preparse data recorder.
49 class ParserRecorder {
52 virtual ~ParserRecorder() { }
54 // Logs the scope and some details of a function literal in the source.
55 virtual void LogFunction(int start, int end, int literals, int properties,
56 LanguageMode language_mode,
57 bool uses_super_property) = 0;
59 // Logs an error message and marks the log as containing an error.
60 // Further logging will be ignored, and ExtractData will return a vector
61 // representing the error only.
62 virtual void LogMessage(int start, int end, const char* message,
63 const char* argument_opt,
64 ParseErrorType error_type) = 0;
67 DISALLOW_COPY_AND_ASSIGN(ParserRecorder);
71 class SingletonLogger : public ParserRecorder {
74 : has_error_(false), start_(-1), end_(-1), error_type_(kSyntaxError) {}
75 virtual ~SingletonLogger() {}
77 void Reset() { has_error_ = false; }
79 virtual void LogFunction(int start, int end, int literals, int properties,
80 LanguageMode language_mode,
81 bool scope_uses_super_property) {
86 properties_ = properties;
87 language_mode_ = language_mode;
88 scope_uses_super_property_ = scope_uses_super_property;
91 // Logs an error message and marks the log as containing an error.
92 // Further logging will be ignored, and ExtractData will return a vector
93 // representing the error only.
94 virtual void LogMessage(int start, int end, const char* message,
95 const char* argument_opt, ParseErrorType error_type) {
96 if (has_error_) return;
101 argument_opt_ = argument_opt;
102 error_type_ = error_type;
105 bool has_error() const { return has_error_; }
107 int start() const { return start_; }
108 int end() const { return end_; }
109 int literals() const {
113 int properties() const {
117 LanguageMode language_mode() const {
119 return language_mode_;
121 bool scope_uses_super_property() const {
123 return scope_uses_super_property_;
125 ParseErrorType error_type() const {
129 const char* message() {
133 const char* argument_opt() const {
135 return argument_opt_;
142 // For function entries.
145 LanguageMode language_mode_;
146 bool scope_uses_super_property_;
147 // For error messages.
148 const char* message_;
149 const char* argument_opt_;
150 ParseErrorType error_type_;
154 class CompleteParserRecorder : public ParserRecorder {
158 Vector<const byte> literal_bytes;
161 CompleteParserRecorder();
162 virtual ~CompleteParserRecorder() {}
164 virtual void LogFunction(int start, int end, int literals, int properties,
165 LanguageMode language_mode,
166 bool scope_uses_super_property) {
167 function_store_.Add(start);
168 function_store_.Add(end);
169 function_store_.Add(literals);
170 function_store_.Add(properties);
171 function_store_.Add(language_mode);
172 function_store_.Add(scope_uses_super_property);
175 // Logs an error message and marks the log as containing an error.
176 // Further logging will be ignored, and ExtractData will return a vector
177 // representing the error only.
178 virtual void LogMessage(int start, int end, const char* message,
179 const char* argument_opt, ParseErrorType error_type);
180 ScriptData* GetScriptData();
183 return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
185 Vector<unsigned> ErrorMessageData() {
187 return function_store_.ToVector();
191 void WriteString(Vector<const char> str);
193 // Write a non-negative number to the symbol store.
194 void WriteNumber(int number);
196 Collector<unsigned> function_store_;
197 unsigned preamble_[PreparseDataConstants::kHeaderSize];
205 } } // namespace v8::internal.
207 #endif // V8_PREPARSE_DATA_H_