BoundFunction.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/BoundFunction.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS {
  11. // 10.4.1.3 BoundFunctionCreate ( targetFunction, boundThis, boundArgs ), https://tc39.es/ecma262/#sec-boundfunctioncreate
  12. ThrowCompletionOr<NonnullGCPtr<BoundFunction>> BoundFunction::create(Realm& realm, FunctionObject& target_function, Value bound_this, Vector<Value> bound_arguments)
  13. {
  14. // 1. Let proto be ? targetFunction.[[GetPrototypeOf]]().
  15. auto* prototype = TRY(target_function.internal_get_prototype_of());
  16. // 2. Let internalSlotsList be the list-concatenation of « [[Prototype]], [[Extensible]] » and the internal slots listed in Table 34.
  17. // 3. Let obj be MakeBasicObject(internalSlotsList).
  18. // 4. Set obj.[[Prototype]] to proto.
  19. // 5. Set obj.[[Call]] as described in 10.4.1.1.
  20. // 6. If IsConstructor(targetFunction) is true, then
  21. // a. Set obj.[[Construct]] as described in 10.4.1.2.
  22. // 7. Set obj.[[BoundTargetFunction]] to targetFunction.
  23. // 8. Set obj.[[BoundThis]] to boundThis.
  24. // 9. Set obj.[[BoundArguments]] to boundArgs.
  25. auto object = realm.heap().allocate<BoundFunction>(realm, realm, target_function, bound_this, move(bound_arguments), prototype);
  26. // 10. Return obj.
  27. return object;
  28. }
  29. BoundFunction::BoundFunction(Realm& realm, FunctionObject& bound_target_function, Value bound_this, Vector<Value> bound_arguments, Object* prototype)
  30. : FunctionObject(realm, prototype)
  31. , m_bound_target_function(&bound_target_function)
  32. , m_bound_this(bound_this)
  33. , m_bound_arguments(move(bound_arguments))
  34. // FIXME: Non-standard and redundant, remove.
  35. , m_name(DeprecatedString::formatted("bound {}", bound_target_function.name()))
  36. {
  37. }
  38. // 10.4.1.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-bound-function-exotic-objects-call-thisargument-argumentslist
  39. ThrowCompletionOr<Value> BoundFunction::internal_call([[maybe_unused]] Value this_argument, MarkedVector<Value> arguments_list)
  40. {
  41. auto& vm = this->vm();
  42. // 1. Let target be F.[[BoundTargetFunction]].
  43. auto& target = *m_bound_target_function;
  44. // 2. Let boundThis be F.[[BoundThis]].
  45. auto bound_this = m_bound_this;
  46. // 3. Let boundArgs be F.[[BoundArguments]].
  47. auto& bound_args = m_bound_arguments;
  48. // 4. Let args be the list-concatenation of boundArgs and argumentsList.
  49. auto args = MarkedVector<Value> { heap() };
  50. args.extend(bound_args);
  51. args.extend(move(arguments_list));
  52. // 5. Return ? Call(target, boundThis, args).
  53. return call(vm, &target, bound_this, move(args));
  54. }
  55. // 10.4.1.2 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-bound-function-exotic-objects-construct-argumentslist-newtarget
  56. ThrowCompletionOr<NonnullGCPtr<Object>> BoundFunction::internal_construct(MarkedVector<Value> arguments_list, FunctionObject& new_target)
  57. {
  58. auto& vm = this->vm();
  59. // 1. Let target be F.[[BoundTargetFunction]].
  60. auto& target = *m_bound_target_function;
  61. // 2. Assert: IsConstructor(target) is true.
  62. VERIFY(Value(&target).is_constructor());
  63. // 3. Let boundArgs be F.[[BoundArguments]].
  64. auto& bound_args = m_bound_arguments;
  65. // 4. Let args be the list-concatenation of boundArgs and argumentsList.
  66. auto args = MarkedVector<Value> { heap() };
  67. args.extend(bound_args);
  68. args.extend(move(arguments_list));
  69. // 5. If SameValue(F, newTarget) is true, set newTarget to target.
  70. auto* final_new_target = &new_target;
  71. if (this == &new_target)
  72. final_new_target = &target;
  73. // 6. Return ? Construct(target, args, newTarget).
  74. return construct(vm, target, move(args), final_new_target);
  75. }
  76. void BoundFunction::visit_edges(Visitor& visitor)
  77. {
  78. Base::visit_edges(visitor);
  79. visitor.visit(m_bound_target_function);
  80. visitor.visit(m_bound_this);
  81. for (auto argument : m_bound_arguments)
  82. visitor.visit(argument);
  83. }
  84. }