| Crates.io | hanzo-wasm |
| lib.rs | hanzo-wasm |
| version | 0.1.1 |
| created_at | 2025-11-30 06:42:19.766848+00 |
| updated_at | 2026-01-07 22:43:53.745966+00 |
| description | WASM runtime for Hanzo AI platform |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1957957 |
| size | 190,744 |
A WebAssembly (WASM) runtime for executing sandboxed code within the Hanzo Node ecosystem. This runtime provides secure, deterministic execution of WASM modules with support for various data types, host functions, and resource limits.
use hanzo_wasm_runtime::{WasmRuntime, WasmConfig};
use serde_json::json;
// Create runtime
let config = WasmConfig::default();
let runtime = WasmRuntime::new(config)?;
// Load a WASM module
let wasm_bytes = wat::parse_str(r#"
(module
(func $add (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add
)
)
"#)?;
runtime.load_module("math".to_string(), wasm_bytes).await?;
// Execute a function
let result = runtime.execute("math", "add", json!([5, 3])).await?;
assert_eq!(result, json!(8));
let config = WasmConfig {
max_memory_bytes: 256 * 1024 * 1024, // 256MB max memory
max_execution_time: Duration::from_secs(30), // 30s timeout
enable_wasi: true, // Enable WASI support
fuel_limit: Some(1_000_000_000), // Optional fuel metering
};
The runtime supports multiple ways to pass parameters:
runtime.execute("module", "func", json!([1, 2, 3])).await?
runtime.execute("module", "func", json!({"a": 10, "b": 20})).await?
runtime.execute("module", "func", json!(42)).await?
Functions that return string pointers are automatically detected based on naming conventions:
hello_strget_stringExample:
(module
(memory (export "memory") 1)
(data (i32.const 0) "Hello, World!")
(func $hello (export "hello") (result i32)
i32.const 0 ;; Return pointer to string
)
)
The runtime provides several host functions that WASM modules can import:
(import "env" "log" (func $log (param i32 i32)))
(import "env" "json_parse" (func $json_parse (param i32 i32) (result i32)))
(import "env" "json_stringify" (func $json_stringify (param i32) (result i32)))
(import "env" "http_request" (func $http_request (param i32 i32 i32) (result i32)))
(import "env" "alloc" (func $alloc (param i32) (result i32)))
(import "env" "free" (func $free (param i32)))
The runtime enforces several resource limits for safe execution:
// Load a module
runtime.load_module("name".to_string(), wasm_bytes).await?;
// List loaded modules
let modules = runtime.list_modules().await;
// Unload a module
runtime.unload_module("name").await?;
// Clear all modules
runtime.clear_modules().await;
Execute WASM bytecode without permanently loading a module:
let result = runtime.execute_bytes(wasm_bytes, "function_name", params).await?;
See the examples/ directory for complete working examples:
basic_usage.rs - Comprehensive example showing various featuresRun examples with:
cargo run --example basic_usage
The runtime includes comprehensive tests covering:
Run tests with:
cargo test -p hanzo-wasm-runtime
The runtime is built on top of Wasmtime, a fast and secure WebAssembly runtime. Key implementation features:
Arcserde_json::Value