BoundFunction.cpp 4.2 KB

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