Skip to content

Commit 25301a3

Browse files
committed
fix: correct WebAssembly.instantiate callback signature in module-wrapper
1 parent 6aee8e2 commit 25301a3

2 files changed

Lines changed: 118 additions & 12 deletions

File tree

lib/node_modules/@stdlib/wasm/module-wrapper/lib/main.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,14 @@ setReadOnly( WasmModule.prototype, 'initialize', function initialize() {
203203
* Callback invoked upon fulfilling a promise.
204204
*
205205
* @private
206-
* @param {Object} module - WebAssembly module
207-
* @param {Object} instance - WebAssembly instance
206+
* @param {Object} result - WebAssembly instantiation result
207+
* @param {Object} result.module - WebAssembly module
208+
* @param {Object} result.instance - WebAssembly instance
208209
* @returns {void}
209210
*/
210-
function onResolve( module, instance ) {
211-
self._module = module;
212-
self._instance = instance;
211+
function onResolve( result ) {
212+
self._module = result.module;
213+
self._instance = result.instance;
213214
resolve( self );
214215
}
215216

@@ -254,13 +255,14 @@ setReadOnly( WasmModule.prototype, 'initializeAsync', function initializeAsync(
254255
* Callback invoked upon fulfilling a promise.
255256
*
256257
* @private
257-
* @param {Object} module - WebAssembly module
258-
* @param {Object} instance - WebAssembly instance
258+
* @param {Object} result - WebAssembly instantiation result
259+
* @param {Object} result.module - WebAssembly module
260+
* @param {Object} result.instance - WebAssembly instance
259261
* @returns {void}
260262
*/
261-
function onResolve( module, instance ) {
262-
self._module = module;
263-
self._instance = instance;
263+
function onResolve( result ) {
264+
self._module = result.module;
265+
self._instance = result.instance;
264266
clbk( null, self );
265267
}
266268

lib/node_modules/@stdlib/wasm/module-wrapper/test/test.js

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,113 @@ tape( 'if an environment does not support `WebAssembly`, the function throws an
6767
});
6868

6969
tape( 'the function is a constructor', opts, function test( t ) {
70-
// TODO: write tests
70+
var mod;
71+
var wasm;
7172

73+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
74+
mod = new Module( wasm, null );
75+
76+
t.strictEqual( mod instanceof Module, true, 'returns an instance' );
77+
t.end();
78+
});
79+
80+
tape( 'the `initialize` method returns a promise', opts, function test( t ) {
81+
var mod;
82+
var wasm;
83+
var p;
84+
85+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
86+
mod = new Module( wasm, null );
87+
p = mod.initialize();
88+
89+
t.strictEqual( typeof p.then, 'function', 'returns a promise' );
7290
t.end();
7391
});
7492

75-
// TODO: add tests
93+
tape( 'the `initialize` method properly sets `_module` and `_instance` properties', opts, function test( t ) {
94+
var mod;
95+
var wasm;
96+
97+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
98+
mod = new Module( wasm, null );
99+
100+
mod.initialize().then( onResolve, onReject );
101+
102+
function onResolve( result ) {
103+
t.strictEqual( result, mod, 'resolves with module instance' );
104+
t.strictEqual( typeof result._module, 'object', '_module is set' );
105+
t.strictEqual( typeof result._instance, 'object', '_instance is set' );
106+
t.strictEqual( result._module.constructor.name, 'Module', '_module is a WebAssembly.Module' );
107+
t.strictEqual( result._instance.constructor.name, 'Instance', '_instance is a WebAssembly.Instance' );
108+
t.end();
109+
}
110+
111+
function onReject( error ) {
112+
t.fail( 'should not reject: ' + error.message );
113+
t.end();
114+
}
115+
});
116+
117+
tape( 'the `initializeAsync` method invokes a callback', opts, function test( t ) {
118+
var mod;
119+
var wasm;
120+
121+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
122+
mod = new Module( wasm, null );
123+
124+
mod.initializeAsync( clbk );
125+
126+
function clbk( error, result ) {
127+
if ( error ) {
128+
t.fail( 'callback received an error: ' + error.message );
129+
} else {
130+
t.pass( 'callback invoked without error' );
131+
}
132+
t.end();
133+
}
134+
});
135+
136+
tape( 'the `initializeAsync` method properly sets `_module` and `_instance` properties', opts, function test( t ) {
137+
var mod;
138+
var wasm;
139+
140+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
141+
mod = new Module( wasm, null );
142+
143+
mod.initializeAsync( clbk );
144+
145+
function clbk( error, result ) {
146+
if ( error ) {
147+
t.fail( 'callback received an error: ' + error.message );
148+
t.end();
149+
return;
150+
}
151+
t.strictEqual( result, mod, 'callback receives module instance' );
152+
t.strictEqual( typeof result._module, 'object', '_module is set' );
153+
t.strictEqual( typeof result._instance, 'object', '_instance is set' );
154+
t.strictEqual( result._module.constructor.name, 'Module', '_module is a WebAssembly.Module' );
155+
t.strictEqual( result._instance.constructor.name, 'Instance', '_instance is a WebAssembly.Instance' );
156+
t.end();
157+
}
158+
});
159+
160+
tape( 'the `exports` property returns instance exports after initialization', opts, function test( t ) {
161+
var mod;
162+
var wasm;
163+
164+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
165+
mod = new Module( wasm, null );
166+
167+
mod.initialize().then( onResolve, onReject );
168+
169+
function onResolve( result ) {
170+
var exports = result.exports;
171+
t.strictEqual( typeof exports, 'object', 'exports is an object' );
172+
t.end();
173+
}
174+
175+
function onReject( error ) {
176+
t.fail( 'should not reject: ' + error.message );
177+
t.end();
178+
}
179+
});

0 commit comments

Comments
 (0)