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 scope_(nullptr) {} | 63 scope_(nullptr) {} |
63 | 64 |
64 ParseInfo::ParseInfo(Zone* zone, Handle<JSFunction> function) | 65 ParseInfo::ParseInfo(Zone* zone, Handle<JSFunction> function) |
65 : ParseInfo(zone, Handle<SharedFunctionInfo>(function->shared())) { | 66 : ParseInfo(zone, Handle<SharedFunctionInfo>(function->shared())) { |
66 set_context(Handle<Context>(function->context())); | 67 set_context(Handle<Context>(function->context())); |
67 } | 68 } |
68 | 69 |
69 | 70 |
70 ParseInfo::ParseInfo(Zone* zone, Handle<SharedFunctionInfo> shared) | 71 ParseInfo::ParseInfo(Zone* zone, Handle<SharedFunctionInfo> shared) |
(...skipping 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1102 shared_info->start_position(), shared_info->end_position())); | 1103 shared_info->start_position(), shared_info->end_position())); |
1103 } else if (source->IsExternalOneByteString()) { | 1104 } else if (source->IsExternalOneByteString()) { |
1104 stream.reset(new ExternalOneByteStringUtf16CharacterStream( | 1105 stream.reset(new ExternalOneByteStringUtf16CharacterStream( |
1105 Handle<ExternalOneByteString>::cast(source), | 1106 Handle<ExternalOneByteString>::cast(source), |
1106 shared_info->start_position(), shared_info->end_position())); | 1107 shared_info->start_position(), shared_info->end_position())); |
1107 } else { | 1108 } else { |
1108 stream.reset(new GenericStringUtf16CharacterStream( | 1109 stream.reset(new GenericStringUtf16CharacterStream( |
1109 source, shared_info->start_position(), shared_info->end_position())); | 1110 source, shared_info->start_position(), shared_info->end_position())); |
1110 } | 1111 } |
1111 Handle<String> name(String::cast(shared_info->name())); | 1112 Handle<String> name(String::cast(shared_info->name())); |
1112 result = DoParseLazy(isolate, info, ast_value_factory()->GetString(name), | 1113 result = |
1113 stream.get()); | 1114 DoParseLazy(info, ast_value_factory()->GetString(name), stream.get()); |
1114 if (result != nullptr) { | 1115 if (result != nullptr) { |
1115 Handle<String> inferred_name(shared_info->inferred_name()); | 1116 Handle<String> inferred_name(shared_info->inferred_name()); |
1116 result->set_inferred_name(inferred_name); | 1117 result->set_inferred_name(inferred_name); |
1117 } | 1118 } |
1118 } | 1119 } |
1119 | 1120 |
1120 if (FLAG_trace_parse && result != NULL) { | 1121 if (FLAG_trace_parse && result != NULL) { |
1121 double ms = timer.Elapsed().InMillisecondsF(); | 1122 double ms = timer.Elapsed().InMillisecondsF(); |
1122 std::unique_ptr<char[]> name_chars = result->debug_name()->ToCString(); | 1123 std::unique_ptr<char[]> name_chars = result->debug_name()->ToCString(); |
1123 PrintF("[parsing function: %s - took %0.3f ms]\n", name_chars.get(), ms); | 1124 PrintF("[parsing function: %s - took %0.3f ms]\n", name_chars.get(), ms); |
1124 } | 1125 } |
1125 return result; | 1126 return result; |
1126 } | 1127 } |
1127 | 1128 |
1128 static FunctionLiteral::FunctionType ComputeFunctionType(ParseInfo* info) { | 1129 static FunctionLiteral::FunctionType ComputeFunctionType(ParseInfo* info) { |
1129 if (info->is_declaration()) { | 1130 if (info->is_declaration()) { |
1130 return FunctionLiteral::kDeclaration; | 1131 return FunctionLiteral::kDeclaration; |
1131 } else if (info->is_named_expression()) { | 1132 } else if (info->is_named_expression()) { |
1132 return FunctionLiteral::kNamedExpression; | 1133 return FunctionLiteral::kNamedExpression; |
1133 } else if (IsConciseMethod(info->function_kind()) || | 1134 } else if (IsConciseMethod(info->function_kind()) || |
1134 IsAccessorFunction(info->function_kind())) { | 1135 IsAccessorFunction(info->function_kind())) { |
1135 return FunctionLiteral::kAccessorOrMethod; | 1136 return FunctionLiteral::kAccessorOrMethod; |
1136 } | 1137 } |
1137 return FunctionLiteral::kAnonymousExpression; | 1138 return FunctionLiteral::kAnonymousExpression; |
1138 } | 1139 } |
1139 | 1140 |
1140 FunctionLiteral* Parser::DoParseLazy(Isolate* isolate, ParseInfo* info, | 1141 FunctionLiteral* Parser::DoParseLazy(ParseInfo* info, |
1141 const AstRawString* raw_name, | 1142 const AstRawString* raw_name, |
1142 Utf16CharacterStream* source) { | 1143 Utf16CharacterStream* source) { |
1143 scanner_.Initialize(source); | 1144 scanner_.Initialize(source); |
1144 DCHECK_NULL(scope_state_); | 1145 DCHECK_NULL(scope_state_); |
1145 DCHECK_NULL(target_stack_); | 1146 DCHECK_NULL(target_stack_); |
1146 | 1147 |
1147 DCHECK(ast_value_factory()); | 1148 DCHECK(ast_value_factory()); |
1148 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); | 1149 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); |
1149 fni_->PushEnclosingName(raw_name); | 1150 fni_->PushEnclosingName(raw_name); |
1150 | 1151 |
(...skipping 4324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5475 DCHECK(ast_value_factory()->IsInternalized()); | 5476 DCHECK(ast_value_factory()->IsInternalized()); |
5476 return (result != NULL); | 5477 return (result != NULL); |
5477 } | 5478 } |
5478 | 5479 |
5479 | 5480 |
5480 void Parser::ParseOnBackground(ParseInfo* info) { | 5481 void Parser::ParseOnBackground(ParseInfo* info) { |
5481 parsing_on_main_thread_ = false; | 5482 parsing_on_main_thread_ = false; |
5482 | 5483 |
5483 DCHECK(info->literal() == NULL); | 5484 DCHECK(info->literal() == NULL); |
5484 FunctionLiteral* result = NULL; | 5485 FunctionLiteral* result = NULL; |
5485 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); | |
5486 | 5486 |
5487 CompleteParserRecorder recorder; | 5487 CompleteParserRecorder recorder; |
5488 if (produce_cached_parse_data()) log_ = &recorder; | 5488 if (produce_cached_parse_data()) log_ = &recorder; |
5489 | 5489 |
5490 std::unique_ptr<Utf16CharacterStream> stream; | 5490 std::unique_ptr<Utf16CharacterStream> stream; |
5491 Utf16CharacterStream* stream_ptr; | 5491 Utf16CharacterStream* stream_ptr; |
5492 if (info->character_stream()) { | 5492 if (info->character_stream()) { |
5493 DCHECK(info->source_stream() == nullptr); | 5493 DCHECK(info->source_stream() == nullptr); |
5494 stream_ptr = info->character_stream(); | 5494 stream_ptr = info->character_stream(); |
5495 } else { | 5495 } else { |
5496 DCHECK(info->character_stream() == nullptr); | 5496 DCHECK(info->character_stream() == nullptr); |
5497 stream.reset(new ExternalStreamingStream(info->source_stream(), | 5497 stream.reset(new ExternalStreamingStream(info->source_stream(), |
5498 info->source_stream_encoding())); | 5498 info->source_stream_encoding())); |
5499 stream_ptr = stream.get(); | 5499 stream_ptr = stream.get(); |
5500 } | 5500 } |
5501 scanner_.Initialize(stream_ptr); | |
5502 DCHECK(info->context().is_null() || info->context()->IsNativeContext()); | 5501 DCHECK(info->context().is_null() || info->context()->IsNativeContext()); |
5503 | 5502 |
5504 DCHECK(original_scope_); | 5503 DCHECK(original_scope_); |
5505 | 5504 |
5506 // When streaming, we don't know the length of the source until we have parsed | 5505 // When streaming, we don't know the length of the source until we have parsed |
5507 // it. The raw data can be UTF-8, so we wouldn't know the source length until | 5506 // it. The raw data can be UTF-8, so we wouldn't know the source length until |
5508 // we have decoded it anyway even if we knew the raw data length (which we | 5507 // we have decoded it anyway even if we knew the raw data length (which we |
5509 // don't). We work around this by storing all the scopes which need their end | 5508 // don't). We work around this by storing all the scopes which need their end |
5510 // position set at the end of the script (the top scope and possible eval | 5509 // position set at the end of the script (the top scope and possible eval |
5511 // scopes) and set their end position after we know the script length. | 5510 // scopes) and set their end position after we know the script length. |
5512 result = DoParseProgram(info); | 5511 if (info->is_lazy()) { |
| 5512 result = DoParseLazy(info, info->function_name(), stream_ptr); |
| 5513 } else { |
| 5514 fni_ = new (zone()) FuncNameInferrer(ast_value_factory(), zone()); |
| 5515 scanner_.Initialize(stream_ptr); |
| 5516 result = DoParseProgram(info); |
| 5517 } |
5513 | 5518 |
5514 info->set_literal(result); | 5519 info->set_literal(result); |
5515 | 5520 |
5516 // We cannot internalize on a background thread; a foreground task will take | 5521 // We cannot internalize on a background thread; a foreground task will take |
5517 // care of calling Parser::Internalize just before compilation. | 5522 // care of calling Parser::Internalize just before compilation. |
5518 | 5523 |
5519 if (produce_cached_parse_data()) { | 5524 if (produce_cached_parse_data()) { |
5520 if (result != NULL) *info->cached_data() = recorder.GetScriptData(); | 5525 if (result != NULL) *info->cached_data() = recorder.GetScriptData(); |
5521 log_ = NULL; | 5526 log_ = NULL; |
5522 } | 5527 } |
(...skipping 1598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7121 node->Print(Isolate::Current()); | 7126 node->Print(Isolate::Current()); |
7122 } | 7127 } |
7123 #endif // DEBUG | 7128 #endif // DEBUG |
7124 | 7129 |
7125 #undef CHECK_OK | 7130 #undef CHECK_OK |
7126 #undef CHECK_OK_VOID | 7131 #undef CHECK_OK_VOID |
7127 #undef CHECK_FAILED | 7132 #undef CHECK_FAILED |
7128 | 7133 |
7129 } // namespace internal | 7134 } // namespace internal |
7130 } // namespace v8 | 7135 } // namespace v8 |
OLD | NEW |