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

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: Remove kTemplateLiteral bailout-reason 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
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 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 pre_parse_timer_(NULL) { 808 pre_parse_timer_(NULL) {
809 DCHECK(!script().is_null() || info->source_stream() != NULL); 809 DCHECK(!script().is_null() || info->source_stream() != NULL);
810 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); 810 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
811 set_allow_modules(!info->is_native() && FLAG_harmony_modules); 811 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
812 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); 812 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
813 set_allow_lazy(false); // Must be explicitly enabled. 813 set_allow_lazy(false); // Must be explicitly enabled.
814 set_allow_arrow_functions(FLAG_harmony_arrow_functions); 814 set_allow_arrow_functions(FLAG_harmony_arrow_functions);
815 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 815 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
816 set_allow_classes(FLAG_harmony_classes); 816 set_allow_classes(FLAG_harmony_classes);
817 set_allow_harmony_object_literals(FLAG_harmony_object_literals); 817 set_allow_harmony_object_literals(FLAG_harmony_object_literals);
818 set_allow_harmony_templates(FLAG_harmony_templates);
818 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 819 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
819 ++feature) { 820 ++feature) {
820 use_counts_[feature] = 0; 821 use_counts_[feature] = 0;
821 } 822 }
822 if (info->ast_value_factory() == NULL) { 823 if (info->ast_value_factory() == NULL) {
823 // info takes ownership of AstValueFactory. 824 // info takes ownership of AstValueFactory.
824 info->SetAstValueFactory( 825 info->SetAstValueFactory(
825 new AstValueFactory(zone(), parse_info->hash_seed)); 826 new AstValueFactory(zone(), parse_info->hash_seed));
826 } 827 }
827 } 828 }
(...skipping 4194 matching lines...) Expand 10 before | Expand all | Expand 10 after
5022 info()->SetFunction(result); 5023 info()->SetFunction(result);
5023 5024
5024 // We cannot internalize on a background thread; a foreground task will take 5025 // We cannot internalize on a background thread; a foreground task will take
5025 // care of calling Parser::Internalize just before compilation. 5026 // care of calling Parser::Internalize just before compilation.
5026 5027
5027 if (compile_options() == ScriptCompiler::kProduceParserCache) { 5028 if (compile_options() == ScriptCompiler::kProduceParserCache) {
5028 if (result != NULL) *info_->cached_data() = recorder.GetScriptData(); 5029 if (result != NULL) *info_->cached_data() = recorder.GetScriptData();
5029 log_ = NULL; 5030 log_ = NULL;
5030 } 5031 }
5031 } 5032 }
5033
5034
5035 ParserTraits::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) {
5036 return new (zone()) ParserTraits::TemplateLiteral(zone(), pos);
5037 }
5038
5039
5040 void Parser::AddTemplateSpan(TemplateLiteralState* state, bool tail) {
5041 int pos = scanner()->location().beg_pos;
5042 int end = scanner()->location().end_pos - (tail ? 1 : 2);
5043 const AstRawString* tv = scanner()->CurrentSymbol(ast_value_factory());
5044 Literal* cooked = factory()->NewStringLiteral(tv, pos);
5045 (*state)->AddTemplateSpan(cooked, end, zone());
5046 }
5047
5048
5049 void Parser::AddTemplateExpression(TemplateLiteralState* state,
5050 Expression* expression) {
5051 (*state)->AddExpression(expression, zone());
5052 }
5053
5054
5055 Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state,
5056 int start, Expression* tag) {
5057 #define COOKED_STRING(i) cookedStrings->at(i)
5058 #define POS(i) cookedStrings->at(i)->position()
5059 #define EXPR(i) expressions->at(i)
5060 TemplateLiteral* lit = *state;
5061 int pos = lit->position();
5062 const ZoneList<Expression*>* cookedStrings = lit->cooked();
5063 const ZoneList<Expression*>* expressions = lit->expressions();
5064 CHECK(cookedStrings->length() == (expressions->length() + 1));
5065
5066 if (!tag) {
5067 // Build tree of BinaryOps to simplify code-generation
5068 Expression* expr = NULL;
5069
5070 if (!expressions->length()) {
5071 // Simple case: treat as string literal
5072 expr = COOKED_STRING(0);
5073 } else {
5074 int i;
5075 expr = factory()->NewBinaryOperation(Token::ADD,
5076 COOKED_STRING(0), EXPR(0), POS(0));
5077 for (i = 1; i < expressions->length(); ++i) {
5078 expr = factory()->NewBinaryOperation(Token::ADD,
5079 expr, factory()->NewBinaryOperation(Token::ADD, COOKED_STRING(i),
5080 EXPR(i), POS(i)), POS(i));
5081 }
5082 expr = factory()->NewBinaryOperation(Token::ADD, expr, COOKED_STRING(i),
5083 POS(i));
5084 }
5085 return expr;
5086 } else {
5087 const ZoneList<int>* lengths = lit->lengths();
5088 ZoneList<Expression*>* rawStrings = new (zone()) ZoneList<Expression*>(
5089 cookedStrings->length(), zone());
5090 Handle<String> source(String::cast(script()->source()));
5091
5092 // Build raw strings
5093 for (int i = 0; i < cookedStrings->length(); ++i) {
5094 int offset = POS(i);
5095 int length = lengths->at(i);
5096 SmartArrayPointer<char> rawChars = source->ToCString(
5097 ALLOW_NULLS, FAST_STRING_TRAVERSAL, offset+1, length-1, &length);
arv (Not doing code reviews) 2014/11/11 17:15:15 nit: ws around + and -
5098 int c = 0;
5099 int j = 0;
5100
5101 // Normalize line endings
5102 for (; j < length; ++j) {
5103 char ch = rawChars[j];
5104 if (ch == '\r') {
5105 rawChars[c++] = '\n';
5106 if (j < length - 1 && rawChars[j + 1] == '\n') {
5107 ++j;
5108 }
5109 } else {
5110 rawChars[c++] = ch;
5111 }
5112 }
5113 rawStrings->Add(factory()->NewStringLiteral(
5114 ast_value_factory()->GetOneByteString(
5115 OneByteVector(rawChars.get(), c)), offset), zone());
5116 }
5117
5118 int cooked_idx = function_state_->NextMaterializedLiteralIndex();
5119 int raw_idx = function_state_->NextMaterializedLiteralIndex();
5120
5121 // GetTemplateCallSite
5122 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone());
5123 args->Add(factory()->NewArrayLiteral(
5124 const_cast<ZoneList<Expression*>*>(cookedStrings), cooked_idx, pos),
5125 zone());
5126 args->Add(factory()->NewArrayLiteral(
5127 const_cast<ZoneList<Expression*>*>(rawStrings), raw_idx, pos), zone());
5128 this->CheckPossibleEvalCall(tag, scope_);
5129 Expression* expr = factory()->NewCallRuntime(
5130 ast_value_factory()->get_template_callsite_string(), NULL, args, start);
arv (Not doing code reviews) 2014/11/11 17:15:15 I'm not sure but isn't it better to use the functi
5131
5132 // Call TagFn
5133 ZoneList<Expression*>* callArgs = new (zone()) ZoneList<Expression*>(
5134 expressions->length() + 1, zone());
5135 callArgs->Add(expr, zone());
5136 callArgs->AddAll(*expressions, zone());
5137 expr = factory()->NewCall(tag, callArgs, pos);
5138 if (fni_ != NULL) fni_->RemoveLastFunction();
arv (Not doing code reviews) 2014/11/11 17:15:15 I don't see where the function was added?
caitp (gmail) 2014/11/11 17:48:40 it's copy-pasted, I tried to make sense of it base
5139 return expr;
5140 }
5141 #undef COOKED_STRING
5142 #undef POS
5143 #undef EXPR
5144 }
5145
5032 } } // namespace v8::internal 5146 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698