ShadowRealmConstructor.cpp 2.7 KB

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