Interpreter.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include <AK/Badge.h>
  27. #include <LibJS/AST.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/ArrayPrototype.h>
  30. #include <LibJS/Runtime/DatePrototype.h>
  31. #include <LibJS/Runtime/Error.h>
  32. #include <LibJS/Runtime/ErrorPrototype.h>
  33. #include <LibJS/Runtime/GlobalObject.h>
  34. #include <LibJS/Runtime/NativeFunction.h>
  35. #include <LibJS/Runtime/Object.h>
  36. #include <LibJS/Runtime/ObjectPrototype.h>
  37. #include <LibJS/Runtime/Shape.h>
  38. #include <LibJS/Runtime/StringPrototype.h>
  39. #include <LibJS/Runtime/Value.h>
  40. namespace JS {
  41. Interpreter::Interpreter()
  42. : m_heap(*this)
  43. {
  44. m_empty_object_shape = heap().allocate<Shape>();
  45. m_object_prototype = heap().allocate<ObjectPrototype>();
  46. m_string_prototype = heap().allocate<StringPrototype>();
  47. m_array_prototype = heap().allocate<ArrayPrototype>();
  48. m_error_prototype = heap().allocate<ErrorPrototype>();
  49. m_date_prototype = heap().allocate<DatePrototype>();
  50. }
  51. Interpreter::~Interpreter()
  52. {
  53. }
  54. Value Interpreter::run(const Statement& statement, Vector<Argument> arguments, ScopeType scope_type)
  55. {
  56. if (!statement.is_scope_node())
  57. return statement.execute(*this);
  58. auto& block = static_cast<const ScopeNode&>(statement);
  59. enter_scope(block, move(arguments), scope_type);
  60. Value last_value = js_undefined();
  61. for (auto& node : block.children()) {
  62. last_value = node.execute(*this);
  63. if (m_unwind_until != ScopeType::None)
  64. break;
  65. }
  66. if (m_unwind_until == scope_type)
  67. m_unwind_until = ScopeType::None;
  68. exit_scope(block);
  69. return last_value;
  70. }
  71. void Interpreter::enter_scope(const ScopeNode& scope_node, Vector<Argument> arguments, ScopeType scope_type)
  72. {
  73. HashMap<FlyString, Variable> scope_variables_with_declaration_type;
  74. for (auto& argument : arguments) {
  75. scope_variables_with_declaration_type.set(argument.name, { argument.value, DeclarationType::Var });
  76. }
  77. m_scope_stack.append({ scope_type, scope_node, move(scope_variables_with_declaration_type) });
  78. }
  79. void Interpreter::exit_scope(const ScopeNode& scope_node)
  80. {
  81. while (!m_scope_stack.is_empty()) {
  82. auto popped_scope = m_scope_stack.take_last();
  83. if (popped_scope.scope_node.ptr() == &scope_node)
  84. break;
  85. }
  86. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  87. if (m_scope_stack.is_empty())
  88. m_unwind_until = ScopeType::None;
  89. }
  90. void Interpreter::declare_variable(const FlyString& name, DeclarationType declaration_type)
  91. {
  92. switch (declaration_type) {
  93. case DeclarationType::Var:
  94. for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
  95. auto& scope = m_scope_stack.at(i);
  96. if (scope.type == ScopeType::Function) {
  97. if (scope.variables.get(name).has_value() && scope.variables.get(name).value().declaration_type != DeclarationType::Var)
  98. ASSERT_NOT_REACHED();
  99. scope.variables.set(move(name), { js_undefined(), declaration_type });
  100. return;
  101. }
  102. }
  103. global_object().put(move(name), js_undefined());
  104. break;
  105. case DeclarationType::Let:
  106. case DeclarationType::Const:
  107. if (m_scope_stack.last().variables.get(name).has_value())
  108. ASSERT_NOT_REACHED();
  109. m_scope_stack.last().variables.set(move(name), { js_undefined(), declaration_type });
  110. break;
  111. }
  112. }
  113. void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
  114. {
  115. for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
  116. auto& scope = m_scope_stack.at(i);
  117. auto possible_match = scope.variables.get(name);
  118. if (possible_match.has_value()) {
  119. if (!first_assignment && possible_match.value().declaration_type == DeclarationType::Const)
  120. ASSERT_NOT_REACHED();
  121. scope.variables.set(move(name), { move(value), possible_match.value().declaration_type });
  122. return;
  123. }
  124. }
  125. global_object().put(move(name), move(value));
  126. }
  127. Optional<Value> Interpreter::get_variable(const FlyString& name)
  128. {
  129. if (name == "this")
  130. return this_value();
  131. for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
  132. auto& scope = m_scope_stack.at(i);
  133. auto value = scope.variables.get(name);
  134. if (value.has_value())
  135. return value.value().value;
  136. }
  137. return global_object().get(name);
  138. }
  139. void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
  140. {
  141. roots.set(m_empty_object_shape);
  142. roots.set(m_global_object);
  143. roots.set(m_string_prototype);
  144. roots.set(m_object_prototype);
  145. roots.set(m_array_prototype);
  146. roots.set(m_error_prototype);
  147. roots.set(m_date_prototype);
  148. roots.set(m_exception);
  149. for (auto& scope : m_scope_stack) {
  150. for (auto& it : scope.variables) {
  151. if (it.value.value.is_cell())
  152. roots.set(it.value.value.as_cell());
  153. }
  154. }
  155. for (auto& call_frame : m_call_stack) {
  156. if (call_frame.this_value.is_cell())
  157. roots.set(call_frame.this_value.as_cell());
  158. for (auto& argument : call_frame.arguments) {
  159. if (argument.is_cell())
  160. roots.set(argument.as_cell());
  161. }
  162. }
  163. }
  164. Value Interpreter::call(Function* function, Value this_value, const Vector<Value>& arguments)
  165. {
  166. auto& call_frame = push_call_frame();
  167. call_frame.this_value = this_value;
  168. call_frame.arguments = arguments;
  169. auto result = function->call(*this);
  170. pop_call_frame();
  171. return result;
  172. }
  173. Value Interpreter::throw_exception(Exception* exception)
  174. {
  175. m_exception = exception;
  176. unwind(ScopeType::Try);
  177. return {};
  178. }
  179. }