Interpreter.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. //#define INTERPRETER_DEBUG
  42. namespace JS {
  43. NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(GlobalObject& global_object)
  44. {
  45. DeferGC defer_gc(global_object.heap());
  46. auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
  47. interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
  48. return interpreter;
  49. }
  50. Interpreter::Interpreter(VM& vm)
  51. : m_vm(vm)
  52. {
  53. }
  54. Interpreter::~Interpreter()
  55. {
  56. }
  57. Value Interpreter::run(GlobalObject& global_object, const Program& program)
  58. {
  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.environment = heap().allocate<LexicalEnvironment>(global_object, LexicalEnvironment::EnvironmentRecordType::Global);
  66. global_call_frame.environment->bind_this_value(global_object, &global_object);
  67. global_call_frame.is_strict_mode = program.is_strict_mode();
  68. if (vm().exception())
  69. return {};
  70. vm().call_stack().append(move(global_call_frame));
  71. auto result = program.execute(*this, global_object);
  72. vm().pop_call_frame();
  73. return result;
  74. }
  75. GlobalObject& Interpreter::global_object()
  76. {
  77. return static_cast<GlobalObject&>(*m_global_object.cell());
  78. }
  79. const GlobalObject& Interpreter::global_object() const
  80. {
  81. return static_cast<const GlobalObject&>(*m_global_object.cell());
  82. }
  83. void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type, GlobalObject& global_object)
  84. {
  85. for (auto& declaration : scope_node.functions()) {
  86. auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_environment(), declaration.is_strict_mode());
  87. vm().set_variable(declaration.name(), function, global_object);
  88. }
  89. if (scope_type == ScopeType::Function) {
  90. push_scope({ scope_type, scope_node, false });
  91. return;
  92. }
  93. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  94. scope_variables_with_declaration_kind.ensure_capacity(16);
  95. for (auto& declaration : scope_node.variables()) {
  96. for (auto& declarator : declaration.declarations()) {
  97. if (scope_node.is_program()) {
  98. global_object.put(declarator.id().string(), js_undefined());
  99. if (exception())
  100. return;
  101. } else {
  102. scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  103. }
  104. }
  105. }
  106. for (auto& argument : arguments) {
  107. scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var });
  108. }
  109. bool pushed_lexical_environment = false;
  110. if (!scope_variables_with_declaration_kind.is_empty()) {
  111. auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_environment());
  112. vm().call_stack().last().environment = block_lexical_environment;
  113. pushed_lexical_environment = true;
  114. }
  115. push_scope({ scope_type, scope_node, pushed_lexical_environment });
  116. }
  117. void Interpreter::exit_scope(const ScopeNode& scope_node)
  118. {
  119. while (!m_scope_stack.is_empty()) {
  120. auto popped_scope = m_scope_stack.take_last();
  121. if (popped_scope.pushed_environment)
  122. vm().call_frame().environment = vm().call_frame().environment->parent();
  123. if (popped_scope.scope_node.ptr() == &scope_node)
  124. break;
  125. }
  126. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  127. if (m_scope_stack.is_empty())
  128. vm().unwind(ScopeType::None);
  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, ArgumentVector arguments, ScopeType scope_type)
  135. {
  136. if (!statement.is_scope_node())
  137. return statement.execute(*this, global_object);
  138. auto& block = static_cast<const ScopeNode&>(statement);
  139. enter_scope(block, move(arguments), 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. }