Skip to content

Commit 5826d1a

Browse files
authored
Merge pull request #45 from rylev/clearer-binary-error
Provide a clearer error message when package is not component
2 parents 07c67fd + cbf47d3 commit 5826d1a

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

crates/wac-parser/src/resolution/package.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ impl Package {
6262
version: Option<&Version>,
6363
bytes: Arc<Vec<u8>>,
6464
) -> Result<Self> {
65+
if !has_magic_header(&bytes) {
66+
bail!("package `{name}` is expected to be a binary WebAssembly component binary but is not");
67+
}
6568
let mut parser = Parser::new(0);
6669
let mut parsers = Vec::new();
6770
let mut validator = Validator::new_with_features(WasmFeatures {
@@ -182,6 +185,11 @@ impl Package {
182185
}
183186
}
184187

188+
/// Whether the given bytes have a magic header indicating a WebAssembly binary.
189+
fn has_magic_header(bytes: &[u8]) -> bool {
190+
bytes.starts_with(b"\0asm")
191+
}
192+
185193
impl fmt::Debug for Package {
186194
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187195
f.debug_struct("Package")

crates/wac-parser/tests/resolution/fail/package-not-wasm.wac.result

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
failed to resolve document
22

33
× failed to parse package `foo:bar`
4-
╰─▶ magic header not detected: bad magic number - expected=[
5-
0x0,
6-
0x61,
7-
0x73,
8-
0x6d,
9-
] actual=[
10-
0x74,
11-
0x68,
12-
0x69,
13-
0x73,
14-
] (at offset 0x0)
4+
╰─▶ package `foo:bar` is expected to be a binary WebAssembly component binary but is not
155
╭─[tests/resolution/fail/package-not-wasm.wac:3:13]
166
2 │
177
3 │ import foo: foo:bar/qux;

0 commit comments

Comments
 (0)