Interpreter.cpp 7.0 KB

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