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

Side by Side Diff: src/parser.cc

Issue 742643003: Cache template literal callSiteObj (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Shorten hash-string 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
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/harmony/templates.js » ('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 5213 matching lines...) Expand 10 before | Expand all | Expand 10 after
5224 Token::ADD, cooked_str, expressions->at(i), 5224 Token::ADD, cooked_str, expressions->at(i),
5225 cooked_str->position()), 5225 cooked_str->position()),
5226 cooked_str->position()); 5226 cooked_str->position());
5227 } 5227 }
5228 cooked_str = cooked_strings->at(i); 5228 cooked_str = cooked_strings->at(i);
5229 expr = factory()->NewBinaryOperation(Token::ADD, expr, cooked_str, 5229 expr = factory()->NewBinaryOperation(Token::ADD, expr, cooked_str,
5230 cooked_str->position()); 5230 cooked_str->position());
5231 } 5231 }
5232 return expr; 5232 return expr;
5233 } else { 5233 } else {
5234 ZoneList<Expression*>* raw_strings = TemplateRawStrings(lit); 5234 uint32_t hash;
5235 ZoneList<Expression*>* raw_strings = TemplateRawStrings(lit, &hash);
5235 Handle<String> source(String::cast(script()->source())); 5236 Handle<String> source(String::cast(script()->source()));
5236 5237
5237 int cooked_idx = function_state_->NextMaterializedLiteralIndex(); 5238 int cooked_idx = function_state_->NextMaterializedLiteralIndex();
5238 int raw_idx = function_state_->NextMaterializedLiteralIndex(); 5239 int raw_idx = function_state_->NextMaterializedLiteralIndex();
5239 5240
5240 // GetTemplateCallSite 5241 // GetTemplateCallSite
5241 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone()); 5242 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone());
5242 args->Add(factory()->NewArrayLiteral( 5243 args->Add(factory()->NewArrayLiteral(
5243 const_cast<ZoneList<Expression*>*>(cooked_strings), 5244 const_cast<ZoneList<Expression*>*>(cooked_strings),
5244 cooked_idx, pos), 5245 cooked_idx, pos),
5245 zone()); 5246 zone());
5246 args->Add( 5247 args->Add(
5247 factory()->NewArrayLiteral( 5248 factory()->NewArrayLiteral(
5248 const_cast<ZoneList<Expression*>*>(raw_strings), raw_idx, pos), 5249 const_cast<ZoneList<Expression*>*>(raw_strings), raw_idx, pos),
5249 zone()); 5250 zone());
5251 args->Add(factory()->NewSmiLiteral(hash, pos), zone());
5250 this->CheckPossibleEvalCall(tag, scope_); 5252 this->CheckPossibleEvalCall(tag, scope_);
5251 Expression* call_site = factory()->NewCallRuntime( 5253 Expression* call_site = factory()->NewCallRuntime(
5252 ast_value_factory()->get_template_callsite_string(), NULL, args, start); 5254 ast_value_factory()->get_template_callsite_string(), NULL, args, start);
5253 5255
5254 // Call TagFn 5256 // Call TagFn
5255 ZoneList<Expression*>* call_args = 5257 ZoneList<Expression*>* call_args =
5256 new (zone()) ZoneList<Expression*>(expressions->length() + 1, zone()); 5258 new (zone()) ZoneList<Expression*>(expressions->length() + 1, zone());
5257 call_args->Add(call_site, zone()); 5259 call_args->Add(call_site, zone());
5258 call_args->AddAll(*expressions, zone()); 5260 call_args->AddAll(*expressions, zone());
5259 return factory()->NewCall(tag, call_args, pos); 5261 return factory()->NewCall(tag, call_args, pos);
5260 } 5262 }
5261 } 5263 }
5262 5264
5263 5265
5264 ZoneList<Expression*>* Parser::TemplateRawStrings(const TemplateLiteral* lit) { 5266 ZoneList<Expression*>* Parser::TemplateRawStrings(const TemplateLiteral* lit,
5267 uint32_t* hash) {
5265 const ZoneList<int>* lengths = lit->lengths(); 5268 const ZoneList<int>* lengths = lit->lengths();
5266 const ZoneList<Expression*>* cooked_strings = lit->cooked(); 5269 const ZoneList<Expression*>* cooked_strings = lit->cooked();
5267 int total = lengths->length(); 5270 int total = lengths->length();
5268 ZoneList<Expression*>* raw_strings; 5271 ZoneList<Expression*>* raw_strings;
5269 5272
5270 // Given a TemplateLiteral, produce a list of raw strings, used for generating 5273 // Given a TemplateLiteral, produce a list of raw strings, used for generating
5271 // a CallSite object for a tagged template invocations. 5274 // a CallSite object for a tagged template invocations.
5272 // 5275 //
5273 // A raw string will consist of the unescaped characters of a template span, 5276 // A raw string will consist of the unescaped characters of a template span,
5274 // with end-of-line sequences normalized to U+000A LINE FEEDs, and without 5277 // with end-of-line sequences normalized to U+000A LINE FEEDs, and without
5275 // leading or trailing template delimiters. 5278 // leading or trailing template delimiters.
5276 // 5279 //
5277 5280
5278 DCHECK(total); 5281 DCHECK(total);
5279 5282
5280 Handle<String> source(String::cast(script()->source())); 5283 Handle<String> source(String::cast(script()->source()));
5281 5284
5282 raw_strings = new (zone()) ZoneList<Expression*>(total, zone()); 5285 raw_strings = new (zone()) ZoneList<Expression*>(total, zone());
5283 5286
5287 int num_dummy_chars = (total - 1) * 3;
arv (Not doing code reviews) 2014/11/19 22:12:31 You lost the comment here.
5288 for (int index = 0; index < total; ++index) {
5289 num_dummy_chars += lengths->at(index);
5290 }
5291
5292 Vector<uint8_t> hash_string = Vector<uint8_t>::New(num_dummy_chars);
5293 num_dummy_chars = 0;
5294
5284 for (int index = 0; index < total; ++index) { 5295 for (int index = 0; index < total; ++index) {
5285 int span_start = cooked_strings->at(index)->position() + 1; 5296 int span_start = cooked_strings->at(index)->position() + 1;
5286 int span_end = lengths->at(index) - 1; 5297 int span_end = lengths->at(index) - 1;
5287 int length; 5298 int length;
5288 int to_index = 0; 5299 int to_index = 0;
5289 5300
5301 if (index) {
5302 hash_string[num_dummy_chars++] = '$';
5303 hash_string[num_dummy_chars++] = '{';
5304 hash_string[num_dummy_chars++] = '}';
5305 }
5306
5290 SmartArrayPointer<char> raw_chars = 5307 SmartArrayPointer<char> raw_chars =
5291 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start, 5308 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start,
5292 span_end, &length); 5309 span_end, &length);
5293 5310
5294 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must 5311 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must
5295 // be translated into U+000A (LF). 5312 // be translated into U+000A (LF).
5296 for (int from_index = 0; from_index < length; ++from_index) { 5313 for (int from_index = 0; from_index < length; ++from_index) {
5297 char ch = raw_chars[from_index]; 5314 char ch = raw_chars[from_index];
5298 if (ch == '\r') { 5315 if (ch == '\r') {
5299 ch = '\n'; 5316 ch = '\n';
5300 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') { 5317 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') {
5301 ++from_index; 5318 ++from_index;
5302 } 5319 }
5303 } 5320 }
5321 hash_string[num_dummy_chars++] = ch;
5304 raw_chars[to_index++] = ch; 5322 raw_chars[to_index++] = ch;
5305 } 5323 }
5306 5324
5307 const AstRawString* raw_str = ast_value_factory()->GetOneByteString( 5325 const AstRawString* raw_str = ast_value_factory()->GetOneByteString(
5308 OneByteVector(raw_chars.get(), to_index)); 5326 OneByteVector(raw_chars.get(), to_index));
5309 Literal* raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1); 5327 Literal* raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1);
5310 raw_strings->Add(raw_lit, zone()); 5328 raw_strings->Add(raw_lit, zone());
5311 } 5329 }
5312 5330
5331 int utf16_length;
5332 *hash = StringHasher::ComputeUtf8Hash(Vector<const char>::cast(hash_string),
5333 num_dummy_chars, &utf16_length);
5334 hash_string.Dispose();
5335
5313 return raw_strings; 5336 return raw_strings;
5314 } 5337 }
5315 } } // namespace v8::internal 5338 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/harmony/templates.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698