ScriptFunction.cpp 8.6 KB

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