Skip to content

Commit f365a7a

Browse files
authored
Merge pull request #34 from jamesstocktonj1/feat/adapt-option
feat: add adapt option for build and test commands
2 parents 9441333 + 73bc046 commit f365a7a

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/command.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ pub struct Build {
8080
/// The path to the Go binary (or look for binary in PATH if `None`).
8181
#[arg(long)]
8282
pub go: Option<PathBuf>,
83+
84+
/// The path to the snapshot adapter to convert a wasip1 module to a component (or use the embedded snapshot if `None`).
85+
#[arg(long)]
86+
pub adapt: Option<PathBuf>,
8387
}
8488

8589
#[derive(Parser)]
@@ -109,6 +113,10 @@ pub struct Test {
109113
/// The path to the Go binary (or look for binary in PATH if `None`).
110114
#[arg(long)]
111115
pub go: Option<PathBuf>,
116+
117+
/// The path to the snapshot adapter to convert a wasip1 module to a component (or use the embedded snapshot if `None`).
118+
#[arg(long)]
119+
pub adapt: Option<PathBuf>,
112120
}
113121

114122
#[derive(Parser)]
@@ -162,7 +170,7 @@ fn build(wit_opts: WitOpts, build: Build) -> Result<()> {
162170
)?;
163171

164172
// Update the wasm module to use the current component model ABI.
165-
module_to_component(&module)?;
173+
module_to_component(&module, &build.adapt)?;
166174
}
167175

168176
Ok(())
@@ -188,7 +196,7 @@ fn test(wit_opts: WitOpts, test: Test) -> Result<()> {
188196
)?;
189197

190198
// Update the wasm module to use the current component model ABI.
191-
module_to_component(&module)?;
199+
module_to_component(&module, &test.adapt)?;
192200
}
193201
}
194202

src/utils.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use std::{
55
};
66
use wit_parser::{PackageId, Resolve, WorldId};
77

8+
// In the rare case the snapshot needs to be updated, the latest version
9+
// can be found here: https://github.com/bytecodealliance/wasmtime/releases
10+
const WASIP1_SNAPSHOT_ADAPT: &[u8] = include_bytes!("wasi_snapshot_preview1.reactor.wasm");
11+
812
pub fn parse_wit(
913
paths: &[impl AsRef<Path>],
1014
world: Option<&str>,
@@ -73,15 +77,18 @@ pub fn embed_wit(
7377
}
7478

7579
/// Update the wasm module to use the current component model ABI.
76-
pub fn module_to_component(wasm_file: &PathBuf) -> Result<()> {
77-
// In the rare case the snapshot needs to be updated, the latest version
78-
// can be found here: https://github.com/bytecodealliance/wasmtime/releases
79-
const WASIP1_SNAPSHOT: &[u8] = include_bytes!("wasi_snapshot_preview1.reactor.wasm");
80+
pub fn module_to_component(wasm_file: &PathBuf, adapt_file: &Option<PathBuf>) -> Result<()> {
8081
let wasm: Vec<u8> = wat::Parser::new().parse_file(wasm_file)?;
8182

8283
let mut encoder = wit_component::ComponentEncoder::default().validate(true);
8384
encoder = encoder.module(&wasm)?;
84-
encoder = encoder.adapter("wasi_snapshot_preview1", WASIP1_SNAPSHOT)?;
85+
let adapt_bytes = if let Some(adapt) = adapt_file {
86+
std::fs::read(adapt)
87+
.with_context(|| format!("failed to read adapt file '{}'", adapt.display()))?
88+
} else {
89+
WASIP1_SNAPSHOT_ADAPT.to_vec()
90+
};
91+
encoder = encoder.adapter("wasi_snapshot_preview1", &adapt_bytes)?;
8592

8693
let bytes = encoder
8794
.encode()

0 commit comments

Comments
 (0)