Interpreter.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ScopeGuard.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibJS/AST.h>
  10. #include <LibJS/Interpreter.h>
  11. #include <LibJS/Runtime/FunctionEnvironment.h>
  12. #include <LibJS/Runtime/GlobalEnvironment.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. #include <LibJS/Runtime/Object.h>
  15. #include <LibJS/Runtime/OrdinaryFunctionObject.h>
  16. #include <LibJS/Runtime/Reference.h>
  17. #include <LibJS/Runtime/Shape.h>
  18. #include <LibJS/Runtime/Value.h>
  19. namespace JS {
  20. NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(GlobalObject& global_object)
  21. {
  22. DeferGC defer_gc(global_object.heap());
  23. auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
  24. interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
  25. return interpreter;
  26. }
  27. Interpreter::Interpreter(VM& vm)
  28. : m_vm(vm)
  29. {
  30. }
  31. Interpreter::~Interpreter()
  32. {
  33. }
  34. void Interpreter::run(GlobalObject& global_object, const Program& program)
  35. {
  36. auto& vm = this->vm();
  37. VERIFY(!vm.exception());
  38. VM::InterpreterExecutionScope scope(*this);
  39. vm.set_last_value(Badge<Interpreter> {}, {});
  40. ExecutionContext execution_context;
  41. execution_context.current_node = &program;
  42. execution_context.this_value = &global_object;
  43. static FlyString global_execution_context_name = "(global execution context)";
  44. execution_context.function_name = global_execution_context_name;
  45. execution_context.lexical_environment = &global_object.environment();
  46. execution_context.variable_environment = &global_object.environment();
  47. VERIFY(!vm.exception());
  48. execution_context.is_strict_mode = program.is_strict_mode();
  49. vm.push_execution_context(execution_context, global_object);
  50. VERIFY(!vm.exception());
  51. auto value = program.execute(*this, global_object);
  52. vm.set_last_value(Badge<Interpreter> {}, value.value_or(js_undefined()));
  53. vm.pop_execution_context();
  54. // At this point we may have already run any queued promise jobs via on_call_stack_emptied,
  55. // in which case this is a no-op.
  56. vm.run_queued_promise_jobs();
  57. vm.run_queued_finalization_registry_cleanup_jobs();
  58. vm.finish_execution_generation();
  59. }
  60. GlobalObject& Interpreter::global_object()
  61. {
  62. return static_cast<GlobalObject&>(*m_global_object.cell());
  63. }
  64. const GlobalObject& Interpreter::global_object() const
  65. {
  66. return static_cast<const GlobalObject&>(*m_global_object.cell());
  67. }
  68. void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
  69. {
  70. ScopeGuard guard([&] {
  71. for (auto& declaration : scope_node.hoisted_functions()) {
  72. lexical_environment()->put_into_environment(declaration.name(), { js_undefined(), DeclarationKind::Var });
  73. }
  74. for (auto& declaration : scope_node.functions()) {
  75. auto* function = OrdinaryFunctionObject::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), lexical_environment(), declaration.kind(), declaration.is_strict_mode());
  76. vm().set_variable(declaration.name(), function, global_object);
  77. }
  78. });
  79. if (scope_type == ScopeType::Function) {
  80. push_scope({ scope_type, scope_node, false });
  81. for (auto& declaration : scope_node.functions())
  82. lexical_environment()->put_into_environment(declaration.name(), { js_undefined(), DeclarationKind::Var });
  83. return;
  84. }
  85. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  86. scope_variables_with_declaration_kind.ensure_capacity(16);
  87. bool is_program_node = is<Program>(scope_node);
  88. for (auto& declaration : scope_node.variables()) {
  89. for (auto& declarator : declaration.declarations()) {
  90. if (is_program_node && declaration.declaration_kind() == DeclarationKind::Var) {
  91. declarator.target().visit(
  92. [&](const NonnullRefPtr<Identifier>& id) {
  93. global_object.put(id->string(), js_undefined());
  94. },
  95. [&](const NonnullRefPtr<BindingPattern>& binding) {
  96. binding->for_each_bound_name([&](const auto& name) {
  97. global_object.put(name, js_undefined());
  98. });
  99. });
  100. if (exception())
  101. return;
  102. } else {
  103. declarator.target().visit(
  104. [&](const NonnullRefPtr<Identifier>& id) {
  105. scope_variables_with_declaration_kind.set(id->string(), { js_undefined(), declaration.declaration_kind() });
  106. },
  107. [&](const NonnullRefPtr<BindingPattern>& binding) {
  108. binding->for_each_bound_name([&](const auto& name) {
  109. scope_variables_with_declaration_kind.set(name, { js_undefined(), declaration.declaration_kind() });
  110. });
  111. });
  112. }
  113. }
  114. }
  115. bool pushed_environment = false;
  116. if (!scope_variables_with_declaration_kind.is_empty()) {
  117. auto* environment = heap().allocate<DeclarativeEnvironment>(global_object, move(scope_variables_with_declaration_kind), lexical_environment());
  118. vm().running_execution_context().lexical_environment = environment;
  119. vm().running_execution_context().variable_environment = environment;
  120. pushed_environment = true;
  121. }
  122. push_scope({ scope_type, scope_node, pushed_environment });
  123. }
  124. void Interpreter::exit_scope(const ScopeNode& scope_node)
  125. {
  126. while (!m_scope_stack.is_empty()) {
  127. auto popped_scope = m_scope_stack.take_last();
  128. if (popped_scope.pushed_environment) {
  129. vm().running_execution_context().lexical_environment = vm().running_execution_context().lexical_environment->outer_environment();
  130. vm().running_execution_context().variable_environment = vm().running_execution_context().variable_environment->outer_environment();
  131. }
  132. if (popped_scope.scope_node.ptr() == &scope_node)
  133. break;
  134. }
  135. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  136. if (m_scope_stack.is_empty())
  137. vm().stop_unwind();
  138. }
  139. void Interpreter::push_scope(ScopeFrame frame)
  140. {
  141. m_scope_stack.append(move(frame));
  142. }
  143. Value Interpreter::execute_statement(GlobalObject& global_object, const Statement& statement, ScopeType scope_type)
  144. {
  145. if (!is<ScopeNode>(statement))
  146. return statement.execute(*this, global_object);
  147. auto& block = static_cast<const ScopeNode&>(statement);
  148. enter_scope(block, scope_type, global_object);
  149. Value last_value;
  150. for (auto& node : block.children()) {
  151. auto value = node.execute(*this, global_object);
  152. if (!value.is_empty())
  153. last_value = value;
  154. if (vm().should_unwind()) {
  155. if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
  156. vm().stop_unwind();
  157. break;
  158. }
  159. }
  160. if (scope_type == ScopeType::Function) {
  161. bool did_return = vm().unwind_until() == ScopeType::Function;
  162. if (!did_return)
  163. last_value = js_undefined();
  164. }
  165. if (vm().unwind_until() == scope_type)
  166. vm().stop_unwind();
  167. exit_scope(block);
  168. return last_value;
  169. }
  170. FunctionEnvironment* Interpreter::current_function_environment()
  171. {
  172. return verify_cast<FunctionEnvironment>(vm().running_execution_context().lexical_environment);
  173. }
  174. }