Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Source/JavaScriptCore/runtime/JSMicrotask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,22 @@ static void promiseFinallyReactionJob(JSGlobalObject* globalObject, VM& vm, JSPr
context->setHandlerOrContext(vm, valueOrReason);
context->setPerCellBit(status == JSPromise::Status::Fulfilled);

#if USE(BUN_JSC_ADDITIONS)
// PromiseFinallyAwaitJob may run as a later microtask (scheduled below via
// performPromiseThenWithInternalMicrotask or
// createResolvingFunctionsWithInternalMicrotask), after this call's async
// context has been unwound. Capture it with the reaction so that phase 2
// can restore it, like PromiseFinallyReactionJob does for phase 1.
JSValue scheduledContext = AsyncContextSwapScope::wrapWithCurrent(vm, globalObject, context);
#else
JSValue scheduledContext = context;
#endif

if (result.inherits<JSPromise>()) {
auto* promise = uncheckedDowncast<JSPromise>(result);
if (promise->realm() == globalObject && promise->isThenFastAndNonObservable()) {
scope.release();
promise->performPromiseThenWithInternalMicrotask(vm, InternalMicrotask::PromiseFinallyAwaitJob, resultPromise, context);
promise->performPromiseThenWithInternalMicrotask(vm, InternalMicrotask::PromiseFinallyAwaitJob, resultPromise, scheduledContext);
return;
}
}
Expand Down Expand Up @@ -846,7 +857,7 @@ static void promiseFinallyReactionJob(JSGlobalObject* globalObject, VM& vm, JSPr
return;
}

auto [resolve, reject] = JSPromise::createResolvingFunctionsWithInternalMicrotask(vm, globalObject, InternalMicrotask::PromiseFinallyAwaitJob, context);
auto [resolve, reject] = JSPromise::createResolvingFunctionsWithInternalMicrotask(vm, globalObject, InternalMicrotask::PromiseFinallyAwaitJob, scheduledContext);
scope.release();
promiseResolveThenableJob(globalObject, resolutionObject, then, resolve, reject);
}
Expand Down Expand Up @@ -1967,8 +1978,13 @@ void runInternalMicrotask(JSGlobalObject* globalObject, VM& vm, InternalMicrotas
// arguments[0] = unused (we get resultPromise from context)
// arguments[1] = settled value from onFinally's result
// arguments[2] = context (JSSlimPromiseReaction: promise=resultPromise, handlerOrContext=originalValue, perCellBit=wasFulfilled)
// OR InternalFieldTuple: [context, asyncContext] when Bun async context is present
// payload = status of onFinally's result
auto* context = uncheckedDowncast<JSSlimPromiseReaction>(arguments[2]);
JSValue contextArg = arguments[2];
#if USE(BUN_JSC_ADDITIONS)
AsyncContextSwapScope asyncContextScope(vm, globalObject, AsyncContextSwapScope::unwrapContextTuple(contextArg));
#endif
auto* context = uncheckedDowncast<JSSlimPromiseReaction>(contextArg);
auto* resultPromise = uncheckedDowncast<JSPromise>(context->promise());
scope.release();
promiseFinallyAwaitJob(resultPromise->realm(), vm,
Expand Down
Loading