Skip to content

Commit ba02e8f

Browse files
authored
json-from-wast: Include binary form of text files where possible (#1775)
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
1 parent 5eac9e1 commit ba02e8f

578 files changed

Lines changed: 28905 additions & 24696 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/bin/wasm-tools/json_from_wast.rs

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,19 @@ impl<'a> JsonBuilder<'a> {
131131
let line = self.lineno(directive.span());
132132
let command = match directive {
133133
WastDirective::Module(module) => {
134-
let (name, _module_type, filename) = self.emit_file(module)?;
134+
let (name, file) = self.emit_file(module, false)?;
135135
json::Command::Module {
136136
line,
137137
name: name.map(|s| self.module_name(s)),
138-
filename,
138+
file,
139139
}
140140
}
141141
WastDirective::ModuleDefinition(module) => {
142-
let (name, _module_type, filename) = self.emit_file(module)?;
142+
let (name, file) = self.emit_file(module, false)?;
143143
json::Command::ModuleDefinition {
144144
line,
145145
name: name.map(|s| self.module_name(s)),
146-
filename,
146+
file,
147147
}
148148
}
149149
WastDirective::ModuleInstance {
@@ -159,12 +159,11 @@ impl<'a> JsonBuilder<'a> {
159159
message,
160160
} => {
161161
let line = self.lineno(module.span());
162-
let (_name, module_type, filename) = self.emit_file(module)?;
162+
let (_name, file) = self.emit_file(module, true)?;
163163
json::Command::AssertMalformed {
164164
line,
165-
filename,
166165
text: message,
167-
module_type,
166+
file,
168167
}
169168
}
170169
WastDirective::AssertInvalid {
@@ -173,12 +172,11 @@ impl<'a> JsonBuilder<'a> {
173172
message,
174173
} => {
175174
let line = self.lineno(module.span());
176-
let (_name, module_type, filename) = self.emit_file(module)?;
175+
let (_name, file) = self.emit_file(module, false)?;
177176
json::Command::AssertInvalid {
178177
line,
179-
filename,
180178
text: message,
181-
module_type,
179+
file,
182180
}
183181
}
184182
WastDirective::Register {
@@ -200,12 +198,11 @@ impl<'a> JsonBuilder<'a> {
200198
message,
201199
} => {
202200
let line = self.lineno(module.span());
203-
let (_name, module_type, filename) = self.emit_file(QuoteWat::Wat(module))?;
201+
let (_name, file) = self.emit_file(QuoteWat::Wat(module), false)?;
204202
json::Command::AssertUninstantiable {
205203
line,
206-
filename,
207204
text: message,
208-
module_type,
205+
file,
209206
}
210207
}
211208
WastDirective::AssertTrap {
@@ -244,12 +241,11 @@ impl<'a> JsonBuilder<'a> {
244241
message,
245242
} => {
246243
let line = self.lineno(module.span());
247-
let (_name, module_type, filename) = self.emit_file(QuoteWat::Wat(module))?;
244+
let (_name, file) = self.emit_file(QuoteWat::Wat(module), false)?;
248245
json::Command::AssertUnlinkable {
249246
line,
250247
text: message,
251-
module_type,
252-
filename,
248+
file,
253249
}
254250
}
255251
WastDirective::AssertException { span: _, exec } => json::Command::AssertException {
@@ -288,7 +284,8 @@ impl<'a> JsonBuilder<'a> {
288284
fn emit_file(
289285
&mut self,
290286
mut module: QuoteWat<'a>,
291-
) -> Result<(Option<&'a str>, &'a str, String)> {
287+
malformed: bool,
288+
) -> Result<(Option<&'a str>, json::WasmFile)> {
292289
let name = module.name().map(|i| i.name());
293290
let (contents, module_type, ext) = match module.to_test()? {
294291
QuoteWatTest::Text(s) => (s, "text", "wat"),
@@ -302,12 +299,25 @@ impl<'a> JsonBuilder<'a> {
302299
let fileno = self.files;
303300
self.files += 1;
304301
let filename = format!("{stem}.{fileno}.{ext}");
305-
let dst = match &self.opts.wasm_dir {
306-
Some(dir) => dir.join(&filename),
307-
None => filename.clone().into(),
302+
let binary_filename = format!("{stem}.{fileno}.wasm");
303+
let (dst, binary_dst) = match &self.opts.wasm_dir {
304+
Some(dir) => (dir.join(&filename), dir.join(&binary_filename)),
305+
None => (filename.clone().into(), binary_filename.clone().into()),
308306
};
309307
std::fs::write(&dst, &contents).with_context(|| format!("failed to write file {dst:?}"))?;
310-
Ok((name, module_type, filename))
308+
let mut ret = json::WasmFile {
309+
module_type,
310+
filename,
311+
binary_filename: None,
312+
};
313+
if module_type == "text" && !malformed {
314+
if let Ok(bytes) = module.encode() {
315+
std::fs::write(&binary_dst, &bytes)
316+
.with_context(|| format!("failed to write file {binary_dst:?}"))?;
317+
ret.binary_filename = Some(binary_filename);
318+
}
319+
}
320+
Ok((name, ret))
311321
}
312322

313323
fn action(&self, exec: WastExecute<'a>) -> Result<json::Action<'a>> {
@@ -565,13 +575,15 @@ mod json {
565575
line: u32,
566576
#[serde(skip_serializing_if = "Option::is_none")]
567577
name: Option<String>,
568-
filename: String,
578+
#[serde(flatten)]
579+
file: WasmFile,
569580
},
570581
ModuleDefinition {
571582
line: u32,
572583
#[serde(skip_serializing_if = "Option::is_none")]
573584
name: Option<String>,
574-
filename: String,
585+
#[serde(flatten)]
586+
file: WasmFile,
575587
},
576588
ModuleInstance {
577589
line: u32,
@@ -582,15 +594,15 @@ mod json {
582594
},
583595
AssertMalformed {
584596
line: u32,
585-
filename: String,
597+
#[serde(flatten)]
598+
file: WasmFile,
586599
text: &'a str,
587-
module_type: &'a str,
588600
},
589601
AssertInvalid {
590602
line: u32,
591-
filename: String,
603+
#[serde(flatten)]
604+
file: WasmFile,
592605
text: &'a str,
593-
module_type: &'a str,
594606
},
595607
Register {
596608
line: u32,
@@ -601,9 +613,9 @@ mod json {
601613
},
602614
AssertUnlinkable {
603615
line: u32,
604-
filename: String,
616+
#[serde(flatten)]
617+
file: WasmFile,
605618
text: &'a str,
606-
module_type: &'a str,
607619
},
608620
AssertReturn {
609621
line: u32,
@@ -630,9 +642,9 @@ mod json {
630642
},
631643
AssertUninstantiable {
632644
line: u32,
633-
filename: String,
645+
#[serde(flatten)]
646+
file: WasmFile,
634647
text: &'a str,
635-
module_type: &'a str,
636648
},
637649
Thread {
638650
line: u32,
@@ -647,6 +659,14 @@ mod json {
647659
},
648660
}
649661

662+
#[derive(Serialize)]
663+
pub struct WasmFile {
664+
pub filename: String,
665+
pub module_type: &'static str,
666+
#[serde(skip_serializing_if = "Option::is_none")]
667+
pub binary_filename: Option<String>,
668+
}
669+
650670
#[derive(Serialize)]
651671
#[serde(tag = "type", rename_all = "snake_case")]
652672
pub enum Action<'a> {

tests/snapshots/local/atomics.wast.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
{
55
"type": "module",
66
"line": 1,
7-
"filename": "atomics.0.wasm"
7+
"filename": "atomics.0.wasm",
8+
"module_type": "binary"
89
},
910
{
1011
"type": "module",
1112
"line": 6,
12-
"filename": "atomics.1.wasm"
13+
"filename": "atomics.1.wasm",
14+
"module_type": "binary"
1315
},
1416
{
1517
"type": "assert_invalid",
1618
"line": 16,
1719
"filename": "atomics.2.wasm",
18-
"text": "unknown memory 1",
19-
"module_type": "binary"
20+
"module_type": "binary",
21+
"text": "unknown memory 1"
2022
}
2123
]
2224
}

tests/snapshots/local/bad-nan.wast.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
"type": "assert_malformed",
66
"line": 2,
77
"filename": "bad-nan.0.wat",
8-
"text": "constant out of range",
9-
"module_type": "text"
8+
"module_type": "text",
9+
"text": "constant out of range"
1010
},
1111
{
1212
"type": "assert_malformed",
1313
"line": 6,
1414
"filename": "bad-nan.1.wat",
15-
"text": "constant out of range",
16-
"module_type": "text"
15+
"module_type": "text",
16+
"text": "constant out of range"
1717
},
1818
{
1919
"type": "assert_malformed",
2020
"line": 10,
2121
"filename": "bad-nan.2.wat",
22-
"text": "constant out of range",
23-
"module_type": "text"
22+
"module_type": "text",
23+
"text": "constant out of range"
2424
}
2525
]
2626
}

tests/snapshots/local/branch-hinting/branch-hinting-simple.wast.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,47 @@
44
{
55
"type": "module",
66
"line": 1,
7-
"filename": "branch-hinting-simple.0.wasm"
7+
"filename": "branch-hinting-simple.0.wasm",
8+
"module_type": "binary"
89
},
910
{
1011
"type": "module",
1112
"line": 26,
12-
"filename": "branch-hinting-simple.1.wasm"
13+
"filename": "branch-hinting-simple.1.wasm",
14+
"module_type": "binary"
1315
},
1416
{
1517
"type": "module",
1618
"line": 37,
17-
"filename": "branch-hinting-simple.2.wasm"
19+
"filename": "branch-hinting-simple.2.wasm",
20+
"module_type": "binary"
1821
},
1922
{
2023
"type": "assert_invalid",
2124
"line": 62,
2225
"filename": "branch-hinting-simple.3.wat",
23-
"text": "expected valid module field",
24-
"module_type": "text"
26+
"module_type": "text",
27+
"text": "expected valid module field"
2528
},
2629
{
2730
"type": "assert_invalid",
2831
"line": 65,
2932
"filename": "branch-hinting-simple.4.wat",
30-
"text": "expected a string",
31-
"module_type": "text"
33+
"module_type": "text",
34+
"text": "expected a string"
3235
},
3336
{
3437
"type": "assert_invalid",
3538
"line": 68,
3639
"filename": "branch-hinting-simple.5.wat",
37-
"text": "invalid value for branch hint",
38-
"module_type": "text"
40+
"module_type": "text",
41+
"text": "invalid value for branch hint"
3942
},
4043
{
4144
"type": "module",
4245
"line": 72,
43-
"filename": "branch-hinting-simple.6.wasm"
46+
"filename": "branch-hinting-simple.6.wasm",
47+
"module_type": "binary"
4448
}
4549
]
4650
}

tests/snapshots/local/code-after-end.wast.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,50 @@
55
"type": "assert_invalid",
66
"line": 2,
77
"filename": "code-after-end.0.wasm",
8-
"text": "operators remaining after end of function",
9-
"module_type": "binary"
8+
"module_type": "binary",
9+
"text": "operators remaining after end of function"
1010
},
1111
{
1212
"type": "assert_invalid",
1313
"line": 7,
1414
"filename": "code-after-end.1.wasm",
15-
"text": "operators remaining after end of function",
16-
"module_type": "binary"
15+
"module_type": "binary",
16+
"text": "operators remaining after end of function"
1717
},
1818
{
1919
"type": "assert_invalid",
2020
"line": 12,
2121
"filename": "code-after-end.2.wasm",
22-
"text": "operators remaining after end of function",
23-
"module_type": "binary"
22+
"module_type": "binary",
23+
"text": "operators remaining after end of function"
2424
},
2525
{
2626
"type": "assert_invalid",
2727
"line": 17,
2828
"filename": "code-after-end.3.wasm",
29-
"text": "operators remaining after end of function",
30-
"module_type": "binary"
29+
"module_type": "binary",
30+
"text": "operators remaining after end of function"
3131
},
3232
{
3333
"type": "assert_invalid",
3434
"line": 22,
3535
"filename": "code-after-end.4.wasm",
36-
"text": "operators remaining after end of function",
37-
"module_type": "binary"
36+
"module_type": "binary",
37+
"text": "operators remaining after end of function"
3838
},
3939
{
4040
"type": "assert_invalid",
4141
"line": 27,
4242
"filename": "code-after-end.5.wasm",
43-
"text": "operators remaining after end of function",
44-
"module_type": "binary"
43+
"module_type": "binary",
44+
"text": "operators remaining after end of function"
4545
},
4646
{
4747
"type": "assert_invalid",
4848
"line": 32,
4949
"filename": "code-after-end.6.wasm",
50-
"text": "operators remaining after end of function",
51-
"module_type": "binary"
50+
"module_type": "binary",
51+
"text": "operators remaining after end of function"
5252
}
5353
]
5454
}

tests/snapshots/local/component-model/a.wast.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
{
55
"type": "module",
66
"line": 4,
7-
"filename": "a.0.wasm"
7+
"filename": "a.0.wasm",
8+
"module_type": "binary"
89
}
910
]
1011
}

0 commit comments

Comments
 (0)