nuclearwwc
nuclearwwc2w ago

Embed Deno into Rust App: How to call other method with poll_event_loop keep going

Hi, all, I'm trying embed deno_core into Rust Gateway. like openresty(lua) in NGINX but for Deno(javascript) After reading the docs, I need continuously call https://docs.rs/deno_core/latest/deno_core/struct.JsRuntime.html#method.run_event_loop to make deno event loop running. Sometime, after gateway received request headers, we need to call method on_recv_request_header in Deno script, but due to the call to run_event_loop, we already await blocked in there, I can't get JsRuntime back, So I need find a way to stop run_event_loop temporarily, get JsRuntime back , then call run_event_loop again Possible solution: It there have a method so I can get JsRuntime handle(like https://docs.rs/tokio/latest/tokio/runtime/struct.Runtime.html#method.handle), move the run_event_loop to another async task, then call other method in current task?
JsRuntime in deno_core - Rust
A single execution context of JavaScript. Corresponds roughly to the “Web Worker” concept in the DOM.
10 Replies
Filou
Filou2w ago
Deno
Roll your own JavaScript runtime | Deno
A walk-through of creating a CLI that executes JavaScript files.
boopus_the_snootus
https://github.com/rscarson/rustyscript This might be helpful, it's exactly kind of use case I wrote it for. And if not my documentation and code should give you some ideas
nuclearwwc
nuclearwwcOP6d ago
I know this, but I don't know how to handle the situation where I call async js function from rust, waiting the promise result in rust, at the same time, the eventloop should continue to run since there have many async task in JavaScript side. the post just call run_eventloop to end, i can't get js runtime to do other operation once call run_eventloop.await Hi, thank you. but the rustyscript also run js to end, and then the control come back to rust. I want to call async js callback when http request coming in rust side. everytime http.request coming from rust side, I need to call async js callback, wait the result, also poll event loop because settimeout need to trigger in js side. many requests, many calls to js, many futures to resolve, plus eventloop
boopus_the_snootus
You should be able to do all that with the built-in stuff rustyscript includes Actually I think one of the examples I have is almost verbatim that If you use my promise type I think it gets you 90% of the way there if I'm understanding you correctly
nuclearwwc
nuclearwwcOP6d ago
does there have code example?
boopus_the_snootus
There does! https://rscarson.github.io/rustyscript-book/ I will find you the specific sections tomorrow because it is late and my brain is mashed potatoes. But it's in the rusty script book, the async section is what you're after
nuclearwwc
nuclearwwcOP6d ago
Let's say we have a HTTP/2 connection which spawn many requests at the same time.
let js_runtime = create_js_runtime();
// Question 2: We need poll event loop constantly to trigger events in js
// but we also need mutable borrow js_runtime in following codes.
// How we can call other method(for example call_async_js_runtime do other ops) and call
// jsruntime.run_event_loop().await to make forward as the same time?
while let Some(request_stream) = h2_conn.accept().await {
// Question 2: method on js_runtime requires &mut self, but we only have one js_runtime
// How I can use it across different task? Rc<RefCell<JsRuntime>>?
tokio::task::spawn_local(async move {
// Compilation error! call_async_js_callback requires &mut T
// If we move js_runtime into first iteration, ownership is taken by loop
let promises = js_runtime.call_async_js_callback(request_stream);
let res = promises.await.unwrap();
})
}
let js_runtime = create_js_runtime();
// Question 2: We need poll event loop constantly to trigger events in js
// but we also need mutable borrow js_runtime in following codes.
// How we can call other method(for example call_async_js_runtime do other ops) and call
// jsruntime.run_event_loop().await to make forward as the same time?
while let Some(request_stream) = h2_conn.accept().await {
// Question 2: method on js_runtime requires &mut self, but we only have one js_runtime
// How I can use it across different task? Rc<RefCell<JsRuntime>>?
tokio::task::spawn_local(async move {
// Compilation error! call_async_js_callback requires &mut T
// If we move js_runtime into first iteration, ownership is taken by loop
let promises = js_runtime.call_async_js_callback(request_stream);
let res = promises.await.unwrap();
})
}
nuclearwwc
nuclearwwcOP6d ago
@boopus_the_snootus Immediate is what I wanted(get promise but resolve later by move to another tokio task), https://rscarson.github.io/rustyscript-book/advanced/asynchronous_javascript.html#_immediate But the Examples shows that async block future still need mutably borrow js runtime, future cannot spread to another tokio::task::spawn_local which requires 'static, restrict the usage in single thread concurrent environment. Also, I don't want block_on current thread until future resolved.
GitHub
rustyscript/examples/async_javascript.rs at master · rscarson/rust...
Effortless JS integration for rust . Contribute to rscarson/rustyscript development by creating an account on GitHub.
nuclearwwc
nuclearwwcOP6d ago
not quite, there no concurrent task(tokio::task::spawn_local) spawned.

Did you find this page helpful?