ScriptFunction.cpp 6.0 KB

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