D
Deno

help

How to call a JS function from Rust?

DADavid Alsh1/29/2024
Hi, I'd like to call a JS function from Rust but I can't seem to find an example that does so. I got this far with the examples and blog tutorials.
// index.js
function add(a, b) {
return a + b
}

// Do I need these?
// globalThis.add = add
// export { add }
// index.js
function add(a, b) {
return a + b
}

// Do I need these?
// globalThis.add = add
// export { add }
// main.rs
async fn main_async() {
let main_module = resolve_path(
"./index.js",
env::current_dir().unwrap().join("js").as_path()
).unwrap();

let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(Rc::new(FsModuleLoader)),
..Default::default()
});

let mod_id = js_runtime.load_main_module(&main_module, None).await.unwrap();

let result = js_runtime.mod_evaluate(mod_id);

js_runtime.run_event_loop(PollEventLoopOptions::default()).await.unwrap();

result.await.unwrap();
}

fn main() {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(main_async())
}
// main.rs
async fn main_async() {
let main_module = resolve_path(
"./index.js",
env::current_dir().unwrap().join("js").as_path()
).unwrap();

let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(Rc::new(FsModuleLoader)),
..Default::default()
});

let mod_id = js_runtime.load_main_module(&main_module, None).await.unwrap();

let result = js_runtime.mod_evaluate(mod_id);

js_runtime.run_event_loop(PollEventLoopOptions::default()).await.unwrap();

result.await.unwrap();
}

fn main() {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(main_async())
}
How can I call add(1, 2) from rust?
DAHDeno AI Helper1/29/2024
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippets you've provided, I can offer some general advice. In your JavaScript code, if you want to make the add function available globally, you can assign it to globalThis:
// index.js
function add(a, b) {
return a + b;
}

globalThis.add = add;
// index.js
function add(a, b) {
return a + b;
}

globalThis.add = add;
This will make the add function accessible as a global function, which you can then attempt to call from your Rust code. In your Rust code, you are setting up a JsRuntime and loading a module, but you are not showing how you are trying to call the add function. To call a JavaScript function from Rust, you would typically use the JsRuntime to evaluate a script that calls the function. However, the specifics of how to do this depend on the APIs provided by the Rust library you are using for JavaScript integration (which seems to be Deno's runtime in this case). Please note that the advice provided here is not based on the Deno documentation, and you should double-check the accuracy of this advice. If you are using Deno's runtime or another library for embedding JavaScript in Rust, you should consult the documentation or community resources for that specific library to find the best way to call JavaScript functions from Rust.
19542/18/2024
i am also having this issue. i wasn to use the call_with_args method but i'm not sure how to get a v8::Function from my runtime (or scope, or wherever those live)

Looking for more? Join the community!