ScriptFunction.cpp 4.4 KB

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