ScriptFunction.cpp 5.6 KB

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