Interpreter.cpp 7.6 KB

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