| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  |  * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  | #include <AK/Badge.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:51:58 +01:00
										 |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:49 -06:00
										 |  |  | #include <AK/StringView.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-14 12:04:10 +01:00
										 |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | #include <LibGC/CellAllocator.h>
 | 
					
						
							|  |  |  | #include <LibGC/Heap.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-17 11:48:30 +01:00
										 |  |  | #include <LibJS/Bytecode/Builtins.h>
 | 
					
						
							| 
									
										
										
										
											2025-07-19 13:49:30 -07:00
										 |  |  | #include <LibJS/Export.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | #include <LibJS/Heap/Cell.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  | #include <LibJS/Runtime/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-17 11:48:30 +01:00
										 |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 9.3 Realms, https://tc39.es/ecma262/#realm-record
 | 
					
						
							| 
									
										
										
										
											2025-07-19 13:49:30 -07:00
										 |  |  | class JS_API Realm final : public Cell { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC_CELL(Realm, Cell); | 
					
						
							|  |  |  |     GC_DECLARE_ALLOCATOR(Realm); | 
					
						
							| 
									
										
										
										
											2022-08-28 22:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-02-07 16:55:35 +01:00
										 |  |  |     struct HostDefined { | 
					
						
							|  |  |  |         virtual ~HostDefined() = default; | 
					
						
							| 
									
										
										
										
											2022-09-01 21:22:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         virtual void visit_edges(Cell::Visitor&) { } | 
					
						
							| 
									
										
										
										
											2025-04-18 10:24:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         template<typename T> | 
					
						
							|  |  |  |         bool fast_is() const = delete; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         virtual bool is_principal_host_defined() const { return false; } | 
					
						
							|  |  |  |         virtual bool is_synthetic_host_defined() const { return false; } | 
					
						
							| 
									
										
										
										
											2022-02-07 16:55:35 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     template<typename T, typename... Args> | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ref<T> create(Args&&... args) | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-11-14 06:13:46 +13:00
										 |  |  |         auto object = heap().allocate<T>(forward<Args>(args)...); | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |         static_cast<Cell*>(object)->initialize(*this); | 
					
						
							|  |  |  |         return *object; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-17 17:09:20 -07:00
										 |  |  |     static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value); | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 15:03:45 +01:00
										 |  |  |     [[nodiscard]] Object& global_object() const { return *m_global_object; } | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     void set_global_object(GC::Ref<Object> global) { m_global_object = global; } | 
					
						
							| 
									
										
										
										
											2024-10-26 22:07:09 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  |     [[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; } | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     void set_global_environment(GC::Ref<GlobalEnvironment> environment) { m_global_environment = environment; } | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  |     [[nodiscard]] Intrinsics const& intrinsics() const { return *m_intrinsics; } | 
					
						
							|  |  |  |     [[nodiscard]] Intrinsics& intrinsics() { return *m_intrinsics; } | 
					
						
							|  |  |  |     void set_intrinsics(Badge<Intrinsics>, Intrinsics& intrinsics) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(!m_intrinsics); | 
					
						
							|  |  |  |         m_intrinsics = &intrinsics; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 16:55:35 +01:00
										 |  |  |     HostDefined* host_defined() { return m_host_defined; } | 
					
						
							| 
									
										
										
										
											2024-10-21 15:25:09 +13:00
										 |  |  |     HostDefined const* host_defined() const { return m_host_defined; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 16:55:35 +01:00
										 |  |  |     void set_host_defined(OwnPtr<HostDefined> host_defined) { m_host_defined = move(host_defined); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     void define_builtin(Bytecode::Builtin builtin, GC::Ref<NativeFunction> value) | 
					
						
							| 
									
										
										
										
											2023-11-17 11:48:30 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         m_builtins[to_underlying(builtin)] = value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ref<NativeFunction> get_builtin_value(Bytecode::Builtin builtin) | 
					
						
							| 
									
										
										
										
											2023-11-30 19:49:29 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-20 21:29:58 +01:00
										 |  |  |         return *m_builtins[to_underlying(builtin)]; | 
					
						
							| 
									
										
										
										
											2023-11-30 19:49:29 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-08-28 23:51:28 +02:00
										 |  |  |     Realm() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  |     virtual void visit_edges(Visitor&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     GC::Ptr<Intrinsics> m_intrinsics;                // [[Intrinsics]]
 | 
					
						
							|  |  |  |     GC::Ptr<Object> m_global_object;                 // [[GlobalObject]]
 | 
					
						
							|  |  |  |     GC::Ptr<GlobalEnvironment> m_global_environment; // [[GlobalEnv]]
 | 
					
						
							|  |  |  |     OwnPtr<HostDefined> m_host_defined;              // [[HostDefined]]
 | 
					
						
							|  |  |  |     AK::Array<GC::Ptr<NativeFunction>, to_underlying(Bytecode::Builtin::__Count)> m_builtins; | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |