Skip to content

Commit a1443d1

Browse files
shiavm006kgryte
andauthored
fix: use correct WebAssembly.instantiate callback signature in wasm/module-wrapper
PR-URL: stdlib-js#9720 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent f6efb5e commit a1443d1

File tree

2 files changed

+112
-12
lines changed

2 files changed

+112
-12
lines changed

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: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,107 @@ 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 wasm;
71+
var mod;
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 expected value' );
7277
t.end();
7378
});
7479

75-
// TODO: add tests
80+
tape( 'the function returns an instance having an `initialize` method which returns a promise', opts, function test( t ) {
81+
var wasm;
82+
var mod;
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 expected value' );
90+
t.end();
91+
});
92+
93+
tape( 'the function returns an instance having an `initialize` method which returns a promise resolving with the module instance', opts, function test( t ) {
94+
var wasm;
95+
var mod;
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 expected value' );
104+
t.strictEqual( typeof result.exports, 'object', 'returns expected value' );
105+
t.end();
106+
}
107+
108+
function onReject( error ) {
109+
t.fail( 'should not reject: ' + error.message );
110+
t.end();
111+
}
112+
});
113+
114+
tape( 'the function returns an instance having an `initializeAsync` method which invokes a callback', opts, function test( t ) {
115+
var wasm;
116+
var mod;
117+
118+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
119+
mod = new Module( wasm, null );
120+
121+
mod.initializeAsync( clbk );
122+
123+
function clbk( error ) {
124+
if ( error ) {
125+
t.fail( 'callback received an error: ' + error.message );
126+
} else {
127+
t.pass( 'callback invoked without error' );
128+
}
129+
t.end();
130+
}
131+
});
132+
133+
tape( 'the function returns an instance having an `initializeAsync` method which invokes a callback with the module instance', opts, function test( t ) {
134+
var wasm;
135+
var mod;
136+
137+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
138+
mod = new Module( wasm, null );
139+
140+
mod.initializeAsync( clbk );
141+
142+
function clbk( error, result ) {
143+
if ( error ) {
144+
t.fail( 'callback received an error: ' + error.message );
145+
t.end();
146+
return;
147+
}
148+
t.strictEqual( result, mod, 'returns expected value' );
149+
t.strictEqual( typeof result.exports, 'object', 'returns expected value' );
150+
t.end();
151+
}
152+
});
153+
154+
tape( 'the function returns an instance having an `exports` property which is available after initialization', opts, function test( t ) {
155+
var wasm;
156+
var mod;
157+
158+
wasm = new Uint8Array( [ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00 ] );
159+
mod = new Module( wasm, null );
160+
161+
mod.initialize().then( onResolve, onReject );
162+
163+
function onResolve( result ) {
164+
var exports = result.exports;
165+
t.strictEqual( typeof exports, 'object', 'returns expected value' );
166+
t.end();
167+
}
168+
169+
function onReject( error ) {
170+
t.fail( 'should not reject: ' + error.message );
171+
t.end();
172+
}
173+
});

0 commit comments

Comments
 (0)