Repository navigation
Conversation
Let an embedder carry a value through the asynchronous flow of the code, like V8's ContinuationPreservedEmbedderData. A job runs with the value that was current when it was queued, and a promise reaction (then, await, async generators) runs with the value that was current when it was registered. The previous value is restored when the job ends. An embedder can build AsyncLocalStorage on it. Callbacks that come from the embedder (timers, I/O) are not tracked. FinalizationRegistry callbacks run without a value.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
JS_GetContinuationDataandJS_SetContinuationData, which let an embedder attach a value to the running code and have it follow the code through jobs, promise reactions andawait, like V8'sContinuationPreservedEmbedderData. The value is only reachable from C, so an embedder that wantsAsyncLocalStorageexposes a getter and a setter to JS and keeps a frame object in the value (the tests do this withgetCDandsetCD). The motivation is in #1919.Behaviour
The engine keeps one value per runtime and carries it from where work is queued to where it runs.
JS_EnqueueJobqueued it.then,catch,finally,await, async generators) runs with the value that was current when it was registered, not when the promise settles.resolve.FinalizationRegistrycallbacks run withundefined.JS_LoadModuleevaluates) are not tracked.quickjs.hsays so.JS_GetContinuationDatareturns a new reference.JS_SetContinuationDatadoes not take ownership ofval. The slot is per runtime and has the same threading rules as the rest of the runtime API. No public struct changes.Implementation
enqueue_jobandperform_promise_thencopy it in).JS_ExecutePendingJobsets the value of the job while it runs. It restores the previous value before it releases the job's value, because releasing it can run a finalizer that queues a job.js_promise_markmarks the value of a reaction, so a cycle through a promise is collected.JS_FreeRuntimereleases the slot.Tests
api-test.chas five new tests:continuation_datathen,await,queueMicrotask, a reaction registered before the promise settles from another value, an awaited thenable, and the host-side APIcontinuation_data_registryFinalizationRegistrycallback runs without a value, also when the object dies while another value is currentcontinuation_data_cyclecontinuation_data_finalizercontinuation_data_free_runtimeJS_FreeRuntimereleases it seesundefinedtests.confgives 0/123 errors,test262-checkgives the known 64, andapi-testandtests.confare clean under ASan/LSan. Each of these changes makes a test fail, checked under ASan: the job ignoring its value, the reaction job taking the value of the code that settles the promise, the mark removed, the order inJS_ExecutePendingJobreversed, and the registry job queued with the current value.Cost
One
JSValuemore per job entry and per reaction, with a fewjs_dupandJS_FreeValuecalls and stores. On 1Mawaitplus 1Mthen(), pinned to one CPU, over 40 alternating pairs, the median was 1001 ms for stock and 993 ms for patched. The difference is within the noise. A run with a value set was not measured.This change adds no
AsyncContextand no JavaScript-visible API.Fixes: #1919