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/utils-inl.h" 11 12 namespace v8 { 13 namespace internal { 14 15 16 // Abstract interface for preparse data recorder. 17 class ParserRecorder { 18 public: ParserRecorder()19 ParserRecorder() { } ~ParserRecorder()20 virtual ~ParserRecorder() { } 21 22 // Logs the scope and some details of a function literal in the source. 23 virtual void LogFunction(int start, 24 int end, 25 int literals, 26 int properties, 27 StrictMode strict_mode) = 0; 28 29 // Logs an error message and marks the log as containing an error. 30 // Further logging will be ignored, and ExtractData will return a vector 31 // representing the error only. 32 virtual void LogMessage(int start, 33 int end, 34 const char* message, 35 const char* argument_opt, 36 bool is_reference_error) = 0; 37 private: 38 DISALLOW_COPY_AND_ASSIGN(ParserRecorder); 39 }; 40 41 42 class SingletonLogger : public ParserRecorder { 43 public: SingletonLogger()44 SingletonLogger() 45 : has_error_(false), start_(-1), end_(-1), is_reference_error_(false) {} ~SingletonLogger()46 virtual ~SingletonLogger() {} 47 Reset()48 void Reset() { has_error_ = false; } 49 LogFunction(int start,int end,int literals,int properties,StrictMode strict_mode)50 virtual void LogFunction(int start, 51 int end, 52 int literals, 53 int properties, 54 StrictMode strict_mode) { 55 ASSERT(!has_error_); 56 start_ = start; 57 end_ = end; 58 literals_ = literals; 59 properties_ = properties; 60 strict_mode_ = strict_mode; 61 } 62 63 // Logs an error message and marks the log as containing an error. 64 // Further logging will be ignored, and ExtractData will return a vector 65 // representing the error only. LogMessage(int start,int end,const char * message,const char * argument_opt,bool is_reference_error)66 virtual void LogMessage(int start, 67 int end, 68 const char* message, 69 const char* argument_opt, 70 bool is_reference_error) { 71 if (has_error_) return; 72 has_error_ = true; 73 start_ = start; 74 end_ = end; 75 message_ = message; 76 argument_opt_ = argument_opt; 77 is_reference_error_ = is_reference_error; 78 } 79 has_error()80 bool has_error() const { return has_error_; } 81 start()82 int start() const { return start_; } end()83 int end() const { return end_; } literals()84 int literals() const { 85 ASSERT(!has_error_); 86 return literals_; 87 } properties()88 int properties() const { 89 ASSERT(!has_error_); 90 return properties_; 91 } strict_mode()92 StrictMode strict_mode() const { 93 ASSERT(!has_error_); 94 return strict_mode_; 95 } is_reference_error()96 int is_reference_error() const { return is_reference_error_; } message()97 const char* message() { 98 ASSERT(has_error_); 99 return message_; 100 } argument_opt()101 const char* argument_opt() const { 102 ASSERT(has_error_); 103 return argument_opt_; 104 } 105 106 private: 107 bool has_error_; 108 int start_; 109 int end_; 110 // For function entries. 111 int literals_; 112 int properties_; 113 StrictMode strict_mode_; 114 // For error messages. 115 const char* message_; 116 const char* argument_opt_; 117 bool is_reference_error_; 118 }; 119 120 121 class CompleteParserRecorder : public ParserRecorder { 122 public: 123 struct Key { 124 bool is_one_byte; 125 Vector<const byte> literal_bytes; 126 }; 127 128 CompleteParserRecorder(); ~CompleteParserRecorder()129 virtual ~CompleteParserRecorder() {} 130 LogFunction(int start,int end,int literals,int properties,StrictMode strict_mode)131 virtual void LogFunction(int start, 132 int end, 133 int literals, 134 int properties, 135 StrictMode strict_mode) { 136 function_store_.Add(start); 137 function_store_.Add(end); 138 function_store_.Add(literals); 139 function_store_.Add(properties); 140 function_store_.Add(strict_mode); 141 } 142 143 // Logs an error message and marks the log as containing an error. 144 // Further logging will be ignored, and ExtractData will return a vector 145 // representing the error only. 146 virtual void LogMessage(int start, 147 int end, 148 const char* message, 149 const char* argument_opt, 150 bool is_reference_error_); 151 Vector<unsigned> ExtractData(); 152 153 private: has_error()154 bool has_error() { 155 return static_cast<bool>(preamble_[PreparseDataConstants::kHasErrorOffset]); 156 } 157 158 void WriteString(Vector<const char> str); 159 160 // Write a non-negative number to the symbol store. 161 void WriteNumber(int number); 162 163 Collector<unsigned> function_store_; 164 unsigned preamble_[PreparseDataConstants::kHeaderSize]; 165 166 #ifdef DEBUG 167 int prev_start_; 168 #endif 169 }; 170 171 172 } } // namespace v8::internal. 173 174 #endif // V8_PREPARSE_DATA_H_ 175