deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / preparse-data.h
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.
4
5 #ifndef V8_PREPARSE_DATA_H_
6 #define V8_PREPARSE_DATA_H_
7
8 #include "src/allocation.h"
9 #include "src/hashmap.h"
10 #include "src/preparse-data-format.h"
11 #include "src/utils-inl.h"
12
13 namespace v8 {
14 namespace internal {
15
16 class ScriptData {
17  public:
18   ScriptData(const byte* data, int length);
19   ~ScriptData() {
20     if (owns_data_) DeleteArray(data_);
21   }
22
23   const byte* data() const { return data_; }
24   int length() const { return length_; }
25   bool rejected() const { return rejected_; }
26
27   void Reject() { rejected_ = true; }
28
29   void AcquireDataOwnership() {
30     DCHECK(!owns_data_);
31     owns_data_ = true;
32   }
33
34   void ReleaseDataOwnership() {
35     DCHECK(owns_data_);
36     owns_data_ = false;
37   }
38
39  private:
40   bool owns_data_ : 1;
41   bool rejected_ : 1;
42   const byte* data_;
43   int length_;
44
45   DISALLOW_COPY_AND_ASSIGN(ScriptData);
46 };
47
48 // Abstract interface for preparse data recorder.
49 class ParserRecorder {
50  public:
51   ParserRecorder() { }
52   virtual ~ParserRecorder() { }
53
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;
58
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;
65
66  private:
67   DISALLOW_COPY_AND_ASSIGN(ParserRecorder);
68 };
69
70
71 class SingletonLogger : public ParserRecorder {
72  public:
73   SingletonLogger()
74       : has_error_(false), start_(-1), end_(-1), error_type_(kSyntaxError) {}
75   virtual ~SingletonLogger() {}
76
77   void Reset() { has_error_ = false; }
78
79   virtual void LogFunction(int start, int end, int literals, int properties,
80                            LanguageMode language_mode,
81                            bool scope_uses_super_property) {
82     DCHECK(!has_error_);
83     start_ = start;
84     end_ = end;
85     literals_ = literals;
86     properties_ = properties;
87     language_mode_ = language_mode;
88     scope_uses_super_property_ = scope_uses_super_property;
89   }
90
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;
97     has_error_ = true;
98     start_ = start;
99     end_ = end;
100     message_ = message;
101     argument_opt_ = argument_opt;
102     error_type_ = error_type;
103   }
104
105   bool has_error() const { return has_error_; }
106
107   int start() const { return start_; }
108   int end() const { return end_; }
109   int literals() const {
110     DCHECK(!has_error_);
111     return literals_;
112   }
113   int properties() const {
114     DCHECK(!has_error_);
115     return properties_;
116   }
117   LanguageMode language_mode() const {
118     DCHECK(!has_error_);
119     return language_mode_;
120   }
121   bool scope_uses_super_property() const {
122     DCHECK(!has_error_);
123     return scope_uses_super_property_;
124   }
125   ParseErrorType error_type() const {
126     DCHECK(has_error_);
127     return error_type_;
128   }
129   const char* message() {
130     DCHECK(has_error_);
131     return message_;
132   }
133   const char* argument_opt() const {
134     DCHECK(has_error_);
135     return argument_opt_;
136   }
137
138  private:
139   bool has_error_;
140   int start_;
141   int end_;
142   // For function entries.
143   int literals_;
144   int properties_;
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_;
151 };
152
153
154 class CompleteParserRecorder : public ParserRecorder {
155  public:
156   struct Key {
157     bool is_one_byte;
158     Vector<const byte> literal_bytes;
159   };
160
161   CompleteParserRecorder();
162   virtual ~CompleteParserRecorder() {}
163
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);
173   }
174
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();
181
182   bool HasError() {
183     return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]);
184   }
185   Vector<unsigned> ErrorMessageData() {
186     DCHECK(HasError());
187     return function_store_.ToVector();
188   }
189
190  private:
191   void WriteString(Vector<const char> str);
192
193   // Write a non-negative number to the symbol store.
194   void WriteNumber(int number);
195
196   Collector<unsigned> function_store_;
197   unsigned preamble_[PreparseDataConstants::kHeaderSize];
198
199 #ifdef DEBUG
200   int prev_start_;
201 #endif
202 };
203
204
205 } }  // namespace v8::internal.
206
207 #endif  // V8_PREPARSE_DATA_H_