|
@@ -1,13 +1,16 @@
|
|
|
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
|
|
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
*
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
*/
|
|
|
|
|
|
+#include <AK/TypeCasts.h>
|
|
|
#include <LibJS/Runtime/GlobalEnvironment.h>
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
#include <LibJS/Runtime/Realm.h>
|
|
|
#include <LibJS/Runtime/VM.h>
|
|
|
+#include <LibJS/Heap/DeferGC.h>
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
@@ -17,25 +20,89 @@ Realm* Realm::create(VM& vm)
|
|
|
return vm.heap().allocate_without_global_object<Realm>();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_defined_realm(VM& vm, Function<Value(Realm&)> create_global_object, Function<Value(Realm&)> create_global_this_value)
|
|
|
+{
|
|
|
+ DeferGC defer_gc(vm.heap());
|
|
|
+
|
|
|
+
|
|
|
+ auto* realm = Realm::create(vm);
|
|
|
+
|
|
|
+
|
|
|
+ auto new_context = make<ExecutionContext>(vm.heap());
|
|
|
+
|
|
|
+
|
|
|
+ new_context->function = nullptr;
|
|
|
+
|
|
|
+
|
|
|
+ new_context->realm = realm;
|
|
|
+
|
|
|
+
|
|
|
+ new_context->script_or_module = {};
|
|
|
+
|
|
|
+
|
|
|
+ vm.push_execution_context(*new_context);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Value global;
|
|
|
+ if (create_global_object)
|
|
|
+ global = create_global_object(*realm);
|
|
|
+ else
|
|
|
+ global = js_undefined();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Value this_value;
|
|
|
+ if (create_global_this_value)
|
|
|
+ this_value = create_global_this_value(*realm);
|
|
|
+ else
|
|
|
+ this_value = js_undefined();
|
|
|
+
|
|
|
+
|
|
|
+ realm->set_global_object(global, this_value);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ realm->global_object().initialize_global_object();
|
|
|
+
|
|
|
+
|
|
|
+ return new_context;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
-void Realm::set_global_object(GlobalObject& global_object, Object* this_value)
|
|
|
+void Realm::set_global_object(Value global_object, Value this_value)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
+ if (global_object.is_undefined()) {
|
|
|
+
|
|
|
+ VERIFY_NOT_REACHED();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ VERIFY(global_object.is_object());
|
|
|
+ VERIFY(is<GlobalObject>(global_object.as_object()));
|
|
|
|
|
|
|
|
|
- global_object.set_associated_realm(*this);
|
|
|
+ verify_cast<GlobalObject>(global_object.as_object()).set_associated_realm(*this);
|
|
|
|
|
|
|
|
|
- if (!this_value)
|
|
|
- this_value = &global_object;
|
|
|
+ if (this_value.is_undefined())
|
|
|
+ this_value = global_object;
|
|
|
+
|
|
|
+
|
|
|
+ VERIFY(this_value.is_object());
|
|
|
|
|
|
|
|
|
- m_global_object = &global_object;
|
|
|
+ m_global_object = &verify_cast<GlobalObject>(global_object.as_object());
|
|
|
|
|
|
|
|
|
|
|
|
- m_global_environment = global_object.heap().allocate_without_global_object<GlobalEnvironment>(global_object, *this_value);
|
|
|
+ m_global_environment = m_global_object->heap().allocate_without_global_object<GlobalEnvironment>(verify_cast<GlobalObject>(global_object.as_object()), this_value.as_object());
|
|
|
|
|
|
|
|
|
}
|