| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-06-21 23:17:24 +02:00
										 |  |  |  * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 13:31:21 -05:00
										 |  |  | #include <LibJS/Interpreter.h>
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | #include <LibJS/Runtime/DeclarativeEnvironment.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-08 13:31:21 -05:00
										 |  |  | #include <LibJS/Runtime/Error.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  | #include <LibJS/Runtime/FunctionObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-27 15:18:55 +02:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-08 13:31:21 -05:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | DeclarativeEnvironment::DeclarativeEnvironment() | 
					
						
							|  |  |  |     : Environment(nullptr) | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_scope) | 
					
						
							|  |  |  |     : Environment(parent_scope) | 
					
						
							| 
									
										
										
										
											2021-06-22 00:56:14 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | DeclarativeEnvironment::DeclarativeEnvironment(HashMap<FlyString, Variable> variables, Environment* parent_scope) | 
					
						
							|  |  |  |     : Environment(parent_scope) | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  |     , m_variables(move(variables)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | DeclarativeEnvironment::~DeclarativeEnvironment() | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | void DeclarativeEnvironment::visit_edges(Visitor& visitor) | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-28 10:13:47 +01:00
										 |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  |     for (auto& it : m_variables) | 
					
						
							|  |  |  |         visitor.visit(it.value.value); | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  |     for (auto& it : m_bindings) | 
					
						
							|  |  |  |         visitor.visit(it.value.value); | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | Optional<Variable> DeclarativeEnvironment::get_from_environment(FlyString const& name) const | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return m_variables.get(name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-02 00:14:38 +02:00
										 |  |  | bool DeclarativeEnvironment::put_into_environment(FlyString const& name, Variable variable) | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  |     m_variables.set(name, variable); | 
					
						
							| 
									
										
										
										
											2021-07-02 00:14:38 +02:00
										 |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | bool DeclarativeEnvironment::delete_from_environment(FlyString const& name) | 
					
						
							| 
									
										
										
										
											2021-06-08 04:00:53 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     return m_variables.remove(name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | // 9.1.1.1.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-hasbinding-n
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | bool DeclarativeEnvironment::has_binding(FlyString const& name) const | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return m_bindings.contains(name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 13:25:57 +02:00
										 |  |  | // 9.1.1.1.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-declarative-environment-records-createmutablebinding-n-d
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | void DeclarativeEnvironment::create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted) | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto result = m_bindings.set(name, | 
					
						
							|  |  |  |         Binding { | 
					
						
							|  |  |  |             .value = {}, | 
					
						
							|  |  |  |             .strict = false, | 
					
						
							|  |  |  |             .mutable_ = true, | 
					
						
							|  |  |  |             .can_be_deleted = can_be_deleted, | 
					
						
							|  |  |  |             .initialized = false, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     VERIFY(result == AK::HashSetResult::InsertedNewEntry); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 13:25:57 +02:00
										 |  |  | // 9.1.1.1.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-createimmutablebinding-n-s
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | void DeclarativeEnvironment::create_immutable_binding(GlobalObject&, FlyString const& name, bool strict) | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto result = m_bindings.set(name, | 
					
						
							|  |  |  |         Binding { | 
					
						
							|  |  |  |             .value = {}, | 
					
						
							|  |  |  |             .strict = strict, | 
					
						
							|  |  |  |             .mutable_ = false, | 
					
						
							|  |  |  |             .can_be_deleted = false, | 
					
						
							|  |  |  |             .initialized = false, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     VERIFY(result == AK::HashSetResult::InsertedNewEntry); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 13:25:57 +02:00
										 |  |  | // 9.1.1.1.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-declarative-environment-records-initializebinding-n-v
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | void DeclarativeEnvironment::initialize_binding(GlobalObject&, FlyString const& name, Value value) | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto it = m_bindings.find(name); | 
					
						
							|  |  |  |     VERIFY(it != m_bindings.end()); | 
					
						
							|  |  |  |     VERIFY(it->value.initialized == false); | 
					
						
							|  |  |  |     it->value.value = value; | 
					
						
							|  |  |  |     it->value.initialized = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 13:25:57 +02:00
										 |  |  | // 9.1.1.1.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | void DeclarativeEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict) | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto it = m_bindings.find(name); | 
					
						
							|  |  |  |     if (it == m_bindings.end()) { | 
					
						
							|  |  |  |         if (strict) { | 
					
						
							|  |  |  |             global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         create_mutable_binding(global_object, name, true); | 
					
						
							|  |  |  |         initialize_binding(global_object, name, value); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (it->value.strict) | 
					
						
							|  |  |  |         strict = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!it->value.initialized) { | 
					
						
							|  |  |  |         global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (it->value.mutable_) { | 
					
						
							|  |  |  |         it->value.value = value; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (strict) { | 
					
						
							|  |  |  |             global_object.vm().throw_exception<TypeError>(global_object, ErrorType::InvalidAssignToConst); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 13:25:57 +02:00
										 |  |  | // 9.1.1.1.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | Value DeclarativeEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool) | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto it = m_bindings.find(name); | 
					
						
							|  |  |  |     VERIFY(it != m_bindings.end()); | 
					
						
							|  |  |  |     if (!it->value.initialized) { | 
					
						
							|  |  |  |         global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name); | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return it->value.value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 13:25:57 +02:00
										 |  |  | // 9.1.1.1.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | bool DeclarativeEnvironment::delete_binding(GlobalObject&, FlyString const& name) | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto it = m_bindings.find(name); | 
					
						
							|  |  |  |     VERIFY(it != m_bindings.end()); | 
					
						
							|  |  |  |     if (!it->value.can_be_deleted) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     m_bindings.remove(it); | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | } |