OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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/ast/ast.h" | 5 #include "src/ast/ast.h" |
6 #include "src/messages.h" | 6 #include "src/messages.h" |
7 #include "src/parsing/parameter-initializer-rewriter.h" | 7 #include "src/parsing/parameter-initializer-rewriter.h" |
8 #include "src/parsing/parser.h" | 8 #include "src/parsing/parser.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 ZoneList<Expression*>* arguments = | 223 ZoneList<Expression*>* arguments = |
224 new (zone()) ZoneList<Expression*>(3, zone()); | 224 new (zone()) ZoneList<Expression*>(3, zone()); |
225 // We have at least 1 parameter. | 225 // We have at least 1 parameter. |
226 arguments->Add( | 226 arguments->Add( |
227 factory()->NewStringLiteral(name, descriptor_->declaration_pos), | 227 factory()->NewStringLiteral(name, descriptor_->declaration_pos), |
228 zone()); | 228 zone()); |
229 CallRuntime* initialize; | 229 CallRuntime* initialize; |
230 | 230 |
231 if (IsImmutableVariableMode(descriptor_->mode)) { | 231 if (IsImmutableVariableMode(descriptor_->mode)) { |
232 arguments->Add(value, zone()); | 232 arguments->Add(value, zone()); |
| 233 value = NULL; // zap the value to avoid the unnecessary assignment |
| 234 |
233 // Construct the call to Runtime_InitializeConstGlobal | 235 // Construct the call to Runtime_InitializeConstGlobal |
234 // and add it to the initialization statement block. | 236 // and add it to the initialization statement block. |
235 // Note that the function does different things depending on | 237 // Note that the function does different things depending on |
236 // the number of arguments (1 or 2). | 238 // the number of arguments (1 or 2). |
237 initialize = factory()->NewCallRuntime(Runtime::kInitializeConstGlobal, | 239 initialize = |
238 arguments, value->position()); | 240 factory()->NewCallRuntime(Runtime::kInitializeConstGlobal, arguments, |
239 value = NULL; // zap the value to avoid the unnecessary assignment | 241 descriptor_->initialization_pos); |
240 } else { | 242 } else { |
241 // Add language mode. | 243 // Add language mode. |
242 // We may want to pass singleton to avoid Literal allocations. | 244 // We may want to pass singleton to avoid Literal allocations. |
243 LanguageMode language_mode = initialization_scope->language_mode(); | 245 LanguageMode language_mode = initialization_scope->language_mode(); |
244 arguments->Add( | 246 arguments->Add(factory()->NewNumberLiteral(language_mode, |
245 factory()->NewNumberLiteral(language_mode, RelocInfo::kNoPosition), | 247 descriptor_->declaration_pos), |
246 zone()); | 248 zone()); |
247 | 249 |
248 // Be careful not to assign a value to the global variable if | 250 // Be careful not to assign a value to the global variable if |
249 // we're in a with. The initialization value should not | 251 // we're in a with. The initialization value should not |
250 // necessarily be stored in the global object in that case, | 252 // necessarily be stored in the global object in that case, |
251 // which is why we need to generate a separate assignment node. | 253 // which is why we need to generate a separate assignment node. |
252 if (value != NULL && !descriptor_->scope->inside_with()) { | 254 if (value != NULL && !descriptor_->scope->inside_with()) { |
253 arguments->Add(value, zone()); | 255 arguments->Add(value, zone()); |
| 256 value = NULL; // zap the value to avoid the unnecessary assignment |
254 // Construct the call to Runtime_InitializeVarGlobal | 257 // Construct the call to Runtime_InitializeVarGlobal |
255 // and add it to the initialization statement block. | 258 // and add it to the initialization statement block. |
256 initialize = factory()->NewCallRuntime(Runtime::kInitializeVarGlobal, | 259 initialize = |
257 arguments, value->position()); | 260 factory()->NewCallRuntime(Runtime::kInitializeVarGlobal, arguments, |
258 value = NULL; // zap the value to avoid the unnecessary assignment | 261 descriptor_->declaration_pos); |
259 } else { | 262 } else { |
260 initialize = NULL; | 263 initialize = NULL; |
261 } | 264 } |
262 } | 265 } |
263 | 266 |
264 if (initialize != NULL) { | 267 if (initialize != NULL) { |
265 block_->statements()->Add( | 268 block_->statements()->Add( |
266 factory()->NewExpressionStatement(initialize, initialize->position()), | 269 factory()->NewExpressionStatement(initialize, initialize->position()), |
267 zone()); | 270 zone()); |
268 } | 271 } |
269 } else if (value != nullptr && (descriptor_->mode == CONST_LEGACY || | 272 } else if (value != nullptr && (descriptor_->mode == CONST_LEGACY || |
270 IsLexicalVariableMode(descriptor_->mode))) { | 273 IsLexicalVariableMode(descriptor_->mode))) { |
271 // Constant initializations always assign to the declared constant which | 274 // Constant initializations always assign to the declared constant which |
272 // is always at the function scope level. This is only relevant for | 275 // is always at the function scope level. This is only relevant for |
273 // dynamically looked-up variables and constants (the | 276 // dynamically looked-up variables and constants (the |
274 // start context for constant lookups is always the function context, | 277 // start context for constant lookups is always the function context, |
275 // while it is the top context for var declared variables). Sigh... | 278 // while it is the top context for var declared variables). Sigh... |
276 // For 'let' and 'const' declared variables in harmony mode the | 279 // For 'let' and 'const' declared variables in harmony mode the |
277 // initialization also always assigns to the declared variable. | 280 // initialization also always assigns to the declared variable. |
278 DCHECK_NOT_NULL(proxy); | 281 DCHECK_NOT_NULL(proxy); |
279 DCHECK_NOT_NULL(proxy->var()); | 282 DCHECK_NOT_NULL(proxy->var()); |
280 DCHECK_NOT_NULL(value); | 283 DCHECK_NOT_NULL(value); |
281 // Add break location for destructured sub-pattern. | 284 // Add break location for destructured sub-pattern. |
282 int pos = IsSubPattern() ? pattern->position() : value->position(); | 285 int pos = |
| 286 IsSubPattern() ? pattern->position() : descriptor_->initialization_pos; |
283 Assignment* assignment = | 287 Assignment* assignment = |
284 factory()->NewAssignment(Token::INIT, proxy, value, pos); | 288 factory()->NewAssignment(Token::INIT, proxy, value, pos); |
285 block_->statements()->Add( | 289 block_->statements()->Add( |
286 factory()->NewExpressionStatement(assignment, pos), zone()); | 290 factory()->NewExpressionStatement(assignment, pos), zone()); |
287 value = NULL; | 291 value = NULL; |
288 } | 292 } |
289 | 293 |
290 // Add an assignment node to the initialization statement block if we still | 294 // Add an assignment node to the initialization statement block if we still |
291 // have a pending initialization value. | 295 // have a pending initialization value. |
292 if (value != NULL) { | 296 if (value != NULL) { |
293 DCHECK(descriptor_->mode == VAR); | 297 DCHECK(descriptor_->mode == VAR); |
294 // 'var' initializations are simply assignments (with all the consequences | 298 // 'var' initializations are simply assignments (with all the consequences |
295 // if they are inside a 'with' statement - they may change a 'with' object | 299 // if they are inside a 'with' statement - they may change a 'with' object |
296 // property). | 300 // property). |
297 VariableProxy* proxy = initialization_scope->NewUnresolved(factory(), name); | 301 VariableProxy* proxy = initialization_scope->NewUnresolved(factory(), name); |
298 // Add break location for destructured sub-pattern. | 302 // Add break location for destructured sub-pattern. |
299 int pos = IsSubPattern() ? pattern->position() : value->position(); | 303 int pos = |
| 304 IsSubPattern() ? pattern->position() : descriptor_->initialization_pos; |
300 Assignment* assignment = | 305 Assignment* assignment = |
301 factory()->NewAssignment(Token::INIT, proxy, value, pos); | 306 factory()->NewAssignment(Token::INIT, proxy, value, pos); |
302 block_->statements()->Add( | 307 block_->statements()->Add( |
303 factory()->NewExpressionStatement(assignment, pos), zone()); | 308 factory()->NewExpressionStatement(assignment, pos), zone()); |
304 } | 309 } |
305 } | 310 } |
306 | 311 |
307 | 312 |
308 Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) { | 313 Variable* Parser::PatternRewriter::CreateTempVar(Expression* value) { |
309 auto temp = scope()->NewTemporary(ast_value_factory()->empty_string()); | 314 auto temp = scope()->NewTemporary(ast_value_factory()->empty_string()); |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 NOT_A_PATTERN(TryFinallyStatement) | 620 NOT_A_PATTERN(TryFinallyStatement) |
616 NOT_A_PATTERN(UnaryOperation) | 621 NOT_A_PATTERN(UnaryOperation) |
617 NOT_A_PATTERN(VariableDeclaration) | 622 NOT_A_PATTERN(VariableDeclaration) |
618 NOT_A_PATTERN(WhileStatement) | 623 NOT_A_PATTERN(WhileStatement) |
619 NOT_A_PATTERN(WithStatement) | 624 NOT_A_PATTERN(WithStatement) |
620 NOT_A_PATTERN(Yield) | 625 NOT_A_PATTERN(Yield) |
621 | 626 |
622 #undef NOT_A_PATTERN | 627 #undef NOT_A_PATTERN |
623 } // namespace internal | 628 } // namespace internal |
624 } // namespace v8 | 629 } // namespace v8 |
OLD | NEW |