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 5213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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) * 4; | |
arv (Not doing code reviews)
2014/11/19 17:18:46
Can you add a comment about why this is needed?
| |
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 hash_string[num_dummy_chars++] = '}'; | |
5306 } | |
5307 | |
5290 SmartArrayPointer<char> raw_chars = | 5308 SmartArrayPointer<char> raw_chars = |
5291 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start, | 5309 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start, |
arv (Not doing code reviews)
2014/11/19 17:18:46
I'm actually starting to wonder if this is correct
| |
5292 span_end, &length); | 5310 span_end, &length); |
5293 | 5311 |
5294 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must | 5312 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must |
5295 // be translated into U+000A (LF). | 5313 // be translated into U+000A (LF). |
5296 for (int from_index = 0; from_index < length; ++from_index) { | 5314 for (int from_index = 0; from_index < length; ++from_index) { |
5297 char ch = raw_chars[from_index]; | 5315 char ch = raw_chars[from_index]; |
5298 if (ch == '\r') { | 5316 if (ch == '\r') { |
5299 ch = '\n'; | 5317 ch = '\n'; |
5300 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') { | 5318 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') { |
5301 ++from_index; | 5319 ++from_index; |
5302 } | 5320 } |
5303 } | 5321 } |
5322 hash_string[num_dummy_chars++] = ch; | |
5304 raw_chars[to_index++] = ch; | 5323 raw_chars[to_index++] = ch; |
5305 } | 5324 } |
5306 | 5325 |
5307 const AstRawString* raw_str = ast_value_factory()->GetOneByteString( | 5326 const AstRawString* raw_str = ast_value_factory()->GetOneByteString( |
5308 OneByteVector(raw_chars.get(), to_index)); | 5327 OneByteVector(raw_chars.get(), to_index)); |
arv (Not doing code reviews)
2014/11/19 17:18:46
I don't think this is correct. The string might ne
| |
5309 Literal* raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1); | 5328 Literal* raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1); |
5310 raw_strings->Add(raw_lit, zone()); | 5329 raw_strings->Add(raw_lit, zone()); |
5311 } | 5330 } |
5312 | 5331 |
5332 int utf16_length; | |
5333 *hash = StringHasher::ComputeUtf8Hash(Vector<const char>::cast(hash_string), | |
5334 num_dummy_chars, &utf16_length); | |
5335 hash_string.Dispose(); | |
5336 | |
5313 return raw_strings; | 5337 return raw_strings; |
5314 } | 5338 } |
5315 } } // namespace v8::internal | 5339 } } // namespace v8::internal |
OLD | NEW |