Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: src/parser.cc

Issue 663683006: Implement ES6 Template Literals (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Prevent fall-through to template token handlers Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 pre_parse_timer_(NULL) { 751 pre_parse_timer_(NULL) {
752 DCHECK(!script().is_null() || info->source_stream() != NULL); 752 DCHECK(!script().is_null() || info->source_stream() != NULL);
753 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); 753 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
754 set_allow_modules(!info->is_native() && FLAG_harmony_modules); 754 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
755 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); 755 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
756 set_allow_lazy(false); // Must be explicitly enabled. 756 set_allow_lazy(false); // Must be explicitly enabled.
757 set_allow_arrow_functions(FLAG_harmony_arrow_functions); 757 set_allow_arrow_functions(FLAG_harmony_arrow_functions);
758 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 758 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
759 set_allow_classes(FLAG_harmony_classes); 759 set_allow_classes(FLAG_harmony_classes);
760 set_allow_harmony_object_literals(FLAG_harmony_object_literals); 760 set_allow_harmony_object_literals(FLAG_harmony_object_literals);
761 set_allow_harmony_templates(FLAG_harmony_templates);
761 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 762 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
762 ++feature) { 763 ++feature) {
763 use_counts_[feature] = 0; 764 use_counts_[feature] = 0;
764 } 765 }
765 if (info->ast_value_factory() == NULL) { 766 if (info->ast_value_factory() == NULL) {
766 // info takes ownership of AstValueFactory. 767 // info takes ownership of AstValueFactory.
767 info->SetAstValueFactory( 768 info->SetAstValueFactory(
768 new AstValueFactory(zone(), parse_info->hash_seed)); 769 new AstValueFactory(zone(), parse_info->hash_seed));
769 } 770 }
770 } 771 }
(...skipping 4189 matching lines...) Expand 10 before | Expand all | Expand 10 after
4960 info()->SetFunction(result); 4961 info()->SetFunction(result);
4961 4962
4962 // We cannot internalize on a background thread; a foreground task will take 4963 // We cannot internalize on a background thread; a foreground task will take
4963 // care of calling Parser::Internalize just before compilation. 4964 // care of calling Parser::Internalize just before compilation.
4964 4965
4965 if (compile_options() == ScriptCompiler::kProduceParserCache) { 4966 if (compile_options() == ScriptCompiler::kProduceParserCache) {
4966 if (result != NULL) *info_->cached_data() = recorder.GetScriptData(); 4967 if (result != NULL) *info_->cached_data() = recorder.GetScriptData();
4967 log_ = NULL; 4968 log_ = NULL;
4968 } 4969 }
4969 } 4970 }
4971
4972
4973 ParserTraits::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) {
4974 return factory()->NewTemplateLiteral(pos);
4975 }
4976
4977
4978 void Parser::AddTemplateSpan(TemplateLiteralState* state) {
4979 int pos = scanner()->location().beg_pos;
4980 const AstRawString* tv = scanner()->CurrentSymbol(ast_value_factory());
4981 const AstRawString* trv = scanner()->CurrentRawSymbol(ast_value_factory());
4982 Literal* cooked = factory()->NewStringLiteral(tv, pos);
4983 Literal* raw = factory()->NewStringLiteral(trv, pos);
4984 (*state)->AddTemplateSpan(cooked, raw, zone());
4985 }
4986
4987
4988 void Parser::AddTemplateExpression(TemplateLiteralState* state,
4989 Expression* expression) {
4990 (*state)->AddExpression(expression, zone());
4991 }
4992
4993
4994 Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state,
4995 int start, Expression* tag) {
4996 #define COOKED_STRING(i) cookedStrings->at(i)
4997 #define POS(i) cookedStrings->at(i)->position()
4998 #define EXPR(i) expressions->at(i)
4999 TemplateLiteral* lit = *state;
5000 int pos = lit->position();
5001 const ZoneList<Expression*>* cookedStrings = lit->cooked();
5002 const ZoneList<Expression*>* expressions = lit->expressions();
5003 CHECK(cookedStrings->length() == (expressions->length() + 1));
5004
5005 if (!tag) {
5006 // Build tree of BinaryOps to simplify code-generation
5007 Expression* expr = NULL;
5008
5009 if (!expressions->length()) {
5010 // Simple case: treat as string literal
5011 expr = COOKED_STRING(0);
5012 } else {
5013 int i;
5014 expr = factory()->NewBinaryOperation(Token::ADD,
5015 COOKED_STRING(0), EXPR(0), POS(0));
5016 for (i = 1; i < expressions->length(); ++i) {
5017 expr = factory()->NewBinaryOperation(Token::ADD,
5018 expr, factory()->NewBinaryOperation(Token::ADD, COOKED_STRING(i),
5019 EXPR(i), POS(i)), POS(i));
5020 }
5021 expr = factory()->NewBinaryOperation(Token::ADD, expr, COOKED_STRING(i),
5022 POS(i));
5023 }
5024 return expr;
5025 } else {
5026 const ZoneList<Expression*>* rawStrings = lit->raw();
5027 int cooked_idx = function_state_->NextMaterializedLiteralIndex();
5028 int raw_idx = function_state_->NextMaterializedLiteralIndex();
5029
caitp (gmail) 2014/11/03 20:30:09 So when it comes to caching the callSiteObj, somet
5030 // GetTemplateCallSite
5031 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone());
5032 args->Add(factory()->NewArrayLiteral(
5033 const_cast<ZoneList<Expression*>*>(cookedStrings), cooked_idx, pos),
5034 zone());
5035 args->Add(factory()->NewArrayLiteral(
5036 const_cast<ZoneList<Expression*>*>(rawStrings), raw_idx, pos), zone());
5037 this->CheckPossibleEvalCall(tag, scope_);
5038 Expression* expr = factory()->NewCallRuntime(
5039 ast_value_factory()->GetTemplateCallSite_string(), NULL, args, start);
5040
5041 // Call TagFn
5042 ZoneList<Expression*>* callArgs = new (zone()) ZoneList<Expression*>(
5043 expressions->length() + 1, zone());
5044 callArgs->Add(expr, zone());
5045 callArgs->AddAll(*expressions, zone());
5046 expr = factory()->NewCall(tag, callArgs, pos);
5047 if (fni_ != NULL) fni_->RemoveLastFunction();
5048 return expr;
5049 }
5050 #undef COOKED_STRING
5051 #undef POS
5052 #undef EXPR
5053 }
5054
4970 } } // namespace v8::internal 5055 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698