ScriptFunction.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/Interpreter.h>
  9. #include <LibJS/Runtime/Array.h>
  10. #include <LibJS/Runtime/Error.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/ScriptFunction.h>
  13. #include <LibJS/Runtime/Value.h>
  14. namespace JS {
  15. static ScriptFunction* typed_this(VM& vm, GlobalObject& global_object)
  16. {
  17. auto* this_object = vm.this_value(global_object).to_object(global_object);
  18. if (!this_object)
  19. return nullptr;
  20. if (!this_object->is_function()) {
  21. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunctionNoParam);
  22. return nullptr;
  23. }
  24. return static_cast<ScriptFunction*>(this_object);
  25. }
  26. 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)
  27. {
  28. 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);
  29. }
  30. 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)
  31. : Function(prototype, is_arrow_function ? vm().this_value(global_object) : Value(), {})
  32. , m_name(name)
  33. , m_body(body)
  34. , m_parameters(move(parameters))
  35. , m_parent_scope(parent_scope)
  36. , m_function_length(m_function_length)
  37. , m_is_strict(is_strict)
  38. , m_is_arrow_function(is_arrow_function)
  39. {
  40. }
  41. void ScriptFunction::initialize(GlobalObject& global_object)
  42. {
  43. auto& vm = this->vm();
  44. Function::initialize(global_object);
  45. if (!m_is_arrow_function) {
  46. Object* prototype = vm.heap().allocate<Object>(global_object, *global_object.new_script_function_prototype_object_shape());
  47. prototype->define_property(vm.names.constructor, this, Attribute::Writable | Attribute::Configurable);
  48. define_property(vm.names.prototype, prototype, Attribute::Writable);
  49. }
  50. define_native_property(vm.names.length, length_getter, {}, Attribute::Configurable);
  51. define_native_property(vm.names.name, name_getter, {}, Attribute::Configurable);
  52. }
  53. ScriptFunction::~ScriptFunction()
  54. {
  55. }
  56. void ScriptFunction::visit_edges(Visitor& visitor)
  57. {
  58. Function::visit_edges(visitor);
  59. visitor.visit(m_parent_scope);
  60. }
  61. LexicalEnvironment* ScriptFunction::create_environment()
  62. {
  63. HashMap<FlyString, Variable> variables;
  64. for (auto& parameter : m_parameters) {
  65. parameter.binding.visit(
  66. [&](const FlyString& name) { variables.set(name, { js_undefined(), DeclarationKind::Var }); },
  67. [&](const NonnullRefPtr<BindingPattern>& binding) {
  68. binding->for_each_assigned_name([&](const auto& name) {
  69. variables.set(name, { js_undefined(), DeclarationKind::Var });
  70. });
  71. });
  72. }
  73. if (is<ScopeNode>(body())) {
  74. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  75. for (auto& declarator : declaration.declarations()) {
  76. declarator.target().visit(
  77. [&](const NonnullRefPtr<Identifier>& id) {
  78. variables.set(id->string(), { js_undefined(), declaration.declaration_kind() });
  79. },
  80. [&](const NonnullRefPtr<BindingPattern>& binding) {
  81. binding->for_each_assigned_name([&](const auto& name) {
  82. variables.set(name, { js_undefined(), declaration.declaration_kind() });
  83. });
  84. });
  85. }
  86. }
  87. }
  88. auto* environment = heap().allocate<LexicalEnvironment>(global_object(), move(variables), m_parent_scope, LexicalEnvironment::EnvironmentRecordType::Function);
  89. environment->set_home_object(home_object());
  90. environment->set_current_function(*this);
  91. if (m_is_arrow_function) {
  92. if (is<LexicalEnvironment>(m_parent_scope))
  93. environment->set_new_target(static_cast<LexicalEnvironment*>(m_parent_scope)->new_target());
  94. }
  95. return environment;
  96. }
  97. Value ScriptFunction::execute_function_body()
  98. {
  99. auto& vm = this->vm();
  100. OwnPtr<Interpreter> local_interpreter;
  101. Interpreter* interpreter = vm.interpreter_if_exists();
  102. if (!interpreter) {
  103. local_interpreter = Interpreter::create_with_existing_global_object(global_object());
  104. interpreter = local_interpreter.ptr();
  105. }
  106. VM::InterpreterExecutionScope scope(*interpreter);
  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. argument_value = parameter.default_value->execute(*interpreter, global_object());
  122. if (vm.exception())
  123. return;
  124. } else {
  125. argument_value = js_undefined();
  126. }
  127. vm.assign(param, argument_value, global_object(), true, vm.current_scope());
  128. });
  129. if (vm.exception())
  130. return {};
  131. }
  132. return interpreter->execute_statement(global_object(), m_body, ScopeType::Function);
  133. }
  134. Value ScriptFunction::call()
  135. {
  136. if (m_is_class_constructor) {
  137. vm().throw_exception<TypeError>(global_object(), ErrorType::ClassConstructorWithoutNew, m_name);
  138. return {};
  139. }
  140. return execute_function_body();
  141. }
  142. Value ScriptFunction::construct(Function&)
  143. {
  144. if (m_is_arrow_function) {
  145. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, m_name);
  146. return {};
  147. }
  148. return execute_function_body();
  149. }
  150. JS_DEFINE_NATIVE_GETTER(ScriptFunction::length_getter)
  151. {
  152. auto* function = typed_this(vm, global_object);
  153. if (!function)
  154. return {};
  155. return Value(static_cast<i32>(function->m_function_length));
  156. }
  157. JS_DEFINE_NATIVE_GETTER(ScriptFunction::name_getter)
  158. {
  159. auto* function = typed_this(vm, global_object);
  160. if (!function)
  161. return {};
  162. return js_string(vm, function->name().is_null() ? "" : function->name());
  163. }
  164. }