Skip to content

Commit

Permalink
test: verify that WASI errors are rethrown
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig committed Mar 13, 2020
1 parent 605615e commit ec204d8
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test/wasi/test-return-on-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { WASI } = require('wasi');
const wasi = new WASI({ returnOnExit: true });
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
const wasmDir = path.join(__dirname, 'wasm');
const modulePath = path.join(wasmDir, 'exitcode.wasm');
const buffer = fs.readFileSync(modulePath);

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

assert.strictEqual(wasi.start(instance), 120);
})().then(common.mustCall());

(async () => {
// Verify that if a WASI application throws an exception, Node rethrows it
// properly.
const wasi = new WASI({ returnOnExit: true });
wasi.wasiImport.proc_exit = () => { throw new Error('test error'); };
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
const { instance } = await WebAssembly.instantiate(buffer, importObject);

assert.throws(() => {
wasi.start(instance);
}, /^Error: test error$/);
})().then(common.mustCall());

0 comments on commit ec204d8

Please sign in to comment.