Skip to content

Commit 09ca76b

Browse files
cjihrigMylesBorins
authored andcommitted
test: verify that WASI errors are rethrown
This commit adds a test to verify that exceptions thrown from a WASI application are properly caught and rethrown. This also gets the code coverage in lib/wasi.js back to 100%. PR-URL: #32157 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3e9012a commit 09ca76b

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

test/wasi/test-return-on-exit.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@ const assert = require('assert');
55
const fs = require('fs');
66
const path = require('path');
77
const { WASI } = require('wasi');
8-
const wasi = new WASI({ returnOnExit: true });
9-
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
108
const wasmDir = path.join(__dirname, 'wasm');
119
const modulePath = path.join(wasmDir, 'exitcode.wasm');
1210
const buffer = fs.readFileSync(modulePath);
1311

1412
(async () => {
13+
const wasi = new WASI({ returnOnExit: true });
14+
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
1515
const { instance } = await WebAssembly.instantiate(buffer, importObject);
1616

1717
assert.strictEqual(wasi.start(instance), 120);
1818
})().then(common.mustCall());
19+
20+
(async () => {
21+
// Verify that if a WASI application throws an exception, Node rethrows it
22+
// properly.
23+
const wasi = new WASI({ returnOnExit: true });
24+
wasi.wasiImport.proc_exit = () => { throw new Error('test error'); };
25+
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
26+
const { instance } = await WebAssembly.instantiate(buffer, importObject);
27+
28+
assert.throws(() => {
29+
wasi.start(instance);
30+
}, /^Error: test error$/);
31+
})().then(common.mustCall());

0 commit comments

Comments
 (0)