Interpreter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. global_call_frame.function_name = "(global execution context)";
  64. global_call_frame.environment = heap().allocate<LexicalEnvironment>(global_object, LexicalEnvironment::EnvironmentRecordType::Global);
  65. global_call_frame.environment->bind_this_value(global_object, &global_object);
  66. global_call_frame.is_strict_mode = program.is_strict_mode();
  67. if (vm().exception())
  68. return {};
  69. vm().call_stack().append(move(global_call_frame));
  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, ArgumentVector arguments, 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_environment(), 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 (scope_node.is_program()) {
  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. for (auto& argument : arguments) {
  106. scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var });
  107. }
  108. bool pushed_lexical_environment = false;
  109. if (!scope_variables_with_declaration_kind.is_empty()) {
  110. auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_environment());
  111. vm().call_stack().last().environment = block_lexical_environment;
  112. pushed_lexical_environment = true;
  113. }
  114. push_scope({ scope_type, scope_node, pushed_lexical_environment });
  115. }
  116. void Interpreter::exit_scope(const ScopeNode& scope_node)
  117. {
  118. while (!m_scope_stack.is_empty()) {
  119. auto popped_scope = m_scope_stack.take_last();
  120. if (popped_scope.pushed_environment)
  121. vm().call_frame().environment = vm().call_frame().environment->parent();
  122. if (popped_scope.scope_node.ptr() == &scope_node)
  123. break;
  124. }
  125. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  126. if (m_scope_stack.is_empty())
  127. vm().unwind(ScopeType::None);
  128. }
  129. void Interpreter::push_scope(ScopeFrame frame)
  130. {
  131. m_scope_stack.append(move(frame));
  132. }
  133. Value Interpreter::execute_statement(GlobalObject& global_object, const Statement& statement, ArgumentVector arguments, ScopeType scope_type)
  134. {
  135. if (!statement.is_scope_node())
  136. return statement.execute(*this, global_object);
  137. auto& block = static_cast<const ScopeNode&>(statement);
  138. enter_scope(block, move(arguments), scope_type, global_object);
  139. if (block.children().is_empty())
  140. vm().set_last_value({}, js_undefined());
  141. for (auto& node : block.children()) {
  142. vm().set_last_value({}, node.execute(*this, global_object));
  143. if (vm().should_unwind()) {
  144. if (!block.label().is_null() && vm().should_unwind_until(ScopeType::Breakable, block.label()))
  145. vm().stop_unwind();
  146. break;
  147. }
  148. }
  149. bool did_return = vm().unwind_until() == ScopeType::Function;
  150. if (vm().unwind_until() == scope_type)
  151. vm().unwind(ScopeType::None);
  152. exit_scope(block);
  153. return did_return ? vm().last_value() : js_undefined();
  154. }
  155. }