Function.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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 <LibJS/Interpreter.h>
  27. #include <LibJS/Runtime/BoundFunction.h>
  28. #include <LibJS/Runtime/Function.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. namespace JS {
  31. Function::Function(Object& prototype)
  32. : Function(prototype, {}, {})
  33. {
  34. }
  35. Function::Function(Object& prototype, Optional<Value> bound_this, Vector<Value> bound_arguments)
  36. : Object(&prototype)
  37. , m_bound_this(bound_this)
  38. , m_bound_arguments(move(bound_arguments))
  39. {
  40. }
  41. BoundFunction* Function::bind(Value bound_this_value, Vector<Value> arguments)
  42. {
  43. Function& target_function = is_bound_function() ? static_cast<BoundFunction&>(*this).target_function() : *this;
  44. auto bound_this_object
  45. = [bound_this_value, this]() -> Value {
  46. if (bound_this().has_value()) {
  47. return bound_this().value();
  48. }
  49. switch (bound_this_value.type()) {
  50. case Value::Type::Undefined:
  51. case Value::Type::Null:
  52. // FIXME: Null or undefined should be passed through in strict mode.
  53. return &interpreter().global_object();
  54. default:
  55. return bound_this_value.to_object(interpreter().heap());
  56. }
  57. }();
  58. i32 computed_length = 0;
  59. auto length_property = get("length");
  60. if (interpreter().exception()) {
  61. return nullptr;
  62. }
  63. if (length_property.is_number()) {
  64. computed_length = max(0, length_property.to_i32() - static_cast<i32>(arguments.size()));
  65. }
  66. Object* constructor_prototype = nullptr;
  67. auto prototype_property = target_function.get("prototype");
  68. if (interpreter().exception()) {
  69. return nullptr;
  70. }
  71. if (prototype_property.is_object()) {
  72. constructor_prototype = &prototype_property.as_object();
  73. }
  74. auto all_bound_arguments = bound_arguments();
  75. all_bound_arguments.append(move(arguments));
  76. return interpreter().heap().allocate<BoundFunction>(target_function, bound_this_object, move(all_bound_arguments), computed_length, constructor_prototype);
  77. }
  78. void Function::visit_children(Visitor& visitor)
  79. {
  80. Object::visit_children(visitor);
  81. if (m_bound_this.has_value()) {
  82. visitor.visit(m_bound_this.value());
  83. }
  84. for (auto argument : m_bound_arguments) {
  85. visitor.visit(argument);
  86. }
  87. }
  88. Function::~Function()
  89. {
  90. }
  91. }