Interpreter.cpp 8.0 KB

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