summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-08-21 16:09:38 +0100
committerLinus Groh <mail@linusgroh.de>2022-08-23 13:58:30 +0100
commitd74f8039eb26f7956e02b4ae091a24338d127f1f (patch)
tree820270f653721970b245bdca1693f1f3bc15f361 /Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp
parentccdfa2320cb93ed299d12107f1f991b7e358fb7f (diff)
downloadserenity-d74f8039eb26f7956e02b4ae091a24338d127f1f.zip
LibJS: Replace GlobalObject with VM in Promise AOs [Part 8/19]
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp36
1 files changed, 22 insertions, 14 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp b/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp
index 4d0eaa979a..6cb4bb20b3 100644
--- a/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp
+++ b/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp
@@ -16,9 +16,10 @@
namespace JS {
// 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
-static ThrowCompletionOr<Value> run_reaction_job(GlobalObject& global_object, PromiseReaction& reaction, Value argument)
+static ThrowCompletionOr<Value> run_reaction_job(VM& vm, PromiseReaction& reaction, Value argument)
{
- auto& vm = global_object.vm();
+ auto& realm = *vm.current_realm();
+ auto& global_object = realm.global_object();
// a. Let promiseCapability be reaction.[[Capability]].
auto& promise_capability = reaction.capability();
@@ -55,7 +56,7 @@ static ThrowCompletionOr<Value> run_reaction_job(GlobalObject& global_object, Pr
dbgln_if(PROMISE_DEBUG, "run_reaction_job: Calling handler callback {} @ {} with argument {}", handler.value().callback.cell()->class_name(), handler.value().callback.cell(), argument);
MarkedVector<Value> arguments(vm.heap());
arguments.append(argument);
- handler_result = vm.host_call_job_callback(global_object, handler.value(), js_undefined(), move(arguments));
+ handler_result = vm.host_call_job_callback(handler.value(), js_undefined(), move(arguments));
}
// f. If promiseCapability is undefined, then
@@ -89,12 +90,15 @@ static ThrowCompletionOr<Value> run_reaction_job(GlobalObject& global_object, Pr
}
// 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
-PromiseJob create_promise_reaction_job(GlobalObject& global_object, PromiseReaction& reaction, Value argument)
+PromiseJob create_promise_reaction_job(VM& vm, PromiseReaction& reaction, Value argument)
{
+ auto& realm = *vm.current_realm();
+ auto& global_object = realm.global_object();
+
// 1. Let job be a new Job Abstract Closure with no parameters that captures reaction and argument and performs the following steps when called:
// See run_reaction_job for "the following steps".
- auto job = [global_object = make_handle(&global_object), reaction = make_handle(&reaction), argument = make_handle(argument)]() mutable {
- return run_reaction_job(*global_object.cell(), *reaction.cell(), argument.value());
+ auto job = [&vm, reaction = make_handle(&reaction), argument = make_handle(argument)]() mutable {
+ return run_reaction_job(vm, *reaction.cell(), argument.value());
};
// 2. Let handlerRealm be null.
@@ -111,7 +115,7 @@ PromiseJob create_promise_reaction_job(GlobalObject& global_object, PromiseReact
handler_realm = get_handler_realm_result.release_value();
} else {
// c. Else, set handlerRealm to the current Realm Record.
- handler_realm = global_object.vm().current_realm();
+ handler_realm = vm.current_realm();
}
// d. NOTE: handlerRealm is never null unless the handler is undefined. When the handler is a revoked Proxy and no ECMAScript code runs, handlerRealm is used to create error objects.
@@ -122,9 +126,10 @@ PromiseJob create_promise_reaction_job(GlobalObject& global_object, PromiseReact
}
// 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
-static ThrowCompletionOr<Value> run_resolve_thenable_job(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback& then)
+static ThrowCompletionOr<Value> run_resolve_thenable_job(VM& vm, Promise& promise_to_resolve, Value thenable, JobCallback& then)
{
- auto& vm = global_object.vm();
+ auto& realm = *vm.current_realm();
+ auto& global_object = realm.global_object();
// a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).
auto [resolve_function, reject_function] = promise_to_resolve.create_resolving_functions();
@@ -134,7 +139,7 @@ static ThrowCompletionOr<Value> run_resolve_thenable_job(GlobalObject& global_ob
MarkedVector<Value> arguments(vm.heap());
arguments.append(Value(&resolve_function));
arguments.append(Value(&reject_function));
- auto then_call_result = vm.host_call_job_callback(global_object, then, thenable, move(arguments));
+ auto then_call_result = vm.host_call_job_callback(then, thenable, move(arguments));
// c. If thenCallResult is an abrupt completion, then
if (then_call_result.is_error()) {
@@ -149,8 +154,11 @@ static ThrowCompletionOr<Value> run_resolve_thenable_job(GlobalObject& global_ob
}
// 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
-PromiseJob create_promise_resolve_thenable_job(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
+PromiseJob create_promise_resolve_thenable_job(VM& vm, Promise& promise_to_resolve, Value thenable, JobCallback then)
{
+ auto& realm = *vm.current_realm();
+ auto& global_object = realm.global_object();
+
// 2. Let getThenRealmResult be Completion(GetFunctionRealm(then.[[Callback]])).
auto get_then_realm_result = get_function_realm(global_object, *then.callback.cell());
@@ -161,7 +169,7 @@ PromiseJob create_promise_resolve_thenable_job(GlobalObject& global_object, Prom
then_realm = get_then_realm_result.release_value();
} else {
// 4. Else, let thenRealm be the current Realm Record.
- then_realm = global_object.vm().current_realm();
+ then_realm = vm.current_realm();
}
// 5. NOTE: thenRealm is never null. When then.[[Callback]] is a revoked Proxy and no code runs, thenRealm is used to create error objects.
@@ -170,8 +178,8 @@ PromiseJob create_promise_resolve_thenable_job(GlobalObject& global_object, Prom
// 1. Let job be a new Job Abstract Closure with no parameters that captures promiseToResolve, thenable, and then and performs the following steps when called:
// See PromiseResolveThenableJob::call() for "the following steps".
// NOTE: This is done out of order, since `then` is moved into the lambda and `then` would be invalid if it was done at the start.
- auto job = [global_object = make_handle(&global_object), promise_to_resolve = make_handle(&promise_to_resolve), thenable = make_handle(thenable), then = move(then)]() mutable {
- return run_resolve_thenable_job(*global_object.cell(), *promise_to_resolve.cell(), thenable.value(), then);
+ auto job = [&vm, promise_to_resolve = make_handle(&promise_to_resolve), thenable = make_handle(thenable), then = move(then)]() mutable {
+ return run_resolve_thenable_job(vm, *promise_to_resolve.cell(), thenable.value(), then);
};
// 6. Return the Record { [[Job]]: job, [[Realm]]: thenRealm }.