Interpreter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <LibJS/Forward.h>
  32. #include <LibJS/Heap/Heap.h>
  33. #include <LibJS/Runtime/Exception.h>
  34. #include <LibJS/Runtime/Value.h>
  35. namespace JS {
  36. enum class ScopeType {
  37. None,
  38. Function,
  39. Block,
  40. Try,
  41. };
  42. struct Variable {
  43. Value value;
  44. DeclarationType declaration_type;
  45. };
  46. struct ScopeFrame {
  47. ScopeType type;
  48. NonnullRefPtr<ScopeNode> scope_node;
  49. HashMap<FlyString, Variable> variables;
  50. };
  51. struct CallFrame {
  52. Value this_value;
  53. Vector<Value> arguments;
  54. };
  55. struct Argument {
  56. FlyString name;
  57. Value value;
  58. };
  59. class Interpreter {
  60. public:
  61. Interpreter();
  62. ~Interpreter();
  63. Value run(const Statement&, Vector<Argument> = {}, ScopeType = ScopeType::Block);
  64. Object& global_object() { return *m_global_object; }
  65. const Object& global_object() const { return *m_global_object; }
  66. Heap& heap() { return m_heap; }
  67. void unwind(ScopeType type) { m_unwind_until = type; }
  68. Optional<Value> get_variable(const FlyString& name);
  69. void set_variable(const FlyString& name, Value, bool first_assignment = false);
  70. void declare_variable(const FlyString& name, DeclarationType);
  71. void gather_roots(Badge<Heap>, HashTable<Cell*>&);
  72. void enter_scope(const ScopeNode&, Vector<Argument>, ScopeType);
  73. void exit_scope(const ScopeNode&);
  74. Value call(Function*, Value this_value, const Vector<Value>& arguments);
  75. CallFrame& push_call_frame()
  76. {
  77. m_call_stack.append({ js_undefined(), {} });
  78. return m_call_stack.last();
  79. }
  80. void pop_call_frame() { m_call_stack.take_last(); }
  81. Value this_value() const
  82. {
  83. if (m_call_stack.is_empty())
  84. return m_global_object;
  85. return m_call_stack.last().this_value;
  86. }
  87. Object* string_prototype() { return m_string_prototype; }
  88. Object* object_prototype() { return m_object_prototype; }
  89. Object* array_prototype() { return m_array_prototype; }
  90. Object* error_prototype() { return m_error_prototype; }
  91. Exception* exception() { return m_exception; }
  92. void clear_exception() { m_exception = nullptr; }
  93. template<typename T, typename... Args>
  94. Value throw_exception(Args&&... args)
  95. {
  96. return throw_exception(heap().allocate<T>(forward<Args>(args)...));
  97. }
  98. Value throw_exception(Exception*);
  99. Value throw_exception(Value value)
  100. {
  101. return throw_exception(heap().allocate<Exception>(value));
  102. }
  103. private:
  104. Heap m_heap;
  105. Vector<ScopeFrame> m_scope_stack;
  106. Vector<CallFrame> m_call_stack;
  107. Object* m_global_object { nullptr };
  108. Object* m_string_prototype { nullptr };
  109. Object* m_object_prototype { nullptr };
  110. Object* m_array_prototype { nullptr };
  111. Object* m_error_prototype { nullptr };
  112. Exception* m_exception { nullptr };
  113. ScopeType m_unwind_until { ScopeType::None };
  114. };
  115. }