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

Side by Side Diff: src/parser.cc

Issue 768203002: Simplify template literal raw string creation (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 6 years 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 | « no previous file | 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 5273 matching lines...) Expand 10 before | Expand all | Expand 10 after
5284 // Given a TemplateLiteral, produce a list of raw strings, used for generating 5284 // Given a TemplateLiteral, produce a list of raw strings, used for generating
5285 // a CallSite object for a tagged template invocations. 5285 // a CallSite object for a tagged template invocations.
5286 // 5286 //
5287 // A raw string will consist of the unescaped characters of a template span, 5287 // A raw string will consist of the unescaped characters of a template span,
5288 // with end-of-line sequences normalized to U+000A LINE FEEDs, and without 5288 // with end-of-line sequences normalized to U+000A LINE FEEDs, and without
5289 // leading or trailing template delimiters. 5289 // leading or trailing template delimiters.
5290 // 5290 //
5291 5291
5292 DCHECK(total); 5292 DCHECK(total);
5293 5293
5294 Handle<String> source(String::cast(script()->source()));
5295
5296 raw_strings = new (zone()) ZoneList<Expression*>(total, zone()); 5294 raw_strings = new (zone()) ZoneList<Expression*>(total, zone());
5297 5295
5298 uint32_t running_hash = 0; 5296 uint32_t running_hash = 0;
5299 5297
5300 for (int index = 0; index < total; ++index) { 5298 for (int index = 0; index < total; ++index) {
5301 int span_start = cooked_strings->at(index)->position() + 1; 5299 int span_start = cooked_strings->at(index)->position() + 1;
5302 int span_end = lengths->at(index) - 1; 5300 int span_end = lengths->at(index) - 1;
5303 int length; 5301 int length = span_end;
5304 int to_index = 0; 5302 int to_index = 0;
5305 5303
5306 if (index) { 5304 if (index) {
5307 running_hash = StringHasher::ComputeRunningHashOneByte( 5305 running_hash = StringHasher::ComputeRunningHashOneByte(
5308 running_hash, "${}", 3); 5306 running_hash, "${}", 3);
5309 } 5307 }
5310 5308
5311 SmartArrayPointer<char> raw_chars =
5312 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start,
5313 span_end, &length);
5314
5315 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must 5309 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must
5316 // be translated into U+000A (LF). 5310 // be translated into U+000A (LF).
5317 for (int from_index = 0; from_index < length; ++from_index) { 5311 StringCharacterStream stream(String::cast(script()->source()), span_start);
marja 2014/12/01 20:01:34 Oops; this problem was already present before this
caitp (gmail) 2014/12/01 20:05:15 I see. What happens is, we are setting up some ar
arv (Not doing code reviews) 2014/12/01 20:18:44 We'll have to think a bit more about this. The sou
5318 char ch = raw_chars[from_index]; 5312 uc16* normalized_raw_chars = zone()->NewArray<uc16>(length);
5319 if (ch == '\r') { 5313 bool last_was_cr = false;
5320 ch = '\n'; 5314 for (int from_index = 0; from_index < length && stream.HasMore();
caitp (gmail) 2014/12/01 19:57:36 are there cases where the character stream would n
Dmitry Lomov (no reviews) 2014/12/01 19:58:05 Can you assert that at the end of this loop, alway
arv (Not doing code reviews) 2014/12/01 20:18:43 The stream is on the entire script so it will most
5321 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') { 5315 ++from_index) {
5322 ++from_index; 5316 uc16 ch = stream.GetNext();
5323 } 5317 switch (ch) {
5318 case '\r':
5319 normalized_raw_chars[to_index++] = '\n';
5320 last_was_cr = true;
5321 continue;
5322 case '\n':
5323 if (last_was_cr) break;
5324 // Fall through.
5325 default:
5326 normalized_raw_chars[to_index++] = ch;
caitp (gmail) 2014/12/01 19:57:36 move `last_was_cr = false;` here I think? (then ca
arv (Not doing code reviews) 2014/12/01 20:18:44 We need to reset it even if last_was_cr was true.
5324 } 5327 }
5325 raw_chars[to_index++] = ch; 5328 last_was_cr = false;
5326 } 5329 }
5327 5330
5328 Access<UnicodeCache::Utf8Decoder> 5331 const AstRawString* raw_str = ast_value_factory()->GetTwoByteString(
5329 decoder(isolate()->unicode_cache()->utf8_decoder()); 5332 Vector<const uc16>(normalized_raw_chars, to_index));
5330 decoder->Reset(raw_chars.get(), to_index); 5333 Literal* raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1);
5331 int utf16_length = decoder->Utf16Length();
5332 Literal* raw_lit = NULL;
5333 if (utf16_length > 0) {
5334 uc16* utf16_buffer = zone()->NewArray<uc16>(utf16_length);
5335 to_index = decoder->WriteUtf16(utf16_buffer, utf16_length);
5336 running_hash = StringHasher::ComputeRunningHash(
5337 running_hash, utf16_buffer, to_index);
5338 const uint16_t* data = reinterpret_cast<const uint16_t*>(utf16_buffer);
5339 const AstRawString* raw_str = ast_value_factory()->GetTwoByteString(
5340 Vector<const uint16_t>(data, to_index));
5341 raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1);
5342 } else {
5343 raw_lit = factory()->NewStringLiteral(
5344 ast_value_factory()->empty_string(), span_start - 1);
5345 }
5346 DCHECK_NOT_NULL(raw_lit);
5347 raw_strings->Add(raw_lit, zone()); 5334 raw_strings->Add(raw_lit, zone());
5348 } 5335 }
5349 5336
5350 // Hash key is used exclusively by template call site caching. There are no 5337 // Hash key is used exclusively by template call site caching. There are no
5351 // real security implications for unseeded hashes, and no issues with changing 5338 // real security implications for unseeded hashes, and no issues with changing
5352 // the hashing algorithm to improve performance or entropy. 5339 // the hashing algorithm to improve performance or entropy.
5353 *hash = running_hash; 5340 *hash = running_hash;
5354 5341
5355 return raw_strings; 5342 return raw_strings;
5356 } 5343 }
5357 } } // namespace v8::internal 5344 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/harmony/templates.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698