Interpreter.h 5.8 KB

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