ScriptFunction.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FlyString> parameters, LexicalEnvironment* parent_environment)
  35. {
  36. auto& interpreter = global_object.interpreter();
  37. return interpreter.heap().allocate<ScriptFunction>(name, body, move(parameters), parent_environment, *interpreter.function_prototype());
  38. }
  39. ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> parameters, LexicalEnvironment* parent_environment, Object& prototype)
  40. : Function(prototype)
  41. , m_name(name)
  42. , m_body(body)
  43. , m_parameters(move(parameters))
  44. , m_parent_environment(parent_environment)
  45. {
  46. put("prototype", Object::create_empty(interpreter(), interpreter().global_object()));
  47. put_native_property("length", length_getter, length_setter);
  48. }
  49. ScriptFunction::~ScriptFunction()
  50. {
  51. }
  52. void ScriptFunction::visit_children(Visitor& visitor)
  53. {
  54. Function::visit_children(visitor);
  55. visitor.visit(m_parent_environment);
  56. }
  57. LexicalEnvironment* ScriptFunction::create_environment()
  58. {
  59. HashMap<FlyString, Variable> variables;
  60. for (auto& parameter : m_parameters) {
  61. variables.set(parameter, { js_undefined(), DeclarationKind::Var });
  62. }
  63. if (body().is_scope_node()) {
  64. for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
  65. for (auto& declarator : declaration.declarations()) {
  66. variables.set(declarator.id().string(), { js_undefined(), DeclarationKind::Var });
  67. }
  68. }
  69. }
  70. if (variables.is_empty())
  71. return m_parent_environment;
  72. return heap().allocate<LexicalEnvironment>(move(variables), m_parent_environment);
  73. }
  74. Value ScriptFunction::call(Interpreter& interpreter)
  75. {
  76. auto& argument_values = interpreter.call_frame().arguments;
  77. ArgumentVector arguments;
  78. for (size_t i = 0; i < m_parameters.size(); ++i) {
  79. auto name = parameters()[i];
  80. auto value = js_undefined();
  81. if (i < argument_values.size())
  82. value = argument_values[i];
  83. arguments.append({ name, value });
  84. interpreter.current_environment()->set(name, { value, DeclarationKind::Var });
  85. }
  86. return interpreter.run(m_body, arguments, ScopeType::Function);
  87. }
  88. Value ScriptFunction::construct(Interpreter& interpreter)
  89. {
  90. return call(interpreter);
  91. }
  92. Value ScriptFunction::length_getter(Interpreter& interpreter)
  93. {
  94. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  95. if (!this_object)
  96. return {};
  97. if (!this_object->is_function())
  98. return interpreter.throw_exception<TypeError>("Not a function");
  99. return Value(static_cast<i32>(static_cast<const ScriptFunction*>(this_object)->parameters().size()));
  100. }
  101. void ScriptFunction::length_setter(Interpreter&, Value)
  102. {
  103. }
  104. }