Interpreter.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/Console.h>
  32. #include <LibJS/Forward.h>
  33. #include <LibJS/Heap/Heap.h>
  34. #include <LibJS/Runtime/Exception.h>
  35. #include <LibJS/Runtime/LexicalEnvironment.h>
  36. #include <LibJS/Runtime/MarkedValueList.h>
  37. #include <LibJS/Runtime/Value.h>
  38. namespace JS {
  39. enum class ScopeType {
  40. None,
  41. Function,
  42. Block,
  43. Try,
  44. Breakable,
  45. Continuable,
  46. };
  47. struct ScopeFrame {
  48. ScopeType type;
  49. NonnullRefPtr<ScopeNode> scope_node;
  50. bool pushed_environment { false };
  51. };
  52. struct CallFrame {
  53. FlyString function_name;
  54. Value this_value;
  55. Vector<Value> arguments;
  56. LexicalEnvironment* environment { nullptr };
  57. };
  58. struct Argument {
  59. FlyString name;
  60. Value value;
  61. };
  62. typedef Vector<Argument, 8> ArgumentVector;
  63. class Interpreter {
  64. public:
  65. template<typename GlobalObjectType, typename... Args>
  66. static NonnullOwnPtr<Interpreter> create(Args&&... args)
  67. {
  68. auto interpreter = adopt_own(*new Interpreter);
  69. interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...);
  70. static_cast<GlobalObjectType*>(interpreter->m_global_object)->initialize();
  71. return interpreter;
  72. }
  73. ~Interpreter();
  74. Value run(const Statement&, ArgumentVector = {}, ScopeType = ScopeType::Block);
  75. GlobalObject& global_object();
  76. const GlobalObject& global_object() const;
  77. Heap& heap() { return m_heap; }
  78. void unwind(ScopeType type) { m_unwind_until = type; }
  79. void stop_unwind() { m_unwind_until = ScopeType::None; }
  80. bool should_unwind_until(ScopeType type) const { return m_unwind_until == type; }
  81. bool should_unwind() const { return m_unwind_until != ScopeType::None; }
  82. Value get_variable(const FlyString& name);
  83. void set_variable(const FlyString& name, Value, bool first_assignment = false);
  84. Reference get_reference(const FlyString& name);
  85. void gather_roots(Badge<Heap>, HashTable<Cell*>&);
  86. void enter_scope(const ScopeNode&, ArgumentVector, ScopeType);
  87. void exit_scope(const ScopeNode&);
  88. Value call(Function&, Value this_value = {}, Optional<MarkedValueList> arguments = {});
  89. Value construct(Function&, Function& new_target, Optional<MarkedValueList> arguments = {});
  90. CallFrame& push_call_frame()
  91. {
  92. m_call_stack.append({ {}, js_undefined(), {}, nullptr });
  93. return m_call_stack.last();
  94. }
  95. void pop_call_frame() { m_call_stack.take_last(); }
  96. const CallFrame& call_frame() { return m_call_stack.last(); }
  97. const Vector<CallFrame>& call_stack() { return m_call_stack; }
  98. void push_environment(LexicalEnvironment*);
  99. void pop_environment();
  100. const LexicalEnvironment* current_environment() const { return m_call_stack.last().environment; }
  101. LexicalEnvironment* current_environment() { return m_call_stack.last().environment; }
  102. size_t argument_count() const
  103. {
  104. if (m_call_stack.is_empty())
  105. return 0;
  106. return m_call_stack.last().arguments.size();
  107. }
  108. Value argument(size_t index) const
  109. {
  110. if (m_call_stack.is_empty())
  111. return {};
  112. auto& arguments = m_call_stack.last().arguments;
  113. return index < arguments.size() ? arguments[index] : js_undefined();
  114. }
  115. Value this_value() const
  116. {
  117. if (m_call_stack.is_empty())
  118. return m_global_object;
  119. return m_call_stack.last().this_value;
  120. }
  121. Exception* exception()
  122. {
  123. return m_exception;
  124. }
  125. void clear_exception() { m_exception = nullptr; }
  126. template<typename T, typename... Args>
  127. Value throw_exception(Args&&... args)
  128. {
  129. return throw_exception(T::create(global_object(), forward<Args>(args)...));
  130. }
  131. Value throw_exception(Exception*);
  132. Value throw_exception(Value value)
  133. {
  134. return throw_exception(heap().allocate<Exception>(value));
  135. }
  136. Value last_value() const { return m_last_value; }
  137. Console& console() { return m_console; }
  138. const Console& console() const { return m_console; }
  139. String join_arguments() const;
  140. Vector<String> get_trace() const;
  141. private:
  142. Interpreter();
  143. Heap m_heap;
  144. Value m_last_value;
  145. Vector<ScopeFrame> m_scope_stack;
  146. Vector<CallFrame> m_call_stack;
  147. Object* m_global_object { nullptr };
  148. Exception* m_exception { nullptr };
  149. ScopeType m_unwind_until { ScopeType::None };
  150. Console m_console;
  151. };
  152. }