| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-01-31 13:00:02 +01:00
										 |  |  |  * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:49 -06:00
										 |  |  | #include <AK/StringView.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-09 16:52:34 +01:00
										 |  |  | #include <LibJS/Runtime/Completion.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | #include <LibJS/Runtime/Object.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct Variable { | 
					
						
							|  |  |  |     Value value; | 
					
						
							|  |  |  |     DeclarationKind declaration_kind; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | #define JS_ENVIRONMENT(class_, base_class) \
 | 
					
						
							|  |  |  | public:                                    \ | 
					
						
							|  |  |  |     using Base = base_class;               \ | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:49 -06:00
										 |  |  |     virtual StringView class_name() const override { return #class_; } | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  | class Environment : public Cell { | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-06-22 13:30:48 +02:00
										 |  |  |     virtual bool has_this_binding() const { return false; } | 
					
						
							| 
									
										
										
										
											2021-10-09 16:52:34 +01:00
										 |  |  |     virtual ThrowCompletionOr<Value> get_this_binding(GlobalObject&) const { return Value {}; } | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-24 13:24:44 +02:00
										 |  |  |     virtual Object* with_base_object() const { return nullptr; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-09 17:07:32 +01:00
										 |  |  |     virtual ThrowCompletionOr<bool> has_binding([[maybe_unused]] FlyString const& name, [[maybe_unused]] Optional<size_t>* out_index = nullptr) const { return false; } | 
					
						
							| 
									
										
										
										
											2021-10-09 18:53:25 +01:00
										 |  |  |     virtual ThrowCompletionOr<void> create_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool can_be_deleted) { return {}; } | 
					
						
							| 
									
										
										
										
											2021-10-09 19:00:06 +01:00
										 |  |  |     virtual ThrowCompletionOr<void> create_immutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return {}; } | 
					
						
							| 
									
										
										
										
											2021-10-09 19:16:24 +01:00
										 |  |  |     virtual ThrowCompletionOr<void> initialize_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value) { return {}; } | 
					
						
							| 
									
										
										
										
											2021-10-09 19:34:54 +01:00
										 |  |  |     virtual ThrowCompletionOr<void> set_mutable_binding(GlobalObject&, [[maybe_unused]] FlyString const& name, Value, [[maybe_unused]] bool strict) { return {}; } | 
					
						
							| 
									
										
										
										
											2021-10-09 19:43:19 +01:00
										 |  |  |     virtual ThrowCompletionOr<Value> get_binding_value(GlobalObject&, [[maybe_unused]] FlyString const& name, [[maybe_unused]] bool strict) { return Value {}; } | 
					
						
							| 
									
										
										
										
											2021-10-09 19:49:08 +01:00
										 |  |  |     virtual ThrowCompletionOr<bool> delete_binding(GlobalObject&, [[maybe_unused]] FlyString const& name) { return false; } | 
					
						
							| 
									
										
										
										
											2021-06-23 12:26:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-21 23:35:30 +02:00
										 |  |  |     // [[OuterEnv]]
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  |     Environment* outer_environment() { return m_outer_environment; } | 
					
						
							|  |  |  |     Environment const* outer_environment() const { return m_outer_environment; } | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  |     virtual bool is_global_environment() const { return false; } | 
					
						
							|  |  |  |     virtual bool is_declarative_environment() const { return false; } | 
					
						
							|  |  |  |     virtual bool is_function_environment() const { return false; } | 
					
						
							| 
									
										
										
										
											2021-06-23 13:04:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     template<typename T> | 
					
						
							|  |  |  |     bool fast_is() const = delete; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:49 -06:00
										 |  |  |     virtual StringView class_name() const override { return "Environment"sv; } | 
					
						
							| 
									
										
										
										
											2021-06-23 13:04:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-07 11:36:44 +02:00
										 |  |  |     // This flag is set on the entire variable environment chain when direct eval() is performed.
 | 
					
						
							|  |  |  |     // It is used to disable non-local variable access caching.
 | 
					
						
							|  |  |  |     bool is_permanently_screwed_by_eval() const { return m_permanently_screwed_by_eval; } | 
					
						
							|  |  |  |     void set_permanently_screwed_by_eval(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  |     explicit Environment(Environment* parent); | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual void visit_edges(Visitor&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  |     virtual bool is_environment() const final { return true; } | 
					
						
							| 
									
										
										
										
											2021-06-23 12:29:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-31 12:36:43 +01:00
										 |  |  |     bool m_permanently_screwed_by_eval { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 12:24:46 +02:00
										 |  |  |     Environment* m_outer_environment { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-11-28 16:02:27 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |