ScriptFunction.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Function.h>
  8. #include <LibJS/AST.h>
  9. #include <LibJS/Bytecode/BasicBlock.h>
  10. #include <LibJS/Bytecode/Generator.h>
  11. #include <LibJS/Bytecode/Interpreter.h>
  12. #include <LibJS/Interpreter.h>
  13. #include <LibJS/Runtime/Array.h>
  14. #include <LibJS/Runtime/Error.h>
  15. #include <LibJS/Runtime/GlobalObject.h>
  16. #include <LibJS/Runtime/ScriptFunction.h>
  17. #include <LibJS/Runtime/Value.h>
  18. namespace JS {
  19. static ScriptFunction* typed_this(VM& vm, GlobalObject& global_object)
  20. {
  21. auto* this_object = vm.this_value(global_object).to_object(global_object);
  22. if (!this_object)
  23. return nullptr;
  24. if (!this_object->is_function()) {
  25. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunctionNoParam);
  26. return nullptr;
  27. }
  28. return static_cast<ScriptFunction*>(this_object);
  29. }
  30. ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, bool is_strict, bool is_arrow_function)
  31. {
  32. return global_object.heap().allocate<ScriptFunction>(global_object, global_object, name, body, move(parameters), m_function_length, parent_scope, *global_object.function_prototype(), is_strict, is_arrow_function);
  33. }
  34. ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, ScopeObject* parent_scope, Object& prototype, bool is_strict, bool is_arrow_function)
  35. : Function(prototype, is_arrow_function ? vm().this_value(global_object) : Value(), {})
  36. , m_name(name)
  37. , m_body(body)
  38. , m_parameters(move(parameters))
  39. , m_parent_scope(parent_scope)
  40. , m_function_length(m_function_length)
  41. , m_is_strict(is_strict)
  42. , m_is_arrow_function(is_arrow_function)
  43. {
  44. }
  45. void ScriptFunction::initialize(GlobalObject& global_object)
  46. {
  47. auto& vm = this->vm();
  48. Function::initialize(global_object);
  49. if (!m_is_arrow_function) {
  50. Object* prototype = vm.heap().allocate<Object>(global_object, *global_object.new_script_function_prototype_object_shape());
  51. prototype->define_property(vm.names.constructor, this, Attribute::Writable | Attribute::Configurable);
  52. define_property(vm.names.prototype, prototype, Attribute::Writable);
  53. }
  54. define_native_property(vm.names.length, length_getter, {}, Attribute::Configurable);
  55. define_native_property(vm.names.name, name_getter, {}, Attribute::Configurable);
  56. }
  57. ScriptFunction::~ScriptFunction()
  58. {
  59. }
  60. void ScriptFunction::visit_edges(Visitor& visitor)
  61. {
  62. Function::visit_edges(visitor);
  63. visitor.visit(m_parent_scope);
  64. }
  65. LexicalEnvironment* ScriptFunction::create_environment()
  66. {
  67. HashMap<FlyString, Variable> variables;
  68. for (auto& parameter : m_parameters) {
  69. parameter.binding.visit(
  70. [&](const FlyString& name) { variables.set(name, { js_undefined(), DeclarationKind::Var }); },
  71. [&](const NonnullRefPtr<BindingPattern>& binding) {
  72. binding->for_each_assigned_name([&](const auto& name) {
  73. variables.set(name, { js_undefined(), DeclarationKind::Var });
  74. });
  75. });
  76. }
  77. if (is<ScopeNode>(body())) {
  78. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  79. for (auto& declarator : declaration.declarations()) {
  80. declarator.target().visit(
  81. [&](const NonnullRefPtr<Identifier>& id) {
  82. variables.set(id->string(), { js_undefined(), declaration.declaration_kind() });
  83. },
  84. [&](const NonnullRefPtr<BindingPattern>& binding) {
  85. binding->for_each_assigned_name([&](const auto& name) {
  86. variables.set(name, { js_undefined(), declaration.declaration_kind() });
  87. });
  88. });
  89. }
  90. }
  91. }
  92. auto* environment = heap().allocate<LexicalEnvironment>(global_object(), move(variables), m_parent_scope, LexicalEnvironment::EnvironmentRecordType::Function);
  93. environment->set_home_object(home_object());
  94. environment->set_current_function(*this);
  95. if (m_is_arrow_function) {
  96. if (is<LexicalEnvironment>(m_parent_scope))
  97. environment->set_new_target(static_cast<LexicalEnvironment*>(m_parent_scope)->new_target());
  98. }
  99. return environment;
  100. }
  101. Value ScriptFunction::execute_function_body()
  102. {
  103. auto& vm = this->vm();
  104. Interpreter* ast_interpreter = nullptr;
  105. auto* bytecode_interpreter = Bytecode::Interpreter::current();
  106. auto prepare_arguments = [&] {
  107. auto& call_frame_args = vm.call_frame().arguments;
  108. for (size_t i = 0; i < m_parameters.size(); ++i) {
  109. auto& parameter = m_parameters[i];
  110. parameter.binding.visit(
  111. [&](const auto& param) {
  112. Value argument_value;
  113. if (parameter.is_rest) {
  114. auto* array = Array::create(global_object());
  115. for (size_t rest_index = i; rest_index < call_frame_args.size(); ++rest_index)
  116. array->indexed_properties().append(call_frame_args[rest_index]);
  117. argument_value = move(array);
  118. } else if (i < call_frame_args.size() && !call_frame_args[i].is_undefined()) {
  119. argument_value = call_frame_args[i];
  120. } else if (parameter.default_value) {
  121. // FIXME: Support default arguments in the bytecode world!
  122. if (!bytecode_interpreter)
  123. argument_value = parameter.default_value->execute(*ast_interpreter, global_object());
  124. if (vm.exception())
  125. return;
  126. } else {
  127. argument_value = js_undefined();
  128. }
  129. vm.assign(param, argument_value, global_object(), true, vm.current_scope());
  130. });
  131. if (vm.exception())
  132. return;
  133. }
  134. };
  135. if (bytecode_interpreter) {
  136. prepare_arguments();
  137. if (!m_bytecode_execution_unit.has_value()) {
  138. m_bytecode_execution_unit = Bytecode::Generator::generate(m_body);
  139. if constexpr (JS_BYTECODE_DEBUG) {
  140. dbgln("Compiled Bytecode::Block for function '{}':", m_name);
  141. for (auto& block : m_bytecode_execution_unit->basic_blocks)
  142. block.dump();
  143. }
  144. }
  145. return bytecode_interpreter->run(*m_bytecode_execution_unit);
  146. } else {
  147. OwnPtr<Interpreter> local_interpreter;
  148. ast_interpreter = vm.interpreter_if_exists();
  149. if (!ast_interpreter) {
  150. local_interpreter = Interpreter::create_with_existing_global_object(global_object());
  151. ast_interpreter = local_interpreter.ptr();
  152. }
  153. VM::InterpreterExecutionScope scope(*ast_interpreter);
  154. prepare_arguments();
  155. if (vm.exception())
  156. return {};
  157. return ast_interpreter->execute_statement(global_object(), m_body, ScopeType::Function);
  158. }
  159. }
  160. Value ScriptFunction::call()
  161. {
  162. if (m_is_class_constructor) {
  163. vm().throw_exception<TypeError>(global_object(), ErrorType::ClassConstructorWithoutNew, m_name);
  164. return {};
  165. }
  166. return execute_function_body();
  167. }
  168. Value ScriptFunction::construct(Function&)
  169. {
  170. if (m_is_arrow_function) {
  171. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, m_name);
  172. return {};
  173. }
  174. return execute_function_body();
  175. }
  176. JS_DEFINE_NATIVE_GETTER(ScriptFunction::length_getter)
  177. {
  178. auto* function = typed_this(vm, global_object);
  179. if (!function)
  180. return {};
  181. return Value(static_cast<i32>(function->m_function_length));
  182. }
  183. JS_DEFINE_NATIVE_GETTER(ScriptFunction::name_getter)
  184. {
  185. auto* function = typed_this(vm, global_object);
  186. if (!function)
  187. return {};
  188. return js_string(vm, function->name().is_null() ? "" : function->name());
  189. }
  190. }