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

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: Add DCHECK in PushBack and remove some unused functions 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 | « src/parser.h ('k') | src/scanner.h » ('j') | src/scanner.h » ('J')
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 5175 matching lines...) Expand 10 before | Expand all | Expand 10 after
5186 5186
5187 ParserTraits::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) { 5187 ParserTraits::TemplateLiteralState Parser::OpenTemplateLiteral(int pos) {
5188 return new (zone()) ParserTraits::TemplateLiteral(zone(), pos); 5188 return new (zone()) ParserTraits::TemplateLiteral(zone(), pos);
5189 } 5189 }
5190 5190
5191 5191
5192 void Parser::AddTemplateSpan(TemplateLiteralState* state, bool tail) { 5192 void Parser::AddTemplateSpan(TemplateLiteralState* state, bool tail) {
5193 int pos = scanner()->location().beg_pos; 5193 int pos = scanner()->location().beg_pos;
5194 int end = scanner()->location().end_pos - (tail ? 1 : 2); 5194 int end = scanner()->location().end_pos - (tail ? 1 : 2);
5195 const AstRawString* tv = scanner()->CurrentSymbol(ast_value_factory()); 5195 const AstRawString* tv = scanner()->CurrentSymbol(ast_value_factory());
5196 const AstRawString* trv = scanner()->CurrentRawSymbol(ast_value_factory());
5196 Literal* cooked = factory()->NewStringLiteral(tv, pos); 5197 Literal* cooked = factory()->NewStringLiteral(tv, pos);
5197 (*state)->AddTemplateSpan(cooked, end, zone()); 5198 Literal* raw = factory()->NewStringLiteral(trv, pos);
5199 (*state)->AddTemplateSpan(cooked, raw, end, zone());
5198 } 5200 }
5199 5201
5200 5202
5201 void Parser::AddTemplateExpression(TemplateLiteralState* state, 5203 void Parser::AddTemplateExpression(TemplateLiteralState* state,
5202 Expression* expression) { 5204 Expression* expression) {
5203 (*state)->AddExpression(expression, zone()); 5205 (*state)->AddExpression(expression, zone());
5204 } 5206 }
5205 5207
5206 5208
5207 Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state, int start, 5209 Expression* Parser::CloseTemplateLiteral(TemplateLiteralState* state, int start,
5208 Expression* tag) { 5210 Expression* tag) {
5209 TemplateLiteral* lit = *state; 5211 TemplateLiteral* lit = *state;
5210 int pos = lit->position(); 5212 int pos = lit->position();
5211 const ZoneList<Expression*>* cooked_strings = lit->cooked(); 5213 const ZoneList<Expression*>* cooked_strings = lit->cooked();
5214 const ZoneList<Expression*>* raw_strings = lit->raw();
5212 const ZoneList<Expression*>* expressions = lit->expressions(); 5215 const ZoneList<Expression*>* expressions = lit->expressions();
5213 CHECK(cooked_strings->length() == (expressions->length() + 1)); 5216 DCHECK_EQ(cooked_strings->length(), raw_strings->length());
5217 DCHECK_EQ(cooked_strings->length(), expressions->length() + 1);
5214 5218
5215 if (!tag) { 5219 if (!tag) {
5216 // Build tree of BinaryOps to simplify code-generation 5220 // Build tree of BinaryOps to simplify code-generation
5217 Expression* expr = NULL; 5221 Expression* expr = NULL;
5218 5222
5219 if (expressions->length() == 0) { 5223 if (expressions->length() == 0) {
5220 // Simple case: treat as string literal 5224 // Simple case: treat as string literal
5221 expr = cooked_strings->at(0); 5225 expr = cooked_strings->at(0);
5222 } else { 5226 } else {
5223 int i; 5227 int i;
5224 Expression* cooked_str = cooked_strings->at(0); 5228 Expression* cooked_str = cooked_strings->at(0);
5225 expr = factory()->NewBinaryOperation( 5229 expr = factory()->NewBinaryOperation(
5226 Token::ADD, cooked_str, expressions->at(0), cooked_str->position()); 5230 Token::ADD, cooked_str, expressions->at(0), cooked_str->position());
5227 for (i = 1; i < expressions->length(); ++i) { 5231 for (i = 1; i < expressions->length(); ++i) {
5228 cooked_str = cooked_strings->at(i); 5232 cooked_str = cooked_strings->at(i);
5229 expr = factory()->NewBinaryOperation( 5233 expr = factory()->NewBinaryOperation(
5230 Token::ADD, expr, factory()->NewBinaryOperation( 5234 Token::ADD, expr, factory()->NewBinaryOperation(
5231 Token::ADD, cooked_str, expressions->at(i), 5235 Token::ADD, cooked_str, expressions->at(i),
5232 cooked_str->position()), 5236 cooked_str->position()),
5233 cooked_str->position()); 5237 cooked_str->position());
5234 } 5238 }
5235 cooked_str = cooked_strings->at(i); 5239 cooked_str = cooked_strings->at(i);
5236 expr = factory()->NewBinaryOperation(Token::ADD, expr, cooked_str, 5240 expr = factory()->NewBinaryOperation(Token::ADD, expr, cooked_str,
5237 cooked_str->position()); 5241 cooked_str->position());
5238 } 5242 }
5239 return expr; 5243 return expr;
5240 } else { 5244 } else {
5241 uint32_t hash; 5245 uint32_t hash = ComputeTemplateLiteralHash(lit);
5242 ZoneList<Expression*>* raw_strings = TemplateRawStrings(lit, &hash);
5243 Handle<String> source(String::cast(script()->source()));
5244 5246
5245 int cooked_idx = function_state_->NextMaterializedLiteralIndex(); 5247 int cooked_idx = function_state_->NextMaterializedLiteralIndex();
5246 int raw_idx = function_state_->NextMaterializedLiteralIndex(); 5248 int raw_idx = function_state_->NextMaterializedLiteralIndex();
5247 5249
5248 // GetTemplateCallSite 5250 // GetTemplateCallSite
5249 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone()); 5251 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(4, zone());
5250 args->Add(factory()->NewArrayLiteral( 5252 args->Add(factory()->NewArrayLiteral(
5251 const_cast<ZoneList<Expression*>*>(cooked_strings), 5253 const_cast<ZoneList<Expression*>*>(cooked_strings),
5252 cooked_idx, pos), 5254 cooked_idx, pos),
5253 zone()); 5255 zone());
5254 args->Add( 5256 args->Add(
5255 factory()->NewArrayLiteral( 5257 factory()->NewArrayLiteral(
5256 const_cast<ZoneList<Expression*>*>(raw_strings), raw_idx, pos), 5258 const_cast<ZoneList<Expression*>*>(raw_strings), raw_idx, pos),
5257 zone()); 5259 zone());
5258 5260
5259 // Ensure hash is suitable as an Smi value 5261 // Ensure hash is suitable as a Smi value
5260 Smi* hash_obj = Smi::cast(Internals::IntToSmi(static_cast<int>(hash))); 5262 Smi* hash_obj = Smi::cast(Internals::IntToSmi(static_cast<int>(hash)));
5261 args->Add(factory()->NewSmiLiteral(hash_obj->value(), pos), zone()); 5263 args->Add(factory()->NewSmiLiteral(hash_obj->value(), pos), zone());
5262 5264
5263 this->CheckPossibleEvalCall(tag, scope_); 5265 this->CheckPossibleEvalCall(tag, scope_);
5264 Expression* call_site = factory()->NewCallRuntime( 5266 Expression* call_site = factory()->NewCallRuntime(
5265 ast_value_factory()->get_template_callsite_string(), NULL, args, start); 5267 ast_value_factory()->get_template_callsite_string(), NULL, args, start);
5266 5268
5267 // Call TagFn 5269 // Call TagFn
5268 ZoneList<Expression*>* call_args = 5270 ZoneList<Expression*>* call_args =
5269 new (zone()) ZoneList<Expression*>(expressions->length() + 1, zone()); 5271 new (zone()) ZoneList<Expression*>(expressions->length() + 1, zone());
5270 call_args->Add(call_site, zone()); 5272 call_args->Add(call_site, zone());
5271 call_args->AddAll(*expressions, zone()); 5273 call_args->AddAll(*expressions, zone());
5272 return factory()->NewCall(tag, call_args, pos); 5274 return factory()->NewCall(tag, call_args, pos);
5273 } 5275 }
5274 } 5276 }
5275 5277
5276 5278
5277 ZoneList<Expression*>* Parser::TemplateRawStrings(const TemplateLiteral* lit, 5279 uint32_t Parser::ComputeTemplateLiteralHash(const TemplateLiteral* lit) {
5278 uint32_t* hash) { 5280 const ZoneList<Expression*>* raw_strings = lit->raw();
5279 const ZoneList<int>* lengths = lit->lengths(); 5281 int total = raw_strings->length();
5280 const ZoneList<Expression*>* cooked_strings = lit->cooked();
5281 int total = lengths->length();
5282 ZoneList<Expression*>* raw_strings;
5283
5284 // Given a TemplateLiteral, produce a list of raw strings, used for generating
5285 // a CallSite object for a tagged template invocations.
5286 //
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
5289 // leading or trailing template delimiters.
5290 //
5291
5292 DCHECK(total); 5282 DCHECK(total);
5293 5283
5294 Handle<String> source(String::cast(script()->source()));
5295
5296 raw_strings = new (zone()) ZoneList<Expression*>(total, zone());
5297
5298 uint32_t running_hash = 0; 5284 uint32_t running_hash = 0;
5299 5285
5300 for (int index = 0; index < total; ++index) { 5286 for (int index = 0; index < total; ++index) {
5301 int span_start = cooked_strings->at(index)->position() + 1;
5302 int span_end = lengths->at(index) - 1;
5303 int length;
5304 int to_index = 0;
5305
5306 if (index) { 5287 if (index) {
5307 running_hash = StringHasher::ComputeRunningHashOneByte( 5288 running_hash = StringHasher::ComputeRunningHashOneByte(
5308 running_hash, "${}", 3); 5289 running_hash, "${}", 3);
5309 } 5290 }
5310 5291
5311 SmartArrayPointer<char> raw_chars = 5292 const AstRawString* raw_string =
5312 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start, 5293 raw_strings->at(index)->AsLiteral()->raw_value()->AsString();
5313 span_end, &length); 5294 if (raw_string->is_one_byte()) {
5314 5295 const char* data = reinterpret_cast<const char*>(raw_string->raw_data());
5315 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must 5296 running_hash = StringHasher::ComputeRunningHashOneByte(
5316 // be translated into U+000A (LF). 5297 running_hash, data, raw_string->length());
5317 for (int from_index = 0; from_index < length; ++from_index) { 5298 } else {
5318 char ch = raw_chars[from_index]; 5299 const uc16* data = reinterpret_cast<const uc16*>(raw_string->raw_data());
5319 if (ch == '\r') { 5300 running_hash = StringHasher::ComputeRunningHash(running_hash, data,
5320 ch = '\n'; 5301 raw_string->length());
5321 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') {
5322 ++from_index;
5323 }
5324 }
5325 raw_chars[to_index++] = ch;
5326 } 5302 }
5327
5328 Access<UnicodeCache::Utf8Decoder>
5329 decoder(isolate()->unicode_cache()->utf8_decoder());
5330 decoder->Reset(raw_chars.get(), to_index);
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());
5348 } 5303 }
5349 5304
5350 // Hash key is used exclusively by template call site caching. There are no 5305 return running_hash;
5351 // real security implications for unseeded hashes, and no issues with changing
5352 // the hashing algorithm to improve performance or entropy.
5353 *hash = running_hash;
5354
5355 return raw_strings;
5356 } 5306 }
5357 } } // namespace v8::internal 5307 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/scanner.h » ('j') | src/scanner.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698