You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current API revolves around Value value = context.exec(Source), where a context is entered, run and a value returned.
There doesn't seem to be any way for the exec stage to be paused and resumed. The specific case I have in mind is interacting with an object from the host environment which needs to retrieve a resource, and that could take some time. It would be great if that object could return a Future to the context, the context.exec method would complete and could be resumed when the future completes.
I find it easier to explain with code
Contextcontext = Context.create("js");
RuntimeContextrctx = context.createRuntime(source); // sets up the context but doesn't start itcontext.getBindings("js").putMember("foo", newMyObject(rctx));
while (!r.isComplete()) {
r.exec(); // Run the context until it completes or is paused
}
Valuevalue = r.getValue().get(); // Retrieve the value that would have been returned from context.exec(source);classMyObject {
MyObject(RuntimeContextrctx) {
this.rctx = rctx;
}
ObjectgetValue() {
Future<?> future = rctx.suspend();
evaluateFutureInAnotherThread(future); // Begin background task, will eventually complete the futurereturnfuture; // When Graal sees this, RuntimeContext is suspended.
}
}
interfaceRuntimeContext {
booleanisCompleted(); // True if the context is complete and getValue() won't blockvoidexec(); // Run the context until it completes or is suspended;Future<?> suspend(); // When the HostObject sees this value returned from a method it suspends the contextFuture<Value> getValue(); // Return a future to get the value.
}
This opens a lot of very interesting possibilities.
Interleaving evaluation of multiple Contexts, all waiting for resources, on a single thread. Like libc select(2) for contexts.
Load management - a suspended context can be delayed until the resources are necessary to restart it
If you could serialize Context as well as RuntimeContext, this would let you Serialization/Migration of contexts mid-execution.
The idea of a RuntimeContext can be extended to do a lot of the things you're doing now in ContextBuilder. For example, rather than declaring a filesystem-access policy up-front, RuntimeContext could have a boolean isFileAccessAllowed(Path path, int mode) so decisions can be made dynamically
There are obvious wrinkles, the main one being if the context has queued tasks for later execution eg with JS setTimeout() - delaying these until resumption will be reasonable in some circumstances, not others. TBD.
This seems fairly similar conceptually to Truffle safepoints and poll(), which I see are used internally - but there's no obvious way to interact with those from user-space, i.e. from the same thread that called Context.exec(). So I'm hoping something like this idea could be put alongside Context.exec().
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
The current API revolves around
Value value = context.exec(Source), where a context is entered, run and a value returned.There doesn't seem to be any way for the
execstage to be paused and resumed. The specific case I have in mind is interacting with an object from the host environment which needs to retrieve a resource, and that could take some time. It would be great if that object could return a Future to the context, the context.exec method would complete and could be resumed when the future completes.I find it easier to explain with code
This opens a lot of very interesting possibilities.
select(2)for contexts.RuntimeContextcan be extended to do a lot of the things you're doing now in ContextBuilder. For example, rather than declaring a filesystem-access policy up-front, RuntimeContext could have aboolean isFileAccessAllowed(Path path, int mode)so decisions can be made dynamicallyThere are obvious wrinkles, the main one being if the context has queued tasks for later execution eg with JS
setTimeout()- delaying these until resumption will be reasonable in some circumstances, not others. TBD.This seems fairly similar conceptually to Truffle safepoints and poll(), which I see are used internally - but there's no obvious way to interact with those from user-space, i.e. from the same thread that called
Context.exec(). So I'm hoping something like this idea could be put alongsideContext.exec().Related: #9317
Beta Was this translation helpful? Give feedback.
All reactions