ProxyConstructor.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. JS_DEFINE_ALLOCATOR(ProxyConstructor);
  14. // 10.5.14 ProxyCreate ( target, handler ), https://tc39.es/ecma262/#sec-proxycreate
  15. static ThrowCompletionOr<ProxyObject*> proxy_create(VM& vm, Value target, Value handler)
  16. {
  17. auto& realm = *vm.current_realm();
  18. // 1. If target is not an Object, throw a TypeError exception.
  19. if (!target.is_object())
  20. return vm.throw_completion<TypeError>(ErrorType::ProxyConstructorBadType, "target", target.to_string_without_side_effects());
  21. // 2. If handler is not an Object, throw a TypeError exception.
  22. if (!handler.is_object())
  23. return vm.throw_completion<TypeError>(ErrorType::ProxyConstructorBadType, "handler", handler.to_string_without_side_effects());
  24. // 3. Let P be MakeBasicObject(« [[ProxyHandler]], [[ProxyTarget]] »).
  25. // 4. Set P's essential internal methods, except for [[Call]] and [[Construct]], to the definitions specified in 10.5.
  26. // 5. IsCallable(target) is true, then
  27. // a. Set P.[[Call]] as specified in 10.5.12.
  28. // b. If IsConstructor(target) is true, then
  29. // i. Set P.[[Construct]] as specified in 10.5.13.
  30. // 6. Set P.[[ProxyTarget]] to target.
  31. // 7. Set P.[[ProxyHandler]] to handler.
  32. // 8. Return P.
  33. return ProxyObject::create(realm, target.as_object(), handler.as_object()).ptr();
  34. }
  35. ProxyConstructor::ProxyConstructor(Realm& realm)
  36. : NativeFunction(realm.vm().names.Proxy.as_string(), realm.intrinsics().function_prototype())
  37. {
  38. }
  39. void ProxyConstructor::initialize(Realm& realm)
  40. {
  41. auto& vm = this->vm();
  42. Base::initialize(realm);
  43. u8 attr = Attribute::Writable | Attribute::Configurable;
  44. define_native_function(realm, vm.names.revocable, revocable, 2, attr);
  45. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  46. }
  47. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  48. ThrowCompletionOr<Value> ProxyConstructor::call()
  49. {
  50. auto& vm = this->vm();
  51. // 1. If NewTarget is undefined, throw a TypeError exception.
  52. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.Proxy);
  53. }
  54. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  55. ThrowCompletionOr<NonnullGCPtr<Object>> ProxyConstructor::construct(FunctionObject&)
  56. {
  57. auto& vm = this->vm();
  58. auto target = vm.argument(0);
  59. auto handler = vm.argument(1);
  60. // 2. Return ? ProxyCreate(target, handler).
  61. return *TRY(proxy_create(vm, target, handler));
  62. }
  63. // 28.2.2.1 Proxy.revocable ( target, handler ), https://tc39.es/ecma262/#sec-proxy.revocable
  64. JS_DEFINE_NATIVE_FUNCTION(ProxyConstructor::revocable)
  65. {
  66. auto& realm = *vm.current_realm();
  67. auto target = vm.argument(0);
  68. auto handler = vm.argument(1);
  69. // 1. Let p be ? ProxyCreate(target, handler).
  70. auto* proxy = TRY(proxy_create(vm, target, handler));
  71. // 2. Let revokerClosure be a new Abstract Closure with no parameters that captures nothing and performs the following steps when called:
  72. auto revoker_closure = [proxy_handle = make_handle(proxy)](auto&) -> ThrowCompletionOr<Value> {
  73. // a. Let F be the active function object.
  74. // b. Let p be F.[[RevocableProxy]].
  75. auto& proxy = const_cast<ProxyObject&>(*proxy_handle.cell());
  76. // c. If p is null, return undefined.
  77. if (proxy.is_revoked())
  78. return js_undefined();
  79. // d. Set F.[[RevocableProxy]] to null.
  80. // e. Assert: p is a Proxy object.
  81. // f. Set p.[[ProxyTarget]] to null.
  82. // g. Set p.[[ProxyHandler]] to null.
  83. proxy.revoke();
  84. // h. Return undefined.
  85. return js_undefined();
  86. };
  87. // 3. Let revoker be CreateBuiltinFunction(revokerClosure, 0, "", « [[RevocableProxy]] »).
  88. // 4. Set revoker.[[RevocableProxy]] to p.
  89. auto revoker = NativeFunction::create(realm, move(revoker_closure), 0, "");
  90. // 5. Let result be OrdinaryObjectCreate(%Object.prototype%).
  91. auto result = Object::create(realm, realm.intrinsics().object_prototype());
  92. // 6. Perform ! CreateDataPropertyOrThrow(result, "proxy", p).
  93. MUST(result->create_data_property_or_throw(vm.names.proxy, proxy));
  94. // 7. Perform ! CreateDataPropertyOrThrow(result, "revoke", revoker).
  95. MUST(result->create_data_property_or_throw(vm.names.revoke, revoker));
  96. // 8. Return result.
  97. return result;
  98. }
  99. }