Skip to content

Commit 4822075

Browse files
authored
Add new check option to ReadWasmOptions, with test. (#53)
* Add new `check` option to `ReadWasmOptions`, with test. * Update README
1 parent ba77069 commit 4822075

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ API
7878

7979
* **readDebugNames**: `boolean`<br />
8080
Reads textual names from the name section.
81+
* **check**: `boolean`<br/>
82+
Check for invalid modules (default: true).
8183

8284
* **ToTextOptions**<br />
8385
Options modifying the behavior of `WasmModule#toText`.

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ interface WasmFeatures { // see: https://github.com/WebAssembly/wabt/blob/main/s
4040
interface ReadWasmOptions {
4141
/** Reads textual names from the name section. */
4242
readDebugNames?: boolean;
43+
44+
/** Check for invalid modules (default is true). */
45+
check?: boolean;
4346
}
4447

4548
/** Options modifying the behavior of `WasmModule#toText`. */

tests/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ require("..")().then(wabt => {
3131
test.end();
3232
});
3333

34+
test("loading an invalid binary module", function (test) {
35+
var buffer = new Uint8Array(
36+
fs.readFileSync(__dirname + "/assembly/module-features.wasm")
37+
);
38+
buffer[buffer.length - 1] = 0x00; // corrupt the last byte
39+
test.throws(function() {
40+
mod = wabt.readWasm(buffer, {});
41+
}, /function body must end with END opcode/);
42+
// Specifying `check: true` allows an invalid module to be loaded.
43+
test.doesNotThrow(function() {
44+
mod = wabt.readWasm(buffer, {
45+
check: false
46+
});
47+
});
48+
test.ok(mod && typeof mod.toBinary === "function", "should return a module");
49+
test.end();
50+
});
51+
3452
test("modifying a module", function(test) {
3553
test.doesNotThrow(function() {
3654
mod.generateNames();

0 commit comments

Comments
 (0)