ScriptFunction.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. variables.set(parameter.name, { js_undefined(), DeclarationKind::Var });
  66. }
  67. if (is<ScopeNode>(body())) {
  68. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  69. for (auto& declarator : declaration.declarations()) {
  70. variables.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
  71. }
  72. }
  73. }
  74. auto* environment = heap().allocate<LexicalEnvironment>(global_object(), move(variables), m_parent_scope, LexicalEnvironment::EnvironmentRecordType::Function);
  75. environment->set_home_object(home_object());
  76. environment->set_current_function(*this);
  77. if (m_is_arrow_function) {
  78. if (is<LexicalEnvironment>(m_parent_scope))
  79. environment->set_new_target(static_cast<LexicalEnvironment*>(m_parent_scope)->new_target());
  80. }
  81. return environment;
  82. }
  83. Value ScriptFunction::execute_function_body()
  84. {
  85. auto& vm = this->vm();
  86. OwnPtr<Interpreter> local_interpreter;
  87. Interpreter* interpreter = vm.interpreter_if_exists();
  88. if (!interpreter) {
  89. local_interpreter = Interpreter::create_with_existing_global_object(global_object());
  90. interpreter = local_interpreter.ptr();
  91. }
  92. VM::InterpreterExecutionScope scope(*interpreter);
  93. auto& call_frame_args = vm.call_frame().arguments;
  94. for (size_t i = 0; i < m_parameters.size(); ++i) {
  95. auto parameter = m_parameters[i];
  96. Value argument_value;
  97. if (parameter.is_rest) {
  98. auto* array = Array::create(global_object());
  99. for (size_t rest_index = i; rest_index < call_frame_args.size(); ++rest_index)
  100. array->indexed_properties().append(call_frame_args[rest_index]);
  101. argument_value = move(array);
  102. } else if (i < call_frame_args.size() && !call_frame_args[i].is_undefined()) {
  103. argument_value = call_frame_args[i];
  104. } else if (parameter.default_value) {
  105. argument_value = parameter.default_value->execute(*interpreter, global_object());
  106. if (vm.exception())
  107. return {};
  108. } else {
  109. argument_value = js_undefined();
  110. }
  111. vm.current_scope()->put_to_scope(parameter.name, { argument_value, DeclarationKind::Var });
  112. }
  113. return interpreter->execute_statement(global_object(), m_body, ScopeType::Function);
  114. }
  115. Value ScriptFunction::call()
  116. {
  117. if (m_is_class_constructor) {
  118. vm().throw_exception<TypeError>(global_object(), ErrorType::ClassConstructorWithoutNew, m_name);
  119. return {};
  120. }
  121. return execute_function_body();
  122. }
  123. Value ScriptFunction::construct(Function&)
  124. {
  125. if (m_is_arrow_function) {
  126. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, m_name);
  127. return {};
  128. }
  129. return execute_function_body();
  130. }
  131. JS_DEFINE_NATIVE_GETTER(ScriptFunction::length_getter)
  132. {
  133. auto* function = typed_this(vm, global_object);
  134. if (!function)
  135. return {};
  136. return Value(static_cast<i32>(function->m_function_length));
  137. }
  138. JS_DEFINE_NATIVE_GETTER(ScriptFunction::name_getter)
  139. {
  140. auto* function = typed_this(vm, global_object);
  141. if (!function)
  142. return {};
  143. return js_string(vm, function->name().is_null() ? "" : function->name());
  144. }
  145. }