ShadowRealmPrototype.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/GlobalObject.h>
  7. #include <LibJS/Runtime/ShadowRealm.h>
  8. #include <LibJS/Runtime/ShadowRealmPrototype.h>
  9. namespace JS {
  10. GC_DEFINE_ALLOCATOR(ShadowRealmPrototype);
  11. // 3.4 Properties of the ShadowRealm Prototype Object, https://tc39.es/proposal-shadowrealm/#sec-properties-of-the-shadowrealm-prototype-object
  12. ShadowRealmPrototype::ShadowRealmPrototype(Realm& realm)
  13. : PrototypeObject(realm.intrinsics().object_prototype())
  14. {
  15. }
  16. void ShadowRealmPrototype::initialize(Realm& realm)
  17. {
  18. auto& vm = this->vm();
  19. Base::initialize(realm);
  20. u8 attr = Attribute::Writable | Attribute::Configurable;
  21. define_native_function(realm, vm.names.evaluate, evaluate, 1, attr);
  22. define_native_function(realm, vm.names.importValue, import_value, 2, attr);
  23. // 3.4.3 ShadowRealm.prototype [ @@toStringTag ], https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype-@@tostringtag
  24. define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, vm.names.ShadowRealm.as_string()), Attribute::Configurable);
  25. }
  26. // 3.4.1 ShadowRealm.prototype.evaluate ( sourceText ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.evaluate
  27. JS_DEFINE_NATIVE_FUNCTION(ShadowRealmPrototype::evaluate)
  28. {
  29. auto source_text = vm.argument(0);
  30. // 1. Let O be this value.
  31. // 2. Perform ? ValidateShadowRealmObject(O).
  32. auto object = TRY(typed_this_object(vm));
  33. // 3. If Type(sourceText) is not String, throw a TypeError exception.
  34. if (!source_text.is_string())
  35. return vm.throw_completion<TypeError>(ErrorType::NotAString, source_text);
  36. // 4. Let callerRealm be the current Realm Record.
  37. auto* caller_realm = vm.current_realm();
  38. // 5. Let evalRealm be O.[[ShadowRealm]].
  39. auto& eval_realm = object->shadow_realm();
  40. // 6. Return ? PerformShadowRealmEval(sourceText, callerRealm, evalRealm).
  41. return perform_shadow_realm_eval(vm, source_text.as_string().byte_string(), *caller_realm, eval_realm);
  42. }
  43. // 3.4.2 ShadowRealm.prototype.importValue ( specifier, exportName ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.importvalue
  44. JS_DEFINE_NATIVE_FUNCTION(ShadowRealmPrototype::import_value)
  45. {
  46. auto specifier = vm.argument(0);
  47. auto export_name = vm.argument(1);
  48. // 1. Let O be this value.
  49. // 2. Perform ? ValidateShadowRealmObject(O).
  50. auto object = TRY(typed_this_object(vm));
  51. // 3. Let specifierString be ? ToString(specifier).
  52. auto specifier_string = TRY(specifier.to_byte_string(vm));
  53. // 4. If Type(exportName) is not String, throw a TypeError exception.
  54. if (!export_name.is_string())
  55. return vm.throw_completion<TypeError>(ErrorType::NotAString, export_name.to_string_without_side_effects());
  56. // 5. Let callerRealm be the current Realm Record.
  57. auto* caller_realm = vm.current_realm();
  58. // 6. Let evalRealm be O.[[ShadowRealm]].
  59. auto& eval_realm = object->shadow_realm();
  60. // 7. Return ShadowRealmImportValue(specifierString, exportName, callerRealm, evalRealm).
  61. return shadow_realm_import_value(vm, move(specifier_string), export_name.as_string().byte_string(), *caller_realm, eval_realm);
  62. }
  63. }