From efc844d00dfa38d1665e3c3ca770cee6f3d8c613 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 9 Mar 2020 11:20:45 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/32157 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- test/wasi/test-return-on-exit.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/wasi/test-return-on-exit.js b/test/wasi/test-return-on-exit.js index 41a240f055b4b7..3f5d40c9ba7fb8 100644 --- a/test/wasi/test-return-on-exit.js +++ b/test/wasi/test-return-on-exit.js @@ -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());