A JavaScript Runtime for Rust Ion is a JavaScript runtime for integrating a JavaScript engine within a Rust program. This is useful for cases like building out a JavaScript powered plug-in system - but it can also be used directly from a stand alone executable. Goals: ✅ Easy to use high-level API (Inspired by napi-rs) ✅ Event-loop built on top of Tokio ✅ Simple API to add a standard library ✅ Positively multi-threaded 👀 C FFI for embedders coming from other languages CLI Usage The repo includes a reference executable that implements Ion, you can find it under ./crates/ion_cli cargo build --release ./target/release/ion_cli eval " console.log('42') " Embedder Usage cargo add --git https://github.com/alshdavid/ion.git ion Basic For more, see ./examples use ion :: * ; pub fn main ( ) -> anyhow :: Result < ( ) > { let runtime = JsRuntime :: initialize_once ( ) ? ; // Create an isolate running on a dedicated thread let worker = runtime . spawn_worker ( ) ? ; // Open a JavaScript context (a fresh globalThis) to execute JavaScript. // You can open multiple contexts, sharing the same thread let ctx = worker . create_context ( ) ? ; // Execute some JavaScript in the context ctx . exec_blocking ( |env| { // Evaluate arbitrary JavaScript, the result of the last line is returned let value = env . eval_script :: < JsNumber > ( "1 + 1" ) ? ; // Cast to Rust type let result = value . get_u32 ( ) ? ; // Prints "2" println ! ( "Returned: {}" , result ) ; Ok ( ( ) ) } ) ? ; Ok ( ( ) ) } Async use ion :: * ; pub fn main ( ) -> anyhow :: Result < ( ) > { let runtime = JsRuntime :: initialize_once ( ) ? ; let worker = runtime . spawn_worker ( ) ? ; let ctx = worker . create_context ( ) ? ; ctx . exec_blocking ( |env| { // Spawn an future on the event loop env . spawn_local ( { let env = env . clone ( ) ; async move { println ! ( "Async Task Started" ) ; let value = env . eval_script :: < JsNumber > ( "1 + 1" ) ? ; // Wait for some time tokio :: time :: sleep ( tokio :: time :: Duration :...
First seen: 2025-09-10 01:06
Last seen: 2025-09-10 03:06