diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 403960dba3c378..965a619c8d4acd 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -1645,7 +1645,8 @@ void WASI::_SetMemory(const FunctionCallbackInfo& args) { if (!args[0]->IsWasmMemoryObject()) { return node::THROW_ERR_INVALID_ARG_TYPE( wasi->env(), - "instance.exports.memory must be a WebAssembly.Memory object"); + "\"instance.exports.memory\" property must be a WebAssembly.Memory " + "object"); } wasi->memory_.Reset(wasi->env()->isolate(), args[0].As()); } diff --git a/test/wasi/test-wasi-initialize-validation.js b/test/wasi/test-wasi-initialize-validation.js index 40dfd864d1874e..044db1c0df46c7 100644 --- a/test/wasi/test-wasi-initialize-validation.js +++ b/test/wasi/test-wasi-initialize-validation.js @@ -47,7 +47,10 @@ const bufferSource = fixtures.readSync('simple.wasm'); Object.defineProperty(instance, 'exports', { get() { - return { _initialize: 5, memory: new Uint8Array() }; + return { + _initialize: 5, + memory: new WebAssembly.Memory({ initial: 1 }), + }; }, }); assert.throws( @@ -70,7 +73,7 @@ const bufferSource = fixtures.readSync('simple.wasm'); return { _start() {}, _initialize() {}, - memory: new Uint8Array(), + memory: new WebAssembly.Memory({ initial: 1 }), }; } }); @@ -97,55 +100,11 @@ const bufferSource = fixtures.readSync('simple.wasm'); () => { wasi.initialize(instance); }, { code: 'ERR_INVALID_ARG_TYPE', - message: /"instance\.exports\.memory" property must be of type object/ + message: /"instance\.exports\.memory" property must be a WebAssembly.Memory object/ } ); } - { - // Verify that a non-ArrayBuffer memory.buffer is rejected. - const wasi = new WASI({}); - const wasm = await WebAssembly.compile(bufferSource); - const instance = await WebAssembly.instantiate(wasm); - - Object.defineProperty(instance, 'exports', { - get() { - return { - _initialize() {}, - memory: {}, - }; - } - }); - // The error message is a little white lie because any object - // with a .buffer property of type ArrayBuffer is accepted, - // but 99% of the time a WebAssembly.Memory object is used. - assert.throws( - () => { wasi.initialize(instance); }, - { - code: 'ERR_INVALID_ARG_TYPE', - message: /"instance\.exports\.memory\.buffer" property must be an WebAssembly\.Memory/ - } - ); - } - - { - // Verify that an argument that duck-types as a WebAssembly.Instance - // is accepted. - const wasi = new WASI({}); - const wasm = await WebAssembly.compile(bufferSource); - const instance = await WebAssembly.instantiate(wasm); - - Object.defineProperty(instance, 'exports', { - get() { - return { - _initialize() {}, - memory: { buffer: new ArrayBuffer(0) }, - }; - } - }); - wasi.initialize(instance); - } - { // Verify that a WebAssembly.Instance from another VM context is accepted. const wasi = new WASI({}); diff --git a/test/wasi/test-wasi-start-validation.js b/test/wasi/test-wasi-start-validation.js index 2059ff081e88dd..6e8d34d0f9e85b 100644 --- a/test/wasi/test-wasi-start-validation.js +++ b/test/wasi/test-wasi-start-validation.js @@ -47,7 +47,7 @@ const bufferSource = fixtures.readSync('simple.wasm'); Object.defineProperty(instance, 'exports', { get() { - return { memory: new Uint8Array() }; + return { memory: new WebAssembly.Memory({ initial: 1 }) }; }, }); assert.throws( @@ -70,7 +70,7 @@ const bufferSource = fixtures.readSync('simple.wasm'); return { _start() {}, _initialize() {}, - memory: new Uint8Array(), + memory: new WebAssembly.Memory({ initial: 1 }), }; } }); @@ -97,55 +97,11 @@ const bufferSource = fixtures.readSync('simple.wasm'); () => { wasi.start(instance); }, { code: 'ERR_INVALID_ARG_TYPE', - message: /"instance\.exports\.memory" property must be of type object/ + message: /"instance\.exports\.memory" property must be a WebAssembly.Memory object/ } ); } - { - // Verify that a non-ArrayBuffer memory.buffer is rejected. - const wasi = new WASI({}); - const wasm = await WebAssembly.compile(bufferSource); - const instance = await WebAssembly.instantiate(wasm); - - Object.defineProperty(instance, 'exports', { - get() { - return { - _start() {}, - memory: {}, - }; - } - }); - // The error message is a little white lie because any object - // with a .buffer property of type ArrayBuffer is accepted, - // but 99% of the time a WebAssembly.Memory object is used. - assert.throws( - () => { wasi.start(instance); }, - { - code: 'ERR_INVALID_ARG_TYPE', - message: /"instance\.exports\.memory\.buffer" property must be an WebAssembly\.Memory/ - } - ); - } - - { - // Verify that an argument that duck-types as a WebAssembly.Instance - // is accepted. - const wasi = new WASI({}); - const wasm = await WebAssembly.compile(bufferSource); - const instance = await WebAssembly.instantiate(wasm); - - Object.defineProperty(instance, 'exports', { - get() { - return { - _start() {}, - memory: { buffer: new ArrayBuffer(0) }, - }; - } - }); - wasi.start(instance); - } - { // Verify that a WebAssembly.Instance from another VM context is accepted. const wasi = new WASI({});