Interpreter.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Breakable,
  42. };
  43. struct Variable {
  44. Value value;
  45. DeclarationType declaration_type;
  46. };
  47. struct ScopeFrame {
  48. ScopeType type;
  49. NonnullRefPtr<ScopeNode> scope_node;
  50. HashMap<FlyString, Variable> variables;
  51. };
  52. struct CallFrame {
  53. Value this_value;
  54. Vector<Value> arguments;
  55. };
  56. struct Argument {
  57. FlyString name;
  58. Value value;
  59. };
  60. class Interpreter {
  61. public:
  62. template<typename GlobalObjectType, typename... Args>
  63. static NonnullOwnPtr<Interpreter> create(Args&&... args)
  64. {
  65. auto interpreter = adopt_own(*new Interpreter);
  66. interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...);
  67. return interpreter;
  68. }
  69. ~Interpreter();
  70. Value run(const Statement&, Vector<Argument> = {}, ScopeType = ScopeType::Block);
  71. Object& global_object() { return *m_global_object; }
  72. const Object& global_object() const { return *m_global_object; }
  73. Heap& heap() { return m_heap; }
  74. void unwind(ScopeType type) { m_unwind_until = type; }
  75. bool should_unwind() const { return m_unwind_until != ScopeType::None; }
  76. Optional<Value> get_variable(const FlyString& name);
  77. void set_variable(const FlyString& name, Value, bool first_assignment = false);
  78. void declare_variable(const FlyString& name, DeclarationType);
  79. void gather_roots(Badge<Heap>, HashTable<Cell*>&);
  80. void enter_scope(const ScopeNode&, Vector<Argument>, ScopeType);
  81. void exit_scope(const ScopeNode&);
  82. Value call(Function*, Value this_value = {}, const Vector<Value>& arguments = {});
  83. CallFrame& push_call_frame()
  84. {
  85. m_call_stack.append({ js_undefined(), {} });
  86. return m_call_stack.last();
  87. }
  88. void pop_call_frame() { m_call_stack.take_last(); }
  89. const CallFrame& call_frame() { return m_call_stack.last(); }
  90. size_t argument_count() const
  91. {
  92. if (m_call_stack.is_empty())
  93. return 0;
  94. return m_call_stack.last().arguments.size();
  95. }
  96. Value argument(size_t index) const
  97. {
  98. if (m_call_stack.is_empty())
  99. return {};
  100. auto& arguments = m_call_stack.last().arguments;
  101. return index < arguments.size() ? arguments[index] : js_undefined();
  102. }
  103. Value this_value() const
  104. {
  105. if (m_call_stack.is_empty())
  106. return m_global_object;
  107. return m_call_stack.last().this_value;
  108. }
  109. Shape* empty_object_shape() { return m_empty_object_shape; }
  110. Object* string_prototype() { return m_string_prototype; }
  111. Object* object_prototype() { return m_object_prototype; }
  112. Object* array_prototype() { return m_array_prototype; }
  113. Object* error_prototype() { return m_error_prototype; }
  114. Object* date_prototype() { return m_date_prototype; }
  115. Object* function_prototype() { return m_function_prototype; }
  116. Object* number_prototype() { return m_number_prototype; }
  117. Exception* exception() { return m_exception; }
  118. void clear_exception() { m_exception = nullptr; }
  119. template<typename T, typename... Args>
  120. Value throw_exception(Args&&... args)
  121. {
  122. return throw_exception(heap().allocate<T>(forward<Args>(args)...));
  123. }
  124. Value throw_exception(Exception*);
  125. Value throw_exception(Value value)
  126. {
  127. return throw_exception(heap().allocate<Exception>(value));
  128. }
  129. Value last_value() const { return m_last_value; }
  130. private:
  131. Interpreter();
  132. Heap m_heap;
  133. Value m_last_value;
  134. Vector<ScopeFrame> m_scope_stack;
  135. Vector<CallFrame> m_call_stack;
  136. Shape* m_empty_object_shape { nullptr };
  137. Object* m_global_object { nullptr };
  138. Object* m_string_prototype { nullptr };
  139. Object* m_object_prototype { nullptr };
  140. Object* m_array_prototype { nullptr };
  141. Object* m_error_prototype { nullptr };
  142. Object* m_date_prototype { nullptr };
  143. Object* m_function_prototype { nullptr };
  144. Object* m_number_prototype { nullptr };
  145. Exception* m_exception { nullptr };
  146. ScopeType m_unwind_until { ScopeType::None };
  147. };
  148. }