diff --git a/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js b/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js index 1653a9971200..036504131b1a 100644 --- a/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js +++ b/lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js @@ -203,13 +203,14 @@ setReadOnly( WasmModule.prototype, 'initialize', function initialize() { * Callback invoked upon fulfilling a promise. * * @private - * @param {Object} module - WebAssembly module - * @param {Object} instance - WebAssembly instance + * @param {Object} result - WebAssembly instantiation result + * @param {Object} result.module - WebAssembly module + * @param {Object} result.instance - WebAssembly instance * @returns {void} */ - function onResolve( module, instance ) { - self._module = module; - self._instance = instance; + function onResolve( result ) { + self._module = result.module; + self._instance = result.instance; resolve( self ); } @@ -254,13 +255,14 @@ setReadOnly( WasmModule.prototype, 'initializeAsync', function initializeAsync( * Callback invoked upon fulfilling a promise. * * @private - * @param {Object} module - WebAssembly module - * @param {Object} instance - WebAssembly instance + * @param {Object} result - WebAssembly instantiation result + * @param {Object} result.module - WebAssembly module + * @param {Object} result.instance - WebAssembly instance * @returns {void} */ - function onResolve( module, instance ) { - self._module = module; - self._instance = instance; + function onResolve( result ) { + self._module = result.module; + self._instance = result.instance; clbk( null, self ); } diff --git a/lib/node_modules/@stdlib/wasm/module-wrapper/test/test.js b/lib/node_modules/@stdlib/wasm/module-wrapper/test/test.js index d505ebcb4a19..c530d2b148ba 100644 --- a/lib/node_modules/@stdlib/wasm/module-wrapper/test/test.js +++ b/lib/node_modules/@stdlib/wasm/module-wrapper/test/test.js @@ -67,9 +67,107 @@ tape( 'if an environment does not support `WebAssembly`, the function throws an }); tape( 'the function is a constructor', opts, function test( t ) { - // TODO: write tests + var wasm; + var mod; + wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] ); + mod = new Module( wasm, null ); + + t.strictEqual( mod instanceof Module, true, 'returns an instance' ); t.end(); }); -// TODO: add tests +tape( 'the `initialize` method returns a promise', opts, function test( t ) { + var wasm; + var mod; + var p; + + wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] ); + mod = new Module( wasm, null ); + p = mod.initialize(); + + t.strictEqual( typeof p.then, 'function', 'returns a promise' ); + t.end(); +}); + +tape( 'the `initialize` method resolves with the module instance', opts, function test( t ) { + var wasm; + var mod; + + wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] ); + mod = new Module( wasm, null ); + + mod.initialize().then( onResolve, onReject ); + + function onResolve( result ) { + t.strictEqual( result, mod, 'resolves with module instance' ); + t.strictEqual( typeof result.exports, 'object', 'exports is accessible after initialization' ); + t.end(); + } + + function onReject( error ) { + t.fail( 'should not reject: ' + error.message ); + t.end(); + } +}); + +tape( 'the `initializeAsync` method invokes a callback', opts, function test( t ) { + var wasm; + var mod; + + wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] ); + mod = new Module( wasm, null ); + + mod.initializeAsync( clbk ); + + function clbk( error ) { + if ( error ) { + t.fail( 'callback received an error: ' + error.message ); + } else { + t.pass( 'callback invoked without error' ); + } + t.end(); + } +}); + +tape( 'the `initializeAsync` method invokes callback with the module instance', opts, function test( t ) { + var wasm; + var mod; + + wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] ); + mod = new Module( wasm, null ); + + mod.initializeAsync( clbk ); + + function clbk( error, result ) { + if ( error ) { + t.fail( 'callback received an error: ' + error.message ); + t.end(); + return; + } + t.strictEqual( result, mod, 'callback receives module instance' ); + t.strictEqual( typeof result.exports, 'object', 'exports is accessible after initialization' ); + t.end(); + } +}); + +tape( 'the `exports` property returns instance exports after initialization', opts, function test( t ) { + var wasm; + var mod; + + wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] ); + mod = new Module( wasm, null ); + + mod.initialize().then( onResolve, onReject ); + + function onResolve( result ) { + var exports = result.exports; + t.strictEqual( typeof exports, 'object', 'exports is an object' ); + t.end(); + } + + function onReject( error ) { + t.fail( 'should not reject: ' + error.message ); + t.end(); + } +});