Skip to content

Commit

Permalink
Expose pack function in wasi-vfs-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Dec 24, 2023
1 parent 7184b64 commit a659ce6
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions crates/wasi-vfs-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,35 @@ 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)?;
}
}
Ok(())
}
}

pub fn pack(wasm_bytes: &[u8], map_dirs: Vec<(PathBuf, PathBuf)>) -> Result<Vec<u8>> {
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) {
Expand Down

0 comments on commit a659ce6

Please sign in to comment.