Interpreter.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibJS/AST.h>
  8. #include <LibJS/Interpreter.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/LexicalEnvironment.h>
  11. #include <LibJS/Runtime/Object.h>
  12. #include <LibJS/Runtime/Reference.h>
  13. #include <LibJS/Runtime/ScriptFunction.h>
  14. #include <LibJS/Runtime/Shape.h>
  15. #include <LibJS/Runtime/Value.h>
  16. namespace JS {
  17. NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(GlobalObject& global_object)
  18. {
  19. DeferGC defer_gc(global_object.heap());
  20. auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
  21. interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
  22. return interpreter;
  23. }
  24. Interpreter::Interpreter(VM& vm)
  25. : m_vm(vm)
  26. {
  27. }
  28. Interpreter::~Interpreter()
  29. {
  30. }
  31. void Interpreter::run(GlobalObject& global_object, const Program& program)
  32. {
  33. auto& vm = this->vm();
  34. VERIFY(!vm.exception());
  35. VM::InterpreterExecutionScope scope(*this);
  36. vm.set_last_value({}, {});
  37. CallFrame global_call_frame;
  38. global_call_frame.current_node = &program;
  39. global_call_frame.this_value = &global_object;
  40. static FlyString global_execution_context_name = "(global execution context)";
  41. global_call_frame.function_name = global_execution_context_name;
  42. global_call_frame.scope = &global_object;
  43. VERIFY(!vm.exception());
  44. global_call_frame.is_strict_mode = program.is_strict_mode();
  45. vm.push_call_frame(global_call_frame, global_object);
  46. VERIFY(!vm.exception());
  47. program.execute(*this, global_object);
  48. // Whatever the promise jobs or on_call_stack_emptied do should not affect the effective
  49. // 'last value'.
  50. auto last_value = vm.last_value();
  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.set_last_value({}, last_value.value_or(js_undefined()));
  56. }
  57. GlobalObject& Interpreter::global_object()
  58. {
  59. return static_cast<GlobalObject&>(*m_global_object.cell());
  60. }
  61. const GlobalObject& Interpreter::global_object() const
  62. {
  63. return static_cast<const GlobalObject&>(*m_global_object.cell());
  64. }
  65. void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
  66. {
  67. for (auto& declaration : scope_node.functions()) {
  68. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.is_strict_mode());
  69. vm().set_variable(declaration.name(), function, global_object);
  70. }
  71. if (scope_type == ScopeType::Function) {
  72. push_scope({ scope_type, scope_node, false });
  73. return;
  74. }
  75. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  76. scope_variables_with_declaration_kind.ensure_capacity(16);
  77. for (auto& declaration : scope_node.variables()) {
  78. for (auto& declarator : declaration.declarations()) {
  79. if (is<Program>(scope_node)) {
  80. global_object.put(declarator.id().string(), js_undefined());
  81. if (exception())
  82. return;
  83. } else {
  84. scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  85. }
  86. }
  87. }
  88. bool pushed_lexical_environment = false;
  89. if (!scope_variables_with_declaration_kind.is_empty()) {
  90. auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_scope());
  91. vm().call_frame().scope = block_lexical_environment;
  92. pushed_lexical_environment = true;
  93. }
  94. push_scope({ scope_type, scope_node, pushed_lexical_environment });
  95. }
  96. void Interpreter::exit_scope(const ScopeNode& scope_node)
  97. {
  98. while (!m_scope_stack.is_empty()) {
  99. auto popped_scope = m_scope_stack.take_last();
  100. if (popped_scope.pushed_environment)
  101. vm().call_frame().scope = vm().call_frame().scope->parent();
  102. if (popped_scope.scope_node.ptr() == &scope_node)
  103. break;
  104. }
  105. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  106. if (m_scope_stack.is_empty())
  107. vm().stop_unwind();
  108. }
  109. void Interpreter::push_scope(ScopeFrame frame)
  110. {
  111. m_scope_stack.append(move(frame));
  112. }
  113. Value Interpreter::execute_statement(GlobalObject& global_object, const Statement& statement, ScopeType scope_type)
  114. {
  115. if (!is<ScopeNode>(statement))
  116. return statement.execute(*this, global_object);
  117. auto& block = static_cast<const ScopeNode&>(statement);
  118. enter_scope(block, scope_type, global_object);
  119. for (auto& node : block.children()) {
  120. auto value = node.execute(*this, global_object);
  121. if (!value.is_empty())
  122. vm().set_last_value({}, value);
  123. if (vm().should_unwind()) {
  124. if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
  125. vm().stop_unwind();
  126. break;
  127. }
  128. }
  129. if (scope_type == ScopeType::Function) {
  130. bool did_return = vm().unwind_until() == ScopeType::Function;
  131. if (!did_return)
  132. vm().set_last_value({}, js_undefined());
  133. }
  134. if (vm().unwind_until() == scope_type)
  135. vm().stop_unwind();
  136. exit_scope(block);
  137. return vm().last_value();
  138. }
  139. LexicalEnvironment* Interpreter::current_environment()
  140. {
  141. VERIFY(is<LexicalEnvironment>(vm().call_frame().scope));
  142. return static_cast<LexicalEnvironment*>(vm().call_frame().scope);
  143. }
  144. }