Interpreter.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_without_global_object<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. Symbol* get_global_symbol(const String& description);
  98. void gather_roots(Badge<Heap>, HashTable<Cell*>&);
  99. void enter_scope(const ScopeNode&, ArgumentVector, ScopeType, GlobalObject&);
  100. void exit_scope(const ScopeNode&);
  101. [[nodiscard]] Value call(Function&, Value this_value, Optional<MarkedValueList> arguments = {});
  102. Value construct(Function&, Function& new_target, Optional<MarkedValueList> arguments, GlobalObject&);
  103. CallFrame& push_call_frame()
  104. {
  105. m_call_stack.append({ {}, js_undefined(), {}, nullptr });
  106. return m_call_stack.last();
  107. }
  108. void pop_call_frame() { m_call_stack.take_last(); }
  109. const CallFrame& call_frame() { return m_call_stack.last(); }
  110. const Vector<CallFrame>& call_stack() { return m_call_stack; }
  111. void push_environment(LexicalEnvironment*);
  112. void pop_environment();
  113. const LexicalEnvironment* current_environment() const { return m_call_stack.last().environment; }
  114. LexicalEnvironment* current_environment() { return m_call_stack.last().environment; }
  115. bool in_strict_mode() const { return m_scope_stack.last().scope_node->in_strict_mode(); }
  116. template<typename Callback>
  117. void for_each_argument(Callback callback)
  118. {
  119. if (m_call_stack.is_empty())
  120. return;
  121. for (auto& value : m_call_stack.last().arguments)
  122. callback(value);
  123. }
  124. size_t argument_count() const
  125. {
  126. if (m_call_stack.is_empty())
  127. return 0;
  128. return m_call_stack.last().arguments.size();
  129. }
  130. Value argument(size_t index) const
  131. {
  132. if (m_call_stack.is_empty())
  133. return {};
  134. auto& arguments = m_call_stack.last().arguments;
  135. return index < arguments.size() ? arguments[index] : js_undefined();
  136. }
  137. Value this_value(Object& global_object) const
  138. {
  139. if (m_call_stack.is_empty())
  140. return &global_object;
  141. return m_call_stack.last().this_value;
  142. }
  143. Exception* exception()
  144. {
  145. return m_exception;
  146. }
  147. void clear_exception() { m_exception = nullptr; }
  148. template<typename T, typename... Args>
  149. Value throw_exception(Args&&... args)
  150. {
  151. return throw_exception(T::create(global_object(), forward<Args>(args)...));
  152. }
  153. Value throw_exception(Exception*);
  154. Value throw_exception(Value value)
  155. {
  156. return throw_exception(heap().allocate<Exception>(global_object(), value));
  157. }
  158. template<typename T, typename... Args>
  159. Value throw_exception(ErrorType type, Args&&... args)
  160. {
  161. return throw_exception(T::create(global_object(), String::format(type.message(), forward<Args>(args)...)));
  162. }
  163. Value last_value() const { return m_last_value; }
  164. bool underscore_is_last_value() const { return m_underscore_is_last_value; }
  165. void set_underscore_is_last_value(bool b) { m_underscore_is_last_value = b; }
  166. Console& console() { return m_console; }
  167. const Console& console() const { return m_console; }
  168. String join_arguments() const;
  169. Value resolve_this_binding() const;
  170. const LexicalEnvironment* get_this_environment() const;
  171. Value get_new_target() const;
  172. #define __JS_ENUMERATE(SymbolName, snake_name) \
  173. Symbol* well_known_symbol_##snake_name() const { return m_well_known_symbol_##snake_name; }
  174. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  175. #undef __JS_ENUMERATE
  176. private:
  177. Interpreter();
  178. Heap m_heap;
  179. Value m_last_value;
  180. Vector<ScopeFrame> m_scope_stack;
  181. Vector<CallFrame> m_call_stack;
  182. Object* m_global_object { nullptr };
  183. Exception* m_exception { nullptr };
  184. ScopeType m_unwind_until { ScopeType::None };
  185. FlyString m_unwind_until_label;
  186. bool m_underscore_is_last_value { false };
  187. Console m_console;
  188. HashMap<String, Symbol*> m_global_symbol_map;
  189. #define __JS_ENUMERATE(SymbolName, snake_name) \
  190. Symbol* m_well_known_symbol_##snake_name { nullptr };
  191. JS_ENUMERATE_WELL_KNOWN_SYMBOLS
  192. #undef __JS_ENUMERATE
  193. };
  194. }