Damian Reeves
Damian Reeves
DDeno
Created by Damian Reeves on 6/13/2024 in #help
Creating custom ops for deno_core that take in arrays and strucks
I am interested in creating custom ops for deno_core. I have one case where I want to take in a Vec<String> and return a Result that possibly includes my own custom enum type or struct as a result. Secondly I want to be able to pass my own custom struct to my op. What do I need to do to achieve this?
3 replies
DDeno
Created by Damian Reeves on 6/4/2024 in #help
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();
}
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);
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
[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
12 replies