Accessor.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Function.h>
  30. #include <LibJS/Runtime/MarkedValueList.h>
  31. namespace JS {
  32. class Accessor final : public Cell {
  33. public:
  34. static Accessor* create(Interpreter& interpreter, Function* getter, Function* setter)
  35. {
  36. return interpreter.heap().allocate<Accessor>(getter, setter);
  37. }
  38. Accessor(Function* getter, Function* setter)
  39. : m_getter(getter)
  40. , m_setter(setter)
  41. {
  42. }
  43. Function* getter() const { return m_getter; }
  44. void set_getter(Function* getter) { m_getter = getter; }
  45. Function* setter() const { return m_setter; }
  46. void set_setter(Function* setter) { m_setter = setter; }
  47. Value call_getter(Value this_value)
  48. {
  49. if (!m_getter)
  50. return js_undefined();
  51. return interpreter().call(*m_getter, this_value);
  52. }
  53. void call_setter(Value this_value, Value setter_value)
  54. {
  55. if (!m_setter)
  56. return;
  57. MarkedValueList arguments(interpreter().heap());
  58. arguments.values().append(setter_value);
  59. interpreter().call(*m_setter, this_value, move(arguments));
  60. }
  61. void visit_children(Cell::Visitor& visitor) override
  62. {
  63. visitor.visit(m_getter);
  64. visitor.visit(m_setter);
  65. }
  66. private:
  67. const char* class_name() const override { return "Accessor"; };
  68. Function* m_getter { nullptr };
  69. Function* m_setter { nullptr };
  70. };
  71. }