ProxyConstructor.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/Error.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/ProxyConstructor.h>
  11. #include <LibJS/Runtime/ProxyObject.h>
  12. namespace JS {
  13. // 10.5.14 ProxyCreate ( target, handler ), https://tc39.es/ecma262/#sec-proxycreate
  14. static ThrowCompletionOr<ProxyObject*> proxy_create(VM& vm, Value target, Value handler)
  15. {
  16. auto& realm = *vm.current_realm();
  17. if (!target.is_object())
  18. return vm.throw_completion<TypeError>(ErrorType::ProxyConstructorBadType, "target", TRY_OR_THROW_OOM(vm, target.to_string_without_side_effects()));
  19. if (!handler.is_object())
  20. return vm.throw_completion<TypeError>(ErrorType::ProxyConstructorBadType, "handler", TRY_OR_THROW_OOM(vm, handler.to_string_without_side_effects()));
  21. return ProxyObject::create(realm, target.as_object(), handler.as_object()).ptr();
  22. }
  23. ProxyConstructor::ProxyConstructor(Realm& realm)
  24. : NativeFunction(realm.vm().names.Proxy.as_string(), realm.intrinsics().function_prototype())
  25. {
  26. }
  27. ThrowCompletionOr<void> ProxyConstructor::initialize(Realm& realm)
  28. {
  29. auto& vm = this->vm();
  30. MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
  31. u8 attr = Attribute::Writable | Attribute::Configurable;
  32. define_native_function(realm, vm.names.revocable, revocable, 2, attr);
  33. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  34. return {};
  35. }
  36. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  37. ThrowCompletionOr<Value> ProxyConstructor::call()
  38. {
  39. auto& vm = this->vm();
  40. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.Proxy);
  41. }
  42. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  43. ThrowCompletionOr<NonnullGCPtr<Object>> ProxyConstructor::construct(FunctionObject&)
  44. {
  45. auto& vm = this->vm();
  46. return *TRY(proxy_create(vm, vm.argument(0), vm.argument(1)));
  47. }
  48. // 28.2.2.1 Proxy.revocable ( target, handler ), https://tc39.es/ecma262/#sec-proxy.revocable
  49. JS_DEFINE_NATIVE_FUNCTION(ProxyConstructor::revocable)
  50. {
  51. auto& realm = *vm.current_realm();
  52. // 1. Let p be ? ProxyCreate(target, handler).
  53. auto* proxy = TRY(proxy_create(vm, vm.argument(0), vm.argument(1)));
  54. // 2. Let revokerClosure be a new Abstract Closure with no parameters that captures nothing and performs the following steps when called:
  55. auto revoker_closure = [proxy_handle = make_handle(proxy)](auto&) -> ThrowCompletionOr<Value> {
  56. // a. Let F be the active function object.
  57. // b. Let p be F.[[RevocableProxy]].
  58. auto& proxy = const_cast<ProxyObject&>(*proxy_handle.cell());
  59. // c. If p is null, return undefined.
  60. if (proxy.is_revoked())
  61. return js_undefined();
  62. // d. Set F.[[RevocableProxy]] to null.
  63. // e. Assert: p is a Proxy object.
  64. // f. Set p.[[ProxyTarget]] to null.
  65. // g. Set p.[[ProxyHandler]] to null.
  66. proxy.revoke();
  67. // h. Return undefined.
  68. return js_undefined();
  69. };
  70. // 3. Let revoker be CreateBuiltinFunction(revokerClosure, 0, "", « [[RevocableProxy]] »).
  71. // 4. Set revoker.[[RevocableProxy]] to p.
  72. auto revoker = NativeFunction::create(realm, move(revoker_closure), 0, "");
  73. // 5. Let result be OrdinaryObjectCreate(%Object.prototype%).
  74. auto result = Object::create(realm, realm.intrinsics().object_prototype());
  75. // 6. Perform ! CreateDataPropertyOrThrow(result, "proxy", p).
  76. MUST(result->create_data_property_or_throw(vm.names.proxy, proxy));
  77. // 7. Perform ! CreateDataPropertyOrThrow(result, "revoke", revoker).
  78. MUST(result->create_data_property_or_throw(vm.names.revoke, revoker));
  79. // 8. Return result.
  80. return result;
  81. }
  82. }