ProxyConstructor.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 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. ProxyConstructor::~ProxyConstructor()
  36. {
  37. }
  38. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  39. ThrowCompletionOr<Value> ProxyConstructor::call()
  40. {
  41. auto& vm = this->vm();
  42. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Proxy);
  43. }
  44. // 28.2.1.1 Proxy ( target, handler ), https://tc39.es/ecma262/#sec-proxy-target-handler
  45. ThrowCompletionOr<Object*> ProxyConstructor::construct(FunctionObject&)
  46. {
  47. auto& vm = this->vm();
  48. return TRY(proxy_create(global_object(), vm.argument(0), vm.argument(1)));
  49. }
  50. // 28.2.2.1 Proxy.revocable ( target, handler ), https://tc39.es/ecma262/#sec-proxy.revocable
  51. JS_DEFINE_NATIVE_FUNCTION(ProxyConstructor::revocable)
  52. {
  53. auto* proxy = TRY(proxy_create(global_object, vm.argument(0), vm.argument(1)));
  54. // 28.2.2.1.1 Proxy Revocation Functions, https://tc39.es/ecma262/#sec-proxy-revocation-functions
  55. auto* revoker = NativeFunction::create(global_object, "", [proxy_handle = make_handle(proxy)](auto&, auto&) -> ThrowCompletionOr<Value> {
  56. auto& proxy = const_cast<ProxyObject&>(*proxy_handle.cell());
  57. if (proxy.is_revoked())
  58. return js_undefined();
  59. // NOTE: The spec wants us to unset [[ProxyTarget]] and [[ProxyHandler]],
  60. // which is their way of revoking the Proxy - this might affect GC-ability,
  61. // but AFAICT not doing that should be ok compatibility-wise.
  62. proxy.revoke();
  63. return js_undefined();
  64. });
  65. revoker->define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  66. revoker->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
  67. auto* result = Object::create(global_object, global_object.object_prototype());
  68. MUST(result->create_data_property_or_throw(vm.names.proxy, proxy));
  69. MUST(result->create_data_property_or_throw(vm.names.revoke, revoker));
  70. return result;
  71. }
  72. }