ScriptFunction.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/Error.h>
  30. #include <LibJS/Runtime/GlobalObject.h>
  31. #include <LibJS/Runtime/ScriptFunction.h>
  32. #include <LibJS/Runtime/Value.h>
  33. namespace JS {
  34. static ScriptFunction* script_function_from(Interpreter& interpreter)
  35. {
  36. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  37. if (!this_object)
  38. return nullptr;
  39. if (!this_object->is_function()) {
  40. interpreter.throw_exception<TypeError>("Not a function");
  41. return nullptr;
  42. }
  43. return static_cast<ScriptFunction*>(this_object);
  44. }
  45. ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, LexicalEnvironment* parent_environment)
  46. {
  47. return global_object.heap().allocate<ScriptFunction>(name, body, move(parameters), parent_environment, *global_object.function_prototype());
  48. }
  49. ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, LexicalEnvironment* parent_environment, Object& prototype)
  50. : Function(prototype)
  51. , m_name(name)
  52. , m_body(body)
  53. , m_parameters(move(parameters))
  54. , m_parent_environment(parent_environment)
  55. {
  56. put("prototype", Object::create_empty(interpreter(), interpreter().global_object()), 0);
  57. put_native_property("length", length_getter, nullptr, Attribute::Configurable);
  58. put_native_property("name", name_getter, nullptr, Attribute::Configurable);
  59. }
  60. ScriptFunction::~ScriptFunction()
  61. {
  62. }
  63. void ScriptFunction::visit_children(Visitor& visitor)
  64. {
  65. Function::visit_children(visitor);
  66. visitor.visit(m_parent_environment);
  67. }
  68. LexicalEnvironment* ScriptFunction::create_environment()
  69. {
  70. HashMap<FlyString, Variable> variables;
  71. for (auto& parameter : m_parameters) {
  72. variables.set(parameter.name, { js_undefined(), DeclarationKind::Var });
  73. }
  74. if (body().is_scope_node()) {
  75. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  76. for (auto& declarator : declaration.declarations()) {
  77. variables.set(declarator.id().string(), { js_undefined(), DeclarationKind::Var });
  78. }
  79. }
  80. }
  81. if (variables.is_empty())
  82. return m_parent_environment;
  83. return heap().allocate<LexicalEnvironment>(move(variables), m_parent_environment);
  84. }
  85. Value ScriptFunction::call(Interpreter& interpreter)
  86. {
  87. auto& argument_values = interpreter.call_frame().arguments;
  88. ArgumentVector arguments;
  89. for (size_t i = 0; i < m_parameters.size(); ++i) {
  90. auto parameter = parameters()[i];
  91. auto value = js_undefined();
  92. if (i < argument_values.size() && !argument_values[i].is_undefined()) {
  93. value = argument_values[i];
  94. } else if (parameter.default_value) {
  95. value = parameter.default_value->execute(interpreter);
  96. }
  97. arguments.append({ parameter.name, value });
  98. interpreter.current_environment()->set(parameter.name, { value, DeclarationKind::Var });
  99. }
  100. return interpreter.run(m_body, arguments, ScopeType::Function);
  101. }
  102. Value ScriptFunction::construct(Interpreter& interpreter)
  103. {
  104. return call(interpreter);
  105. }
  106. Value ScriptFunction::length_getter(Interpreter& interpreter)
  107. {
  108. auto* function = script_function_from(interpreter);
  109. if (!function)
  110. return {};
  111. return Value(static_cast<i32>(function->parameters().size()));
  112. }
  113. Value ScriptFunction::name_getter(Interpreter& interpreter)
  114. {
  115. auto* function = script_function_from(interpreter);
  116. if (!function)
  117. return {};
  118. return js_string(interpreter, function->name().is_null() ? "" : function->name());
  119. }
  120. }