Interpreter.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <LibJS/AST.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/ArrayPrototype.h>
  30. #include <LibJS/Runtime/BooleanPrototype.h>
  31. #include <LibJS/Runtime/DatePrototype.h>
  32. #include <LibJS/Runtime/Error.h>
  33. #include <LibJS/Runtime/ErrorPrototype.h>
  34. #include <LibJS/Runtime/FunctionPrototype.h>
  35. #include <LibJS/Runtime/GlobalObject.h>
  36. #include <LibJS/Runtime/NativeFunction.h>
  37. #include <LibJS/Runtime/NumberPrototype.h>
  38. #include <LibJS/Runtime/Object.h>
  39. #include <LibJS/Runtime/ObjectPrototype.h>
  40. #include <LibJS/Runtime/Shape.h>
  41. #include <LibJS/Runtime/StringPrototype.h>
  42. #include <LibJS/Runtime/Value.h>
  43. namespace JS {
  44. Interpreter::Interpreter()
  45. : m_heap(*this)
  46. {
  47. m_empty_object_shape = heap().allocate<Shape>();
  48. // These are done first since other prototypes depend on their presence.
  49. m_object_prototype = heap().allocate<ObjectPrototype>();
  50. m_function_prototype = heap().allocate<FunctionPrototype>();
  51. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  52. if (!m_##snake_name##_prototype) \
  53. m_##snake_name##_prototype = heap().allocate<PrototypeName>();
  54. JS_ENUMERATE_BUILTIN_TYPES
  55. #undef __JS_ENUMERATE
  56. }
  57. Interpreter::~Interpreter()
  58. {
  59. }
  60. Value Interpreter::run(const Statement& statement, ArgumentVector arguments, ScopeType scope_type)
  61. {
  62. if (!statement.is_scope_node())
  63. return statement.execute(*this);
  64. auto& block = static_cast<const ScopeNode&>(statement);
  65. enter_scope(block, move(arguments), scope_type);
  66. m_last_value = js_undefined();
  67. for (auto& node : block.children()) {
  68. m_last_value = node.execute(*this);
  69. if (m_unwind_until != ScopeType::None)
  70. break;
  71. }
  72. bool did_return = m_unwind_until == ScopeType::Function;
  73. if (m_unwind_until == scope_type)
  74. m_unwind_until = ScopeType::None;
  75. exit_scope(block);
  76. return did_return ? m_last_value : js_undefined();
  77. }
  78. void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type)
  79. {
  80. HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
  81. scope_variables_with_declaration_kind.ensure_capacity(16);
  82. for (auto& declaration : scope_node.variables()) {
  83. for (auto& declarator : declaration.declarations()) {
  84. if (scope_node.is_program())
  85. global_object().put(declarator.id().string(), js_undefined());
  86. else
  87. scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  88. }
  89. }
  90. for (auto& argument : arguments) {
  91. scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var });
  92. }
  93. m_scope_stack.append({ scope_type, scope_node, move(scope_variables_with_declaration_kind) });
  94. }
  95. void Interpreter::exit_scope(const ScopeNode& scope_node)
  96. {
  97. while (!m_scope_stack.is_empty()) {
  98. auto popped_scope = m_scope_stack.take_last();
  99. if (popped_scope.scope_node.ptr() == &scope_node)
  100. break;
  101. }
  102. // If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
  103. if (m_scope_stack.is_empty())
  104. m_unwind_until = ScopeType::None;
  105. }
  106. void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
  107. {
  108. for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
  109. auto& scope = m_scope_stack.at(i);
  110. auto possible_match = scope.variables.get(name);
  111. if (possible_match.has_value()) {
  112. if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) {
  113. throw_exception<TypeError>("Assignment to constant variable");
  114. return;
  115. }
  116. scope.variables.set(move(name), { move(value), possible_match.value().declaration_kind });
  117. return;
  118. }
  119. }
  120. global_object().put(move(name), move(value));
  121. }
  122. Optional<Value> Interpreter::get_variable(const FlyString& name)
  123. {
  124. for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) {
  125. auto& scope = m_scope_stack.at(i);
  126. auto value = scope.variables.get(name);
  127. if (value.has_value())
  128. return value.value().value;
  129. }
  130. return global_object().get(name);
  131. }
  132. void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
  133. {
  134. roots.set(m_empty_object_shape);
  135. roots.set(m_global_object);
  136. roots.set(m_exception);
  137. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
  138. roots.set(m_##snake_name##_prototype);
  139. JS_ENUMERATE_BUILTIN_TYPES
  140. #undef __JS_ENUMERATE
  141. if (m_last_value.is_cell())
  142. roots.set(m_last_value.as_cell());
  143. for (auto& scope : m_scope_stack) {
  144. for (auto& it : scope.variables) {
  145. if (it.value.value.is_cell())
  146. roots.set(it.value.value.as_cell());
  147. }
  148. }
  149. for (auto& call_frame : m_call_stack) {
  150. if (call_frame.this_value.is_cell())
  151. roots.set(call_frame.this_value.as_cell());
  152. for (auto& argument : call_frame.arguments) {
  153. if (argument.is_cell())
  154. roots.set(argument.as_cell());
  155. }
  156. }
  157. }
  158. Value Interpreter::call(Function* function, Value this_value, const Vector<Value>& arguments)
  159. {
  160. auto& call_frame = push_call_frame();
  161. call_frame.function_name = function->name();
  162. call_frame.this_value = this_value;
  163. call_frame.arguments = arguments;
  164. auto result = function->call(*this);
  165. pop_call_frame();
  166. return result;
  167. }
  168. Value Interpreter::throw_exception(Exception* exception)
  169. {
  170. if (exception->value().is_object() && exception->value().as_object().is_error()) {
  171. auto& error = static_cast<Error&>(exception->value().as_object());
  172. dbg() << "Throwing JavaScript Error: " << error.name() << ", " << error.message();
  173. }
  174. m_exception = exception;
  175. unwind(ScopeType::Try);
  176. return {};
  177. }
  178. GlobalObject& Interpreter::global_object()
  179. {
  180. return static_cast<GlobalObject&>(*m_global_object);
  181. }
  182. const GlobalObject& Interpreter::global_object() const
  183. {
  184. return static_cast<const GlobalObject&>(*m_global_object);
  185. }
  186. }