Accessor.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibJS/Runtime/FunctionObject.h>
  9. #include <LibJS/Runtime/VM.h>
  10. namespace JS {
  11. class Accessor final : public Cell {
  12. public:
  13. static Accessor* create(VM& vm, FunctionObject* getter, FunctionObject* setter)
  14. {
  15. return vm.heap().allocate_without_global_object<Accessor>(getter, setter);
  16. }
  17. Accessor(FunctionObject* getter, FunctionObject* setter)
  18. : m_getter(getter)
  19. , m_setter(setter)
  20. {
  21. }
  22. FunctionObject* getter() const { return m_getter; }
  23. void set_getter(FunctionObject* getter) { m_getter = getter; }
  24. FunctionObject* setter() const { return m_setter; }
  25. void set_setter(FunctionObject* setter) { m_setter = setter; }
  26. Value call_getter(Value this_value)
  27. {
  28. if (!m_getter)
  29. return js_undefined();
  30. return TRY_OR_DISCARD(vm().call(*m_getter, this_value));
  31. }
  32. void call_setter(Value this_value, Value setter_value)
  33. {
  34. if (!m_setter)
  35. return;
  36. // FIXME: It might be nice if we had a way to communicate to our caller if an exception happened after this.
  37. [[maybe_unused]] auto rc = vm().call(*m_setter, this_value, setter_value);
  38. }
  39. void visit_edges(Cell::Visitor& visitor) override
  40. {
  41. visitor.visit(m_getter);
  42. visitor.visit(m_setter);
  43. }
  44. private:
  45. const char* class_name() const override { return "Accessor"; };
  46. FunctionObject* m_getter { nullptr };
  47. FunctionObject* m_setter { nullptr };
  48. };
  49. }