1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const fs = require('fs'); 5const path = require('path'); 6const { WASI } = require('wasi'); 7const wasmDir = path.join(__dirname, 'wasm'); 8const modulePath = path.join(wasmDir, 'exitcode.wasm'); 9const buffer = fs.readFileSync(modulePath); 10 11(async () => { 12 const wasi = new WASI({ returnOnExit: true }); 13 const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; 14 const { instance } = await WebAssembly.instantiate(buffer, importObject); 15 16 assert.strictEqual(wasi.start(instance), 120); 17})().then(common.mustCall()); 18 19(async () => { 20 // Verify that if a WASI application throws an exception, Node rethrows it 21 // properly. 22 const wasi = new WASI({ returnOnExit: true }); 23 const patchedExit = () => { throw new Error('test error'); }; 24 wasi.wasiImport.proc_exit = patchedExit.bind(wasi.wasiImport); 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()); 32