Interpreter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/Badge.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibJS/AST.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/LexicalEnvironment.h>
  33. #include <LibJS/Runtime/MarkedValueList.h>
  34. #include <LibJS/Runtime/NativeFunction.h>
  35. #include <LibJS/Runtime/Object.h>
  36. #include <LibJS/Runtime/Reference.h>
  37. #include <LibJS/Runtime/ScriptFunction.h>
  38. #include <LibJS/Runtime/Shape.h>
  39. #include <LibJS/Runtime/SymbolObject.h>
  40. #include <LibJS/Runtime/Value.h>
  41. namespace JS {
  42. NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(GlobalObject& global_object)
  43. {
  44. DeferGC defer_gc(global_object.heap());
  45. auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
  46. interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
  47. return interpreter;
  48. }
  49. Interpreter::Interpreter(VM& vm)
  50. : m_vm(vm)
  51. {
  52. }
  53. Interpreter::~Interpreter()
  54. {
  55. }
  56. Value Interpreter::run(GlobalObject& global_object, const Program& program)
  57. {
  58. auto& vm = this->vm();
  59. ASSERT(!vm.exception());
  60. VM::InterpreterExecutionScope scope(*this);
  61. CallFrame global_call_frame;
  62. global_call_frame.this_value = &global_object;
  63. static FlyString global_execution_context_name = "(global execution context)";
  64. global_call_frame.function_name = global_execution_context_name;
  65. global_call_frame.scope = &global_object;
  66. ASSERT(!vm.exception());
  67. global_call_frame.is_strict_mode = program.is_strict_mode();
  68. vm.push_call_frame(global_call_frame, global_object);
  69. ASSERT(!vm.exception());
  70. auto result = program.execute(*this, global_object);
  71. vm.pop_call_frame();
  72. return result;
  73. }
  74. GlobalObject& Interpreter::global_object()
  75. {
  76. return static_cast<GlobalObject&>(*m_global_object.cell());
  77. }
  78. const GlobalObject& Interpreter::global_object() const
  79. {
  80. return static_cast<const GlobalObject&>(*m_global_object.cell());
  81. }
  82. void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
  83. {
  84. for (auto& declaration : scope_node.functions()) {
  85. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.is_strict_mode());
  86. vm().set_variable(declaration.name(), function, global_object);
  87. }
  88. if (scope_type == ScopeType::Function) {
  89. push_scope({ scope_type, scope_node, false });
  90. return;
  91. }
  92. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  93. scope_variables_with_declaration_kind.ensure_capacity(16);
  94. for (auto& declaration : scope_node.variables()) {
  95. for (auto& declarator : declaration.declarations()) {
  96. if (is<Program>(scope_node)) {
  97. global_object.put(declarator.id().string(), js_undefined());
  98. if (exception())
  99. return;
  100. } else {
  101. scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  102. }
  103. }
  104. }
  105. bool pushed_lexical_environment = false;
  106. if (!scope_variables_with_declaration_kind.is_empty()) {
  107. auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_scope());
  108. vm().call_frame().scope = block_lexical_environment;
  109. pushed_lexical_environment = true;
  110. }
  111. push_scope({ scope_type, scope_node, pushed_lexical_environment });
  112. }
  113. void Interpreter::exit_scope(const ScopeNode& scope_node)
  114. {
  115. while (!m_scope_stack.is_empty()) {
  116. auto popped_scope = m_scope_stack.take_last();
  117. if (popped_scope.pushed_environment)
  118. vm().call_frame().scope = vm().call_frame().scope->parent();
  119. if (popped_scope.scope_node.ptr() == &scope_node)
  120. break;
  121. }
  122. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  123. if (m_scope_stack.is_empty())
  124. vm().unwind(ScopeType::None);
  125. }
  126. void Interpreter::enter_node(const ASTNode& node)
  127. {
  128. vm().push_ast_node(node);
  129. }
  130. void Interpreter::exit_node(const ASTNode&)
  131. {
  132. vm().pop_ast_node();
  133. }
  134. void Interpreter::push_scope(ScopeFrame frame)
  135. {
  136. m_scope_stack.append(move(frame));
  137. }
  138. Value Interpreter::execute_statement(GlobalObject& global_object, const Statement& statement, ScopeType scope_type)
  139. {
  140. if (!is<ScopeNode>(statement))
  141. return statement.execute(*this, global_object);
  142. auto& block = static_cast<const ScopeNode&>(statement);
  143. enter_scope(block, scope_type, global_object);
  144. if (block.children().is_empty())
  145. vm().set_last_value({}, js_undefined());
  146. for (auto& node : block.children()) {
  147. vm().set_last_value({}, node.execute(*this, global_object));
  148. if (vm().should_unwind()) {
  149. if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
  150. vm().stop_unwind();
  151. break;
  152. }
  153. }
  154. bool did_return = vm().unwind_until() == ScopeType::Function;
  155. if (vm().unwind_until() == scope_type)
  156. vm().unwind(ScopeType::None);
  157. exit_scope(block);
  158. return did_return ? vm().last_value() : js_undefined();
  159. }
  160. LexicalEnvironment* Interpreter::current_environment()
  161. {
  162. ASSERT(is<LexicalEnvironment>(vm().call_frame().scope));
  163. return static_cast<LexicalEnvironment*>(vm().call_frame().scope);
  164. }
  165. }