Skip to content

Commit

Permalink
json-from-wast: Include binary form of text files where possible (#1775)
Browse files Browse the repository at this point in the history
This commit is aimed at addressing #1771 by including a new
`binary_filename` field in the JSON which is populated when the test
itself is `module quote`, or a textual module, but the module parse
successfully. This should help avoid the need for a text parser for
external users using the tool while still reflecting the test case
itself where it's text-based.

Closes #1771
  • Loading branch information
alexcrichton authored Sep 12, 2024
1 parent 5eac9e1 commit ba02e8f
Show file tree
Hide file tree
Showing 578 changed files with 28,905 additions and 24,696 deletions.
82 changes: 51 additions & 31 deletions src/bin/wasm-tools/json_from_wast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ impl<'a> JsonBuilder<'a> {
let line = self.lineno(directive.span());
let command = match directive {
WastDirective::Module(module) => {
let (name, _module_type, filename) = self.emit_file(module)?;
let (name, file) = self.emit_file(module, false)?;
json::Command::Module {
line,
name: name.map(|s| self.module_name(s)),
filename,
file,
}
}
WastDirective::ModuleDefinition(module) => {
let (name, _module_type, filename) = self.emit_file(module)?;
let (name, file) = self.emit_file(module, false)?;
json::Command::ModuleDefinition {
line,
name: name.map(|s| self.module_name(s)),
filename,
file,
}
}
WastDirective::ModuleInstance {
Expand All @@ -159,12 +159,11 @@ impl<'a> JsonBuilder<'a> {
message,
} => {
let line = self.lineno(module.span());
let (_name, module_type, filename) = self.emit_file(module)?;
let (_name, file) = self.emit_file(module, true)?;
json::Command::AssertMalformed {
line,
filename,
text: message,
module_type,
file,
}
}
WastDirective::AssertInvalid {
Expand All @@ -173,12 +172,11 @@ impl<'a> JsonBuilder<'a> {
message,
} => {
let line = self.lineno(module.span());
let (_name, module_type, filename) = self.emit_file(module)?;
let (_name, file) = self.emit_file(module, false)?;
json::Command::AssertInvalid {
line,
filename,
text: message,
module_type,
file,
}
}
WastDirective::Register {
Expand All @@ -200,12 +198,11 @@ impl<'a> JsonBuilder<'a> {
message,
} => {
let line = self.lineno(module.span());
let (_name, module_type, filename) = self.emit_file(QuoteWat::Wat(module))?;
let (_name, file) = self.emit_file(QuoteWat::Wat(module), false)?;
json::Command::AssertUninstantiable {
line,
filename,
text: message,
module_type,
file,
}
}
WastDirective::AssertTrap {
Expand Down Expand Up @@ -244,12 +241,11 @@ impl<'a> JsonBuilder<'a> {
message,
} => {
let line = self.lineno(module.span());
let (_name, module_type, filename) = self.emit_file(QuoteWat::Wat(module))?;
let (_name, file) = self.emit_file(QuoteWat::Wat(module), false)?;
json::Command::AssertUnlinkable {
line,
text: message,
module_type,
filename,
file,
}
}
WastDirective::AssertException { span: _, exec } => json::Command::AssertException {
Expand Down Expand Up @@ -288,7 +284,8 @@ impl<'a> JsonBuilder<'a> {
fn emit_file(
&mut self,
mut module: QuoteWat<'a>,
) -> Result<(Option<&'a str>, &'a str, String)> {
malformed: bool,
) -> Result<(Option<&'a str>, json::WasmFile)> {
let name = module.name().map(|i| i.name());
let (contents, module_type, ext) = match module.to_test()? {
QuoteWatTest::Text(s) => (s, "text", "wat"),
Expand All @@ -302,12 +299,25 @@ impl<'a> JsonBuilder<'a> {
let fileno = self.files;
self.files += 1;
let filename = format!("{stem}.{fileno}.{ext}");
let dst = match &self.opts.wasm_dir {
Some(dir) => dir.join(&filename),
None => filename.clone().into(),
let binary_filename = format!("{stem}.{fileno}.wasm");
let (dst, binary_dst) = match &self.opts.wasm_dir {
Some(dir) => (dir.join(&filename), dir.join(&binary_filename)),
None => (filename.clone().into(), binary_filename.clone().into()),
};
std::fs::write(&dst, &contents).with_context(|| format!("failed to write file {dst:?}"))?;
Ok((name, module_type, filename))
let mut ret = json::WasmFile {
module_type,
filename,
binary_filename: None,
};
if module_type == "text" && !malformed {
if let Ok(bytes) = module.encode() {
std::fs::write(&binary_dst, &bytes)
.with_context(|| format!("failed to write file {binary_dst:?}"))?;
ret.binary_filename = Some(binary_filename);
}
}
Ok((name, ret))
}

fn action(&self, exec: WastExecute<'a>) -> Result<json::Action<'a>> {
Expand Down Expand Up @@ -565,13 +575,15 @@ mod json {
line: u32,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
filename: String,
#[serde(flatten)]
file: WasmFile,
},
ModuleDefinition {
line: u32,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
filename: String,
#[serde(flatten)]
file: WasmFile,
},
ModuleInstance {
line: u32,
Expand All @@ -582,15 +594,15 @@ mod json {
},
AssertMalformed {
line: u32,
filename: String,
#[serde(flatten)]
file: WasmFile,
text: &'a str,
module_type: &'a str,
},
AssertInvalid {
line: u32,
filename: String,
#[serde(flatten)]
file: WasmFile,
text: &'a str,
module_type: &'a str,
},
Register {
line: u32,
Expand All @@ -601,9 +613,9 @@ mod json {
},
AssertUnlinkable {
line: u32,
filename: String,
#[serde(flatten)]
file: WasmFile,
text: &'a str,
module_type: &'a str,
},
AssertReturn {
line: u32,
Expand All @@ -630,9 +642,9 @@ mod json {
},
AssertUninstantiable {
line: u32,
filename: String,
#[serde(flatten)]
file: WasmFile,
text: &'a str,
module_type: &'a str,
},
Thread {
line: u32,
Expand All @@ -647,6 +659,14 @@ mod json {
},
}

#[derive(Serialize)]
pub struct WasmFile {
pub filename: String,
pub module_type: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub binary_filename: Option<String>,
}

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Action<'a> {
Expand Down
10 changes: 6 additions & 4 deletions tests/snapshots/local/atomics.wast.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
{
"type": "module",
"line": 1,
"filename": "atomics.0.wasm"
"filename": "atomics.0.wasm",
"module_type": "binary"
},
{
"type": "module",
"line": 6,
"filename": "atomics.1.wasm"
"filename": "atomics.1.wasm",
"module_type": "binary"
},
{
"type": "assert_invalid",
"line": 16,
"filename": "atomics.2.wasm",
"text": "unknown memory 1",
"module_type": "binary"
"module_type": "binary",
"text": "unknown memory 1"
}
]
}
12 changes: 6 additions & 6 deletions tests/snapshots/local/bad-nan.wast.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
"type": "assert_malformed",
"line": 2,
"filename": "bad-nan.0.wat",
"text": "constant out of range",
"module_type": "text"
"module_type": "text",
"text": "constant out of range"
},
{
"type": "assert_malformed",
"line": 6,
"filename": "bad-nan.1.wat",
"text": "constant out of range",
"module_type": "text"
"module_type": "text",
"text": "constant out of range"
},
{
"type": "assert_malformed",
"line": 10,
"filename": "bad-nan.2.wat",
"text": "constant out of range",
"module_type": "text"
"module_type": "text",
"text": "constant out of range"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,47 @@
{
"type": "module",
"line": 1,
"filename": "branch-hinting-simple.0.wasm"
"filename": "branch-hinting-simple.0.wasm",
"module_type": "binary"
},
{
"type": "module",
"line": 26,
"filename": "branch-hinting-simple.1.wasm"
"filename": "branch-hinting-simple.1.wasm",
"module_type": "binary"
},
{
"type": "module",
"line": 37,
"filename": "branch-hinting-simple.2.wasm"
"filename": "branch-hinting-simple.2.wasm",
"module_type": "binary"
},
{
"type": "assert_invalid",
"line": 62,
"filename": "branch-hinting-simple.3.wat",
"text": "expected valid module field",
"module_type": "text"
"module_type": "text",
"text": "expected valid module field"
},
{
"type": "assert_invalid",
"line": 65,
"filename": "branch-hinting-simple.4.wat",
"text": "expected a string",
"module_type": "text"
"module_type": "text",
"text": "expected a string"
},
{
"type": "assert_invalid",
"line": 68,
"filename": "branch-hinting-simple.5.wat",
"text": "invalid value for branch hint",
"module_type": "text"
"module_type": "text",
"text": "invalid value for branch hint"
},
{
"type": "module",
"line": 72,
"filename": "branch-hinting-simple.6.wasm"
"filename": "branch-hinting-simple.6.wasm",
"module_type": "binary"
}
]
}
28 changes: 14 additions & 14 deletions tests/snapshots/local/code-after-end.wast.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,50 @@
"type": "assert_invalid",
"line": 2,
"filename": "code-after-end.0.wasm",
"text": "operators remaining after end of function",
"module_type": "binary"
"module_type": "binary",
"text": "operators remaining after end of function"
},
{
"type": "assert_invalid",
"line": 7,
"filename": "code-after-end.1.wasm",
"text": "operators remaining after end of function",
"module_type": "binary"
"module_type": "binary",
"text": "operators remaining after end of function"
},
{
"type": "assert_invalid",
"line": 12,
"filename": "code-after-end.2.wasm",
"text": "operators remaining after end of function",
"module_type": "binary"
"module_type": "binary",
"text": "operators remaining after end of function"
},
{
"type": "assert_invalid",
"line": 17,
"filename": "code-after-end.3.wasm",
"text": "operators remaining after end of function",
"module_type": "binary"
"module_type": "binary",
"text": "operators remaining after end of function"
},
{
"type": "assert_invalid",
"line": 22,
"filename": "code-after-end.4.wasm",
"text": "operators remaining after end of function",
"module_type": "binary"
"module_type": "binary",
"text": "operators remaining after end of function"
},
{
"type": "assert_invalid",
"line": 27,
"filename": "code-after-end.5.wasm",
"text": "operators remaining after end of function",
"module_type": "binary"
"module_type": "binary",
"text": "operators remaining after end of function"
},
{
"type": "assert_invalid",
"line": 32,
"filename": "code-after-end.6.wasm",
"text": "operators remaining after end of function",
"module_type": "binary"
"module_type": "binary",
"text": "operators remaining after end of function"
}
]
}
3 changes: 2 additions & 1 deletion tests/snapshots/local/component-model/a.wast.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{
"type": "module",
"line": 4,
"filename": "a.0.wasm"
"filename": "a.0.wasm",
"module_type": "binary"
}
]
}
Loading

0 comments on commit ba02e8f

Please sign in to comment.