ProxyConstructor.cpp 3.9 KB

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