Using Deno core in an app that already had its own tokio runtime

I am using the deno_core and deno_ast crates to "Roll my own JavaScript runtime", as seen here (https://github.com/denoland/roll-your-own-javascript-runtime/blob/main/src/main.rs#L153-L159 ), but I don't know how to launch my deno runtime properly within my existing tokio runtime.

I've tried using a Handle and spawning a thread and using block_on, like below:

pub(crate) fn run_js(args: &RunArgs) {
    let file_path = args.file.to_owned().into_string().unwrap();
    let handle = tokio::runtime::Handle::current();
    std::thread::spawn(move || {
        // Using Handle::block_on to run async code in the new thread.
        if let Err(error) = handle.block_on(run_js(file_path.as_str())) {
            eprintln!("error: {error}");
        }
    })
    .join()
    .unwrap();
}


But the moment I execute the example script, it fails at setTimeout.

console.log("Waiting 2 seconds for timeout...");
setTimeout(() => {
  console.log("Hello World from timeout!");
}, 2000);


I end up with output like below:

[out]: "Waiting 2 seconds for timeout..."
Error:   × Main thread panicked.
  ├─▶ at /Users/my_user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_unsync-0.3.4/src/task.rs:56:3
  ╰─▶ assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread
  help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace.

fatal runtime error: failed to initiate panic, error 5
GitHub
Contribute to denoland/roll-your-own-javascript-runtime development by creating an account on GitHub.
Was this page helpful?