Interpreter.h 6.7 KB

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