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