OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 #include "src/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/ast/ast.h" | 10 #include "src/ast/ast.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 script_scope_(nullptr), | 51 script_scope_(nullptr), |
52 unicode_cache_(nullptr), | 52 unicode_cache_(nullptr), |
53 stack_limit_(0), | 53 stack_limit_(0), |
54 hash_seed_(0), | 54 hash_seed_(0), |
55 compiler_hints_(0), | 55 compiler_hints_(0), |
56 start_position_(0), | 56 start_position_(0), |
57 end_position_(0), | 57 end_position_(0), |
58 isolate_(nullptr), | 58 isolate_(nullptr), |
59 cached_data_(nullptr), | 59 cached_data_(nullptr), |
60 ast_value_factory_(nullptr), | 60 ast_value_factory_(nullptr), |
| 61 function_name_(nullptr), |
61 literal_(nullptr) {} | 62 literal_(nullptr) {} |
62 | 63 |
63 ParseInfo::ParseInfo(Zone* zone, Handle<JSFunction> function) | 64 ParseInfo::ParseInfo(Zone* zone, Handle<JSFunction> function) |
64 : ParseInfo(zone, Handle<SharedFunctionInfo>(function->shared())) { | 65 : ParseInfo(zone, Handle<SharedFunctionInfo>(function->shared())) { |
65 set_context(Handle<Context>(function->context())); | 66 set_context(Handle<Context>(function->context())); |
66 } | 67 } |
67 | 68 |
68 | 69 |
69 ParseInfo::ParseInfo(Zone* zone, Handle<SharedFunctionInfo> shared) | 70 ParseInfo::ParseInfo(Zone* zone, Handle<SharedFunctionInfo> shared) |
70 : ParseInfo(zone) { | 71 : ParseInfo(zone) { |
(...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1113 shared_info->start_position(), shared_info->end_position())); | 1114 shared_info->start_position(), shared_info->end_position())); |
1114 } else if (source->IsExternalOneByteString()) { | 1115 } else if (source->IsExternalOneByteString()) { |
1115 stream.reset(new ExternalOneByteStringUtf16CharacterStream( | 1116 stream.reset(new ExternalOneByteStringUtf16CharacterStream( |
1116 Handle<ExternalOneByteString>::cast(source), | 1117 Handle<ExternalOneByteString>::cast(source), |
1117 shared_info->start_position(), shared_info->end_position())); | 1118 shared_info->start_position(), shared_info->end_position())); |
1118 } else { | 1119 } else { |
1119 stream.reset(new GenericStringUtf16CharacterStream( | 1120 stream.reset(new GenericStringUtf16CharacterStream( |
1120 source, shared_info->start_position(), shared_info->end_position())); | 1121 source, shared_info->start_position(), shared_info->end_position())); |
1121 } | 1122 } |
1122 Handle<String> name(String::cast(shared_info->name())); | 1123 Handle<String> name(String::cast(shared_info->name())); |
1123 result = DoParseLazy(isolate, info, ast_value_factory()->GetString(name), | 1124 result = |
1124 stream.get()); | 1125 DoParseLazy(info, ast_value_factory()->GetString(name), stream.get()); |
1125 if (result != nullptr) { | 1126 if (result != nullptr) { |
1126 Handle<String> inferred_name(shared_info->inferred_name()); | 1127 Handle<String> inferred_name(shared_info->inferred_name()); |
1127 result->set_inferred_name(inferred_name); | 1128 result->set_inferred_name(inferred_name); |
1128 } | 1129 } |
1129 } | 1130 } |
1130 | 1131 |
1131 if (FLAG_trace_parse && result != NULL) { | 1132 if (FLAG_trace_parse && result != NULL) { |
1132 double ms = timer.Elapsed().InMillisecondsF(); | 1133 double ms = timer.Elapsed().InMillisecondsF(); |
1133 std::unique_ptr<char[]> name_chars = result->debug_name()->ToCString(); | 1134 std::unique_ptr<char[]> name_chars = result->debug_name()->ToCString(); |
1134 PrintF("[parsing function: %s - took %0.3f ms]\n", name_chars.get(), ms); | 1135 PrintF("[parsing function: %s - took %0.3f ms]\n", name_chars.get(), ms); |
1135 } | 1136 } |
1136 return result; | 1137 return result; |
1137 } | 1138 } |
1138 | 1139 |
1139 static FunctionLiteral::FunctionType ComputeFunctionType(ParseInfo* info) { | 1140 static FunctionLiteral::FunctionType ComputeFunctionType(ParseInfo* info) { |
1140 if (info->is_declaration()) { | 1141 if (info->is_declaration()) { |
1141 return FunctionLiteral::kDeclaration; | 1142 return FunctionLiteral::kDeclaration; |
1142 } else if (info->is_named_expression()) { | 1143 } else if (info->is_named_expression()) { |
1143 return FunctionLiteral::kNamedExpression; | 1144 return FunctionLiteral::kNamedExpression; |
1144 } else if (IsConciseMethod(info->function_kind()) || | 1145 } else if (IsConciseMethod(info->function_kind()) || |
1145 IsAccessorFunction(info->function_kind())) { | 1146 IsAccessorFunction(info->function_kind())) { |
1146 return FunctionLiteral::kAccessorOrMethod; | 1147 return FunctionLiteral::kAccessorOrMethod; |
1147 } | 1148 } |
1148 return FunctionLiteral::kAnonymousExpression; | 1149 return FunctionLiteral::kAnonymousExpression; |
1149 } | 1150 } |
1150 | 1151 |
1151 FunctionLiteral* Parser::DoParseLazy(Isolate* isolate, ParseInfo* info, | 1152 FunctionLiteral* Parser::DoParseLazy(ParseInfo* info, |
1152 const AstRawString* raw_name, | 1153 const AstRawString* raw_name, |
1153 Utf16CharacterStream* source) { | 1154 Utf16CharacterStream* source) { |
1154 scanner_.Initialize(source); | 1155 scanner_.Initialize(source); |
1155 DCHECK_NULL(scope_state_); | 1156 DCHECK_NULL(scope_state_); |
1156 DCHECK_NULL(target_stack_); | 1157 DCHECK_NULL(target_stack_); |
1157 | 1158 |
1158 DCHECK(ast_value_factory()); | 1159 DCHECK(ast_value_factory()); |
1159 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); | 1160 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); |
1160 fni_->PushEnclosingName(raw_name); | 1161 fni_->PushEnclosingName(raw_name); |
1161 | 1162 |
(...skipping 4351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5513 DCHECK(ast_value_factory()->IsInternalized()); | 5514 DCHECK(ast_value_factory()->IsInternalized()); |
5514 return (result != NULL); | 5515 return (result != NULL); |
5515 } | 5516 } |
5516 | 5517 |
5517 | 5518 |
5518 void Parser::ParseOnBackground(ParseInfo* info) { | 5519 void Parser::ParseOnBackground(ParseInfo* info) { |
5519 parsing_on_main_thread_ = false; | 5520 parsing_on_main_thread_ = false; |
5520 | 5521 |
5521 DCHECK(info->literal() == NULL); | 5522 DCHECK(info->literal() == NULL); |
5522 FunctionLiteral* result = NULL; | 5523 FunctionLiteral* result = NULL; |
5523 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); | |
5524 | 5524 |
5525 CompleteParserRecorder recorder; | 5525 CompleteParserRecorder recorder; |
5526 if (produce_cached_parse_data()) log_ = &recorder; | 5526 if (produce_cached_parse_data()) log_ = &recorder; |
5527 | 5527 |
5528 std::unique_ptr<Utf16CharacterStream> stream; | 5528 std::unique_ptr<Utf16CharacterStream> stream; |
5529 Utf16CharacterStream* stream_ptr; | 5529 Utf16CharacterStream* stream_ptr; |
5530 if (info->character_stream()) { | 5530 if (info->character_stream()) { |
5531 DCHECK(info->source_stream() == nullptr); | 5531 DCHECK(info->source_stream() == nullptr); |
5532 stream_ptr = info->character_stream(); | 5532 stream_ptr = info->character_stream(); |
5533 } else { | 5533 } else { |
5534 DCHECK(info->character_stream() == nullptr); | 5534 DCHECK(info->character_stream() == nullptr); |
5535 stream.reset(new ExternalStreamingStream(info->source_stream(), | 5535 stream.reset(new ExternalStreamingStream(info->source_stream(), |
5536 info->source_stream_encoding())); | 5536 info->source_stream_encoding())); |
5537 stream_ptr = stream.get(); | 5537 stream_ptr = stream.get(); |
5538 } | 5538 } |
5539 scanner_.Initialize(stream_ptr); | |
5540 DCHECK(info->context().is_null() || info->context()->IsNativeContext()); | 5539 DCHECK(info->context().is_null() || info->context()->IsNativeContext()); |
5541 | 5540 |
5542 DCHECK(original_scope_); | 5541 DCHECK(original_scope_); |
5543 | 5542 |
5544 // When streaming, we don't know the length of the source until we have parsed | 5543 // When streaming, we don't know the length of the source until we have parsed |
5545 // it. The raw data can be UTF-8, so we wouldn't know the source length until | 5544 // it. The raw data can be UTF-8, so we wouldn't know the source length until |
5546 // we have decoded it anyway even if we knew the raw data length (which we | 5545 // we have decoded it anyway even if we knew the raw data length (which we |
5547 // don't). We work around this by storing all the scopes which need their end | 5546 // don't). We work around this by storing all the scopes which need their end |
5548 // position set at the end of the script (the top scope and possible eval | 5547 // position set at the end of the script (the top scope and possible eval |
5549 // scopes) and set their end position after we know the script length. | 5548 // scopes) and set their end position after we know the script length. |
5550 result = DoParseProgram(info); | 5549 if (info->is_lazy()) { |
| 5550 result = DoParseLazy(info, info->function_name(), stream_ptr); |
| 5551 } else { |
| 5552 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); |
| 5553 scanner_.Initialize(stream_ptr); |
| 5554 result = DoParseProgram(info); |
| 5555 } |
5551 | 5556 |
5552 info->set_literal(result); | 5557 info->set_literal(result); |
5553 | 5558 |
5554 // We cannot internalize on a background thread; a foreground task will take | 5559 // We cannot internalize on a background thread; a foreground task will take |
5555 // care of calling Parser::Internalize just before compilation. | 5560 // care of calling Parser::Internalize just before compilation. |
5556 | 5561 |
5557 if (produce_cached_parse_data()) { | 5562 if (produce_cached_parse_data()) { |
5558 if (result != NULL) *info->cached_data() = recorder.GetScriptData(); | 5563 if (result != NULL) *info->cached_data() = recorder.GetScriptData(); |
5559 log_ = NULL; | 5564 log_ = NULL; |
5560 } | 5565 } |
(...skipping 1598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7159 node->Print(Isolate::Current()); | 7164 node->Print(Isolate::Current()); |
7160 } | 7165 } |
7161 #endif // DEBUG | 7166 #endif // DEBUG |
7162 | 7167 |
7163 #undef CHECK_OK | 7168 #undef CHECK_OK |
7164 #undef CHECK_OK_VOID | 7169 #undef CHECK_OK_VOID |
7165 #undef CHECK_FAILED | 7170 #undef CHECK_FAILED |
7166 | 7171 |
7167 } // namespace internal | 7172 } // namespace internal |
7168 } // namespace v8 | 7173 } // namespace v8 |
OLD | NEW |