VM.h 7.2 KB

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