ProxyObject.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #pragma once
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. namespace JS {
  11. class ProxyObject final : public FunctionObject {
  12. JS_OBJECT(ProxyObject, FunctionObject);
  13. public:
  14. static ProxyObject* create(GlobalObject&, Object& target, Object& handler);
  15. ProxyObject(Object& target, Object& handler, Object& prototype);
  16. virtual ~ProxyObject() override;
  17. virtual const FlyString& name() const override;
  18. virtual bool has_constructor() const override;
  19. const Object& target() const { return m_target; }
  20. const Object& handler() const { return m_handler; }
  21. bool is_revoked() const { return m_is_revoked; }
  22. void revoke() { m_is_revoked = true; }
  23. // 10.5 Proxy Object Internal Methods and Internal Slots, https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots
  24. virtual ThrowCompletionOr<Object*> internal_get_prototype_of() const override;
  25. virtual ThrowCompletionOr<bool> internal_set_prototype_of(Object* prototype) override;
  26. virtual ThrowCompletionOr<bool> internal_is_extensible() const override;
  27. virtual ThrowCompletionOr<bool> internal_prevent_extensions() override;
  28. virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override;
  29. virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&) override;
  30. virtual ThrowCompletionOr<bool> internal_has_property(PropertyKey const&) const override;
  31. virtual ThrowCompletionOr<Value> internal_get(PropertyKey const&, Value receiver) const override;
  32. virtual ThrowCompletionOr<bool> internal_set(PropertyKey const&, Value value, Value receiver) override;
  33. virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override;
  34. virtual ThrowCompletionOr<MarkedValueList> internal_own_property_keys() const override;
  35. virtual ThrowCompletionOr<Value> internal_call(Value this_argument, MarkedValueList arguments_list) override;
  36. virtual ThrowCompletionOr<Object*> internal_construct(MarkedValueList arguments_list, FunctionObject& new_target) override;
  37. private:
  38. virtual void visit_edges(Visitor&) override;
  39. virtual bool is_function() const override { return m_target.is_function(); }
  40. virtual bool is_proxy_object() const final { return true; }
  41. Object& m_target;
  42. Object& m_handler;
  43. bool m_is_revoked { false };
  44. };
  45. template<>
  46. inline bool Object::fast_is<ProxyObject>() const { return is_proxy_object(); }
  47. }