Skip to content

Commit c2d9552

Browse files
cjihrigMylesBorins
authored andcommitted
test: improve WASI start() coverage
This commit adds additional test cases to test-wasi-start-validation.js, which gets the JS test coverage of start() to 100%. PR-URL: #30972 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 72b4aee commit c2d9552

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

test/wasi/test-wasi-start-validation.js

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Flags: --experimental-wasi-unstable-preview0
22
'use strict';
33

4-
require('../common');
4+
const common = require('../common');
55
const assert = require('assert');
66
const { WASI } = require('wasi');
77

@@ -30,3 +30,68 @@ const fixtures = require('../common/fixtures');
3030
);
3131
})();
3232
}
33+
34+
(async () => {
35+
const wasi = new WASI();
36+
const bufferSource = fixtures.readSync('simple.wasm');
37+
const wasm = await WebAssembly.compile(bufferSource);
38+
const instance = await WebAssembly.instantiate(wasm);
39+
const values = [undefined, null, 'foo', 42, true, false, () => {}];
40+
let cnt = 0;
41+
42+
// Mock instance.exports to trigger start() validation.
43+
Object.defineProperty(instance, 'exports', {
44+
get() { return values[cnt++]; }
45+
});
46+
47+
values.forEach((val) => {
48+
assert.throws(
49+
() => { wasi.start(instance); },
50+
{ code: 'ERR_INVALID_ARG_TYPE', message: /\binstance\.exports\b/ }
51+
);
52+
});
53+
})();
54+
55+
(async () => {
56+
const wasi = new WASI();
57+
const bufferSource = fixtures.readSync('simple.wasm');
58+
const wasm = await WebAssembly.compile(bufferSource);
59+
const instance = await WebAssembly.instantiate(wasm);
60+
61+
// Mock instance.exports.memory to bypass start() validation.
62+
Object.defineProperty(instance, 'exports', {
63+
get() {
64+
return {
65+
memory: new WebAssembly.Memory({ initial: 1 })
66+
};
67+
}
68+
});
69+
70+
wasi.start(instance);
71+
assert.throws(
72+
() => { wasi.start(instance); },
73+
{
74+
code: 'ERR_WASI_ALREADY_STARTED',
75+
message: /^WASI instance has already started$/
76+
}
77+
);
78+
})();
79+
80+
(async () => {
81+
const wasi = new WASI();
82+
const bufferSource = fixtures.readSync('simple.wasm');
83+
const wasm = await WebAssembly.compile(bufferSource);
84+
const instance = await WebAssembly.instantiate(wasm);
85+
86+
// Mock instance.exports to bypass start() validation.
87+
Object.defineProperty(instance, 'exports', {
88+
get() {
89+
return {
90+
memory: new WebAssembly.Memory({ initial: 1 }),
91+
__wasi_unstable_reactor_start: common.mustCall()
92+
};
93+
}
94+
});
95+
96+
wasi.start(instance);
97+
})();

0 commit comments

Comments
 (0)