RustyV8: Example of using PromiseResolver / async?
Is there an example using PromiseResolver? Because it uses a HandleScope, I'm unsure about how to reference it at a later point when async work has been done.
Could you provide an example of implementing a async simple sleep function, ideally by pushing an async task to the main Tokio runtime?
Problem:
From JS:
await doRustAsync(5000);
Calling RS:
This Rust code obviously doesn't work, since:
1. the tokio runtime handle isn't available to the thread.
2. the promise/resolver uses scope, which is borrowed.9 Replies
That's gonna fairly complex. I'l try to give an example tomorrow
@bartlomieju I managed to get it all to work, but it was quite a fight ðŸ«
I did manage to implement bidirectional async without MaskFutureAsSend, so that's something 🥂 Can I get back to you if I face another blocking issue? I realize this is a Deno support forum and not really for RustyV8 🙂
I did manage to implement bidirectional async without MaskFutureAsSend, so that's something 🥂 Can I get back to you if I face another blocking issue? I realize this is a Deno support forum and not really for RustyV8 🙂
Sure. In general you probably want to use
deno_core
for easier async integration@bartlomieju
When I call a JS function that returns a promise, polling if the promise has finished is expensive.
So I implemented a callback on .then, but it needs both the promise and the oneshot sender, so I'm embedding it as data:
Unfortunately this either causes a
1) use-after-free, if I create a short-lived scope (callback outlives it)
2) memory leak
I tried to use CallbackScope but that isnt accepted as a scope arg.
What's the right approach?
Basically I want all of the above to get dropped only when the callback has finished.
The callback looks like this:
You'd need to create a
v8::Global
out of that promise first to store it in external
Other than that it should pretty much work correctly
I tried to use CallbackScope but that isnt accepted as a scope arg. What's the right approach?Where did you use CallbackScope?
@bartlomieju Oh, very nice, that makes sense. Thanks a lot!
I was thinking that the result of the js call would be saved to the scope, which would be released before the callback is executed, so that I would need to run it in a special callbackscope that is then destroyed with the callback, but apparently this works.
Nah, the
HandleScope
is getting dropped at the end of the lexical scope, so you can't really hold onto the values after that
You can always leak something, but as you saw that leads to a problems
If you need to use v8::Local<...>
later you always need to create a v8::Global<...>
Oh sorry, yes of course, the promise is the result 🙂
but the External and the Function is created on a scope that is destroyed
But you "assign it" to the promise and V8 figures out that they need to be kept alive since they are now associated with that promise