OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/v8.h" |
| 6 |
| 7 #include "src/parameter-initializer-rewriter.h" |
| 8 |
| 9 #include "src/ast.h" |
| 10 #include "src/ast-expression-visitor.h" |
| 11 #include "src/scopes.h" |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 |
| 16 namespace { |
| 17 |
| 18 |
| 19 class Rewriter final : public AstExpressionVisitor { |
| 20 public: |
| 21 Rewriter(uintptr_t stack_limit, Expression* initializer, Scope* old_scope, |
| 22 Scope* new_scope) |
| 23 : AstExpressionVisitor(stack_limit, initializer), |
| 24 old_scope_(old_scope), |
| 25 new_scope_(new_scope) {} |
| 26 |
| 27 private: |
| 28 void VisitExpression(Expression* expr) override {} |
| 29 |
| 30 void VisitFunctionLiteral(FunctionLiteral* expr) override; |
| 31 void VisitClassLiteral(ClassLiteral* expr) override; |
| 32 void VisitVariableProxy(VariableProxy* expr) override; |
| 33 |
| 34 Scope* old_scope_; |
| 35 Scope* new_scope_; |
| 36 }; |
| 37 |
| 38 |
| 39 void Rewriter::VisitFunctionLiteral(FunctionLiteral* function_literal) { |
| 40 function_literal->scope()->ReplaceOuterScope(new_scope_); |
| 41 } |
| 42 |
| 43 |
| 44 void Rewriter::VisitClassLiteral(ClassLiteral* class_literal) { |
| 45 class_literal->scope()->ReplaceOuterScope(new_scope_); |
| 46 if (class_literal->extends() != nullptr) { |
| 47 Visit(class_literal->extends()); |
| 48 } |
| 49 // No need to visit the constructor since it will have the class |
| 50 // scope on its scope chain. |
| 51 ZoneList<ObjectLiteralProperty*>* props = class_literal->properties(); |
| 52 for (int i = 0; i < props->length(); ++i) { |
| 53 ObjectLiteralProperty* prop = props->at(i); |
| 54 if (!prop->key()->IsLiteral()) { |
| 55 Visit(prop->key()); |
| 56 } |
| 57 // No need to visit the values, since all values are functions with |
| 58 // the class scope on their scope chain. |
| 59 DCHECK(prop->value()->IsFunctionLiteral()); |
| 60 } |
| 61 } |
| 62 |
| 63 |
| 64 void Rewriter::VisitVariableProxy(VariableProxy* proxy) { |
| 65 DCHECK(!proxy->is_resolved()); |
| 66 if (old_scope_->RemoveUnresolved(proxy)) { |
| 67 new_scope_->AddUnresolved(proxy); |
| 68 } |
| 69 } |
| 70 |
| 71 |
| 72 } // anonymous namespace |
| 73 |
| 74 |
| 75 void RewriteParameterInitializerScope(uintptr_t stack_limit, |
| 76 Expression* initializer, Scope* old_scope, |
| 77 Scope* new_scope) { |
| 78 Rewriter rewriter(stack_limit, initializer, old_scope, new_scope); |
| 79 rewriter.Run(); |
| 80 } |
| 81 |
| 82 |
| 83 } // namespace internal |
| 84 } // namespace v8 |
OLD | NEW |