ProxyConstructor.cpp 3.0 KB

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