From a659ce63041f8c8b2a670ed1753d28da11a525f4 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 24 Dec 2023 15:40:33 +0000 Subject: [PATCH] Expose pack function in wasi-vfs-cli --- crates/wasi-vfs-cli/src/lib.rs | 36 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/wasi-vfs-cli/src/lib.rs b/crates/wasi-vfs-cli/src/lib.rs index 8b11eb8..90f6c34 100644 --- a/crates/wasi-vfs-cli/src/lib.rs +++ b/crates/wasi-vfs-cli/src/lib.rs @@ -51,22 +51,8 @@ impl App { map_dirs, output, } => { - std::env::set_var("__WASI_VFS_PACKING", "1"); - let mut wizer = wizer::Wizer::new(); - wizer.allow_wasi(true)?; - wizer.init_func("wasi_vfs_pack_fs"); - wizer.inherit_stdio(true); - wizer.inherit_env(true); - wizer.keep_init_func(true); - wizer.wasm_bulk_memory(true); - for (guest_dir, host_dir) in map_dirs { - wizer.map_dir(guest_dir, host_dir); - } let wasm_bytes = std::fs::read(&input)?; - if is_wasi_reactor(&wasm_bytes) { - wizer.func_rename("_initialize", "__wasi_vfs_rt_init"); - } - let output_bytes = wizer.run(&wasm_bytes)?; + let output_bytes = pack(&wasm_bytes, map_dirs)?; std::fs::write(output, output_bytes)?; } } @@ -74,6 +60,26 @@ impl App { } } +pub fn pack(wasm_bytes: &[u8], map_dirs: Vec<(PathBuf, PathBuf)>) -> Result> { + std::env::set_var("__WASI_VFS_PACKING", "1"); + let mut wizer = wizer::Wizer::new(); + wizer.allow_wasi(true)?; + wizer.init_func("wasi_vfs_pack_fs"); + wizer.inherit_stdio(true); + wizer.inherit_env(true); + wizer.keep_init_func(true); + wizer.wasm_bulk_memory(true); + wizer.wasm_reference_types(true); + for (guest_dir, host_dir) in map_dirs { + wizer.map_dir(guest_dir, host_dir); + } + if is_wasi_reactor(&wasm_bytes) { + wizer.func_rename("_initialize", "__wasi_vfs_rt_init"); + } + let output_bytes = wizer.run(&wasm_bytes)?; + Ok(output_bytes) +} + fn is_wasi_reactor(bytes: &[u8]) -> bool { let parser = wasmparser::Parser::new(0); for payload in parser.parse_all(bytes) {