ScriptFunction.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(Interpreter& interpreter, GlobalObject& global_object)
  36. {
  37. auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
  38. if (!this_object)
  39. return nullptr;
  40. if (!this_object->is_function()) {
  41. interpreter.throw_exception<TypeError>(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_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_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_arrow_function)
  51. : Function(prototype, is_arrow_function ? interpreter().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_arrow_function(is_arrow_function)
  58. {
  59. }
  60. void ScriptFunction::initialize(GlobalObject& global_object)
  61. {
  62. Function::initialize(global_object);
  63. if (!m_is_arrow_function) {
  64. Object* prototype = Object::create_empty(global_object);
  65. prototype->define_property("constructor", this, Attribute::Writable | Attribute::Configurable);
  66. define_property("prototype", prototype, 0);
  67. }
  68. define_native_property("length", length_getter, nullptr, Attribute::Configurable);
  69. define_native_property("name", name_getter, nullptr, Attribute::Configurable);
  70. }
  71. ScriptFunction::~ScriptFunction()
  72. {
  73. }
  74. void ScriptFunction::visit_children(Visitor& visitor)
  75. {
  76. Function::visit_children(visitor);
  77. visitor.visit(m_parent_environment);
  78. }
  79. LexicalEnvironment* ScriptFunction::create_environment()
  80. {
  81. HashMap<FlyString, Variable> variables;
  82. for (auto& parameter : m_parameters) {
  83. variables.set(parameter.name, { js_undefined(), DeclarationKind::Var });
  84. }
  85. if (body().is_scope_node()) {
  86. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  87. for (auto& declarator : declaration.declarations()) {
  88. variables.set(declarator.id().string(), { js_undefined(), DeclarationKind::Var });
  89. }
  90. }
  91. }
  92. auto* environment = heap().allocate<LexicalEnvironment>(global_object(), move(variables), m_parent_environment, LexicalEnvironment::EnvironmentRecordType::Function);
  93. environment->set_home_object(home_object());
  94. environment->set_current_function(*this);
  95. return environment;
  96. }
  97. Value ScriptFunction::call(Interpreter& interpreter)
  98. {
  99. auto& argument_values = interpreter.call_frame().arguments;
  100. ArgumentVector arguments;
  101. for (size_t i = 0; i < m_parameters.size(); ++i) {
  102. auto parameter = parameters()[i];
  103. auto value = js_undefined();
  104. if (parameter.is_rest) {
  105. auto* array = Array::create(global_object());
  106. for (size_t rest_index = i; rest_index < argument_values.size(); ++rest_index)
  107. array->indexed_properties().append(argument_values[rest_index]);
  108. value = Value(array);
  109. } else {
  110. if (i < argument_values.size() && !argument_values[i].is_undefined()) {
  111. value = argument_values[i];
  112. } else if (parameter.default_value) {
  113. value = parameter.default_value->execute(interpreter, global_object());
  114. if (interpreter.exception())
  115. return {};
  116. }
  117. }
  118. arguments.append({ parameter.name, value });
  119. interpreter.current_environment()->set(parameter.name, { value, DeclarationKind::Var });
  120. }
  121. return interpreter.execute_statement(global_object(), m_body, arguments, ScopeType::Function);
  122. }
  123. Value ScriptFunction::construct(Interpreter& interpreter, Function&)
  124. {
  125. if (m_is_arrow_function) {
  126. interpreter.throw_exception<TypeError>(ErrorType::NotAConstructor, m_name.characters());
  127. return {};
  128. }
  129. return call(interpreter);
  130. }
  131. JS_DEFINE_NATIVE_GETTER(ScriptFunction::length_getter)
  132. {
  133. auto* function = typed_this(interpreter, 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(interpreter, global_object);
  141. if (!function)
  142. return {};
  143. return js_string(interpreter, function->name().is_null() ? "" : function->name());
  144. }
  145. }