BoundFunction.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. BoundFunction::BoundFunction(GlobalObject& global_object, FunctionObject& bound_target_function, Value bound_this, Vector<Value> bound_arguments, i32 length, Object* constructor_prototype)
  12. : FunctionObject(*global_object.function_prototype())
  13. , m_bound_target_function(&bound_target_function)
  14. , m_bound_this(bound_this)
  15. , m_bound_arguments(move(bound_arguments))
  16. , m_constructor_prototype(constructor_prototype)
  17. , m_name(String::formatted("bound {}", bound_target_function.name()))
  18. , m_length(length)
  19. {
  20. }
  21. void BoundFunction::initialize(GlobalObject& global_object)
  22. {
  23. auto& vm = this->vm();
  24. Base::initialize(global_object);
  25. define_direct_property(vm.names.length, Value(m_length), Attribute::Configurable);
  26. }
  27. BoundFunction::~BoundFunction()
  28. {
  29. }
  30. // 10.4.1.1 [[Call]] ( thisArgument, argumentsList ), https://tc39.es/ecma262/#sec-bound-function-exotic-objects-call-thisargument-argumentslist
  31. ThrowCompletionOr<Value> BoundFunction::internal_call([[maybe_unused]] Value this_argument, MarkedValueList arguments_list)
  32. {
  33. // 1. Let target be F.[[BoundTargetFunction]].
  34. auto& target = *m_bound_target_function;
  35. // 2. Let boundThis be F.[[BoundThis]].
  36. auto bound_this = m_bound_this;
  37. // 3. Let boundArgs be F.[[BoundArguments]].
  38. auto& bound_args = m_bound_arguments;
  39. // 4. Let args be the list-concatenation of boundArgs and argumentsList.
  40. auto args = MarkedValueList { heap() };
  41. args.extend(bound_args);
  42. args.extend(move(arguments_list));
  43. // 5. Return ? Call(target, boundThis, args).
  44. return call(global_object(), &target, bound_this, move(args));
  45. }
  46. // 10.4.1.2 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-bound-function-exotic-objects-construct-argumentslist-newtarget
  47. ThrowCompletionOr<Object*> BoundFunction::internal_construct(MarkedValueList arguments_list, FunctionObject& new_target)
  48. {
  49. // 1. Let target be F.[[BoundTargetFunction]].
  50. auto& target = *m_bound_target_function;
  51. // 2. Assert: IsConstructor(target) is true.
  52. VERIFY(Value(&target).is_constructor());
  53. // 3. Let boundArgs be F.[[BoundArguments]].
  54. auto& bound_args = m_bound_arguments;
  55. // 4. Let args be the list-concatenation of boundArgs and argumentsList.
  56. auto args = MarkedValueList { heap() };
  57. args.extend(bound_args);
  58. args.extend(move(arguments_list));
  59. // 5. If SameValue(F, newTarget) is true, set newTarget to target.
  60. auto* final_new_target = &new_target;
  61. if (this == &new_target)
  62. final_new_target = &target;
  63. // 6. Return ? Construct(target, args, newTarget).
  64. return construct(global_object(), target, move(args), final_new_target);
  65. }
  66. void BoundFunction::visit_edges(Visitor& visitor)
  67. {
  68. Base::visit_edges(visitor);
  69. visitor.visit(m_bound_target_function);
  70. visitor.visit(m_bound_this);
  71. for (auto argument : m_bound_arguments)
  72. visitor.visit(argument);
  73. visitor.visit(m_constructor_prototype);
  74. }
  75. }