k-xo
k-xo9mo ago

Return results from execute_main_module

I currently have this function to run some execute some js
pub async fn run(&self, path_to_main_module: &Path) -> Result<()> {
let main_module = ModuleSpecifier::from_file_path(path_to_main_module).map_err(|_| {
anyhow::anyhow!(
"Failed to create module specifier from path: {:?}",
path_to_main_module
)
})?;

let mut main_worker = deno_runtime::worker::MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::new(Permissions {
write: UnaryPermission::default(),
read: UnaryPermission::default(),
ffi: UnaryPermission::default(),
env: UnaryPermission::default(),
..Permissions::allow_all()
}),
Default::default(),
);

main_worker.execute_main_module(&main_module).await?;
Ok(())
}
pub async fn run(&self, path_to_main_module: &Path) -> Result<()> {
let main_module = ModuleSpecifier::from_file_path(path_to_main_module).map_err(|_| {
anyhow::anyhow!(
"Failed to create module specifier from path: {:?}",
path_to_main_module
)
})?;

let mut main_worker = deno_runtime::worker::MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::new(Permissions {
write: UnaryPermission::default(),
read: UnaryPermission::default(),
ffi: UnaryPermission::default(),
env: UnaryPermission::default(),
..Permissions::allow_all()
}),
Default::default(),
);

main_worker.execute_main_module(&main_module).await?;
Ok(())
}
I was wondering how I could modify this to return the result of the executed javascript?
4 Replies
Andreu Botella (they/them)
JS modules don't have results, unless you mean the module namespace object
k-xo
k-xo9mo ago
Not quite sure what you mean, but overall goal is to sort maintain similar flow you see above, and then be able to return the result to the Rust program, is that something that possible? I saw this issue --> https://github.com/denoland/deno/issues/11105, but when I tried to run certain js programs that used things like 'fetch' with execute_script nothing occurred, so opted for execute_main_module. But yeah if you have any advice for overall the best way to go about this, would really appreciate it!
GitHub
Issues · denoland/deno
A modern runtime for JavaScript and TypeScript. Contribute to denoland/deno development by creating an account on GitHub.
Andreu Botella (they/them)
you'd have to use run_event_loop but IIRC you'd also need to use it for execute_main_module if execute_script returns a promise, you can run the event loop and wait until it resolves with JsRuntime::resolve_value
k-xo
k-xo9mo ago
I see, do you know if there are any examples / docs that I could take a look at / build off of?