1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Debug.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/JobCallback.h>
#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/PromiseJobs.h>
#include <LibJS/Runtime/PromiseReaction.h>
namespace JS {
PromiseReactionJob* PromiseReactionJob::create(GlobalObject& global_object, PromiseReaction& reaction, Value argument)
{
return global_object.heap().allocate<PromiseReactionJob>(global_object, reaction, argument, *global_object.function_prototype());
}
PromiseReactionJob::PromiseReactionJob(PromiseReaction& reaction, Value argument, Object& prototype)
: NativeFunction(prototype)
, m_reaction(reaction)
, m_argument(argument)
{
}
// 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
Value PromiseReactionJob::call()
{
auto& vm = this->vm();
auto& promise_capability = m_reaction.capability();
auto type = m_reaction.type();
auto handler = m_reaction.handler();
Value handler_result;
if (!handler.has_value()) {
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Handler is empty", this);
switch (type) {
case PromiseReaction::Type::Fulfill:
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument);
handler_result = m_argument;
break;
case PromiseReaction::Type::Reject:
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Reject, throwing exception with argument {}", this, m_argument);
vm.throw_exception(global_object(), m_argument);
// handler_result is set to exception value further below
break;
}
} else {
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling handler callback {} @ {} with argument {}", this, handler.value().callback->class_name(), handler.value().callback, m_argument);
handler_result = call_job_callback(vm, handler.value(), js_undefined(), m_argument);
}
if (!promise_capability.has_value()) {
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction has no PromiseCapability, returning empty value", this);
return {};
}
if (vm.exception()) {
handler_result = vm.exception()->value();
vm.clear_exception();
vm.stop_unwind();
auto* reject_function = promise_capability.value().reject;
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function);
return vm.call(*reject_function, js_undefined(), handler_result);
} else {
auto* resolve_function = promise_capability.value().resolve;
dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function);
return vm.call(*resolve_function, js_undefined(), handler_result);
}
}
void PromiseReactionJob::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(&m_reaction);
visitor.visit(m_argument);
}
PromiseResolveThenableJob* PromiseResolveThenableJob::create(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
{
// FIXME: A bunch of stuff regarding realms, see step 2-5 in the spec linked below
return global_object.heap().allocate<PromiseResolveThenableJob>(global_object, promise_to_resolve, thenable, then, *global_object.function_prototype());
}
PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve, Value thenable, JobCallback then, Object& prototype)
: NativeFunction(prototype)
, m_promise_to_resolve(promise_to_resolve)
, m_thenable(thenable)
, m_then(then)
{
}
// 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
Value PromiseResolveThenableJob::call()
{
auto& vm = this->vm();
auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions();
dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Calling then job callback for thenable {}", this, &m_thenable);
auto then_call_result = call_job_callback(vm, m_then, m_thenable, &resolve_function, &reject_function);
if (vm.exception()) {
auto error = vm.exception()->value();
vm.clear_exception();
vm.stop_unwind();
dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: An exception was thrown, returning error {}", this, error);
return error;
}
dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result);
return then_call_result;
}
void PromiseResolveThenableJob::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(&m_promise_to_resolve);
visitor.visit(m_thenable);
visitor.visit(m_then.callback);
}
}
|