From cd391aa8ef88065d50b51ba384cd5f89968175f0 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 14 Nov 2021 00:49:01 +0000 Subject: [PATCH] LibJS: Convert call_job_callback() to ThrowCompletionOr --- .../Libraries/LibJS/Runtime/JobCallback.h | 12 ++++++--- .../Libraries/LibJS/Runtime/PromiseJobs.cpp | 26 +++++++++---------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/JobCallback.h b/Userland/Libraries/LibJS/Runtime/JobCallback.h index a9d95607b3c..1b596455bd3 100644 --- a/Userland/Libraries/LibJS/Runtime/JobCallback.h +++ b/Userland/Libraries/LibJS/Runtime/JobCallback.h @@ -6,8 +6,9 @@ #pragma once +#include +#include #include -#include namespace JS { @@ -19,16 +20,19 @@ struct JobCallback { // 9.5.2 HostMakeJobCallback ( callback ), https://tc39.es/ecma262/#sec-hostmakejobcallback inline JobCallback make_job_callback(FunctionObject& callback) { + // 1. Return the JobCallback Record { [[Callback]]: callback, [[HostDefined]]: empty }. return { &callback }; } // 9.5.3 HostCallJobCallback ( jobCallback, V, argumentsList ), https://tc39.es/ecma262/#sec-hostcalljobcallback template -[[nodiscard]] inline Value call_job_callback(VM& vm, JobCallback& job_callback, Value this_value, Args... args) +inline ThrowCompletionOr call_job_callback(GlobalObject& global_object, JobCallback& job_callback, Value this_value, Args... args) { + // 1. Assert: IsCallable(jobCallback.[[Callback]]) is true. VERIFY(job_callback.callback); - auto& callback = *job_callback.callback; - return TRY_OR_DISCARD(vm.call(callback, this_value, args...)); + + // 2. Return ? Call(jobCallback.[[Callback]], V, argumentsList). + return call(global_object, job_callback.callback, this_value, args...); } } diff --git a/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp b/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp index cf8e7f23aeb..99d7eb13548 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp +++ b/Userland/Libraries/LibJS/Runtime/PromiseJobs.cpp @@ -29,6 +29,7 @@ PromiseReactionJob::PromiseReactionJob(PromiseReaction& reaction, Value argument ThrowCompletionOr PromiseReactionJob::call() { auto& vm = this->vm(); + auto& global_object = this->global_object(); // a. Let promiseCapability be reaction.[[Capability]]. auto& promise_capability = m_reaction.capability(); @@ -39,7 +40,7 @@ ThrowCompletionOr PromiseReactionJob::call() // c. Let handler be reaction.[[Handler]]. auto handler = m_reaction.handler(); - Value handler_result; + Completion handler_result; // d. If handler is empty, then if (!handler.has_value()) { @@ -48,7 +49,7 @@ ThrowCompletionOr PromiseReactionJob::call() // i. If type is Fulfill, let handlerResult be NormalCompletion(argument). if (type == PromiseReaction::Type::Fulfill) { dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument); - handler_result = m_argument; + handler_result = normal_completion(m_argument); } // ii. Else, else { @@ -56,15 +57,14 @@ ThrowCompletionOr PromiseReactionJob::call() VERIFY(type == PromiseReaction::Type::Reject); // 2. Let handlerResult be ThrowCompletion(argument). - // NOTE: handler_result is set to exception value further below 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 = throw_completion(m_argument); } } // e. Else, let handlerResult be HostCallJobCallback(handler, undefined, « argument »). 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); + handler_result = call_job_callback(global_object, handler.value(), js_undefined(), m_argument); } // f. If promiseCapability is undefined, then @@ -82,22 +82,21 @@ ThrowCompletionOr PromiseReactionJob::call() // g. Assert: promiseCapability is a PromiseCapability Record. // h. If handlerResult is an abrupt completion, then - if (vm.exception()) { - handler_result = vm.exception()->value(); + if (handler_result.is_abrupt()) { vm.clear_exception(); vm.stop_unwind(); // i. Let status be Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »). 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); + return vm.call(*reject_function, js_undefined(), handler_result.value()); } // i. Else, else { // i. Let status be Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »). 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); + return vm.call(*resolve_function, js_undefined(), handler_result.value()); } // j. Return Completion(status). @@ -128,17 +127,18 @@ PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve ThrowCompletionOr PromiseResolveThenableJob::call() { auto& vm = this->vm(); + auto& global_object = this->global_object(); // a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve). auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions(); // b. Let thenCallResult be HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »). 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); + auto then_call_result = call_job_callback(global_object, m_then, m_thenable, &resolve_function, &reject_function); // c. If thenCallResult is an abrupt completion, then - if (vm.exception()) { - auto error = vm.exception()->value(); + if (then_call_result.is_error()) { + auto error = then_call_result.throw_completion().value(); vm.clear_exception(); vm.stop_unwind(); @@ -151,7 +151,7 @@ ThrowCompletionOr PromiseResolveThenableJob::call() } // d. Return Completion(thenCallResult). - dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result); + dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result.value()); return then_call_result; }