• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --experimental-wasi-unstable-preview1 --experimental-wasm-bigint
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const fs = require('fs');
6const path = require('path');
7const { WASI } = require('wasi');
8const wasmDir = path.join(__dirname, 'wasm');
9const modulePath = path.join(wasmDir, 'exitcode.wasm');
10const buffer = fs.readFileSync(modulePath);
11
12(async () => {
13  const wasi = new WASI({ returnOnExit: true });
14  const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
15  const { instance } = await WebAssembly.instantiate(buffer, importObject);
16
17  assert.strictEqual(wasi.start(instance), 120);
18})().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  const patchedExit = () => { throw new Error('test error'); };
25  wasi.wasiImport.proc_exit = patchedExit.bind(wasi.wasiImport);
26  const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
27  const { instance } = await WebAssembly.instantiate(buffer, importObject);
28
29  assert.throws(() => {
30    wasi.start(instance);
31  }, /^Error: test error$/);
32})().then(common.mustCall());
33