Whats the best way to implement "pause" #938
Unanswered
asdfgasdfsafgsdfa
asked this question in
Q&A
Replies: 2 comments
-
You could try and implement something like JS's var engine = new Jint.Engine();
Func<Jint.Native.JsValue, Jint.Native.JsValue, Jint.Native.JsValue> setTimeout = (fn, delay) => {
var timer = new System.Timers.Timer(delay.AsNumber());
timer.AutoReset = false;
timer.Elapsed += (_, args) =>
fn.As<Jint.Native.Function.FunctionInstance>().Call(Jint.Native.JsValue.Undefined, new Jint.Native.JsValue[] {});
timer.Enabled = true;
return Jint.Native.JsValue.Undefined;
};
Func<Jint.Native.JsValue, Jint.Native.JsValue> log = logged => {
Console.WriteLine(logged);
return Jint.Native.JsValue.Undefined;
};
var envRecord = new Jint.Runtime.Environments.DeclarativeEnvironmentRecord(engine);
envRecord.CreateImmutableBinding(nameof(setTimeout));
envRecord.InitializeImmutableBinding(nameof(setTimeout), new Jint.Runtime.Interop.DelegateWrapper(engine, setTimeout));
envRecord.CreateImmutableBinding(nameof(log));
envRecord.InitializeImmutableBinding(nameof(log), new Jint.Runtime.Interop.DelegateWrapper(engine, log));
var env = new Jint.Runtime.Environments.LexicalEnvironment(record: envRecord, outer: engine.GlobalEnvironment);
engine.EnterExecutionContext(lexicalEnvironment: env, variableEnvironment: env, thisBinding: Jint.Native.JsValue.Null);
engine.Execute("setTimeout(function() { log('!'); }, 100);");
Console.ReadLine(); // Keep program from closing before the timeout is called Note that a real |
Beta Was this translation helpful? Give feedback.
0 replies
-
@asdfgasdfsafgsdfa it might not be the easiest way that you were asking for, however one way would be to design the JS code to be reentrant, which I think is what @koromorix is suggesting |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey,
I want to make a function available that is equivalent to this:
But without blocking the thread.
engine.Execute would have to be either a
Task
or anIEnumerable
.Assuming I'd need that feature now and I'm willing to put in effort to make it happen, what would you ( @sebastienros ) suggest I do?
Maybe add a new keyword for it instead of setting it as a function? (
wait 500
)I think at least that I could do on my own.
But making it continue... I think I would need to save the execution state and then return.
But that seems to be impossible because the "callstack" of the js vm is literally the real c# stack.
So I cannot just save the stack and return and later restore it.
Now one could convert all functions into
IEnumerable<int>
(where that would yield the milliseconds to wait, or yield return if the call is complete), but is that really the way to go?A third option would be to somehow use
Task
as return value and then continue that way... ?@sebastienros What would be the easiest way for me to implement that?
What would I have to look out for?
Beta Was this translation helpful? Give feedback.
All reactions