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.
Motivation
Rquickjs support mapping Javascript promises to futures and vice versa via the
Promise
andPromised
types.This allows the rquickjs library to interface with async rust.
However the current implementation has a few problems:
First, any access to the Jjavascript runtime in rquickjs has to go through a lock.
When the
parallel
feature is enabled this lock is done with a mutex.Blocking locks are a big no-no in async rust.
Ideally rquickjs would support asynchronous aware mutexes for locking the runtime.
Second, futures in rquickjs currently require that the future is valid for
'static
.This leads to a bit of a problem when trying to use rquickjs functions as basically all functionality has the
'js
lifetime attached to it.This prevents functions like the one below from being implemented as
Ctx
is not valid for 'static' as it has the
'js` lifetime.This severely limits the actual usefulness of the using futures with rquickjs.
Ideally futures would only have to be valid for the
'js
lifetime.Implementation
This PR is an attempt to address these problems.
First it introduces
AsyncContext
a version of Context which uses asynchronous locking instead of the blocking lock.This version of context is used together with the
async_with
macro, which allows the with closure to return a future:Second it lifts the
'static
requirement for futures used inPromise
andPromised
.This allows futures to keep Javascript objects, as in the above example, allowing one to implement for example the
delay
function much easier than is currently possible (see #128).Currently futures are send to a separate task where the are polled in depended of whether the runtime is actually locked requiring the futures to be static.
This restriction is lifted by keeping any future spawned inside the
async_with
closure inside the runtime lock.