diff --git a/Cargo.lock b/Cargo.lock index b8e10c7..3f045d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2489,7 +2489,7 @@ dependencies = [ [[package]] name = "west" -version = "0.1.0-alpha.1" +version = "0.1.0-alpha.2" dependencies = [ "anyhow", "http", diff --git a/Cargo.toml b/Cargo.toml index 93c00e9..727f020 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "west" -version = "0.1.0-alpha.1" +version = "0.1.0-alpha.2" description = "WebAssembly component test runtime" authors.workspace = true @@ -31,11 +31,6 @@ wasmtime-wasi = { workspace = true } wasmtime-wasi-http = { workspace = true } wit-component = { workspace = true } -[build-dependencies] -anyhow = { workspace = true } -wasi-preview1-component-adapter-provider = { workspace = true } -wit-component = { workspace = true } - [workspace.dependencies] anyhow = "1" cbindgen = "0.27" @@ -49,6 +44,6 @@ wasmtime = "24" wasmtime-wasi = "24" wasmtime-wasi-http = "24" west-passthrough = { version = "0.1.0-alpha.1", path = "./crates/passthrough" } -west = { version = "0.1.0-alpha.1", path = "." } +west = { version = "0.1.0-alpha.2", path = "." } wit-bindgen = "0.32" wit-component = "0.217" diff --git a/build.rs b/build.rs deleted file mode 100644 index 5062cc0..0000000 --- a/build.rs +++ /dev/null @@ -1,53 +0,0 @@ -use std::path::PathBuf; -use std::process::{Command, Stdio}; -use std::{env, fs}; - -use anyhow::{ensure, Context as _}; -use wasi_preview1_component_adapter_provider::{ - WASI_SNAPSHOT_PREVIEW1_ADAPTER_NAME, WASI_SNAPSHOT_PREVIEW1_REACTOR_ADAPTER, -}; - -fn main() -> anyhow::Result<()> { - println!("cargo:rerun-if-changed=crates/passthrough"); - - let out_dir = env::var("OUT_DIR") - .map(PathBuf::from) - .context("failed to lookup `OUT_DIR`")?; - let status = Command::new(env::var("CARGO").unwrap()) - .args([ - "build", - "-p", - "west-passthrough", - "--release", - "--target", - "wasm32-wasip1", - "--target-dir", - ]) - .arg(&out_dir) - .stderr(Stdio::inherit()) - .stdout(Stdio::inherit()) - .status() - .context("failed to invoke `cargo`")?; - ensure!(status.success(), "`cargo` invocation failed"); - let path = out_dir - .join("wasm32-wasip1") - .join("release") - .join("west_passthrough.wasm"); - let module = fs::read(&path).with_context(|| format!("failed to read `{}`", path.display()))?; - let component = wit_component::ComponentEncoder::default() - .validate(true) - .module(&module) - .context("failed to set core component module")? - .adapter( - WASI_SNAPSHOT_PREVIEW1_ADAPTER_NAME, - WASI_SNAPSHOT_PREVIEW1_REACTOR_ADAPTER, - ) - .context("failed to add WASI adapter")? - .encode() - .with_context(|| format!("failed to encode `{}`", path.display()))?; - - let path = out_dir.join("west_passthrough.wasm"); - fs::write(&path, component).with_context(|| format!("failed to write `{}`", path.display()))?; - - Ok(()) -} diff --git a/crates/sys/src/ffi.rs b/crates/sys/src/ffi.rs index 55f5a0e..6cc8920 100644 --- a/crates/sys/src/ffi.rs +++ b/crates/sys/src/ffi.rs @@ -4,7 +4,7 @@ use core::ptr::{self, null_mut}; use std::ffi::CString; use std::sync::{LazyLock, Mutex}; -use crate::{call, instantiate, Config, List, PASSTHROUGH_LEN, PASSTHROUGH_PTR}; +use crate::{call, instantiate, Config}; static ERROR: LazyLock>> = LazyLock::new(Mutex::default); @@ -15,16 +15,6 @@ fn store_error(err: anyhow::Error) { .insert(CString::new(format!("{err:?}")).expect("failed to construct error string")); } -#[no_mangle] -pub extern "C" fn default_config() -> Config { - Config { - wasm: List { - ptr: PASSTHROUGH_PTR, - len: PASSTHROUGH_LEN, - }, - } -} - #[no_mangle] pub extern "C" fn error_take(buf: *mut c_char, len: usize) -> usize { if let Some(err) = ERROR.lock().unwrap().take() { diff --git a/crates/sys/src/lib.rs b/crates/sys/src/lib.rs index da63ca6..c8d8d69 100644 --- a/crates/sys/src/lib.rs +++ b/crates/sys/src/lib.rs @@ -21,9 +21,6 @@ mod ffi; static ENGINE: LazyLock = LazyLock::new(wasmtime::Engine::default); -pub const PASSTHROUGH_PTR: *const u8 = west::PASSTHROUGH.as_ptr(); -pub const PASSTHROUGH_LEN: usize = west::PASSTHROUGH.len(); - #[repr(C)] #[derive(Debug)] pub struct List { diff --git a/include/west.h b/include/west.h index 8ed096b..d41030b 100644 --- a/include/west.h +++ b/include/west.h @@ -12,8 +12,6 @@ typedef struct Config { struct List_u8 wasm; } Config; -struct Config default_config(void); - uintptr_t error_take(char *buf, uintptr_t len); uintptr_t error_len(void); diff --git a/src/lib.rs b/src/lib.rs index 8e30c6d..ae9510d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,8 +24,6 @@ mod bindings { }); } -pub const PASSTHROUGH: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/west_passthrough.wasm")); - struct Ctx { wasi: WasiCtx, http: WasiHttpCtx, @@ -175,15 +173,6 @@ pub struct Config<'a> { pub wasm: &'a [u8], } -impl Default for Config<'_> { - fn default() -> Self { - Self { - engine: Engine::default(), - wasm: PASSTHROUGH, - } - } -} - pub struct Func<'a> { func: wasmtime::component::Func, store: &'a mut Store, diff --git a/west.go b/west.go index 7cfcb89..31d90a6 100644 --- a/west.go +++ b/west.go @@ -1,4 +1,6 @@ //go:generate cargo build -p west-sys --release +//go:generate cargo build -p west-passthrough --target wasm32-unknown-unknown --release +//go:generate wasm-tools component new target/wasm32-unknown-unknown/release/west_passthrough.wasm -o lib/passthrough.wasm //go:generate go run github.com/ydnar/wasm-tools-go/cmd/wit-bindgen-go@v0.1.5 generate -w imports -o bindings ./wit package west @@ -13,6 +15,7 @@ package west import "C" import ( + _ "embed" "errors" "fmt" "log" @@ -23,6 +26,9 @@ import ( "unsafe" ) +//go:embed lib/passthrough.wasm +var Passthrough []byte + var ( errorHandlerMu sync.RWMutex errorHandler atomic.Value @@ -148,19 +154,20 @@ func NewInstance(conf *Config) (*Instance, error) { var pinner runtime.Pinner defer pinner.Unpin() - c := C.default_config() + wasm := Passthrough if conf != nil { if len(conf.Wasm) > 0 { - ptr := unsafe.SliceData(conf.Wasm) - pinner.Pin(ptr) - - c.wasm = C.List_u8{ - ptr: (*C.uchar)(ptr), - len: C.ulong(len(conf.Wasm)), - } + wasm = conf.Wasm } } - ptr := C.instance_new(c) + wasmPtr := unsafe.SliceData(wasm) + pinner.Pin(wasmPtr) + ptr := C.instance_new(C.Config{ + wasm: C.List_u8{ + ptr: (*C.uchar)(wasmPtr), + len: C.ulong(len(wasm)), + }, + }) if ptr == nil { n := C.error_len() buf := make([]C.char, n)