Skip to content

Commit cbcf3e7

Browse files
authored
feat(cli-support): Bindgen - support providing bytes input (#2916)
* feat(cli-support): Bindgen - support providing bytes as input * Format. * Remove needless assignment
1 parent 96eca58 commit cbcf3e7

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

crates/cli-support/src/lib.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enum OutputMode {
8181
enum Input {
8282
Path(PathBuf),
8383
Module(Module, String),
84+
Bytes(Vec<u8>, String),
8485
None,
8586
}
8687

@@ -147,6 +148,13 @@ impl Bindgen {
147148
return self;
148149
}
149150

151+
/// Specify the input as the provided Wasm bytes.
152+
pub fn input_bytes(&mut self, name: &str, bytes: Vec<u8>) -> &mut Bindgen {
153+
let name = name.to_string();
154+
self.input = Input::Bytes(bytes, name);
155+
return self;
156+
}
157+
150158
fn switch_mode(&mut self, mode: OutputMode, flag: &str) -> Result<(), Error> {
151159
match self.mode {
152160
OutputMode::Bundler { .. } => self.mode = mode,
@@ -296,7 +304,7 @@ impl Bindgen {
296304
pub fn stem(&self) -> Result<&str, Error> {
297305
Ok(match &self.input {
298306
Input::None => bail!("must have an input by now"),
299-
Input::Module(_, name) => name,
307+
Input::Module(_, name) | Input::Bytes(_, name) => name,
300308
Input::Path(path) => match &self.out_name {
301309
Some(name) => name,
302310
None => path.file_stem().unwrap().to_str().unwrap(),
@@ -312,26 +320,15 @@ impl Bindgen {
312320
mem::replace(m, blank_module)
313321
}
314322
Input::Path(ref path) => {
315-
let wasm = wit_text::parse_file(&path)
316-
.with_context(|| format!("failed to read `{}`", path.display()))?;
317-
wit_validator::validate(&wasm)
318-
.with_context(|| format!("failed to validate `{}`", path.display()))?;
319-
let module = walrus::ModuleConfig::new()
320-
// Skip validation of the module as LLVM's output is
321-
// generally already well-formed and so we won't gain much
322-
// from re-validating. Additionally LLVM's current output
323-
// for threads includes atomic instructions but doesn't
324-
// include shared memory, so it fails that part of
325-
// validation!
326-
.strict_validate(false)
327-
.generate_dwarf(self.keep_debug)
328-
.generate_name_section(!self.remove_name_section)
329-
.generate_producers_section(!self.remove_producers_section)
330-
.on_parse(wit_walrus::on_parse)
331-
.parse(&wasm)
332-
.context("failed to parse input file as wasm")?;
333-
module
323+
let bytes = std::fs::read(path)
324+
.with_context(|| format!("failed reading '{}'", path.display()))?;
325+
self.module_from_bytes(&bytes).with_context(|| {
326+
format!("failed getting Wasm module for '{}'", path.display())
327+
})?
334328
}
329+
Input::Bytes(ref bytes, _) => self
330+
.module_from_bytes(&bytes)
331+
.context("failed getting Wasm module")?,
335332
};
336333

337334
self.threads
@@ -466,6 +463,25 @@ impl Bindgen {
466463
})
467464
}
468465

466+
fn module_from_bytes(&self, bytes: &[u8]) -> Result<Module, Error> {
467+
let wasm = wit_text::parse_bytes(bytes).context("failed to parse bytes")?;
468+
wit_validator::validate(&wasm).context("failed to validate")?;
469+
walrus::ModuleConfig::new()
470+
// Skip validation of the module as LLVM's output is
471+
// generally already well-formed and so we won't gain much
472+
// from re-validating. Additionally LLVM's current output
473+
// for threads includes atomic instructions but doesn't
474+
// include shared memory, so it fails that part of
475+
// validation!
476+
.strict_validate(false)
477+
.generate_dwarf(self.keep_debug)
478+
.generate_name_section(!self.remove_name_section)
479+
.generate_producers_section(!self.remove_producers_section)
480+
.on_parse(wit_walrus::on_parse)
481+
.parse(&wasm)
482+
.context("failed to parse input as wasm")
483+
}
484+
469485
fn local_module_name(&self, module: &str) -> String {
470486
format!("./snippets/{}", module)
471487
}

0 commit comments

Comments
 (0)