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. CallFrame& call_frame() { return m_call_stack.last(); }
  95. const CallFrame& call_frame() const { return m_call_stack.last(); }
  96. const Vector<CallFrame>& call_stack() const { return m_call_stack; }
  97. Vector<CallFrame>& call_stack() { return m_call_stack; }
  98. const LexicalEnvironment* current_environment() const { return m_call_stack.last().environment; }
  99. LexicalEnvironment* current_environment() { return m_call_stack.last().environment; }
  100. bool in_strict_mode() const;
  101. template<typename Callback>
  102. void for_each_argument(Callback callback)
  103. {
  104. if (m_call_stack.is_empty())
  105. return;
  106. for (auto& value : m_call_stack.last().arguments)
  107. callback(value);
  108. }
  109. size_t argument_count() const
  110. {
  111. if (m_call_stack.is_empty())
  112. return 0;
  113. return m_call_stack.last().arguments.size();
  114. }
  115. Value argument(size_t index) const
  116. {
  117. if (m_call_stack.is_empty())
  118. return {};
  119. auto& arguments = m_call_stack.last().arguments;
  120. return index < arguments.size() ? arguments[index] : js_undefined();
  121. }
  122. Value this_value(Object& global_object) const
  123. {
  124. if (m_call_stack.is_empty())
  125. return &global_object;
  126. return m_call_stack.last().this_value;
  127. }
  128. Value last_value() const { return m_last_value; }
  129. void set_last_value(Badge<Interpreter>, Value value) { m_last_value = value; }
  130. bool underscore_is_last_value() const { return m_underscore_is_last_value; }
  131. void set_underscore_is_last_value(bool b) { m_underscore_is_last_value = b; }
  132. void unwind(ScopeType type, FlyString label = {})
  133. {
  134. m_unwind_until = type;
  135. m_unwind_until_label = label;
  136. }
  137. void stop_unwind() { m_unwind_until = ScopeType::None; }
  138. bool should_unwind_until(ScopeType type, FlyString label) const
  139. {
  140. if (m_unwind_until_label.is_null())
  141. return m_unwind_until == type;
  142. return m_unwind_until == type && m_unwind_until_label == label;
  143. }
  144. bool should_unwind() const { return m_unwind_until != ScopeType::None; }
  145. ScopeType unwind_until() const { return m_unwind_until; }
  146. Value get_variable(const FlyString& name, GlobalObject&);
  147. void set_variable(const FlyString& name, Value, GlobalObject&, bool first_assignment = false);
  148. Reference get_reference(const FlyString& name);
  149. template<typename T, typename... Args>
  150. void throw_exception(GlobalObject& global_object, Args&&... args)
  151. {
  152. return throw_exception(global_object, T::create(global_object, forward<Args>(args)...));
  153. }
  154. void throw_exception(Exception*);
  155. void throw_exception(GlobalObject& global_object, Value value)
  156. {
  157. return throw_exception(heap().allocate<Exception>(global_object, value));
  158. }
  159. template<typename T, typename... Args>
  160. void throw_exception(GlobalObject& global_object, ErrorType type, Args&&... args)
  161. {
  162. return throw_exception(global_object, T::create(global_object, String::format(type.message(), forward<Args>(args)...)));
  163. }
  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. [[nodiscard]] Value call(Function&, Value this_value, Optional<MarkedValueList> arguments);
  170. private:
  171. VM();
  172. Exception* m_exception { nullptr };
  173. Heap m_heap;
  174. Vector<Interpreter*> m_interpreters;
  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. }