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