Interpreter.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. CallFrame global_call_frame;
  57. global_call_frame.current_node = &program;
  58. global_call_frame.this_value = &global_object;
  59. static FlyString global_execution_context_name = "(global execution context)";
  60. global_call_frame.function_name = global_execution_context_name;
  61. global_call_frame.scope = &global_object;
  62. VERIFY(!vm.exception());
  63. global_call_frame.is_strict_mode = program.is_strict_mode();
  64. vm.push_call_frame(global_call_frame, global_object);
  65. VERIFY(!vm.exception());
  66. program.execute(*this, global_object);
  67. vm.pop_call_frame();
  68. }
  69. GlobalObject& Interpreter::global_object()
  70. {
  71. return static_cast<GlobalObject&>(*m_global_object.cell());
  72. }
  73. const GlobalObject& Interpreter::global_object() const
  74. {
  75. return static_cast<const GlobalObject&>(*m_global_object.cell());
  76. }
  77. void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
  78. {
  79. for (auto& declaration : scope_node.functions()) {
  80. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.is_strict_mode());
  81. vm().set_variable(declaration.name(), function, global_object);
  82. }
  83. if (scope_type == ScopeType::Function) {
  84. push_scope({ scope_type, scope_node, false });
  85. return;
  86. }
  87. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  88. scope_variables_with_declaration_kind.ensure_capacity(16);
  89. for (auto& declaration : scope_node.variables()) {
  90. for (auto& declarator : declaration.declarations()) {
  91. if (is<Program>(scope_node)) {
  92. global_object.put(declarator.id().string(), js_undefined());
  93. if (exception())
  94. return;
  95. } else {
  96. scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  97. }
  98. }
  99. }
  100. bool pushed_lexical_environment = false;
  101. if (!scope_variables_with_declaration_kind.is_empty()) {
  102. auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_scope());
  103. vm().call_frame().scope = block_lexical_environment;
  104. pushed_lexical_environment = true;
  105. }
  106. push_scope({ scope_type, scope_node, pushed_lexical_environment });
  107. }
  108. void Interpreter::exit_scope(const ScopeNode& scope_node)
  109. {
  110. while (!m_scope_stack.is_empty()) {
  111. auto popped_scope = m_scope_stack.take_last();
  112. if (popped_scope.pushed_environment)
  113. vm().call_frame().scope = vm().call_frame().scope->parent();
  114. if (popped_scope.scope_node.ptr() == &scope_node)
  115. break;
  116. }
  117. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  118. if (m_scope_stack.is_empty())
  119. vm().unwind(ScopeType::None);
  120. }
  121. void Interpreter::enter_node(const ASTNode& node)
  122. {
  123. vm().call_frame().current_node = &node;
  124. vm().push_ast_node(node);
  125. }
  126. void Interpreter::exit_node(const ASTNode&)
  127. {
  128. vm().pop_ast_node();
  129. }
  130. void Interpreter::push_scope(ScopeFrame frame)
  131. {
  132. m_scope_stack.append(move(frame));
  133. }
  134. Value Interpreter::execute_statement(GlobalObject& global_object, const Statement& statement, ScopeType scope_type)
  135. {
  136. if (!is<ScopeNode>(statement))
  137. return statement.execute(*this, global_object);
  138. auto& block = static_cast<const ScopeNode&>(statement);
  139. enter_scope(block, scope_type, global_object);
  140. if (block.children().is_empty())
  141. vm().set_last_value({}, js_undefined());
  142. for (auto& node : block.children()) {
  143. vm().set_last_value({}, node.execute(*this, global_object));
  144. if (vm().should_unwind()) {
  145. if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
  146. vm().stop_unwind();
  147. break;
  148. }
  149. }
  150. bool did_return = vm().unwind_until() == ScopeType::Function;
  151. if (vm().unwind_until() == scope_type)
  152. vm().unwind(ScopeType::None);
  153. exit_scope(block);
  154. return did_return ? vm().last_value() : js_undefined();
  155. }
  156. LexicalEnvironment* Interpreter::current_environment()
  157. {
  158. VERIFY(is<LexicalEnvironment>(vm().call_frame().scope));
  159. return static_cast<LexicalEnvironment*>(vm().call_frame().scope);
  160. }
  161. }