Interpreter.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/LexicalEnvironment.h>
  32. #include <LibJS/Runtime/MarkedValueList.h>
  33. #include <LibJS/Runtime/NativeFunction.h>
  34. #include <LibJS/Runtime/Object.h>
  35. #include <LibJS/Runtime/Shape.h>
  36. #include <LibJS/Runtime/Value.h>
  37. namespace JS {
  38. Interpreter::Interpreter()
  39. : m_heap(*this)
  40. {
  41. }
  42. Interpreter::~Interpreter()
  43. {
  44. }
  45. Value Interpreter::run(const Statement& statement, ArgumentVector arguments, ScopeType scope_type)
  46. {
  47. if (statement.is_program()) {
  48. if (m_call_stack.is_empty()) {
  49. CallFrame global_call_fram;
  50. global_call_fram.this_value = m_global_object;
  51. global_call_fram.function_name = "(global execution context)";
  52. global_call_fram.environment = heap().allocate<LexicalEnvironment>();
  53. m_call_stack.append(move(global_call_fram));
  54. }
  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. m_last_value = js_undefined();
  61. for (auto& node : block.children()) {
  62. m_last_value = node.execute(*this);
  63. if (m_unwind_until != ScopeType::None)
  64. break;
  65. }
  66. bool did_return = m_unwind_until == ScopeType::Function;
  67. if (m_unwind_until == scope_type)
  68. m_unwind_until = ScopeType::None;
  69. exit_scope(block);
  70. return did_return ? m_last_value : js_undefined();
  71. }
  72. void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type)
  73. {
  74. if (scope_type == ScopeType::Function) {
  75. m_scope_stack.append({ scope_type, scope_node, false });
  76. return;
  77. }
  78. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  79. scope_variables_with_declaration_kind.ensure_capacity(16);
  80. for (auto& declaration : scope_node.variables()) {
  81. for (auto& declarator : declaration.declarations()) {
  82. if (scope_node.is_program())
  83. global_object().put(declarator.id().string(), js_undefined());
  84. else
  85. scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  86. }
  87. }
  88. for (auto& argument : arguments) {
  89. scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var });
  90. }
  91. bool pushed_lexical_environment = false;
  92. if (!scope_variables_with_declaration_kind.is_empty()) {
  93. auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(move(scope_variables_with_declaration_kind), current_environment());
  94. m_call_stack.last().environment = block_lexical_environment;
  95. pushed_lexical_environment = true;
  96. }
  97. m_scope_stack.append({ scope_type, scope_node, pushed_lexical_environment });
  98. }
  99. void Interpreter::exit_scope(const ScopeNode& scope_node)
  100. {
  101. while (!m_scope_stack.is_empty()) {
  102. auto popped_scope = m_scope_stack.take_last();
  103. if (popped_scope.pushed_environment)
  104. m_call_stack.last().environment = m_call_stack.last().environment->parent();
  105. if (popped_scope.scope_node.ptr() == &scope_node)
  106. break;
  107. }
  108. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  109. if (m_scope_stack.is_empty())
  110. m_unwind_until = ScopeType::None;
  111. }
  112. void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
  113. {
  114. for (auto* environment = current_environment(); environment; environment = environment->parent()) {
  115. auto possible_match = environment->get(name);
  116. if (possible_match.has_value()) {
  117. if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) {
  118. throw_exception<TypeError>("Assignment to constant variable");
  119. return;
  120. }
  121. environment->set(name, { value, possible_match.value().declaration_kind });
  122. return;
  123. }
  124. }
  125. global_object().put(move(name), move(value));
  126. }
  127. Optional<Value> Interpreter::get_variable(const FlyString& name)
  128. {
  129. for (auto* environment = current_environment(); environment; environment = environment->parent()) {
  130. auto possible_match = environment->get(name);
  131. if (possible_match.has_value())
  132. return possible_match.value().value;
  133. }
  134. return global_object().get(name);
  135. }
  136. void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
  137. {
  138. roots.set(m_global_object);
  139. roots.set(m_exception);
  140. if (m_last_value.is_cell())
  141. roots.set(m_last_value.as_cell());
  142. for (auto& call_frame : m_call_stack) {
  143. if (call_frame.this_value.is_cell())
  144. roots.set(call_frame.this_value.as_cell());
  145. for (auto& argument : call_frame.arguments) {
  146. if (argument.is_cell())
  147. roots.set(argument.as_cell());
  148. }
  149. roots.set(call_frame.environment);
  150. }
  151. }
  152. Value Interpreter::call(Function* function, Value this_value, Optional<MarkedValueList> arguments)
  153. {
  154. auto& call_frame = push_call_frame();
  155. call_frame.function_name = function->name();
  156. call_frame.this_value = this_value;
  157. if (arguments.has_value())
  158. call_frame.arguments = arguments.value().values();
  159. call_frame.environment = function->create_environment();
  160. auto result = function->call(*this);
  161. pop_call_frame();
  162. return result;
  163. }
  164. Value Interpreter::throw_exception(Exception* exception)
  165. {
  166. if (exception->value().is_object() && exception->value().as_object().is_error()) {
  167. auto& error = static_cast<Error&>(exception->value().as_object());
  168. dbg() << "Throwing JavaScript Error: " << error.name() << ", " << error.message();
  169. for (ssize_t i = m_call_stack.size() - 1; i >= 0; --i) {
  170. auto function_name = m_call_stack[i].function_name;
  171. if (function_name.is_empty())
  172. function_name = "<anonymous>";
  173. dbg() << " " << function_name;
  174. }
  175. }
  176. m_exception = exception;
  177. unwind(ScopeType::Try);
  178. return {};
  179. }
  180. GlobalObject& Interpreter::global_object()
  181. {
  182. return static_cast<GlobalObject&>(*m_global_object);
  183. }
  184. const GlobalObject& Interpreter::global_object() const
  185. {
  186. return static_cast<const GlobalObject&>(*m_global_object);
  187. }
  188. }