2021-10-13 21:09:45 +01:00
|
|
|
|
/*
|
2023-04-13 00:47:15 +02:00
|
|
|
|
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
2024-10-23 17:27:20 +13:00
|
|
|
|
* Copyright (c) 2024, Shannon Booth <shannon@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 {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(ShadowRealmConstructor);
|
2023-11-19 09:45:05 +01:00
|
|
|
|
|
2021-10-13 21:09:45 +01:00
|
|
|
|
// 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)
|
2023-04-13 00:47:15 +02:00
|
|
|
|
: NativeFunction(realm.vm().names.ShadowRealm.as_string(), realm.intrinsics().function_prototype())
|
2021-10-13 21:09:45 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void ShadowRealmConstructor::initialize(Realm& realm)
|
2021-10-13 21:09:45 +01:00
|
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 18:15:19 +13:00
|
|
|
|
// 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm
|
2024-11-15 04:01:23 +13:00
|
|
|
|
ThrowCompletionOr<GC::Ref<Object>> ShadowRealmConstructor::construct(FunctionObject& new_target)
|
2024-10-23 18:15:19 +13:00
|
|
|
|
{
|
|
|
|
|
auto& vm = this->vm();
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
2024-11-03 21:29:30 +13:00
|
|
|
|
// 2. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%ShadowRealm.prototype%", « [[ShadowRealm]] »).
|
2024-10-23 18:15:19 +13:00
|
|
|
|
auto object = TRY(ordinary_create_from_constructor<ShadowRealm>(vm, new_target, &Intrinsics::shadow_realm_prototype));
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
2024-11-03 21:29:30 +13:00
|
|
|
|
// 3. Let callerContext be the running execution context.
|
|
|
|
|
// 4. Perform ? InitializeHostDefinedRealm().
|
|
|
|
|
// 5. Let innerContext be the running execution context.
|
|
|
|
|
auto inner_context = TRY(Realm::initialize_host_defined_realm(vm, nullptr, nullptr));
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
2024-11-03 21:29:30 +13:00
|
|
|
|
// 6. Remove innerContext from the execution context stack and restore callerContext as the running execution context.
|
|
|
|
|
vm.pop_execution_context();
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
2024-11-03 21:29:30 +13:00
|
|
|
|
// 7. Let realmRec be the Realm of innerContext.
|
|
|
|
|
auto& realm_record = *inner_context->realm;
|
|
|
|
|
|
|
|
|
|
// 8. Set O.[[ShadowRealm]] to realmRec.
|
2024-10-23 18:15:19 +13:00
|
|
|
|
object->set_shadow_realm(realm_record);
|
2021-10-13 21:09:45 +01:00
|
|
|
|
|
2024-11-17 05:55:20 +13:00
|
|
|
|
// 9. Perform ? HostInitializeShadowRealm(realmRec, innerContext, O).
|
2024-11-03 21:29:30 +13:00
|
|
|
|
TRY(vm.host_initialize_shadow_realm(realm_record, move(inner_context), object));
|
|
|
|
|
|
|
|
|
|
// 10. Return O.
|
2022-12-14 19:18:10 +00:00
|
|
|
|
return object;
|
2021-10-13 21:09:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|