VM.h 8.5 KB

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