ScriptFunction.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <LibJS/AST.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Array.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/ScriptFunction.h>
  33. #include <LibJS/Runtime/Value.h>
  34. namespace JS {
  35. static ScriptFunction* typed_this(VM& vm, GlobalObject& global_object)
  36. {
  37. auto* this_object = vm.this_value(global_object).to_object(global_object);
  38. if (!this_object)
  39. return nullptr;
  40. if (!this_object->is_function()) {
  41. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunctionNoParam);
  42. return nullptr;
  43. }
  44. return static_cast<ScriptFunction*>(this_object);
  45. }
  46. ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, bool is_strict, bool is_arrow_function)
  47. {
  48. return global_object.heap().allocate<ScriptFunction>(global_object, global_object, name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype(), is_strict, is_arrow_function);
  49. }
  50. ScriptFunction::ScriptFunction(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype, bool is_strict, bool is_arrow_function)
  51. : Function(prototype, is_arrow_function ? vm().this_value(global_object) : Value(), {})
  52. , m_name(name)
  53. , m_body(body)
  54. , m_parameters(move(parameters))
  55. , m_parent_environment(parent_environment)
  56. , m_function_length(m_function_length)
  57. , m_is_strict(is_strict)
  58. , m_is_arrow_function(is_arrow_function)
  59. {
  60. }
  61. void ScriptFunction::initialize(GlobalObject& global_object)
  62. {
  63. auto& vm = this->vm();
  64. Function::initialize(global_object);
  65. if (!m_is_arrow_function) {
  66. Object* prototype = vm.heap().allocate<Object>(global_object, *global_object.new_script_function_prototype_object_shape());
  67. prototype->define_property(vm.names.constructor, this, Attribute::Writable | Attribute::Configurable);
  68. define_property(vm.names.prototype, prototype, 0);
  69. }
  70. define_native_property(vm.names.length, length_getter, nullptr, Attribute::Configurable);
  71. define_native_property(vm.names.name, name_getter, nullptr, Attribute::Configurable);
  72. }
  73. ScriptFunction::~ScriptFunction()
  74. {
  75. }
  76. void ScriptFunction::visit_children(Visitor& visitor)
  77. {
  78. Function::visit_children(visitor);
  79. visitor.visit(m_parent_environment);
  80. }
  81. LexicalEnvironment* ScriptFunction::create_environment()
  82. {
  83. HashMap<FlyString, Variable> variables;
  84. for (auto& parameter : m_parameters) {
  85. variables.set(parameter.name, { js_undefined(), DeclarationKind::Var });
  86. }
  87. if (body().is_scope_node()) {
  88. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  89. for (auto& declarator : declaration.declarations()) {
  90. variables.set(declarator.id().string(), { js_undefined(), DeclarationKind::Var });
  91. }
  92. }
  93. }
  94. auto* environment = heap().allocate<LexicalEnvironment>(global_object(), move(variables), m_parent_environment, LexicalEnvironment::EnvironmentRecordType::Function);
  95. environment->set_home_object(home_object());
  96. environment->set_current_function(*this);
  97. if (m_is_arrow_function)
  98. environment->set_new_target(m_parent_environment->new_target());
  99. return environment;
  100. }
  101. Value ScriptFunction::execute_function_body()
  102. {
  103. auto& vm = this->vm();
  104. OwnPtr<Interpreter> local_interpreter;
  105. Interpreter* interpreter = vm.interpreter_if_exists();
  106. if (!interpreter) {
  107. local_interpreter = Interpreter::create_with_existing_global_object(global_object());
  108. interpreter = local_interpreter.ptr();
  109. }
  110. VM::InterpreterExecutionScope scope(*interpreter);
  111. auto& call_frame_args = vm.call_frame().arguments;
  112. ArgumentVector arguments;
  113. for (size_t i = 0; i < m_parameters.size(); ++i) {
  114. auto parameter = m_parameters[i];
  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. argument_value = parameter.default_value->execute(*interpreter, global_object());
  125. if (vm.exception())
  126. return {};
  127. } else {
  128. argument_value = js_undefined();
  129. }
  130. arguments.append({ parameter.name, argument_value });
  131. vm.current_environment()->set(global_object(), parameter.name, { argument_value, DeclarationKind::Var });
  132. }
  133. return interpreter->execute_statement(global_object(), m_body, move(arguments), ScopeType::Function);
  134. }
  135. Value ScriptFunction::call()
  136. {
  137. if (m_is_class_constructor) {
  138. vm().throw_exception<TypeError>(global_object(), ErrorType::ClassConstructorWithoutNew, m_name);
  139. return {};
  140. }
  141. return execute_function_body();
  142. }
  143. Value ScriptFunction::construct(Function&)
  144. {
  145. if (m_is_arrow_function) {
  146. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, m_name);
  147. return {};
  148. }
  149. return execute_function_body();
  150. }
  151. JS_DEFINE_NATIVE_GETTER(ScriptFunction::length_getter)
  152. {
  153. auto* function = typed_this(vm, global_object);
  154. if (!function)
  155. return {};
  156. return Value(static_cast<i32>(function->m_function_length));
  157. }
  158. JS_DEFINE_NATIVE_GETTER(ScriptFunction::name_getter)
  159. {
  160. auto* function = typed_this(vm, global_object);
  161. if (!function)
  162. return {};
  163. return js_string(vm, function->name().is_null() ? "" : function->name());
  164. }
  165. }