ProxyConstructor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021, 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 ProxyObject* proxy_create(GlobalObject& global_object, Value target, Value handler)
  15. {
  16. auto& vm = global_object.vm();
  17. if (!target.is_object()) {
  18. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyConstructorBadType, "target", target.to_string_without_side_effects());
  19. return {};
  20. }
  21. if (!handler.is_object()) {
  22. vm.throw_exception<TypeError>(global_object, ErrorType::ProxyConstructorBadType, "handler", handler.to_string_without_side_effects());
  23. return {};
  24. }
  25. return ProxyObject::create(global_object, target.as_object(), handler.as_object());
  26. }
  27. ProxyConstructor::ProxyConstructor(GlobalObject& global_object)
  28. : NativeFunction(vm().names.Proxy, *global_object.function_prototype())
  29. {
  30. }
  31. void ProxyConstructor::initialize(GlobalObject& global_object)
  32. {
  33. auto& vm = this->vm();
  34. NativeFunction::initialize(global_object);
  35. define_property(vm.names.length, Value(2), Attribute::Configurable);
  36. u8 attr = Attribute::Writable | Attribute::Configurable;
  37. define_native_function(vm.names.revocable, revocable, 2, attr);
  38. }
  39. ProxyConstructor::~ProxyConstructor()
  40. {
  41. }
  42. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  43. Value ProxyConstructor::call()
  44. {
  45. auto& vm = this->vm();
  46. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Proxy);
  47. return {};
  48. }
  49. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  50. Value ProxyConstructor::construct(Function&)
  51. {
  52. auto& vm = this->vm();
  53. return proxy_create(global_object(), vm.argument(0), vm.argument(1));
  54. }
  55. // 28.2.2.1 Proxy.revocable ( target, handler ), https://tc39.es/ecma262/#sec-proxy.revocable
  56. JS_DEFINE_NATIVE_FUNCTION(ProxyConstructor::revocable)
  57. {
  58. auto* proxy = proxy_create(global_object, vm.argument(0), vm.argument(1));
  59. if (vm.exception())
  60. return {};
  61. // 28.2.2.1.1 Proxy Revocation Functions, https://tc39.es/ecma262/#sec-proxy-revocation-functions
  62. auto* revoker = NativeFunction::create(global_object, "", [proxy_handle = make_handle(proxy)](auto&, auto&) -> Value {
  63. auto& proxy = const_cast<ProxyObject&>(*proxy_handle.cell());
  64. if (proxy.is_revoked())
  65. return js_undefined();
  66. // NOTE: The spec wants us to unset [[ProxyTarget]] and [[ProxyHandler]],
  67. // which is their way of revoking the Proxy - this might affect GC-ability,
  68. // but AFAICT not doing that should be ok compatibility-wise.
  69. proxy.revoke();
  70. return js_undefined();
  71. });
  72. revoker->define_property(vm.names.length, Value(0));
  73. auto* result = Object::create(global_object, global_object.object_prototype());
  74. result->define_property(vm.names.proxy, proxy);
  75. result->define_property(vm.names.revoke, revoker);
  76. return result;
  77. }
  78. }