| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |  * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  | #include <AK/TypeCasts.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-05 12:55:25 +02:00
										 |  |  | #include <LibJS/Heap/DeferGC.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:51:58 +01:00
										 |  |  | #include <LibJS/Runtime/GlobalEnvironment.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:51:58 +01:00
										 |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:51:58 +01:00
										 |  |  | // 9.3.1 CreateRealm ( ), https://tc39.es/ecma262/#sec-createrealm
 | 
					
						
							|  |  |  | Realm* Realm::create(VM& vm) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  |     // 1. Let realmRec be a new Realm Record.
 | 
					
						
							|  |  |  |     auto* realm = vm.heap().allocate_without_realm<Realm>(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Perform CreateIntrinsics(realmRec).
 | 
					
						
							|  |  |  |     Intrinsics::create(*realm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Set realmRec.[[GlobalObject]] to undefined.
 | 
					
						
							|  |  |  |     // 4. Set realmRec.[[GlobalEnv]] to undefined.
 | 
					
						
							|  |  |  |     // 5. Set realmRec.[[TemplateMap]] to a new empty List.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. Return realmRec.
 | 
					
						
							|  |  |  |     return realm; | 
					
						
							| 
									
										
										
										
											2022-02-07 18:51:58 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  | // 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
 | 
					
						
							| 
									
										
										
										
											2022-08-28 15:03:45 +01:00
										 |  |  | ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_defined_realm(VM& vm, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value) | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     DeferGC defer_gc(vm.heap()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Let realm be CreateRealm().
 | 
					
						
							|  |  |  |     auto* realm = Realm::create(vm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Let newContext be a new execution context.
 | 
					
						
							|  |  |  |     auto new_context = make<ExecutionContext>(vm.heap()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Set the Function of newContext to null.
 | 
					
						
							|  |  |  |     new_context->function = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. Set the Realm of newContext to realm.
 | 
					
						
							|  |  |  |     new_context->realm = realm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. Set the ScriptOrModule of newContext to null.
 | 
					
						
							|  |  |  |     new_context->script_or_module = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 6. Push newContext onto the execution context stack; newContext is now the running execution context.
 | 
					
						
							|  |  |  |     vm.push_execution_context(*new_context); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 7. If the host requires use of an exotic object to serve as realm's global object,
 | 
					
						
							|  |  |  |     //    let global be such an object created in a host-defined manner.
 | 
					
						
							|  |  |  |     //    Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 15:03:45 +01:00
										 |  |  |     Object* global = nullptr; | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  |     if (create_global_object) | 
					
						
							|  |  |  |         global = create_global_object(*realm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 8. If the host requires that the this binding in realm's global scope return an object other than the global object,
 | 
					
						
							|  |  |  |     //    let thisValue be such an object created in a host-defined manner.
 | 
					
						
							|  |  |  |     //    Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 15:03:45 +01:00
										 |  |  |     Object* this_value = nullptr; | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  |     if (create_global_this_value) | 
					
						
							|  |  |  |         this_value = create_global_this_value(*realm); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
 | 
					
						
							|  |  |  |     realm->set_global_object(global, this_value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // NOTE: Steps 10 & 11 are somewhat ad-hoc, since we store intrinsics on the global object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
 | 
					
						
							|  |  |  |     // 11. Create any host-defined global object properties on globalObj.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 14:42:50 +01:00
										 |  |  |     realm->global_object().initialize(*realm); | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 12. Return unused.
 | 
					
						
							|  |  |  |     return new_context; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  | // 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
 | 
					
						
							| 
									
										
										
										
											2022-08-28 15:03:45 +01:00
										 |  |  | void Realm::set_global_object(Object* global_object, Object* this_value) | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  |     // 1. If globalObj is undefined, then
 | 
					
						
							| 
									
										
										
										
											2022-08-06 01:01:13 +01:00
										 |  |  |     if (global_object == nullptr) { | 
					
						
							| 
									
										
										
										
											2022-08-06 01:02:41 +01:00
										 |  |  |         // a. Let intrinsics be realmRec.[[Intrinsics]].
 | 
					
						
							|  |  |  |         // b. Set globalObj to OrdinaryObjectCreate(intrinsics.[[%Object.prototype%]]).
 | 
					
						
							|  |  |  |         // NOTE: We allocate a proper GlobalObject directly as this plain object is
 | 
					
						
							|  |  |  |         //       turned into one via SetDefaultGlobalBindings in the spec.
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:50 +01:00
										 |  |  |         global_object = heap().allocate_without_realm<GlobalObject>(*this); | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  |     // 2. Assert: Type(globalObj) is Object.
 | 
					
						
							| 
									
										
										
										
											2022-08-06 01:01:13 +01:00
										 |  |  |     VERIFY(global_object); | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. If thisValue is undefined, set thisValue to globalObj.
 | 
					
						
							| 
									
										
										
										
											2022-08-06 01:01:13 +01:00
										 |  |  |     if (this_value == nullptr) | 
					
						
							| 
									
										
										
										
											2022-08-04 21:21:50 +02:00
										 |  |  |         this_value = global_object; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Non-standard
 | 
					
						
							| 
									
										
										
										
											2022-08-06 01:01:13 +01:00
										 |  |  |     VERIFY(this_value); | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. Set realmRec.[[GlobalObject]] to globalObj.
 | 
					
						
							| 
									
										
										
										
											2022-08-06 01:01:13 +01:00
										 |  |  |     m_global_object = global_object; | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
 | 
					
						
							|  |  |  |     // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:50 +01:00
										 |  |  |     m_global_environment = m_global_object->heap().allocate_without_realm<GlobalEnvironment>(*global_object, *this_value); | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |     // 7. Return unused.
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:36:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  | void Realm::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-27 00:54:55 +01:00
										 |  |  |     visitor.visit(m_intrinsics); | 
					
						
							| 
									
										
										
										
											2021-09-11 19:20:07 +01:00
										 |  |  |     visitor.visit(m_global_object); | 
					
						
							|  |  |  |     visitor.visit(m_global_environment); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |