2021-10-13 21:09:45 +01:00
|
|
|
/*
|
2022-08-16 20:33:17 +01:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2021-10-13 21:09:45 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
|
|
|
#include <LibJS/Runtime/ShadowRealm.h>
|
|
|
|
#include <LibJS/Runtime/ShadowRealmConstructor.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
// 3.2 The ShadowRealm Constructor, https://tc39.es/proposal-shadowrealm/#sec-shadowrealm-constructor
|
2022-08-16 00:20:49 +01:00
|
|
|
ShadowRealmConstructor::ShadowRealmConstructor(Realm& realm)
|
2022-09-14 19:10:27 -04:00
|
|
|
: NativeFunction(realm.vm().names.ShadowRealm.as_string(), *realm.intrinsics().function_prototype())
|
2021-10-13 21:09:45 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
ThrowCompletionOr<void> ShadowRealmConstructor::initialize(Realm& realm)
|
2021-10-13 21:09:45 +01:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
|
|
// 3.3.1 ShadowRealm.prototype, https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype
|
2022-08-27 00:54:55 +01:00
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().shadow_realm_prototype(), 0);
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
return {};
|
2021-10-13 21:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Value> ShadowRealmConstructor::call()
|
2021-10-13 21:09:45 +01:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
// 1. If NewTarget is undefined, throw a TypeError exception.
|
2022-08-16 20:33:17 +01:00
|
|
|
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.ShadowRealm);
|
2021-10-13 21:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm
|
2022-12-14 19:18:10 +00:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<Object>> ShadowRealmConstructor::construct(FunctionObject& new_target)
|
2021-10-13 21:09:45 +01:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
|
|
|
|
// 3. Let realmRec be CreateRealm().
|
2022-12-13 20:49:50 +00:00
|
|
|
auto realm = Realm::create(vm);
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
|
|
// 5. Let context be a new execution context.
|
|
|
|
auto context = ExecutionContext { vm.heap() };
|
|
|
|
|
|
|
|
// 6. Set the Function of context to null.
|
|
|
|
context.function = nullptr;
|
|
|
|
|
|
|
|
// 7. Set the Realm of context to realmRec.
|
|
|
|
context.realm = realm;
|
|
|
|
|
|
|
|
// 8. Set the ScriptOrModule of context to null.
|
2022-01-17 14:48:22 +01:00
|
|
|
// Note: This is already the default value.
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
|
|
// 2. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%ShadowRealm.prototype%", « [[ShadowRealm]], [[ExecutionContext]] »).
|
|
|
|
// 4. Set O.[[ShadowRealm]] to realmRec.
|
|
|
|
// 9. Set O.[[ExecutionContext]] to context.
|
2022-12-14 18:34:32 +00:00
|
|
|
auto object = TRY(ordinary_create_from_constructor<ShadowRealm>(vm, new_target, &Intrinsics::shadow_realm_prototype, *realm, move(context)));
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
|
|
// 10. Perform ? SetRealmGlobalObject(realmRec, undefined, undefined).
|
2022-08-28 17:37:04 +01:00
|
|
|
realm->set_global_object(nullptr, nullptr);
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
|
|
// 11. Perform ? SetDefaultGlobalBindings(O.[[ShadowRealm]]).
|
2022-08-28 17:37:04 +01:00
|
|
|
auto& global_object = set_default_global_bindings(object->shadow_realm());
|
|
|
|
|
|
|
|
// FIXME: 12. Perform ? HostInitializeShadowRealm(O.[[ShadowRealm]]).
|
2023-02-27 22:41:47 +00:00
|
|
|
MUST_OR_THROW_OOM(global_object.initialize(object->shadow_realm()));
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
|
|
// 13. Return O.
|
2022-12-14 19:18:10 +00:00
|
|
|
return object;
|
2021-10-13 21:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|