2019-12-15 17:30:45 +01:00
|
|
|
// Flags: --experimental-wasi-unstable-preview1
|
2019-12-12 20:20:46 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-12-15 02:59:04 +01:00
|
|
|
const common = require('../common');
|
2019-12-12 20:20:46 +01:00
|
|
|
const assert = require('assert');
|
|
|
|
const { WASI } = require('wasi');
|
|
|
|
|
|
|
|
const fixtures = require('../common/fixtures');
|
2019-12-15 03:05:27 +01:00
|
|
|
const bufferSource = fixtures.readSync('simple.wasm');
|
2019-12-12 20:20:46 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
const wasi = new WASI();
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
wasi.start();
|
|
|
|
},
|
|
|
|
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bWebAssembly\.Instance\b/ }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-15 03:05:27 +01:00
|
|
|
(async () => {
|
2019-12-12 20:20:46 +01:00
|
|
|
const wasi = new WASI({});
|
2019-12-15 03:05:27 +01:00
|
|
|
const wasm = await WebAssembly.compile(bufferSource);
|
|
|
|
const instance = await WebAssembly.instantiate(wasm);
|
2019-12-12 20:20:46 +01:00
|
|
|
|
2019-12-15 03:05:27 +01:00
|
|
|
assert.throws(
|
|
|
|
() => { wasi.start(instance); },
|
|
|
|
{ code: 'ERR_INVALID_ARG_TYPE', message: /\bWebAssembly\.Memory\b/ }
|
|
|
|
);
|
|
|
|
})();
|
2019-12-15 02:59:04 +01:00
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const wasi = new WASI();
|
|
|
|
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 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 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);
|
|
|
|
})();
|