| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2011 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/ast/variables.h" |
| 6 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | #include "src/ast/scopes.h" |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 8 | #include "src/common/globals.h" |
| 9 | #include "src/objects/objects-inl.h" |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
| 14 | // ---------------------------------------------------------------------------- |
| 15 | // Implementation Variable. |
| 16 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 17 | Variable::Variable(Variable* other) |
| 18 | : scope_(other->scope_), |
| 19 | name_(other->name_), |
| Ben Murdoch | f3b273f | 2017-01-17 12:11:28 +0000 | [diff] [blame] | 20 | local_if_not_shadowed_(nullptr), |
| Ben Murdoch | c8c1d9e | 2017-03-08 14:04:23 +0000 | [diff] [blame] | 21 | next_(nullptr), |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 22 | index_(other->index_), |
| 23 | initializer_position_(other->initializer_position_), |
| 24 | bit_field_(other->bit_field_) {} |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 25 | |
| 26 | bool Variable::IsGlobalObjectProperty() const { |
| 27 | // Temporaries are never global, they must always be allocated in the |
| 28 | // activation frame. |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 29 | return (IsDynamicVariableMode(mode()) || mode() == VariableMode::kVar) && |
| Ben Murdoch | 62ed631 | 2017-06-06 11:06:27 +0100 | [diff] [blame] | 30 | scope_ != nullptr && scope_->is_script_scope(); |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| Rubin Xu | 7bc1b61 | 2021-02-16 09:38:50 +0000 | [diff] [blame^] | 33 | bool Variable::IsReplGlobalLet() const { |
| 34 | return scope()->is_repl_mode_scope() && mode() == VariableMode::kLet; |
| 35 | } |
| 36 | |
| 37 | void Variable::RewriteLocationForRepl() { |
| 38 | DCHECK(scope_->is_repl_mode_scope()); |
| 39 | |
| 40 | if (mode() == VariableMode::kLet) { |
| 41 | DCHECK_EQ(location(), VariableLocation::CONTEXT); |
| 42 | bit_field_ = |
| 43 | LocationField::update(bit_field_, VariableLocation::REPL_GLOBAL); |
| 44 | } |
| 45 | } |
| 46 | |
| Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 47 | } // namespace internal |
| 48 | } // namespace v8 |