| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_PREPARSE_DATA_H_ | 5 #ifndef V8_PREPARSE_DATA_H_ |
| 6 #define V8_PREPARSE_DATA_H_ | 6 #define V8_PREPARSE_DATA_H_ |
| 7 | 7 |
| 8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
| 9 #include "src/ast-value-factory.h" |
| 9 #include "src/hashmap.h" | 10 #include "src/hashmap.h" |
| 10 #include "src/preparse-data-format.h" | 11 #include "src/preparse-data-format.h" |
| 11 #include "src/utils-inl.h" | 12 #include "src/utils-inl.h" |
| 12 | 13 |
| 13 namespace v8 { | 14 namespace v8 { |
| 14 namespace internal { | 15 namespace internal { |
| 15 | 16 |
| 17 class AstRawString; |
| 16 class ScriptData; | 18 class ScriptData; |
| 17 | 19 |
| 18 | |
| 19 // Abstract interface for preparse data recorder. | 20 // Abstract interface for preparse data recorder. |
| 20 class ParserRecorder { | 21 class ParserRecorder { |
| 21 public: | 22 public: |
| 22 ParserRecorder() { } | 23 ParserRecorder() : identifiers_(AstRawString::Compare) {} |
| 23 virtual ~ParserRecorder() { } | 24 virtual ~ParserRecorder() { } |
| 24 | 25 |
| 26 // Logs that the next function makes a direct call to eval |
| 27 virtual void LogEvalCall() = 0; |
| 28 |
| 29 // Logs an identifier accessed by the next function |
| 30 virtual void LogIdentifier(const AstRawString* identifier) { |
| 31 // Lookup will insert the identifier into the HashMap with a NULL value |
| 32 // if the key (identifier) is not found |
| 33 identifiers_.Lookup(const_cast<AstRawString*>(identifier), |
| 34 identifier->hash(), true); |
| 35 } |
| 36 |
| 25 // Logs the scope and some details of a function literal in the source. | 37 // Logs the scope and some details of a function literal in the source. |
| 26 virtual void LogFunction(int start, | 38 virtual void LogFunction(int start, |
| 27 int end, | 39 int end, |
| 28 int literals, | 40 int literals, |
| 29 int properties, | 41 int properties, |
| 30 StrictMode strict_mode) = 0; | 42 StrictMode strict_mode) = 0; |
| 31 | 43 |
| 32 // Logs an error message and marks the log as containing an error. | 44 // Logs an error message and marks the log as containing an error. |
| 33 // Further logging will be ignored, and ExtractData will return a vector | 45 // Further logging will be ignored, and ExtractData will return a vector |
| 34 // representing the error only. | 46 // representing the error only. |
| 35 virtual void LogMessage(int start, | 47 virtual void LogMessage(int start, |
| 36 int end, | 48 int end, |
| 37 const char* message, | 49 const char* message, |
| 38 const char* argument_opt, | 50 const char* argument_opt, |
| 39 bool is_reference_error) = 0; | 51 bool is_reference_error) = 0; |
| 52 |
| 53 protected: |
| 54 HashMap identifiers_; |
| 55 |
| 40 private: | 56 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(ParserRecorder); | 57 DISALLOW_COPY_AND_ASSIGN(ParserRecorder); |
| 42 }; | 58 }; |
| 43 | 59 |
| 44 | 60 |
| 45 class SingletonLogger : public ParserRecorder { | 61 class SingletonLogger : public ParserRecorder { |
| 46 public: | 62 public: |
| 47 SingletonLogger() | 63 SingletonLogger() |
| 48 : has_error_(false), start_(-1), end_(-1), is_reference_error_(false) {} | 64 : has_error_(false), |
| 65 start_(-1), |
| 66 end_(-1), |
| 67 calls_eval_(false), |
| 68 is_reference_error_(false) {} |
| 49 virtual ~SingletonLogger() {} | 69 virtual ~SingletonLogger() {} |
| 50 | 70 |
| 51 void Reset() { has_error_ = false; } | 71 void Reset() { |
| 72 has_error_ = false; |
| 73 calls_eval_ = false; |
| 74 identifiers_.Clear(); |
| 75 } |
| 76 |
| 77 virtual void LogEvalCall() { calls_eval_ = true; } |
| 52 | 78 |
| 53 virtual void LogFunction(int start, | 79 virtual void LogFunction(int start, |
| 54 int end, | 80 int end, |
| 55 int literals, | 81 int literals, |
| 56 int properties, | 82 int properties, |
| 57 StrictMode strict_mode) { | 83 StrictMode strict_mode) { |
| 58 DCHECK(!has_error_); | 84 DCHECK(!has_error_); |
| 59 start_ = start; | 85 start_ = start; |
| 60 end_ = end; | 86 end_ = end; |
| 61 literals_ = literals; | 87 literals_ = literals; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 73 bool is_reference_error) { | 99 bool is_reference_error) { |
| 74 if (has_error_) return; | 100 if (has_error_) return; |
| 75 has_error_ = true; | 101 has_error_ = true; |
| 76 start_ = start; | 102 start_ = start; |
| 77 end_ = end; | 103 end_ = end; |
| 78 message_ = message; | 104 message_ = message; |
| 79 argument_opt_ = argument_opt; | 105 argument_opt_ = argument_opt; |
| 80 is_reference_error_ = is_reference_error; | 106 is_reference_error_ = is_reference_error; |
| 81 } | 107 } |
| 82 | 108 |
| 109 class IdentifierIterator { |
| 110 friend class SingletonLogger; |
| 111 |
| 112 public: |
| 113 const AstRawString* Next() { |
| 114 if (entry_ == NULL) return NULL; |
| 115 const AstRawString* result = static_cast<AstRawString*>(entry_->key); |
| 116 entry_ = identifiers_.Next(entry_); |
| 117 return result; |
| 118 } |
| 119 |
| 120 private: |
| 121 IdentifierIterator(const HashMap& identifiers, HashMap::Entry* entry) |
| 122 : identifiers_(identifiers), entry_(entry) {} |
| 123 |
| 124 const HashMap& identifiers_; |
| 125 HashMap::Entry* entry_; |
| 126 }; |
| 127 |
| 128 IdentifierIterator IdentifiersStart() { |
| 129 return IdentifierIterator(identifiers_, identifiers_.Start()); |
| 130 } |
| 131 |
| 83 bool has_error() const { return has_error_; } | 132 bool has_error() const { return has_error_; } |
| 84 | 133 |
| 85 int start() const { return start_; } | 134 int start() const { return start_; } |
| 86 int end() const { return end_; } | 135 int end() const { return end_; } |
| 87 int literals() const { | 136 int literals() const { |
| 88 DCHECK(!has_error_); | 137 DCHECK(!has_error_); |
| 89 return literals_; | 138 return literals_; |
| 90 } | 139 } |
| 91 int properties() const { | 140 int properties() const { |
| 92 DCHECK(!has_error_); | 141 DCHECK(!has_error_); |
| 93 return properties_; | 142 return properties_; |
| 94 } | 143 } |
| 95 StrictMode strict_mode() const { | 144 StrictMode strict_mode() const { |
| 96 DCHECK(!has_error_); | 145 DCHECK(!has_error_); |
| 97 return strict_mode_; | 146 return strict_mode_; |
| 98 } | 147 } |
| 148 bool calls_eval() const { |
| 149 DCHECK(!has_error_); |
| 150 return calls_eval_; |
| 151 } |
| 99 int is_reference_error() const { return is_reference_error_; } | 152 int is_reference_error() const { return is_reference_error_; } |
| 100 const char* message() { | 153 const char* message() { |
| 101 DCHECK(has_error_); | 154 DCHECK(has_error_); |
| 102 return message_; | 155 return message_; |
| 103 } | 156 } |
| 104 const char* argument_opt() const { | 157 const char* argument_opt() const { |
| 105 DCHECK(has_error_); | 158 DCHECK(has_error_); |
| 106 return argument_opt_; | 159 return argument_opt_; |
| 107 } | 160 } |
| 108 | 161 |
| 109 private: | 162 private: |
| 110 bool has_error_; | 163 bool has_error_; |
| 111 int start_; | 164 int start_; |
| 112 int end_; | 165 int end_; |
| 113 // For function entries. | 166 // For function entries. |
| 114 int literals_; | 167 int literals_; |
| 115 int properties_; | 168 int properties_; |
| 116 StrictMode strict_mode_; | 169 StrictMode strict_mode_; |
| 170 bool calls_eval_; |
| 117 // For error messages. | 171 // For error messages. |
| 118 const char* message_; | 172 const char* message_; |
| 119 const char* argument_opt_; | 173 const char* argument_opt_; |
| 120 bool is_reference_error_; | 174 bool is_reference_error_; |
| 121 }; | 175 }; |
| 122 | 176 |
| 123 | 177 |
| 124 class CompleteParserRecorder : public ParserRecorder { | 178 class CompleteParserRecorder : public ParserRecorder { |
| 125 public: | 179 public: |
| 126 struct Key { | 180 struct Key { |
| 127 bool is_one_byte; | 181 bool is_one_byte; |
| 128 Vector<const byte> literal_bytes; | 182 Vector<const byte> literal_bytes; |
| 129 }; | 183 }; |
| 130 | 184 |
| 131 CompleteParserRecorder(); | 185 CompleteParserRecorder(); |
| 132 virtual ~CompleteParserRecorder() {} | 186 virtual ~CompleteParserRecorder() {} |
| 133 | 187 |
| 188 virtual void LogEvalCall() { next_function_calls_eval_ = true; } |
| 189 |
| 134 virtual void LogFunction(int start, | 190 virtual void LogFunction(int start, |
| 135 int end, | 191 int end, |
| 136 int literals, | 192 int literals, |
| 137 int properties, | 193 int properties, |
| 138 StrictMode strict_mode) { | 194 StrictMode strict_mode) { |
| 139 function_store_.Add(start); | 195 function_store_.Add(start); |
| 140 function_store_.Add(end); | 196 function_store_.Add(end); |
| 141 function_store_.Add(literals); | 197 function_store_.Add(literals); |
| 142 function_store_.Add(properties); | 198 function_store_.Add(properties); |
| 143 function_store_.Add(strict_mode); | 199 function_store_.Add(strict_mode); |
| 200 function_store_.Add(next_function_calls_eval_); |
| 201 function_store_.Add(identifiers_.occupancy()); |
| 202 for (HashMap::Entry* p = identifiers_.Start(); p != NULL; |
| 203 p = identifiers_.Next(p)) { |
| 204 WriteString(static_cast<AstRawString*>(p->key)); |
| 205 } |
| 206 identifiers_.Clear(); |
| 207 next_function_calls_eval_ = false; |
| 144 } | 208 } |
| 145 | 209 |
| 146 // Logs an error message and marks the log as containing an error. | 210 // Logs an error message and marks the log as containing an error. |
| 147 // Further logging will be ignored, and ExtractData will return a vector | 211 // Further logging will be ignored, and ExtractData will return a vector |
| 148 // representing the error only. | 212 // representing the error only. |
| 149 virtual void LogMessage(int start, | 213 virtual void LogMessage(int start, |
| 150 int end, | 214 int end, |
| 151 const char* message, | 215 const char* message, |
| 152 const char* argument_opt, | 216 const char* argument_opt, |
| 153 bool is_reference_error_); | 217 bool is_reference_error_); |
| 154 ScriptData* GetScriptData(); | 218 ScriptData* GetScriptData(); |
| 155 | 219 |
| 156 bool HasError() { | 220 bool HasError() { |
| 157 return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]); | 221 return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]); |
| 158 } | 222 } |
| 159 Vector<unsigned> ErrorMessageData() { | 223 Vector<unsigned> ErrorMessageData() { |
| 160 DCHECK(HasError()); | 224 DCHECK(HasError()); |
| 161 return function_store_.ToVector(); | 225 return function_store_.ToVector(); |
| 162 } | 226 } |
| 163 | 227 |
| 164 private: | 228 private: |
| 165 void WriteString(Vector<const char> str); | 229 void WriteString(Vector<const char> str); |
| 230 void WriteString(const AstRawString* str); |
| 166 | 231 |
| 167 // Write a non-negative number to the symbol store. | 232 // Write a non-negative number to the symbol store. |
| 168 void WriteNumber(int number); | 233 void WriteNumber(int number); |
| 169 | 234 |
| 170 Collector<unsigned> function_store_; | 235 Collector<unsigned> function_store_; |
| 171 unsigned preamble_[PreparseDataConstants::kHeaderSize]; | 236 unsigned preamble_[PreparseDataConstants::kHeaderSize]; |
| 237 bool next_function_calls_eval_; |
| 172 | 238 |
| 173 #ifdef DEBUG | 239 #ifdef DEBUG |
| 174 int prev_start_; | 240 int prev_start_; |
| 175 #endif | 241 #endif |
| 176 }; | 242 }; |
| 177 | 243 |
| 178 | 244 |
| 179 } } // namespace v8::internal. | 245 } } // namespace v8::internal. |
| 180 | 246 |
| 181 #endif // V8_PREPARSE_DATA_H_ | 247 #endif // V8_PREPARSE_DATA_H_ |
| OLD | NEW |