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/parsing/parameter-initializer-rewriter.h" | 5 #include "src/parsing/parameter-initializer-rewriter.h" |
6 | 6 |
7 #include "src/ast/ast.h" | 7 #include "src/ast/ast.h" |
8 #include "src/ast/ast-expression-visitor.h" | 8 #include "src/ast/ast-expression-visitor.h" |
9 #include "src/ast/scopes.h" | 9 #include "src/ast/scopes.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 | 16 |
17 class Rewriter final : public AstExpressionVisitor { | 17 class Rewriter final : public AstExpressionVisitor { |
18 public: | 18 public: |
19 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* param_scope) | 19 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope, |
| 20 Scope* param_scope) |
20 : AstExpressionVisitor(stack_limit, initializer), | 21 : AstExpressionVisitor(stack_limit, initializer), |
| 22 old_scope_(old_scope), |
21 param_scope_(param_scope) {} | 23 param_scope_(param_scope) {} |
22 | 24 |
23 private: | 25 private: |
24 void VisitExpression(Expression* expr) override {} | 26 void VisitExpression(Expression* expr) override {} |
25 | 27 |
26 void VisitFunctionLiteral(FunctionLiteral* expr) override; | 28 void VisitFunctionLiteral(FunctionLiteral* expr) override; |
27 void VisitClassLiteral(ClassLiteral* expr) override; | 29 void VisitClassLiteral(ClassLiteral* expr) override; |
28 void VisitVariableProxy(VariableProxy* expr) override; | 30 void VisitVariableProxy(VariableProxy* expr) override; |
29 | 31 |
30 void VisitBlock(Block* stmt) override; | 32 void VisitBlock(Block* stmt) override; |
31 void VisitTryCatchStatement(TryCatchStatement* stmt) override; | 33 void VisitTryCatchStatement(TryCatchStatement* stmt) override; |
32 void VisitWithStatement(WithStatement* stmt) override; | 34 void VisitWithStatement(WithStatement* stmt) override; |
33 | 35 |
| 36 Scope* old_scope_; |
34 Scope* param_scope_; | 37 Scope* param_scope_; |
35 }; | 38 }; |
36 | 39 |
37 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { | 40 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { |
38 function_literal->scope()->ReplaceOuterScope(param_scope_); | 41 if (function_literal->scope()->outer_scope() == old_scope_) { |
| 42 function_literal->scope()->ReplaceOuterScope(param_scope_); |
| 43 } |
39 } | 44 } |
40 | 45 |
41 | 46 |
42 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { | 47 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { |
43 class_literal->scope()->ReplaceOuterScope(param_scope_); | |
44 if (class_literal->extends() != nullptr) { | 48 if (class_literal->extends() != nullptr) { |
45 Visit(class_literal->extends()); | 49 Visit(class_literal->extends()); |
46 } | 50 } |
47 // No need to visit the constructor since it will have the class | 51 // No need to visit the constructor since it will have the class |
48 // scope on its scope chain. | 52 // scope on its scope chain. |
49 ZoneList<ObjectLiteralProperty*>* props = class_literal->properties(); | 53 ZoneList<ObjectLiteralProperty*>* props = class_literal->properties(); |
50 for (int i = 0; i < props->length(); ++i) { | 54 for (int i = 0; i < props->length(); ++i) { |
51 ObjectLiteralProperty* prop = props->at(i); | 55 ObjectLiteralProperty* prop = props->at(i); |
52 if (!prop->key()->IsLiteral()) { | 56 if (!prop->key()->IsLiteral()) { |
53 Visit(prop->key()); | 57 Visit(prop->key()); |
(...skipping 12 matching lines...) Expand all Loading... |
66 } | 70 } |
67 } else { | 71 } else { |
68 // Ensure that temporaries we find are already in the correct scope. | 72 // Ensure that temporaries we find are already in the correct scope. |
69 DCHECK(proxy->var()->mode() != TEMPORARY || | 73 DCHECK(proxy->var()->mode() != TEMPORARY || |
70 proxy->var()->scope() == param_scope_->ClosureScope()); | 74 proxy->var()->scope() == param_scope_->ClosureScope()); |
71 } | 75 } |
72 } | 76 } |
73 | 77 |
74 | 78 |
75 void Rewriter::VisitBlock(Block* stmt) { | 79 void Rewriter::VisitBlock(Block* stmt) { |
76 if (stmt->scope() != nullptr) | 80 if (stmt->scope() != nullptr && stmt->scope()->outer_scope() == old_scope_) { |
77 stmt->scope()->ReplaceOuterScope(param_scope_); | 81 stmt->scope()->ReplaceOuterScope(param_scope_); |
78 else | 82 } |
79 VisitStatements(stmt->statements()); | 83 VisitStatements(stmt->statements()); |
80 } | 84 } |
81 | 85 |
82 | 86 |
83 void Rewriter::VisitTryCatchStatement(TryCatchStatement* stmt) { | 87 void Rewriter::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 88 if (stmt->scope()->outer_scope() == old_scope_) { |
| 89 stmt->scope()->ReplaceOuterScope(param_scope_); |
| 90 } |
84 Visit(stmt->try_block()); | 91 Visit(stmt->try_block()); |
85 stmt->scope()->ReplaceOuterScope(param_scope_); | |
86 } | 92 } |
87 | 93 |
88 | 94 |
89 void Rewriter::VisitWithStatement(WithStatement* stmt) { | 95 void Rewriter::VisitWithStatement(WithStatement* stmt) { |
90 Visit(stmt->expression()); | 96 Visit(stmt->expression()); |
91 stmt->scope()->ReplaceOuterScope(param_scope_); | 97 if (stmt->scope()->outer_scope() == old_scope_) { |
| 98 stmt->scope()->ReplaceOuterScope(param_scope_); |
| 99 } |
92 } | 100 } |
93 | 101 |
94 | 102 |
95 } // anonymous namespace | 103 } // anonymous namespace |
96 | 104 |
97 void ReparentParameterExpressionScope(uintptr_t stack_limit, Expression* expr, | 105 void ReparentParameterExpressionScope(uintptr_t stack_limit, Expression* expr, |
98 Scope* param_scope) { | 106 Scope* old_scope, Scope* param_scope) { |
99 // The only case that uses this code is block scopes for parameters containing | 107 // The only case that uses this code is block scopes for parameters containing |
100 // sloppy eval. | 108 // sloppy eval. |
101 DCHECK(param_scope->is_block_scope()); | 109 DCHECK(param_scope->is_block_scope()); |
102 DCHECK(param_scope->is_declaration_scope()); | 110 DCHECK(param_scope->is_declaration_scope()); |
103 DCHECK(param_scope->calls_sloppy_eval()); | 111 DCHECK(param_scope->calls_sloppy_eval()); |
104 DCHECK(param_scope->outer_scope()->is_function_scope()); | 112 DCHECK(param_scope->outer_scope()->is_function_scope()); |
105 | 113 |
106 Rewriter rewriter(stack_limit, expr, param_scope); | 114 Rewriter rewriter(stack_limit, expr, old_scope, param_scope); |
107 rewriter.Run(); | 115 rewriter.Run(); |
108 } | 116 } |
109 | 117 |
110 | 118 |
111 } // namespace internal | 119 } // namespace internal |
112 } // namespace v8 | 120 } // namespace v8 |
OLD | NEW |