Interpreter.cpp 6.7 KB

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