ProxyConstructor.cpp 3.6 KB

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