Interpreter.cpp 8.0 KB

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