Skip to content

Commit ff9bd3a

Browse files
authored
Make sure binaryen.js tests validate (#2269)
Without `assert`, even if a test does not validate, the errors will only show up in its corresponding `.txt` file while the test will succeed. This makes sure it errors out when a test fails to validate. This also adds validation checks if there is none.
1 parent be6f7c4 commit ff9bd3a

11 files changed

Lines changed: 61 additions & 12 deletions

File tree

test/binaryen.js/atomics.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
var module = Binaryen.parseText(`
26
(module
37
(memory $0 (shared 1 1))
@@ -60,5 +64,5 @@ module.addFunction("main", signature, [], module.block("", [
6064
]));
6165

6266
module.setFeatures(Binaryen.Features.Atomics);
63-
module.validate();
67+
assert(module.validate());
6468
console.log(module.emitText());

test/binaryen.js/debug-info.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
var wast = `
26
(module
37
(type $v (func))
@@ -15,6 +19,7 @@ var binary = module.emitBinary();
1519
module.dispose();
1620
module = Binaryen.readBinary(binary);
1721
console.log(module.emitText());
22+
assert(module.validate());
1823
module.dispose();
1924

2025
// With debug info
@@ -26,6 +31,7 @@ binary = module.emitBinary();
2631
module.dispose();
2732
module = Binaryen.readBinary(binary);
2833
console.log(module.emitText());
34+
assert(module.validate());
2935
module.dispose();
3036

3137
// Without debug info
@@ -37,4 +43,5 @@ binary = module.emitBinary();
3743
module.dispose();
3844
module = Binaryen.readBinary(binary);
3945
console.log(module.emitText());
46+
assert(module.validate());
4047
module.dispose();

test/binaryen.js/emit_asmjs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
var module = new Binaryen.Module();
26

37
var signature = module.addFunctionType("ii", Binaryen.i32, [ Binaryen.i32 ]);
@@ -6,6 +10,6 @@ module.addFunction("main", signature, [], module.local.get(0, Binaryen.i32));
610

711
module.addFunctionExport("main", "main");
812

9-
module.validate(); // should validate before calling emitAsmjs
13+
assert(module.validate()); // should validate before calling emitAsmjs
1014

1115
console.log(module.emitAsmjs());

test/binaryen.js/event.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
function cleanInfo(info) {
26
var ret = {};
37
for (var x in info) {
@@ -22,11 +26,11 @@ console.log("getEventInfo=" + JSON.stringify(cleanInfo(eventInfo)));
2226
module.addEventExport("a-event", "a-event-exp");
2327
module.addEventImport("a-event-imp", "module", "base", 0, vif);
2428

25-
module.validate();
29+
assert(module.validate());
2630
console.log(module.emitText());
2731

2832
module.removeExport("a-event-exp");
2933
module.removeEvent("a-event");
3034

31-
module.validate();
35+
assert(module.validate());
3236
console.log(module.emitText());

test/binaryen.js/functions.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
function cleanInfo(info) {
26
var ret = {};
37
for (var x in info) {
@@ -33,6 +37,6 @@ console.log(Binaryen.emitText(funcInfo.body));
3337

3438
module.removeFunction("a-function");
3539

36-
module.validate();
40+
assert(module.validate());
3741

3842
console.log(module.emitText());

test/binaryen.js/global.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
function cleanInfo(info) {
26
var ret = {};
37
for (var x in info) {
@@ -25,11 +29,11 @@ console.log(Binaryen.emitText(globalInfo.init));
2529
module.addGlobalExport("a-global", "a-global-exp");
2630
module.addGlobalImport("a-global-imp", "module", "base", Binaryen.i32);
2731

28-
module.validate();
32+
assert(module.validate());
2933
console.log(module.emitText());
3034

3135
module.removeGlobal("a-global");
3236
module.removeExport("a-global-exp");
3337

34-
module.validate();
38+
assert(module.validate());
3539
console.log(module.emitText());

test/binaryen.js/hello-world.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
14

25
// "hello world" type example: create a function that adds two i32s and
36
// returns the result
@@ -38,6 +41,7 @@ console.log('optimized:\n\n' + module.emitText());
3841
var binary = module.emitBinary();
3942
console.log('binary size: ' + binary.length);
4043
console.log();
44+
assert(module.validate());
4145

4246
// We don't need the Binaryen module anymore, so we can tell it to
4347
// clean itself up

test/binaryen.js/optimize-levels.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
var wast = `
26
(module
37
(type $i (func (param i32) (result i32)))
@@ -25,13 +29,13 @@ function printOptions() {
2529
var module = Binaryen.parseText(wast);
2630

2731
console.log("=== unoptimized ===");
28-
module.validate();
32+
assert(module.validate());
2933
console.log(module.emitText());
3034

3135
module.optimize();
3236
console.log("=== optimized using defaults ===");
3337
printOptions();
34-
module.validate();
38+
assert(module.validate());
3539
console.log(module.emitText());
3640
module.dispose();
3741

@@ -43,7 +47,7 @@ Binaryen.setShrinkLevel(0);
4347
module.optimize();
4448
console.log("=== optimized with -O0 ===");
4549
printOptions();
46-
module.validate();
50+
assert(module.validate());
4751
console.log(module.emitText());
4852
module.dispose();
4953

@@ -55,6 +59,6 @@ Binaryen.setShrinkLevel(1);
5559
module.optimize();
5660
console.log("=== optimized with -Os ===");
5761
printOptions();
58-
module.validate();
62+
assert(module.validate());
5963
console.log(module.emitText());
6064
module.dispose();

test/binaryen.js/push-pop.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
function cleanInfo(info) {
26
var ret = {};
37
for (var x in info) {
@@ -28,7 +32,7 @@ var func = module.addFunction("func", v, [],
2832
)
2933
)
3034

31-
module.validate();
35+
assert(module.validate());
3236
console.log(module.emitText());
3337

3438
console.log("getExpressionInfo(i32.pop) = " + stringify(module.i32.pop()));

test/binaryen.js/sourcemap.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
15
var module = new Binaryen.Module();
26

37
var signature = module.addFunctionType("i", Binaryen.i32, []);
@@ -18,6 +22,7 @@ module.setDebugLocation(func, expr, fileIndex, 1, 2);
1822
module.setDebugLocation(func, body, fileIndex, 0, 3);
1923

2024
var output = module.emitBinary("module.wasm.map");
25+
assert(module.validate());
2126

2227
function dumpBinary(buffer) {
2328
var hex = [], o, b, h;

0 commit comments

Comments
 (0)