summaryrefslogtreecommitdiff
path: root/test/wasi/test-wasi-start-validation.js
blob: 3f530a42f791f8b5007f160ede1a8672997bf80f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Flags: --experimental-wasi-unstable-preview0
'use strict';

const common = require('../common');
const assert = require('assert');
const { WASI } = require('wasi');

const fixtures = require('../common/fixtures');

{
  const wasi = new WASI();
  assert.throws(
    () => {
      wasi.start();
    },
    { code: 'ERR_INVALID_ARG_TYPE', message: /\bWebAssembly\.Instance\b/ }
  );
}

{
  const wasi = new WASI({});
  (async () => {
    const bufferSource = fixtures.readSync('simple.wasm');
    const wasm = await WebAssembly.compile(bufferSource);
    const instance = await WebAssembly.instantiate(wasm);

    assert.throws(
      () => { wasi.start(instance); },
      { code: 'ERR_INVALID_ARG_TYPE', message: /\bWebAssembly\.Memory\b/ }
    );
  })();
}

(async () => {
  const wasi = new WASI();
  const bufferSource = fixtures.readSync('simple.wasm');
  const wasm = await WebAssembly.compile(bufferSource);
  const instance = await WebAssembly.instantiate(wasm);
  const values = [undefined, null, 'foo', 42, true, false, () => {}];
  let cnt = 0;

  // Mock instance.exports to trigger start() validation.
  Object.defineProperty(instance, 'exports', {
    get() { return values[cnt++]; }
  });

  values.forEach((val) => {
    assert.throws(
      () => { wasi.start(instance); },
      { code: 'ERR_INVALID_ARG_TYPE', message: /\binstance\.exports\b/ }
    );
  });
})();

(async () => {
  const wasi = new WASI();
  const bufferSource = fixtures.readSync('simple.wasm');
  const wasm = await WebAssembly.compile(bufferSource);
  const instance = await WebAssembly.instantiate(wasm);

  // Mock instance.exports.memory to bypass start() validation.
  Object.defineProperty(instance, 'exports', {
    get() {
      return {
        memory: new WebAssembly.Memory({ initial: 1 })
      };
    }
  });

  wasi.start(instance);
  assert.throws(
    () => { wasi.start(instance); },
    {
      code: 'ERR_WASI_ALREADY_STARTED',
      message: /^WASI instance has already started$/
    }
  );
})();

(async () => {
  const wasi = new WASI();
  const bufferSource = fixtures.readSync('simple.wasm');
  const wasm = await WebAssembly.compile(bufferSource);
  const instance = await WebAssembly.instantiate(wasm);

  // Mock instance.exports to bypass start() validation.
  Object.defineProperty(instance, 'exports', {
    get() {
      return {
        memory: new WebAssembly.Memory({ initial: 1 }),
        __wasi_unstable_reactor_start: common.mustCall()
      };
    }
  });

  wasi.start(instance);
})();