| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 19 matching lines...) Expand all Loading... |
| 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 "codegen.h" | 33 #include "codegen.h" |
| 34 #include "compiler.h" | 34 #include "compiler.h" |
| 35 #include "func-name-inferrer.h" | 35 #include "func-name-inferrer.h" |
| 36 #include "messages.h" | 36 #include "messages.h" |
| 37 #include "parser.h" | 37 #include "parser.h" |
| 38 #include "platform.h" | 38 #include "platform.h" |
| 39 #include "preparser.h" | 39 #include "preparser.h" |
| 40 #include "prescanner.h" | |
| 41 #include "runtime.h" | 40 #include "runtime.h" |
| 42 #include "scopeinfo.h" | 41 #include "scopeinfo.h" |
| 43 #include "string-stream.h" | 42 #include "string-stream.h" |
| 44 | 43 |
| 45 #include "ast-inl.h" | 44 #include "ast-inl.h" |
| 46 #include "jump-target-inl.h" | 45 #include "jump-target-inl.h" |
| 47 | 46 |
| 48 namespace v8 { | 47 namespace v8 { |
| 49 namespace internal { | 48 namespace internal { |
| 50 | 49 |
| (...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 FunctionLiteral* Parser::ParseProgram(Handle<String> source, | 720 FunctionLiteral* Parser::ParseProgram(Handle<String> source, |
| 722 bool in_global_context) { | 721 bool in_global_context) { |
| 723 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); | 722 CompilationZoneScope zone_scope(DONT_DELETE_ON_EXIT); |
| 724 | 723 |
| 725 HistogramTimerScope timer(&Counters::parse); | 724 HistogramTimerScope timer(&Counters::parse); |
| 726 Counters::total_parse_size.Increment(source->length()); | 725 Counters::total_parse_size.Increment(source->length()); |
| 727 fni_ = new FuncNameInferrer(); | 726 fni_ = new FuncNameInferrer(); |
| 728 | 727 |
| 729 // Initialize parser state. | 728 // Initialize parser state. |
| 730 source->TryFlatten(); | 729 source->TryFlatten(); |
| 731 scanner_.Initialize(source, JAVASCRIPT); | 730 scanner_.Initialize(source); |
| 732 ASSERT(target_stack_ == NULL); | 731 ASSERT(target_stack_ == NULL); |
| 733 if (pre_data_ != NULL) pre_data_->Initialize(); | 732 if (pre_data_ != NULL) pre_data_->Initialize(); |
| 734 | 733 |
| 735 // Compute the parsing mode. | 734 // Compute the parsing mode. |
| 736 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY; | 735 mode_ = FLAG_lazy ? PARSE_LAZILY : PARSE_EAGERLY; |
| 737 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY; | 736 if (allow_natives_syntax_ || extension_ != NULL) mode_ = PARSE_EAGERLY; |
| 738 | 737 |
| 739 Scope::Type type = | 738 Scope::Type type = |
| 740 in_global_context | 739 in_global_context |
| 741 ? Scope::GLOBAL_SCOPE | 740 ? Scope::GLOBAL_SCOPE |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 HistogramTimerScope timer(&Counters::parse_lazy); | 783 HistogramTimerScope timer(&Counters::parse_lazy); |
| 785 Handle<String> source(String::cast(script_->source())); | 784 Handle<String> source(String::cast(script_->source())); |
| 786 Counters::total_parse_size.Increment(source->length()); | 785 Counters::total_parse_size.Increment(source->length()); |
| 787 | 786 |
| 788 Handle<String> name(String::cast(info->name())); | 787 Handle<String> name(String::cast(info->name())); |
| 789 fni_ = new FuncNameInferrer(); | 788 fni_ = new FuncNameInferrer(); |
| 790 fni_->PushEnclosingName(name); | 789 fni_->PushEnclosingName(name); |
| 791 | 790 |
| 792 // Initialize parser state. | 791 // Initialize parser state. |
| 793 source->TryFlatten(); | 792 source->TryFlatten(); |
| 794 scanner_.Initialize(source, info->start_position(), info->end_position(), | 793 scanner_.Initialize(source, info->start_position(), info->end_position()); |
| 795 JAVASCRIPT); | |
| 796 ASSERT(target_stack_ == NULL); | 794 ASSERT(target_stack_ == NULL); |
| 797 mode_ = PARSE_EAGERLY; | 795 mode_ = PARSE_EAGERLY; |
| 798 | 796 |
| 799 // Place holder for the result. | 797 // Place holder for the result. |
| 800 FunctionLiteral* result = NULL; | 798 FunctionLiteral* result = NULL; |
| 801 | 799 |
| 802 { | 800 { |
| 803 // Parse the function literal. | 801 // Parse the function literal. |
| 804 Handle<String> no_name = Factory::empty_symbol(); | 802 Handle<String> no_name = Factory::empty_symbol(); |
| 805 Scope* scope = | 803 Scope* scope = |
| (...skipping 1465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2271 // properties if we haven't seen them before. Otherwise we'll | 2269 // properties if we haven't seen them before. Otherwise we'll |
| 2272 // probably overestimate the number of properties. | 2270 // probably overestimate the number of properties. |
| 2273 Property* property = expression ? expression->AsProperty() : NULL; | 2271 Property* property = expression ? expression->AsProperty() : NULL; |
| 2274 if (op == Token::ASSIGN && | 2272 if (op == Token::ASSIGN && |
| 2275 property != NULL && | 2273 property != NULL && |
| 2276 property->obj()->AsVariableProxy() != NULL && | 2274 property->obj()->AsVariableProxy() != NULL && |
| 2277 property->obj()->AsVariableProxy()->is_this()) { | 2275 property->obj()->AsVariableProxy()->is_this()) { |
| 2278 temp_scope_->AddProperty(); | 2276 temp_scope_->AddProperty(); |
| 2279 } | 2277 } |
| 2280 | 2278 |
| 2279 // If we assign a function literal to a property we pretenure the |
| 2280 // literal so it can be added as a constant function property. |
| 2281 if (property != NULL && right->AsFunctionLiteral() != NULL) { |
| 2282 right->AsFunctionLiteral()->set_pretenure(true); |
| 2283 } |
| 2284 |
| 2281 if (fni_ != NULL) { | 2285 if (fni_ != NULL) { |
| 2282 // Check if the right hand side is a call to avoid inferring a | 2286 // Check if the right hand side is a call to avoid inferring a |
| 2283 // name if we're dealing with "a = function(){...}();"-like | 2287 // name if we're dealing with "a = function(){...}();"-like |
| 2284 // expression. | 2288 // expression. |
| 2285 if ((op == Token::INIT_VAR | 2289 if ((op == Token::INIT_VAR |
| 2286 || op == Token::INIT_CONST | 2290 || op == Token::INIT_CONST |
| 2287 || op == Token::ASSIGN) | 2291 || op == Token::ASSIGN) |
| 2288 && (right->AsCall() == NULL)) { | 2292 && (right->AsCall() == NULL)) { |
| 2289 fni_->Infer(); | 2293 fni_->Infer(); |
| 2290 } | 2294 } |
| (...skipping 1315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3606 args->Add(new Literal(array)); | 3610 args->Add(new Literal(array)); |
| 3607 return new Throw(new CallRuntime(constructor, NULL, args), | 3611 return new Throw(new CallRuntime(constructor, NULL, args), |
| 3608 scanner().location().beg_pos); | 3612 scanner().location().beg_pos); |
| 3609 } | 3613 } |
| 3610 | 3614 |
| 3611 // ---------------------------------------------------------------------------- | 3615 // ---------------------------------------------------------------------------- |
| 3612 // JSON | 3616 // JSON |
| 3613 | 3617 |
| 3614 Handle<Object> JsonParser::ParseJson(Handle<String> source) { | 3618 Handle<Object> JsonParser::ParseJson(Handle<String> source) { |
| 3615 source->TryFlatten(); | 3619 source->TryFlatten(); |
| 3616 scanner_.Initialize(source, JSON); | 3620 scanner_.Initialize(source); |
| 3617 Handle<Object> result = ParseJsonValue(); | 3621 Handle<Object> result = ParseJsonValue(); |
| 3618 if (result.is_null() || scanner_.Next() != Token::EOS) { | 3622 if (result.is_null() || scanner_.Next() != Token::EOS) { |
| 3619 if (scanner_.stack_overflow()) { | 3623 if (scanner_.stack_overflow()) { |
| 3620 // Scanner failed. | 3624 // Scanner failed. |
| 3621 Top::StackOverflow(); | 3625 Top::StackOverflow(); |
| 3622 } else { | 3626 } else { |
| 3623 // Parse failed. Scanner's current token is the unexpected token. | 3627 // Parse failed. Scanner's current token is the unexpected token. |
| 3624 Token::Value token = scanner_.current_token(); | 3628 Token::Value token = scanner_.current_token(); |
| 3625 | 3629 |
| 3626 const char* message; | 3630 const char* message; |
| (...skipping 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4631 if (data >= symbol_data_end_) return -1; | 4635 if (data >= symbol_data_end_) return -1; |
| 4632 input = *data; | 4636 input = *data; |
| 4633 result = (result << 7) | (input & 0x7f); | 4637 result = (result << 7) | (input & 0x7f); |
| 4634 data++; | 4638 data++; |
| 4635 } | 4639 } |
| 4636 *source = data; | 4640 *source = data; |
| 4637 return result; | 4641 return result; |
| 4638 } | 4642 } |
| 4639 | 4643 |
| 4640 | 4644 |
| 4641 static ScriptDataImpl* DoPreParse(UTF16Buffer* stream, | 4645 // Create a Scanner for the preparser to use as input, and preparse the source. |
| 4646 static ScriptDataImpl* DoPreParse(Handle<String> source, |
| 4647 unibrow::CharacterStream* stream, |
| 4642 bool allow_lazy, | 4648 bool allow_lazy, |
| 4643 PartialParserRecorder* recorder) { | 4649 PartialParserRecorder* recorder, |
| 4644 typedef preparser::Scanner<UTF16Buffer, UTF8Buffer> PreScanner; | 4650 int literal_flags) { |
| 4645 PreScanner scanner; | 4651 V8JavaScriptScanner scanner; |
| 4646 scanner.Initialize(stream); | 4652 scanner.Initialize(source, stream, literal_flags); |
| 4647 preparser::PreParser<PreScanner, PartialParserRecorder> preparser; | 4653 preparser::PreParser<JavaScriptScanner, PartialParserRecorder> preparser; |
| 4648 if (!preparser.PreParseProgram(&scanner, recorder, allow_lazy)) { | 4654 if (!preparser.PreParseProgram(&scanner, recorder, allow_lazy)) { |
| 4649 Top::StackOverflow(); | 4655 Top::StackOverflow(); |
| 4650 return NULL; | 4656 return NULL; |
| 4651 } | 4657 } |
| 4652 | 4658 |
| 4653 // Extract the accumulated data from the recorder as a single | 4659 // Extract the accumulated data from the recorder as a single |
| 4654 // contiguous vector that we are responsible for disposing. | 4660 // contiguous vector that we are responsible for disposing. |
| 4655 Vector<unsigned> store = recorder->ExtractData(); | 4661 Vector<unsigned> store = recorder->ExtractData(); |
| 4656 return new ScriptDataImpl(store); | 4662 return new ScriptDataImpl(store); |
| 4657 } | 4663 } |
| 4658 | 4664 |
| 4659 | 4665 |
| 4660 // Create an UTF16Buffer for the preparser to use as input, | |
| 4661 // and preparse the source. | |
| 4662 static ScriptDataImpl* DoPreParse(Handle<String> source, | |
| 4663 unibrow::CharacterStream* stream, | |
| 4664 bool allow_lazy, | |
| 4665 PartialParserRecorder* recorder) { | |
| 4666 if (source.is_null()) { | |
| 4667 CharacterStreamUTF16Buffer buffer; | |
| 4668 int length = stream->Length(); | |
| 4669 buffer.Initialize(source, stream, 0, length); | |
| 4670 return DoPreParse(&buffer, allow_lazy, recorder); | |
| 4671 } else if (source->IsExternalAsciiString()) { | |
| 4672 ExternalStringUTF16Buffer<ExternalAsciiString, char> buffer; | |
| 4673 int length = source->length(); | |
| 4674 buffer.Initialize(Handle<ExternalAsciiString>::cast(source), 0, length); | |
| 4675 return DoPreParse(&buffer, allow_lazy, recorder); | |
| 4676 } else if (source->IsExternalTwoByteString()) { | |
| 4677 ExternalStringUTF16Buffer<ExternalTwoByteString, uint16_t> buffer; | |
| 4678 int length = source->length(); | |
| 4679 buffer.Initialize(Handle<ExternalTwoByteString>::cast(source), 0, length); | |
| 4680 return DoPreParse(&buffer, allow_lazy, recorder); | |
| 4681 } else { | |
| 4682 CharacterStreamUTF16Buffer buffer; | |
| 4683 SafeStringInputBuffer input; | |
| 4684 input.Reset(0, source.location()); | |
| 4685 int length = source->length(); | |
| 4686 buffer.Initialize(source, &input, 0, length); | |
| 4687 return DoPreParse(&buffer, allow_lazy, recorder); | |
| 4688 } | |
| 4689 } | |
| 4690 | |
| 4691 | |
| 4692 // Preparse, but only collect data that is immediately useful, | 4666 // Preparse, but only collect data that is immediately useful, |
| 4693 // even if the preparser data is only used once. | 4667 // even if the preparser data is only used once. |
| 4694 ScriptDataImpl* ParserApi::PartialPreParse(Handle<String> source, | 4668 ScriptDataImpl* ParserApi::PartialPreParse(Handle<String> source, |
| 4695 unibrow::CharacterStream* stream, | 4669 unibrow::CharacterStream* stream, |
| 4696 v8::Extension* extension) { | 4670 v8::Extension* extension) { |
| 4697 Handle<Script> no_script; | |
| 4698 bool allow_lazy = FLAG_lazy && (extension == NULL); | 4671 bool allow_lazy = FLAG_lazy && (extension == NULL); |
| 4699 if (!allow_lazy) { | 4672 if (!allow_lazy) { |
| 4700 // Partial preparsing is only about lazily compiled functions. | 4673 // Partial preparsing is only about lazily compiled functions. |
| 4701 // If we don't allow lazy compilation, the log data will be empty. | 4674 // If we don't allow lazy compilation, the log data will be empty. |
| 4702 return NULL; | 4675 return NULL; |
| 4703 } | 4676 } |
| 4704 PartialParserRecorder recorder; | 4677 PartialParserRecorder recorder; |
| 4705 | 4678 |
| 4706 return DoPreParse(source, stream, allow_lazy, &recorder); | 4679 return DoPreParse(source, stream, allow_lazy, &recorder, |
| 4680 JavaScriptScanner::kNoLiterals); |
| 4707 } | 4681 } |
| 4708 | 4682 |
| 4709 | 4683 |
| 4710 ScriptDataImpl* ParserApi::PreParse(Handle<String> source, | 4684 ScriptDataImpl* ParserApi::PreParse(Handle<String> source, |
| 4711 unibrow::CharacterStream* stream, | 4685 unibrow::CharacterStream* stream, |
| 4712 v8::Extension* extension) { | 4686 v8::Extension* extension) { |
| 4713 Handle<Script> no_script; | 4687 Handle<Script> no_script; |
| 4714 bool allow_lazy = FLAG_lazy && (extension == NULL); | 4688 bool allow_lazy = FLAG_lazy && (extension == NULL); |
| 4715 CompleteParserRecorder recorder; | 4689 CompleteParserRecorder recorder; |
| 4716 return DoPreParse(source, stream, allow_lazy, &recorder); | 4690 int kPreParseLiteralsFlags = |
| 4691 JavaScriptScanner::kLiteralString | JavaScriptScanner::kLiteralIdentifier; |
| 4692 return DoPreParse(source, stream, allow_lazy, |
| 4693 &recorder, kPreParseLiteralsFlags); |
| 4717 } | 4694 } |
| 4718 | 4695 |
| 4719 | 4696 |
| 4720 bool RegExpParser::ParseRegExp(FlatStringReader* input, | 4697 bool RegExpParser::ParseRegExp(FlatStringReader* input, |
| 4721 bool multiline, | 4698 bool multiline, |
| 4722 RegExpCompileData* result) { | 4699 RegExpCompileData* result) { |
| 4723 ASSERT(result != NULL); | 4700 ASSERT(result != NULL); |
| 4724 RegExpParser parser(input, &result->error, multiline); | 4701 RegExpParser parser(input, &result->error, multiline); |
| 4725 RegExpTree* tree = parser.ParsePattern(); | 4702 RegExpTree* tree = parser.ParsePattern(); |
| 4726 if (parser.failed()) { | 4703 if (parser.failed()) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4766 Handle<String> source = Handle<String>(String::cast(script->source())); | 4743 Handle<String> source = Handle<String>(String::cast(script->source())); |
| 4767 result = parser.ParseProgram(source, info->is_global()); | 4744 result = parser.ParseProgram(source, info->is_global()); |
| 4768 } | 4745 } |
| 4769 } | 4746 } |
| 4770 | 4747 |
| 4771 info->SetFunction(result); | 4748 info->SetFunction(result); |
| 4772 return (result != NULL); | 4749 return (result != NULL); |
| 4773 } | 4750 } |
| 4774 | 4751 |
| 4775 } } // namespace v8::internal | 4752 } } // namespace v8::internal |
| OLD | NEW |