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/messages.h"
11 #include "src/preparse-data-format.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, bool uses_super_property,
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, MessageTemplate::Template 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, bool uses_super_property,
86 properties_ = properties;
87 language_mode_ = language_mode;
88 uses_super_property_ = uses_super_property;
89 calls_eval_ = calls_eval;
92 // Logs an error message and marks the log as containing an error.
93 // Further logging will be ignored, and ExtractData will return a vector
94 // representing the error only.
95 virtual void LogMessage(int start, int end, MessageTemplate::Template message,
96 const char* argument_opt, ParseErrorType error_type) {
97 if (has_error_) return;
102 argument_opt_ = argument_opt;
103 error_type_ = error_type;
106 bool has_error() const { return has_error_; }
108 int start() const { return start_; }
109 int end() const { return end_; }
110 int literals() const {
114 int properties() const {
118 LanguageMode language_mode() const {
120 return language_mode_;
122 bool uses_super_property() const {
124 return uses_super_property_;
126 bool calls_eval() const {
130 ParseErrorType error_type() const {
134 MessageTemplate::Template message() {
138 const char* argument_opt() const {
140 return argument_opt_;
147 // For function entries.
150 LanguageMode language_mode_;
151 bool uses_super_property_;
153 // For error messages.
154 MessageTemplate::Template message_;
155 const char* argument_opt_;
156 ParseErrorType error_type_;
160 class CompleteParserRecorder : public ParserRecorder {
164 Vector<const byte> literal_bytes;
167 CompleteParserRecorder();
168 virtual ~CompleteParserRecorder() {}
170 virtual void LogFunction(int start, int end, int literals, int properties,
171 LanguageMode language_mode, bool uses_super_property,
173 function_store_.Add(start);
174 function_store_.Add(end);
175 function_store_.Add(literals);
176 function_store_.Add(properties);
177 function_store_.Add(language_mode);
178 function_store_.Add(uses_super_property);
179 function_store_.Add(calls_eval);
182 // Logs an error message and marks the log as containing an error.
183 // Further logging will be ignored, and ExtractData will return a vector
184 // representing the error only.
185 virtual void LogMessage(int start, int end, MessageTemplate::Template message,
186 const char* argument_opt, ParseErrorType error_type);
187 ScriptData* GetScriptData();
190 return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
192 Vector<unsigned> ErrorMessageData() {
194 return function_store_.ToVector();
198 void WriteString(Vector<const char> str);
200 Collector<unsigned> function_store_;
201 unsigned preamble_[PreparseDataConstants::kHeaderSize];
209 } // namespace internal
212 #endif // V8_PREPARSE_DATA_H_