Tiny Async Wasi Preview 2 Runtime
wasm wasi preview2 async runtime rust
Rust 99.9%
Other 0.1%
49 1 0

Clone this repository

https://tangled.org/staticanxiety.tngl.sh/tiny-wasm-async-runtime
git@tangled.org:staticanxiety.tngl.sh/tiny-wasm-async-runtime

For self-hosted knots, clone URLs may differ based on your setup.

README.md

tiny-async-runtime#

tiny-async-runtime is a minimal, WASI-compatible async runtime designed to run on WebAssembly with WASI Preview 2.
It provides:

[x] Single-threaded cooperative task scheduling
[x] Futures spawning and cancellation
[x] Timeout support
[x] Socket I/O integration
[ ] File IO Intergration This runtime is inspired by mio but is purpose-built for WASI environments.

Features#

  • block_on()
    Runs an async function to completion, driving timers, I/O readiness, and spawned tasks.

  • spawn()
    Launches a future in the runtime. Returns a JoinHandle for cancellation or awaiting completion.

  • Timers

    • Timer::sleep(duration) creates a future that resolves after a given time.
    • Timer futures integrate into the same event loop.
  • Cancellation

    • Calling JoinHandle::cancel() removes the task from the scheduler.
    • Timers and sockets are left in a completed state (future enhancement: automatic cleanup).
  • Partial Support for Sockets

    • At this point sockets can only connect but planned support is coming to the future

Example#

Here’s a minimal example using block_on and spawn:

use tiny_async_runtime::{WasmRuntimeAsyncEngine, Timer};

fn main() {
    WasmRuntimeAsyncEngine::block_on(async {
        // Spawn a background task
        let handle = WasmRuntimeAsyncEngine::spawn(async {
            println!("Background task sleeping...");
            Timer::sleep(std::time::Duration::from_secs(1)).await;
            println!("Background task done.");
            42
        });

        // Wait for the result
        let result = handle.await;
        println!("Background task returned: {result}");

        // Sleep in main
        Timer::sleep(std::time::Duration::from_millis(500)).await;
        println!("Main task done.");
    });
}