Skip to content

Commit

Permalink
Add APIs necessary to implement AsyncLocal and AsyncSnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed May 6, 2023
1 parent 7a0b136 commit 008c355
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Source/JavaScriptCore/builtins/BuiltinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ namespace JSC {
macro(hasOwn) \
macro(indexOf) \
macro(pop) \
macro(pushAsyncContextFrame) \
macro(popAsyncContextFrame) \
macro(getAsyncContextFrame) \


namespace Symbols {
Expand Down
26 changes: 24 additions & 2 deletions Source/JavaScriptCore/builtins/PromiseOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function pushNewPromiseReaction(thenable, existingReactions, promiseOrCapability
@promiseOrCapability: promiseOrCapability,
@onFulfilled: onFulfilled,
@onRejected: onRejected,
@context: context,
@context: @getAsyncContextFrame ? @getAsyncContextFrame(context) : context,
// This is 4x the number of out of line reactions (promise, fulfill callback, reject callback, context).
@outOfLineReactionCounts: 0,
};
Expand All @@ -46,7 +46,7 @@ function pushNewPromiseReaction(thenable, existingReactions, promiseOrCapability
@putByValDirect(existingReactions, outOfLineReactionCounts++, promiseOrCapability);
@putByValDirect(existingReactions, outOfLineReactionCounts++, onFulfilled);
@putByValDirect(existingReactions, outOfLineReactionCounts++, onRejected);
@putByValDirect(existingReactions, outOfLineReactionCounts++, context);
@putByValDirect(existingReactions, outOfLineReactionCounts++, @getAsyncContextFrame ? @getAsyncContextFrame(context) : context);
existingReactions.@outOfLineReactionCounts = outOfLineReactionCounts;
}
}
Expand Down Expand Up @@ -318,6 +318,10 @@ function createResolvingFunctions(promise)
function promiseReactionJobWithoutPromise(handler, argument, context)
{
"use strict";
var pushed;
if (@pushAsyncContextFrame) {
pushed = @pushAsyncContextFrame(context);
}

try {
if (context)
Expand All @@ -326,6 +330,10 @@ function promiseReactionJobWithoutPromise(handler, argument, context)
handler(argument);
} catch {
// This is user-uncatchable promise. We just ignore the error here.
} finally {
if (@popAsyncContextFrame) {
@popAsyncContextFrame(pushed);
}
}
}

Expand Down Expand Up @@ -457,6 +465,11 @@ function promiseReactionJob(promiseOrCapability, handler, argument, contextOrSta
return;
}

var pushed;
if (@pushAsyncContextFrame) {
pushed = @pushAsyncContextFrame(contextOrState);
}

// Case (1), or (2).
try {
var result = (contextOrState) ? handler(argument, contextOrState) : handler(argument);
Expand All @@ -466,14 +479,23 @@ function promiseReactionJob(promiseOrCapability, handler, argument, contextOrSta
return;
}
promiseOrCapability.@reject.@call(@undefined, error);
if (@popAsyncContextFrame) {
@popAsyncContextFrame(pushed);
}
return;
}

if (@isPromise(promiseOrCapability)) {
@resolvePromise(promiseOrCapability, result);
if (@popAsyncContextFrame) {
@popAsyncContextFrame(pushed);
}
return;
}
promiseOrCapability.@resolve.@call(@undefined, result);
if (@popAsyncContextFrame) {
@popAsyncContextFrame(pushed);
}
}

@linkTimeConstant
Expand Down
4 changes: 4 additions & 0 deletions Source/JavaScriptCore/runtime/JSGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,10 @@ capitalName ## Constructor* lowerName ## Constructor = featureFlag ? capitalName
putDirectWithoutTransition(vm, Identifier::fromString(vm, "__signpostStop"_s), JSFunction::create(vm, this, 1, "signpostStop"_s, signpostStop, ImplementationVisibility::Public), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
}

putDirectWithoutTransition(vm, vm.propertyNames->builtinNames().pushAsyncContextFramePrivateName(), jsUndefined(), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | 0);
putDirectWithoutTransition(vm, vm.propertyNames->builtinNames().popAsyncContextFramePrivateName(), jsUndefined(), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | 0);
putDirectWithoutTransition(vm, vm.propertyNames->builtinNames().getAsyncContextFramePrivateName(), jsUndefined(), PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | 0);

initStaticGlobals(vm);

if (UNLIKELY(Options::useDollarVM()))
Expand Down

0 comments on commit 008c355

Please sign in to comment.