Interpreter.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/FunctionEnvironmentRecord.h>
  12. #include <LibJS/Runtime/GlobalEnvironmentRecord.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_record();
  46. execution_context.variable_environment = &global_object.environment_record();
  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.functions()) {
  72. auto* function = OrdinaryFunctionObject::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), lexical_environment(), declaration.kind(), declaration.is_strict_mode());
  73. vm().set_variable(declaration.name(), function, global_object);
  74. }
  75. });
  76. if (scope_type == ScopeType::Function) {
  77. push_scope({ scope_type, scope_node, false });
  78. for (auto& declaration : scope_node.functions())
  79. lexical_environment()->put_into_environment_record(declaration.name(), { js_undefined(), DeclarationKind::Var });
  80. return;
  81. }
  82. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  83. scope_variables_with_declaration_kind.ensure_capacity(16);
  84. bool is_program_node = is<Program>(scope_node);
  85. for (auto& declaration : scope_node.variables()) {
  86. for (auto& declarator : declaration.declarations()) {
  87. if (is_program_node && declaration.declaration_kind() == DeclarationKind::Var) {
  88. declarator.target().visit(
  89. [&](const NonnullRefPtr<Identifier>& id) {
  90. global_object.put(id->string(), js_undefined());
  91. },
  92. [&](const NonnullRefPtr<BindingPattern>& binding) {
  93. binding->for_each_bound_name([&](const auto& name) {
  94. global_object.put(name, js_undefined());
  95. });
  96. });
  97. if (exception())
  98. return;
  99. } else {
  100. declarator.target().visit(
  101. [&](const NonnullRefPtr<Identifier>& id) {
  102. scope_variables_with_declaration_kind.set(id->string(), { js_undefined(), declaration.declaration_kind() });
  103. },
  104. [&](const NonnullRefPtr<BindingPattern>& binding) {
  105. binding->for_each_bound_name([&](const auto& name) {
  106. scope_variables_with_declaration_kind.set(name, { js_undefined(), declaration.declaration_kind() });
  107. });
  108. });
  109. }
  110. }
  111. }
  112. bool pushed_environment_record = false;
  113. if (!scope_variables_with_declaration_kind.is_empty()) {
  114. auto* environment_record = heap().allocate<DeclarativeEnvironmentRecord>(global_object, move(scope_variables_with_declaration_kind), lexical_environment());
  115. vm().running_execution_context().lexical_environment = environment_record;
  116. vm().running_execution_context().variable_environment = environment_record;
  117. pushed_environment_record = true;
  118. }
  119. push_scope({ scope_type, scope_node, pushed_environment_record });
  120. }
  121. void Interpreter::exit_scope(const ScopeNode& scope_node)
  122. {
  123. while (!m_scope_stack.is_empty()) {
  124. auto popped_scope = m_scope_stack.take_last();
  125. if (popped_scope.pushed_environment) {
  126. vm().running_execution_context().lexical_environment = vm().running_execution_context().lexical_environment->outer_environment();
  127. vm().running_execution_context().variable_environment = vm().running_execution_context().variable_environment->outer_environment();
  128. }
  129. if (popped_scope.scope_node.ptr() == &scope_node)
  130. break;
  131. }
  132. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  133. if (m_scope_stack.is_empty())
  134. vm().stop_unwind();
  135. }
  136. void Interpreter::push_scope(ScopeFrame frame)
  137. {
  138. m_scope_stack.append(move(frame));
  139. }
  140. Value Interpreter::execute_statement(GlobalObject& global_object, const Statement& statement, ScopeType scope_type)
  141. {
  142. if (!is<ScopeNode>(statement))
  143. return statement.execute(*this, global_object);
  144. auto& block = static_cast<const ScopeNode&>(statement);
  145. enter_scope(block, scope_type, global_object);
  146. Value last_value;
  147. for (auto& node : block.children()) {
  148. auto value = node.execute(*this, global_object);
  149. if (!value.is_empty())
  150. last_value = value;
  151. if (vm().should_unwind()) {
  152. if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
  153. vm().stop_unwind();
  154. break;
  155. }
  156. }
  157. if (scope_type == ScopeType::Function) {
  158. bool did_return = vm().unwind_until() == ScopeType::Function;
  159. if (!did_return)
  160. last_value = js_undefined();
  161. }
  162. if (vm().unwind_until() == scope_type)
  163. vm().stop_unwind();
  164. exit_scope(block);
  165. return last_value;
  166. }
  167. FunctionEnvironmentRecord* Interpreter::current_function_environment_record()
  168. {
  169. return verify_cast<FunctionEnvironmentRecord>(vm().running_execution_context().lexical_environment);
  170. }
  171. }