ScriptFunction.cpp 7.9 KB

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