Does V8 engine eventloop/threading model conflict with rust?

hi, I am trying to embed deno's rusty_v8 into rust application. I am confused about the eventloop/threading model between v8 engine and rust. because js/ts has the async/await feature (which helps to avoid blocking IO/network, and other things), and v8 engine also has an eventloop itself. I think when v8 engine is running it's eventloop, it will cause rust main thread blocking? How should I do to make v8 and rust running together?
DAH
Deno AI Helper40d ago
Hello! Embedding V8 into a Rust application using deno_core is indeed an interesting challenge, but it's one that the Deno team has worked to make as smooth as possible. The documentation provides a guide on how to create your own JavaScript runtime with Rust and deno_core, which can help clarify how the event loop and threading model work together. In JavaScript, the event loop is a fundamental part of the runtime, enabling non-blocking I/O and asynchronous operations. V8, the JavaScript engine used by Deno, does not come with an event loop out of the box; it's just the engine that executes JavaScript code. When embedding V8 into a Rust application, you need to integrate it with an event loop, and this is where Rust's async runtime, such as tokio, comes into play. deno_core provides a JsRuntime struct that encapsulates a V8 engine instance (called an Isolate) and allows integration with an event loop. By using tokio as the asynchronous Rust runtime, you can map JavaScript's Promises onto Rust's Futures. This means that when JavaScript code performs an asynchronous operation, it can be executed in a non-blocking way by the tokio runtime in Rust. Here's a simplified view of how you can set this up, based on the documentation: 1. Create an instance of JsRuntime with the necessary configuration to use a module loader and potentially other extensions. 2. Load and evaluate your JavaScript code within this runtime. 3. Run the tokio event loop, which will handle the async operations initiated by the JavaScript code. Here's a snippet of Rust code from the documentation that demonstrates creating a JsRuntime and running it with tokio: ```rust use deno_core::error::AnyError; use std::rc::Rc; async fn run_js(file_path: &str) -> Result<(), AnyError> {
DAH
Deno AI Helper40d ago
let main_module = deno_core::resolve_path(file_path)?; let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions { module_loader: Some(Rc::new(deno_core::FsModuleLoader)), ..Default::default() }); let mod_id = js_runtime.load_main_module(&main_module, None).await?; let result = js_runtime.mod_evaluate(mod_id); js_runtime.run_event_loop(false).await?; result.await? } fn main() { let runtime = tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap(); if let Err(error) = runtime.block_on(run_js("./example.js")) { eprintln!("error: {}", error); } } `` In this setup, the Rust tokio` runtime is responsible for running the event loop, and it will not block the main thread. Instead, it will execute