| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "api.h" | 30 #include "api.h" |
| 31 #include "ast.h" | 31 #include "ast.h" |
| 32 #include "bootstrapper.h" | 32 #include "bootstrapper.h" |
| 33 #include "compiler.h" | 33 #include "compiler.h" |
| 34 #include "messages.h" |
| 34 #include "platform.h" | 35 #include "platform.h" |
| 35 #include "runtime.h" | 36 #include "runtime.h" |
| 36 #include "parser.h" | 37 #include "parser.h" |
| 37 #include "scopes.h" | 38 #include "scopes.h" |
| 38 #include "string-stream.h" | 39 #include "string-stream.h" |
| 39 | 40 |
| 40 namespace v8 { | 41 namespace v8 { |
| 41 namespace internal { | 42 namespace internal { |
| 42 | 43 |
| 43 class ParserFactory; | 44 class ParserFactory; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 bool PreParseProgram(Handle<String> source, unibrow::CharacterStream* stream); | 101 bool PreParseProgram(Handle<String> source, unibrow::CharacterStream* stream); |
| 101 | 102 |
| 102 void ReportMessage(const char* message, Vector<const char*> args); | 103 void ReportMessage(const char* message, Vector<const char*> args); |
| 103 virtual void ReportMessageAt(Scanner::Location loc, | 104 virtual void ReportMessageAt(Scanner::Location loc, |
| 104 const char* message, | 105 const char* message, |
| 105 Vector<const char*> args) = 0; | 106 Vector<const char*> args) = 0; |
| 106 | 107 |
| 107 | 108 |
| 108 // Returns NULL if parsing failed. | 109 // Returns NULL if parsing failed. |
| 109 FunctionLiteral* ParseProgram(Handle<String> source, | 110 FunctionLiteral* ParseProgram(Handle<String> source, |
| 110 unibrow::CharacterStream* stream, | |
| 111 bool in_global_context); | 111 bool in_global_context); |
| 112 FunctionLiteral* ParseLazy(Handle<String> source, | 112 FunctionLiteral* ParseLazy(Handle<String> source, |
| 113 Handle<String> name, | 113 Handle<String> name, |
| 114 int start_position, bool is_expression); | 114 int start_position, |
| 115 FunctionLiteral* ParseJson(Handle<String> source, | 115 int end_position, |
| 116 unibrow::CharacterStream* stream); | 116 bool is_expression); |
| 117 FunctionLiteral* ParseJson(Handle<String> source); |
| 117 | 118 |
| 118 // The minimum number of contiguous assignment that will | 119 // The minimum number of contiguous assignment that will |
| 119 // be treated as an initialization block. Benchmarks show that | 120 // be treated as an initialization block. Benchmarks show that |
| 120 // the overhead exceeds the savings below this limit. | 121 // the overhead exceeds the savings below this limit. |
| 121 static const int kMinInitializationBlock = 3; | 122 static const int kMinInitializationBlock = 3; |
| 122 | 123 |
| 123 protected: | 124 protected: |
| 124 | 125 |
| 125 enum Mode { | 126 enum Mode { |
| 126 PARSE_LAZILY, | 127 PARSE_LAZILY, |
| (...skipping 1078 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1205 pre_data_(pre_data) { | 1206 pre_data_(pre_data) { |
| 1206 } | 1207 } |
| 1207 | 1208 |
| 1208 | 1209 |
| 1209 bool Parser::PreParseProgram(Handle<String> source, | 1210 bool Parser::PreParseProgram(Handle<String> source, |
| 1210 unibrow::CharacterStream* stream) { | 1211 unibrow::CharacterStream* stream) { |
| 1211 HistogramTimerScope timer(&Counters::pre_parse); | 1212 HistogramTimerScope timer(&Counters::pre_parse); |
| 1212 AssertNoZoneAllocation assert_no_zone_allocation; | 1213 AssertNoZoneAllocation assert_no_zone_allocation; |
| 1213 AssertNoAllocation assert_no_allocation; | 1214 AssertNoAllocation assert_no_allocation; |
| 1214 NoHandleAllocation no_handle_allocation; | 1215 NoHandleAllocation no_handle_allocation; |
| 1215 scanner_.Init(source, stream, 0, JAVASCRIPT); | 1216 scanner_.Initialize(source, stream, JAVASCRIPT); |
| 1216 ASSERT(target_stack_ == NULL); | 1217 ASSERT(target_stack_ == NULL); |
| 1217 mode_ = PARSE_EAGERLY; | 1218 mode_ = PARSE_EAGERLY; |
| 1218 DummyScope top_scope; | 1219 DummyScope top_scope; |
| 1219 LexicalScope scope(this, &top_scope); | 1220 LexicalScope scope(this, &top_scope); |
| 1220 TemporaryScope temp_scope(this); | 1221 TemporaryScope temp_scope(this); |
| 1221 ZoneListWrapper<Statement> processor; | 1222 ZoneListWrapper<Statement> processor; |
| 1222 bool ok = true; | 1223 bool ok = true; |
| 1223 ParseSourceElements(&processor, Token::EOS, &ok); | 1224 ParseSourceElements(&processor, Token::EOS, &ok); |
| 1224 return !scanner().stack_overflow(); | 1225 return !scanner().stack_overflow(); |
| 1225 } | 1226 } |
| 1226 | 1227 |
| 1227 | 1228 |
| 1228 FunctionLiteral* Parser::ParseProgram(Handle<String> source, | 1229 FunctionLiteral* Parser::ParseProgram(Handle<String> source, |
| 1229 unibrow::CharacterStream* stream, | |
| 1230 bool in_global_context) { | 1230 bool in_global_context) { |
| 1231 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); | 1231 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); |
| 1232 | 1232 |
| 1233 HistogramTimerScope timer(&Counters::parse); | 1233 HistogramTimerScope timer(&Counters::parse); |
| 1234 Counters::total_parse_size.Increment(source->length()); | 1234 Counters::total_parse_size.Increment(source->length()); |
| 1235 | 1235 |
| 1236 // Initialize parser state. | 1236 // Initialize parser state. |
| 1237 source->TryFlattenIfNotFlat(); | 1237 source->TryFlatten(); |
| 1238 scanner_.Init(source, stream, 0, JAVASCRIPT); | 1238 scanner_.Initialize(source, JAVASCRIPT); |
| 1239 ASSERT(target_stack_ == NULL); | 1239 ASSERT(target_stack_ == NULL); |
| 1240 | 1240 |
| 1241 // Compute the parsing mode. | 1241 // Compute the parsing mode. |
| 1242 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY; | 1242 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY; |
| 1243 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY; | 1243 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY; |
| 1244 | 1244 |
| 1245 Scope::Type type = | 1245 Scope::Type type = |
| 1246 in_global_context | 1246 in_global_context |
| 1247 ? Scope::GLOBAL_SCOPE | 1247 ? Scope::GLOBAL_SCOPE |
| 1248 : Scope::EVAL_SCOPE; | 1248 : Scope::EVAL_SCOPE; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1279 // If there was a syntax error we have to get rid of the AST | 1279 // If there was a syntax error we have to get rid of the AST |
| 1280 // and it is not safe to do so before the scope has been deleted. | 1280 // and it is not safe to do so before the scope has been deleted. |
| 1281 if (result == NULL) zone_scope.DeleteOnExit(); | 1281 if (result == NULL) zone_scope.DeleteOnExit(); |
| 1282 return result; | 1282 return result; |
| 1283 } | 1283 } |
| 1284 | 1284 |
| 1285 | 1285 |
| 1286 FunctionLiteral* Parser::ParseLazy(Handle<String> source, | 1286 FunctionLiteral* Parser::ParseLazy(Handle<String> source, |
| 1287 Handle<String> name, | 1287 Handle<String> name, |
| 1288 int start_position, | 1288 int start_position, |
| 1289 int end_position, |
| 1289 bool is_expression) { | 1290 bool is_expression) { |
| 1290 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); | 1291 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); |
| 1291 HistogramTimerScope timer(&Counters::parse_lazy); | 1292 HistogramTimerScope timer(&Counters::parse_lazy); |
| 1292 source->TryFlattenIfNotFlat(); | |
| 1293 Counters::total_parse_size.Increment(source->length()); | 1293 Counters::total_parse_size.Increment(source->length()); |
| 1294 SafeStringInputBuffer buffer(source.location()); | |
| 1295 | 1294 |
| 1296 // Initialize parser state. | 1295 // Initialize parser state. |
| 1297 scanner_.Init(source, &buffer, start_position, JAVASCRIPT); | 1296 source->TryFlatten(); |
| 1297 scanner_.Initialize(source, start_position, end_position, JAVASCRIPT); |
| 1298 ASSERT(target_stack_ == NULL); | 1298 ASSERT(target_stack_ == NULL); |
| 1299 mode_ = PARSE_EAGERLY; | 1299 mode_ = PARSE_EAGERLY; |
| 1300 | 1300 |
| 1301 // Place holder for the result. | 1301 // Place holder for the result. |
| 1302 FunctionLiteral* result = NULL; | 1302 FunctionLiteral* result = NULL; |
| 1303 | 1303 |
| 1304 { | 1304 { |
| 1305 // Parse the function literal. | 1305 // Parse the function literal. |
| 1306 Handle<String> no_name = factory()->EmptySymbol(); | 1306 Handle<String> no_name = factory()->EmptySymbol(); |
| 1307 Scope* scope = | 1307 Scope* scope = |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1323 | 1323 |
| 1324 // If there was a stack overflow we have to get rid of AST and it is | 1324 // If there was a stack overflow we have to get rid of AST and it is |
| 1325 // not safe to do before scope has been deleted. | 1325 // not safe to do before scope has been deleted. |
| 1326 if (result == NULL) { | 1326 if (result == NULL) { |
| 1327 Top::StackOverflow(); | 1327 Top::StackOverflow(); |
| 1328 zone_scope.DeleteOnExit(); | 1328 zone_scope.DeleteOnExit(); |
| 1329 } | 1329 } |
| 1330 return result; | 1330 return result; |
| 1331 } | 1331 } |
| 1332 | 1332 |
| 1333 FunctionLiteral* Parser::ParseJson(Handle<String> source, | 1333 FunctionLiteral* Parser::ParseJson(Handle<String> source) { |
| 1334 unibrow::CharacterStream* stream) { | |
| 1335 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); | 1334 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); |
| 1336 | 1335 |
| 1337 HistogramTimerScope timer(&Counters::parse); | 1336 HistogramTimerScope timer(&Counters::parse); |
| 1338 Counters::total_parse_size.Increment(source->length()); | 1337 Counters::total_parse_size.Increment(source->length()); |
| 1339 | 1338 |
| 1340 // Initialize parser state. | 1339 // Initialize parser state. |
| 1341 source->TryFlattenIfNotFlat(); | 1340 source->TryFlatten(TENURED); |
| 1342 scanner_.Init(source, stream, 0, JSON); | 1341 scanner_.Initialize(source, JSON); |
| 1343 ASSERT(target_stack_ == NULL); | 1342 ASSERT(target_stack_ == NULL); |
| 1344 | 1343 |
| 1345 FunctionLiteral* result = NULL; | 1344 FunctionLiteral* result = NULL; |
| 1346 Handle<String> no_name = factory()->EmptySymbol(); | 1345 Handle<String> no_name = factory()->EmptySymbol(); |
| 1347 | 1346 |
| 1348 { | 1347 { |
| 1349 Scope* scope = factory()->NewScope(top_scope_, Scope::GLOBAL_SCOPE, false); | 1348 Scope* scope = factory()->NewScope(top_scope_, Scope::GLOBAL_SCOPE, false); |
| 1350 LexicalScope lexical_scope(this, scope); | 1349 LexicalScope lexical_scope(this, scope); |
| 1351 TemporaryScope temp_scope(this); | 1350 TemporaryScope temp_scope(this); |
| 1352 bool ok = true; | 1351 bool ok = true; |
| (...skipping 1898 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3251 // '(' Expression ')' | 3250 // '(' Expression ')' |
| 3252 | 3251 |
| 3253 Expression* result = NULL; | 3252 Expression* result = NULL; |
| 3254 switch (peek()) { | 3253 switch (peek()) { |
| 3255 case Token::THIS: { | 3254 case Token::THIS: { |
| 3256 Consume(Token::THIS); | 3255 Consume(Token::THIS); |
| 3257 if (is_pre_parsing_) { | 3256 if (is_pre_parsing_) { |
| 3258 result = VariableProxySentinel::this_proxy(); | 3257 result = VariableProxySentinel::this_proxy(); |
| 3259 } else { | 3258 } else { |
| 3260 VariableProxy* recv = top_scope_->receiver(); | 3259 VariableProxy* recv = top_scope_->receiver(); |
| 3261 recv->var_uses()->RecordRead(1); | |
| 3262 result = recv; | 3260 result = recv; |
| 3263 } | 3261 } |
| 3264 break; | 3262 break; |
| 3265 } | 3263 } |
| 3266 | 3264 |
| 3267 case Token::NULL_LITERAL: | 3265 case Token::NULL_LITERAL: |
| 3268 Consume(Token::NULL_LITERAL); | 3266 Consume(Token::NULL_LITERAL); |
| 3269 result = NEW(Literal(Factory::null_value())); | 3267 result = NEW(Literal(Factory::null_value())); |
| 3270 break; | 3268 break; |
| 3271 | 3269 |
| (...skipping 1786 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5058 Vector<const char*> args = pre_data->BuildArgs(); | 5056 Vector<const char*> args = pre_data->BuildArgs(); |
| 5059 parser.ReportMessageAt(loc, message, args); | 5057 parser.ReportMessageAt(loc, message, args); |
| 5060 DeleteArray(message); | 5058 DeleteArray(message); |
| 5061 for (int i = 0; i < args.length(); i++) { | 5059 for (int i = 0; i < args.length(); i++) { |
| 5062 DeleteArray(args[i]); | 5060 DeleteArray(args[i]); |
| 5063 } | 5061 } |
| 5064 DeleteArray(args.start()); | 5062 DeleteArray(args.start()); |
| 5065 return NULL; | 5063 return NULL; |
| 5066 } | 5064 } |
| 5067 Handle<String> source = Handle<String>(String::cast(script->source())); | 5065 Handle<String> source = Handle<String>(String::cast(script->source())); |
| 5068 SafeStringInputBuffer input(source.location()); | |
| 5069 FunctionLiteral* result; | 5066 FunctionLiteral* result; |
| 5070 if (is_json) { | 5067 if (is_json) { |
| 5071 ASSERT(compile_in_global_context); | 5068 ASSERT(compile_in_global_context); |
| 5072 result = parser.ParseJson(source, &input); | 5069 result = parser.ParseJson(source); |
| 5073 } else { | 5070 } else { |
| 5074 result = parser.ParseProgram(source, &input, compile_in_global_context); | 5071 result = parser.ParseProgram(source, compile_in_global_context); |
| 5075 } | 5072 } |
| 5076 return result; | 5073 return result; |
| 5077 } | 5074 } |
| 5078 | 5075 |
| 5079 | 5076 |
| 5080 FunctionLiteral* MakeLazyAST(Handle<Script> script, | 5077 FunctionLiteral* MakeLazyAST(Handle<Script> script, |
| 5081 Handle<String> name, | 5078 Handle<String> name, |
| 5082 int start_position, | 5079 int start_position, |
| 5083 int end_position, | 5080 int end_position, |
| 5084 bool is_expression) { | 5081 bool is_expression) { |
| 5085 bool allow_natives_syntax_before = always_allow_natives_syntax; | 5082 bool allow_natives_syntax_before = always_allow_natives_syntax; |
| 5086 always_allow_natives_syntax = true; | 5083 always_allow_natives_syntax = true; |
| 5087 AstBuildingParser parser(script, true, NULL, NULL); // always allow | 5084 AstBuildingParser parser(script, true, NULL, NULL); // always allow |
| 5088 always_allow_natives_syntax = allow_natives_syntax_before; | 5085 always_allow_natives_syntax = allow_natives_syntax_before; |
| 5089 // Parse the function by pulling the function source from the script source. | 5086 // Parse the function by pointing to the function source in the script source. |
| 5090 Handle<String> script_source(String::cast(script->source())); | 5087 Handle<String> script_source(String::cast(script->source())); |
| 5091 FunctionLiteral* result = | 5088 FunctionLiteral* result = |
| 5092 parser.ParseLazy(SubString(script_source, start_position, end_position), | 5089 parser.ParseLazy(script_source, name, |
| 5093 name, | 5090 start_position, end_position, is_expression); |
| 5094 start_position, | |
| 5095 is_expression); | |
| 5096 return result; | 5091 return result; |
| 5097 } | 5092 } |
| 5098 | 5093 |
| 5099 | 5094 |
| 5100 #undef NEW | 5095 #undef NEW |
| 5101 | 5096 |
| 5102 | 5097 |
| 5103 } } // namespace v8::internal | 5098 } } // namespace v8::internal |
| OLD | NEW |