Interpreter.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibJS/AST.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. #include <LibJS/Runtime/LexicalEnvironment.h>
  31. #include <LibJS/Runtime/Object.h>
  32. #include <LibJS/Runtime/Reference.h>
  33. #include <LibJS/Runtime/ScriptFunction.h>
  34. #include <LibJS/Runtime/Shape.h>
  35. #include <LibJS/Runtime/Value.h>
  36. namespace JS {
  37. NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(GlobalObject& global_object)
  38. {
  39. DeferGC defer_gc(global_object.heap());
  40. auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
  41. interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
  42. return interpreter;
  43. }
  44. Interpreter::Interpreter(VM& vm)
  45. : m_vm(vm)
  46. {
  47. }
  48. Interpreter::~Interpreter()
  49. {
  50. }
  51. void Interpreter::run(GlobalObject& global_object, const Program& program)
  52. {
  53. auto& vm = this->vm();
  54. VERIFY(!vm.exception());
  55. VM::InterpreterExecutionScope scope(*this);
  56. vm.set_last_value({}, {});
  57. CallFrame global_call_frame;
  58. global_call_frame.current_node = &program;
  59. global_call_frame.this_value = &global_object;
  60. static FlyString global_execution_context_name = "(global execution context)";
  61. global_call_frame.function_name = global_execution_context_name;
  62. global_call_frame.scope = &global_object;
  63. VERIFY(!vm.exception());
  64. global_call_frame.is_strict_mode = program.is_strict_mode();
  65. vm.push_call_frame(global_call_frame, global_object);
  66. VERIFY(!vm.exception());
  67. program.execute(*this, global_object);
  68. vm.pop_call_frame();
  69. if (vm.last_value().is_empty())
  70. vm.set_last_value({}, js_undefined());
  71. }
  72. GlobalObject& Interpreter::global_object()
  73. {
  74. return static_cast<GlobalObject&>(*m_global_object.cell());
  75. }
  76. const GlobalObject& Interpreter::global_object() const
  77. {
  78. return static_cast<const GlobalObject&>(*m_global_object.cell());
  79. }
  80. void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
  81. {
  82. for (auto& declaration : scope_node.functions()) {
  83. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.is_strict_mode());
  84. vm().set_variable(declaration.name(), function, global_object);
  85. }
  86. if (scope_type == ScopeType::Function) {
  87. push_scope({ scope_type, scope_node, false });
  88. return;
  89. }
  90. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  91. scope_variables_with_declaration_kind.ensure_capacity(16);
  92. for (auto& declaration : scope_node.variables()) {
  93. for (auto& declarator : declaration.declarations()) {
  94. if (is<Program>(scope_node)) {
  95. global_object.put(declarator.id().string(), js_undefined());
  96. if (exception())
  97. return;
  98. } else {
  99. scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  100. }
  101. }
  102. }
  103. bool pushed_lexical_environment = false;
  104. if (!scope_variables_with_declaration_kind.is_empty()) {
  105. auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_scope());
  106. vm().call_frame().scope = block_lexical_environment;
  107. pushed_lexical_environment = true;
  108. }
  109. push_scope({ scope_type, scope_node, pushed_lexical_environment });
  110. }
  111. void Interpreter::exit_scope(const ScopeNode& scope_node)
  112. {
  113. while (!m_scope_stack.is_empty()) {
  114. auto popped_scope = m_scope_stack.take_last();
  115. if (popped_scope.pushed_environment)
  116. vm().call_frame().scope = vm().call_frame().scope->parent();
  117. if (popped_scope.scope_node.ptr() == &scope_node)
  118. break;
  119. }
  120. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  121. if (m_scope_stack.is_empty())
  122. vm().unwind(ScopeType::None);
  123. }
  124. void Interpreter::push_scope(ScopeFrame frame)
  125. {
  126. m_scope_stack.append(move(frame));
  127. }
  128. Value Interpreter::execute_statement(GlobalObject& global_object, const Statement& statement, ScopeType scope_type)
  129. {
  130. if (!is<ScopeNode>(statement))
  131. return statement.execute(*this, global_object);
  132. auto& block = static_cast<const ScopeNode&>(statement);
  133. enter_scope(block, scope_type, global_object);
  134. for (auto& node : block.children()) {
  135. auto value = node.execute(*this, global_object);
  136. if (!value.is_empty())
  137. vm().set_last_value({}, value);
  138. if (vm().should_unwind()) {
  139. if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
  140. vm().stop_unwind();
  141. break;
  142. }
  143. }
  144. if (scope_type == ScopeType::Function) {
  145. bool did_return = vm().unwind_until() == ScopeType::Function;
  146. if (!did_return)
  147. vm().set_last_value({}, js_undefined());
  148. }
  149. if (vm().unwind_until() == scope_type)
  150. vm().unwind(ScopeType::None);
  151. exit_scope(block);
  152. return vm().last_value();
  153. }
  154. LexicalEnvironment* Interpreter::current_environment()
  155. {
  156. VERIFY(is<LexicalEnvironment>(vm().call_frame().scope));
  157. return static_cast<LexicalEnvironment*>(vm().call_frame().scope);
  158. }
  159. }