ScriptFunction.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. Function::initialize(global_object);
  64. if (!m_is_arrow_function) {
  65. Object* prototype = Object::create_empty(global_object);
  66. prototype->define_property("constructor", this, Attribute::Writable | Attribute::Configurable);
  67. define_property("prototype", prototype, 0);
  68. }
  69. define_native_property("length", length_getter, nullptr, Attribute::Configurable);
  70. define_native_property("name", name_getter, nullptr, Attribute::Configurable);
  71. }
  72. ScriptFunction::~ScriptFunction()
  73. {
  74. }
  75. void ScriptFunction::visit_children(Visitor& visitor)
  76. {
  77. Function::visit_children(visitor);
  78. visitor.visit(m_parent_environment);
  79. }
  80. LexicalEnvironment* ScriptFunction::create_environment()
  81. {
  82. HashMap<FlyString, Variable> variables;
  83. for (auto& parameter : m_parameters) {
  84. variables.set(parameter.name, { js_undefined(), DeclarationKind::Var });
  85. }
  86. if (body().is_scope_node()) {
  87. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  88. for (auto& declarator : declaration.declarations()) {
  89. variables.set(declarator.id().string(), { js_undefined(), DeclarationKind::Var });
  90. }
  91. }
  92. }
  93. auto* environment = heap().allocate<LexicalEnvironment>(global_object(), move(variables), m_parent_environment, LexicalEnvironment::EnvironmentRecordType::Function);
  94. environment->set_home_object(home_object());
  95. environment->set_current_function(*this);
  96. return environment;
  97. }
  98. Value ScriptFunction::call()
  99. {
  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& argument_values = vm().call_frame().arguments;
  108. ArgumentVector arguments;
  109. for (size_t i = 0; i < m_parameters.size(); ++i) {
  110. auto parameter = parameters()[i];
  111. auto value = js_undefined();
  112. if (parameter.is_rest) {
  113. auto* array = Array::create(global_object());
  114. for (size_t rest_index = i; rest_index < argument_values.size(); ++rest_index)
  115. array->indexed_properties().append(argument_values[rest_index]);
  116. value = Value(array);
  117. } else {
  118. if (i < argument_values.size() && !argument_values[i].is_undefined()) {
  119. value = argument_values[i];
  120. } else if (parameter.default_value) {
  121. value = parameter.default_value->execute(*interpreter, global_object());
  122. if (vm().exception())
  123. return {};
  124. }
  125. }
  126. arguments.append({ parameter.name, value });
  127. vm().current_environment()->set(global_object(), parameter.name, { value, DeclarationKind::Var });
  128. }
  129. return interpreter->execute_statement(global_object(), m_body, arguments, ScopeType::Function);
  130. }
  131. Value ScriptFunction::construct(Function&)
  132. {
  133. if (m_is_arrow_function) {
  134. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, m_name.characters());
  135. return {};
  136. }
  137. return call();
  138. }
  139. JS_DEFINE_NATIVE_GETTER(ScriptFunction::length_getter)
  140. {
  141. auto* function = typed_this(vm, global_object);
  142. if (!function)
  143. return {};
  144. return Value(static_cast<i32>(function->m_function_length));
  145. }
  146. JS_DEFINE_NATIVE_GETTER(ScriptFunction::name_getter)
  147. {
  148. auto* function = typed_this(vm, global_object);
  149. if (!function)
  150. return {};
  151. return js_string(vm, function->name().is_null() ? "" : function->name());
  152. }
  153. }