2020-03-05 02:46:02 +01:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const { WASI } = require('wasi');
|
|
|
|
const wasmDir = path.join(__dirname, 'wasm');
|
|
|
|
const modulePath = path.join(wasmDir, 'exitcode.wasm');
|
|
|
|
const buffer = fs.readFileSync(modulePath);
|
|
|
|
|
|
|
|
(async () => {
|
2023-04-03 23:50:18 +02:00
|
|
|
const wasi = new WASI({ version: 'preview1', returnOnExit: true });
|
2020-03-09 16:20:45 +01:00
|
|
|
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
|
2020-03-05 02:46:02 +01:00
|
|
|
const { instance } = await WebAssembly.instantiate(buffer, importObject);
|
|
|
|
|
|
|
|
assert.strictEqual(wasi.start(instance), 120);
|
|
|
|
})().then(common.mustCall());
|
2020-03-09 16:20:45 +01:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
// Verify that if a WASI application throws an exception, Node rethrows it
|
|
|
|
// properly.
|
2023-04-03 23:50:18 +02:00
|
|
|
const wasi = new WASI({ version: 'preview1', returnOnExit: true });
|
2021-03-01 20:32:22 +01:00
|
|
|
const patchedExit = () => { throw new Error('test error'); };
|
|
|
|
wasi.wasiImport.proc_exit = patchedExit.bind(wasi.wasiImport);
|
2020-03-09 16:20:45 +01:00
|
|
|
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());
|