BoundFunction.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020, Jack Karamanian <karamanian.jack@gmail.com>
  3. * Copyright (c) 2021, 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<BoundFunction*> BoundFunction::create(GlobalObject& global_object, 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 33.
  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 = global_object.heap().allocate<BoundFunction>(global_object, global_object, target_function, bound_this, move(bound_arguments), prototype);
  26. // 10. Return obj.
  27. return object;
  28. }
  29. BoundFunction::BoundFunction(GlobalObject& global_object, FunctionObject& bound_target_function, Value bound_this, Vector<Value> bound_arguments, Object* prototype)
  30. : FunctionObject(global_object, 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(String::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. // 1. Let target be F.[[BoundTargetFunction]].
  42. auto& target = *m_bound_target_function;
  43. // 2. Let boundThis be F.[[BoundThis]].
  44. auto bound_this = m_bound_this;
  45. // 3. Let boundArgs be F.[[BoundArguments]].
  46. auto& bound_args = m_bound_arguments;
  47. // 4. Let args be the list-concatenation of boundArgs and argumentsList.
  48. auto args = MarkedVector<Value> { heap() };
  49. args.extend(bound_args);
  50. args.extend(move(arguments_list));
  51. // 5. Return ? Call(target, boundThis, args).
  52. return call(global_object(), &target, bound_this, move(args));
  53. }
  54. // 10.4.1.2 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-bound-function-exotic-objects-construct-argumentslist-newtarget
  55. ThrowCompletionOr<Object*> BoundFunction::internal_construct(MarkedVector<Value> arguments_list, FunctionObject& new_target)
  56. {
  57. // 1. Let target be F.[[BoundTargetFunction]].
  58. auto& target = *m_bound_target_function;
  59. // 2. Assert: IsConstructor(target) is true.
  60. VERIFY(Value(&target).is_constructor());
  61. // 3. Let boundArgs be F.[[BoundArguments]].
  62. auto& bound_args = m_bound_arguments;
  63. // 4. Let args be the list-concatenation of boundArgs and argumentsList.
  64. auto args = MarkedVector<Value> { heap() };
  65. args.extend(bound_args);
  66. args.extend(move(arguments_list));
  67. // 5. If SameValue(F, newTarget) is true, set newTarget to target.
  68. auto* final_new_target = &new_target;
  69. if (this == &new_target)
  70. final_new_target = &target;
  71. // 6. Return ? Construct(target, args, newTarget).
  72. return construct(global_object(), target, move(args), final_new_target);
  73. }
  74. void BoundFunction::visit_edges(Visitor& visitor)
  75. {
  76. Base::visit_edges(visitor);
  77. visitor.visit(m_bound_target_function);
  78. visitor.visit(m_bound_this);
  79. for (auto argument : m_bound_arguments)
  80. visitor.visit(argument);
  81. }
  82. }