Interpreter.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <LibJS/Forward.h>
  32. #include <LibJS/Heap/Heap.h>
  33. #include <LibJS/Runtime/Exception.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 Variable {
  45. Value value;
  46. DeclarationKind declaration_kind;
  47. };
  48. struct ScopeFrame {
  49. ScopeType type;
  50. NonnullRefPtr<ScopeNode> scope_node;
  51. HashMap<FlyString, Variable> variables;
  52. };
  53. struct CallFrame {
  54. FlyString function_name;
  55. Value this_value;
  56. Vector<Value> arguments;
  57. };
  58. struct Argument {
  59. FlyString name;
  60. Value value;
  61. };
  62. typedef Vector<Argument, 8> ArgumentVector;
  63. class Interpreter {
  64. public:
  65. template<typename GlobalObjectType, typename... Args>
  66. static NonnullOwnPtr<Interpreter> create(Args&&... args)
  67. {
  68. auto interpreter = adopt_own(*new Interpreter);
  69. interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...);
  70. return interpreter;
  71. }
  72. ~Interpreter();
  73. Value run(const Statement&, ArgumentVector = {}, ScopeType = ScopeType::Block);
  74. GlobalObject& global_object();
  75. const GlobalObject& global_object() const;
  76. Heap& heap() { return m_heap; }
  77. void unwind(ScopeType type) { m_unwind_until = type; }
  78. void stop_unwind() { m_unwind_until = ScopeType::None; }
  79. bool should_unwind_until(ScopeType type) const { return m_unwind_until == type; }
  80. bool should_unwind() const { return m_unwind_until != ScopeType::None; }
  81. Optional<Value> get_variable(const FlyString& name);
  82. void set_variable(const FlyString& name, Value, bool first_assignment = false);
  83. void gather_roots(Badge<Heap>, HashTable<Cell*>&);
  84. void enter_scope(const ScopeNode&, ArgumentVector, ScopeType);
  85. void exit_scope(const ScopeNode&);
  86. Value call(Function*, Value this_value = {}, const Vector<Value>& arguments = {});
  87. CallFrame& push_call_frame()
  88. {
  89. m_call_stack.append({ {}, js_undefined(), {} });
  90. return m_call_stack.last();
  91. }
  92. void pop_call_frame() { m_call_stack.take_last(); }
  93. const CallFrame& call_frame() { return m_call_stack.last(); }
  94. const Vector<CallFrame> call_stack() { return m_call_stack; }
  95. size_t argument_count() const
  96. {
  97. if (m_call_stack.is_empty())
  98. return 0;
  99. return m_call_stack.last().arguments.size();
  100. }
  101. Value argument(size_t index) const
  102. {
  103. if (m_call_stack.is_empty())
  104. return {};
  105. auto& arguments = m_call_stack.last().arguments;
  106. return index < arguments.size() ? arguments[index] : js_undefined();
  107. }
  108. Value this_value() const
  109. {
  110. if (m_call_stack.is_empty())
  111. return m_global_object;
  112. return m_call_stack.last().this_value;
  113. }
  114. Shape* empty_object_shape() { return m_empty_object_shape; }
  115. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  116. Object* snake_name##_prototype() { return m_##snake_name##_prototype; }
  117. JS_ENUMERATE_BUILTIN_TYPES
  118. #undef __JS_ENUMERATE
  119. Exception* exception()
  120. {
  121. return m_exception;
  122. }
  123. void clear_exception() { m_exception = nullptr; }
  124. template<typename T, typename... Args>
  125. Value throw_exception(Args&&... args)
  126. {
  127. return throw_exception(heap().allocate<T>(forward<Args>(args)...));
  128. }
  129. Value throw_exception(Exception*);
  130. Value throw_exception(Value value)
  131. {
  132. return throw_exception(heap().allocate<Exception>(value));
  133. }
  134. Value last_value() const { return m_last_value; }
  135. private:
  136. Interpreter();
  137. Heap m_heap;
  138. Value m_last_value;
  139. Vector<ScopeFrame> m_scope_stack;
  140. Vector<CallFrame> m_call_stack;
  141. Shape* m_empty_object_shape { nullptr };
  142. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  143. Object* m_##snake_name##_prototype { nullptr };
  144. JS_ENUMERATE_BUILTIN_TYPES
  145. #undef __JS_ENUMERATE
  146. Object* m_global_object { nullptr };
  147. Exception* m_exception { nullptr };
  148. ScopeType m_unwind_until { ScopeType::None };
  149. };
  150. }