ShadowRealmConstructor.cpp 2.7 KB

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