Compare commits

...

2 Commits

Author SHA1 Message Date
yuroitaki
317f9a7c9b Add wasmtime host and async. 2025-08-05 19:48:58 +08:00
yuroitaki
9ecf34a8a4 Init. 2025-08-01 18:35:03 +08:00
9 changed files with 1059 additions and 6 deletions

905
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -27,6 +27,8 @@ members = [
"crates/harness/runner",
"crates/harness/plot",
"crates/tlsn",
"crates/wasmtime-plugin",
"crates/wasmtime-host",
]
resolver = "2"

View File

@@ -0,0 +1,14 @@
[package]
name = "wasmtime-host"
version = "0.1.0"
edition = "2024"
[lints]
workspace = true
[dependencies]
anyhow = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", branch = "main", features = ["component-model-async"] }

View File

@@ -0,0 +1,73 @@
use serde::{Deserialize, Serialize};
use wasmtime::{component::{bindgen, Component, HasSelf, Linker}, Config, Engine, Store};
const WASM_PLUGIN_PATH: &str = "../../target/wasm32-unknown-unknown/release/wasmtime_plugin_component.wasm";
bindgen!({
path: "../wasmtime-plugin/wit/world.wit",
imports: {
"read": async,
"write": async,
},
exports: {
"main": async,
}
});
#[derive(Serialize, Debug)]
struct Input {
id: u8
}
#[derive(Serialize, Deserialize, Debug)]
struct Output {
result: bool
}
struct HostState {}
impl PluginImports for HostState {
async fn read(&mut self, id: u8) -> Vec<u8> {
println!("Id received from plugin: {id}");
serde_json::to_vec(&format!("Hello from host {}", id)).unwrap()
}
async fn write(&mut self, payload: Vec<u8>) -> Vec<u8> {
let payload: String = serde_json::from_slice(&payload).unwrap();
println!("Payload received from plugin: {payload}");
let success = true;
serde_json::to_vec(&success).unwrap()
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("Starting wasmtime");
let mut config = Config::new();
config
.async_support(true)
.wasm_component_model_async(true);
let engine = Engine::new(&config)?;
let component = Component::from_file(&engine, WASM_PLUGIN_PATH)?;
let mut linker = Linker::new(&engine);
Plugin::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?;
let mut store = Store::new(&engine, HostState {});
let plugin = Plugin::instantiate_async(&mut store, &component, &linker).await?;
let input = Input { id: 0 };
println!("Input: {:?}", input);
let input_bytes = serde_json::to_vec(&input)?;
let res_byte = plugin.call_main(&mut store, &input_bytes).await?;
let res: Output = serde_json::from_slice(&res_byte)?;
println!("Output: {:?}", res);
Ok(())
}

View File

@@ -0,0 +1,5 @@
[build]
target = "wasm32-unknown-unknown"
[unstable]
build-std = ["panic_abort", "std"]

View File

@@ -0,0 +1,15 @@
[package]
name = "wasmtime-plugin"
version = "0.1.0"
edition = "2024"
[lints]
workspace = true
[lib]
crate-type = ["cdylib"]
[dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
wit-bindgen = { version = "0.43.0" }

13
crates/wasmtime-plugin/build.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
PACKAGE_NAME="wasmtime_plugin"
set -e
# Build the project
cargo build --release
# Convert the wasm binary to a component
wasm-tools component new ../../target/wasm32-unknown-unknown/release/${PACKAGE_NAME}.wasm -o ../../target/wasm32-unknown-unknown/release/${PACKAGE_NAME}_component.wasm
echo "Component created: ${PACKAGE_NAME}_component.wasm"

View File

@@ -0,0 +1,31 @@
use serde::{Deserialize, Serialize};
wit_bindgen::generate!({
path: "wit/world.wit",
async: true
});
struct Component;
#[derive(Deserialize)]
struct Input {
id: u8
}
#[derive(Serialize)]
struct Output {
result: bool
}
impl Guest for Component {
async fn main(input: Vec<u8>) -> Vec<u8> {
let input: Input = serde_json::from_slice(&input).unwrap();
let payload_bytes = read(input.id).await;
let result_bytes = write(payload_bytes).await;
let result: bool = serde_json::from_slice(&result_bytes).unwrap();
let output = Output { result };
serde_json::to_vec(&output).unwrap()
}
}
export!(Component);

View File

@@ -0,0 +1,7 @@
package component:wasmtime-plugin;
world plugin {
export main: func(input: list<u8>) -> list<u8>;
import read: func(id: u8) -> list<u8>;
import write: func(payload: list<u8>) -> list<u8>;
}