@@ -43,8 +43,8 @@ const CHUNKSIZE = 1024;
43
43
function getStringImpl ( buffer , ptr ) {
44
44
const U32 = new Uint32Array ( buffer ) ;
45
45
const U16 = new Uint16Array ( buffer ) ;
46
- var length = U32 [ ( ptr + SIZE_OFFSET ) >>> 2 ] >>> 1 ;
47
- var offset = ptr >>> 1 ;
46
+ let length = U32 [ ( ptr + SIZE_OFFSET ) >>> 2 ] >>> 1 ;
47
+ let offset = ptr >>> 1 ;
48
48
if ( length <= CHUNKSIZE ) return String . fromCharCode . apply ( String , U16 . subarray ( offset , offset + length ) ) ;
49
49
const parts = [ ] ;
50
50
do {
@@ -58,7 +58,7 @@ function getStringImpl(buffer, ptr) {
58
58
59
59
/** Prepares the base module prior to instantiation. */
60
60
function preInstantiate ( imports ) {
61
- const baseModule = { } ;
61
+ const extendedExports = { } ;
62
62
63
63
function getString ( memory , ptr ) {
64
64
if ( ! memory ) return "<yet unknown>" ;
@@ -67,31 +67,31 @@ function preInstantiate(imports) {
67
67
68
68
// add common imports used by stdlib for convenience
69
69
const env = ( imports . env = imports . env || { } ) ;
70
- env . abort = env . abort || function abort ( mesg , file , line , colm ) {
71
- const memory = baseModule . memory || env . memory ; // prefer exported, otherwise try imported
72
- throw Error ( "abort: " + getString ( memory , mesg ) + " in " + getString ( memory , file ) + "( " + line + ":" + colm + ")" ) ;
70
+ env . abort = env . abort || function abort ( msg , file , line , colm ) {
71
+ const memory = extendedExports . memory || env . memory ; // prefer exported, otherwise try imported
72
+ throw Error ( "abort: " + getString ( memory , msg ) + " at " + getString ( memory , file ) + ": " + line + ":" + colm ) ;
73
73
} ;
74
- env . trace = env . trace || function trace ( mesg , n ) {
75
- const memory = baseModule . memory || env . memory ;
76
- console . log ( "trace: " + getString ( memory , mesg ) + ( n ? " " : "" ) + Array . prototype . slice . call ( arguments , 2 , 2 + n ) . join ( ", " ) ) ;
74
+ env . trace = env . trace || function trace ( msg , n ) {
75
+ const memory = extendedExports . memory || env . memory ;
76
+ console . log ( "trace: " + getString ( memory , msg ) + ( n ? " " : "" ) + Array . prototype . slice . call ( arguments , 2 , 2 + n ) . join ( ", " ) ) ;
77
77
} ;
78
78
env . seed = env . seed || function seed ( ) {
79
79
return Date . now ( ) ;
80
80
} ;
81
81
imports . Math = imports . Math || Math ;
82
82
imports . Date = imports . Date || Date ;
83
83
84
- return baseModule ;
84
+ return extendedExports ;
85
85
}
86
86
87
87
/** Prepares the final module once instantiation is complete. */
88
- function postInstantiate ( baseModule , instance ) {
89
- const rawExports = instance . exports ;
90
- const memory = rawExports . memory ;
91
- const table = rawExports . table ;
92
- const alloc = rawExports [ "__alloc" ] ;
93
- const retain = rawExports [ "__retain" ] ;
94
- const rttiBase = rawExports [ "__rtti_base" ] || ~ 0 ; // oob if not present
88
+ function postInstantiate ( extendedExports , instance ) {
89
+ const exports = instance . exports ;
90
+ const memory = exports . memory ;
91
+ const table = exports . table ;
92
+ const alloc = exports [ "__alloc" ] ;
93
+ const retain = exports [ "__retain" ] ;
94
+ const rttiBase = exports [ "__rtti_base" ] || ~ 0 ; // oob if not present
95
95
96
96
/** Gets the runtime type info for the given id. */
97
97
function getInfo ( id ) {
@@ -128,7 +128,7 @@ function postInstantiate(baseModule, instance) {
128
128
return ptr ;
129
129
}
130
130
131
- baseModule . __allocString = __allocString ;
131
+ extendedExports . __allocString = __allocString ;
132
132
133
133
/** Reads a string from the module's memory by its pointer. */
134
134
function __getString ( ptr ) {
@@ -138,7 +138,7 @@ function postInstantiate(baseModule, instance) {
138
138
return getStringImpl ( buffer , ptr ) ;
139
139
}
140
140
141
- baseModule . __getString = __getString ;
141
+ extendedExports . __getString = __getString ;
142
142
143
143
/** Gets the view matching the specified alignment, signedness and floatness. */
144
144
function getView ( alignLog2 , signed , float ) {
@@ -181,7 +181,7 @@ function postInstantiate(baseModule, instance) {
181
181
return arr ;
182
182
}
183
183
184
- baseModule . __allocArray = __allocArray ;
184
+ extendedExports . __allocArray = __allocArray ;
185
185
186
186
/** Gets a live view on an array's values in the module's memory. Infers the array type from RTTI. */
187
187
function __getArrayView ( arr ) {
@@ -190,15 +190,15 @@ function postInstantiate(baseModule, instance) {
190
190
const info = getInfo ( id ) ;
191
191
if ( ! ( info & ( ARRAYBUFFERVIEW | ARRAY ) ) ) throw Error ( "not an array: " + id + ", flags=" + info ) ;
192
192
const align = getValueAlign ( info ) ;
193
- var buf = U32 [ arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2 ] ;
193
+ let buf = U32 [ arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2 ] ;
194
194
const length = info & ARRAY
195
195
? U32 [ arr + ARRAY_LENGTH_OFFSET >>> 2 ]
196
196
: U32 [ buf + SIZE_OFFSET >>> 2 ] >>> align ;
197
197
return getView ( align , info & VAL_SIGNED , info & VAL_FLOAT )
198
198
. subarray ( buf >>>= align , buf + length ) ;
199
199
}
200
200
201
- baseModule . __getArrayView = __getArrayView ;
201
+ extendedExports . __getArrayView = __getArrayView ;
202
202
203
203
/** Copies an array's values from the module's memory. Infers the array type from RTTI. */
204
204
function __getArray ( arr ) {
@@ -209,7 +209,7 @@ function postInstantiate(baseModule, instance) {
209
209
return out ;
210
210
}
211
211
212
- baseModule . __getArray = __getArray ;
212
+ extendedExports . __getArray = __getArray ;
213
213
214
214
/** Copies an ArrayBuffer's value from the module's memory. */
215
215
function __getArrayBuffer ( ptr ) {
@@ -218,7 +218,7 @@ function postInstantiate(baseModule, instance) {
218
218
return buffer . slice ( ptr , ptr + length ) ;
219
219
}
220
220
221
- baseModule . __getArrayBuffer = __getArrayBuffer ;
221
+ extendedExports . __getArrayBuffer = __getArrayBuffer ;
222
222
223
223
/** Copies a typed array's values from the module's memory. */
224
224
function getTypedArray ( Type , alignLog2 , ptr ) {
@@ -233,89 +233,85 @@ function postInstantiate(baseModule, instance) {
233
233
return new Type ( buffer , bufPtr , U32 [ bufPtr + SIZE_OFFSET >>> 2 ] >>> alignLog2 ) ;
234
234
}
235
235
236
- baseModule . __getInt8Array = getTypedArray . bind ( null , Int8Array , 0 ) ;
237
- baseModule . __getInt8ArrayView = getTypedArrayView . bind ( null , Int8Array , 0 ) ;
238
- baseModule . __getUint8Array = getTypedArray . bind ( null , Uint8Array , 0 ) ;
239
- baseModule . __getUint8ArrayView = getTypedArrayView . bind ( null , Uint8Array , 0 ) ;
240
- baseModule . __getUint8ClampedArray = getTypedArray . bind ( null , Uint8ClampedArray , 0 ) ;
241
- baseModule . __getUint8ClampedArrayView = getTypedArrayView . bind ( null , Uint8ClampedArray , 0 ) ;
242
- baseModule . __getInt16Array = getTypedArray . bind ( null , Int16Array , 1 ) ;
243
- baseModule . __getInt16ArrayView = getTypedArrayView . bind ( null , Int16Array , 1 ) ;
244
- baseModule . __getUint16Array = getTypedArray . bind ( null , Uint16Array , 1 ) ;
245
- baseModule . __getUint16ArrayView = getTypedArrayView . bind ( null , Uint16Array , 1 ) ;
246
- baseModule . __getInt32Array = getTypedArray . bind ( null , Int32Array , 2 ) ;
247
- baseModule . __getInt32ArrayView = getTypedArrayView . bind ( null , Int32Array , 2 ) ;
248
- baseModule . __getUint32Array = getTypedArray . bind ( null , Uint32Array , 2 ) ;
249
- baseModule . __getUint32ArrayView = getTypedArrayView . bind ( null , Uint32Array , 2 ) ;
236
+ extendedExports . __getInt8Array = getTypedArray . bind ( null , Int8Array , 0 ) ;
237
+ extendedExports . __getInt8ArrayView = getTypedArrayView . bind ( null , Int8Array , 0 ) ;
238
+ extendedExports . __getUint8Array = getTypedArray . bind ( null , Uint8Array , 0 ) ;
239
+ extendedExports . __getUint8ArrayView = getTypedArrayView . bind ( null , Uint8Array , 0 ) ;
240
+ extendedExports . __getUint8ClampedArray = getTypedArray . bind ( null , Uint8ClampedArray , 0 ) ;
241
+ extendedExports . __getUint8ClampedArrayView = getTypedArrayView . bind ( null , Uint8ClampedArray , 0 ) ;
242
+ extendedExports . __getInt16Array = getTypedArray . bind ( null , Int16Array , 1 ) ;
243
+ extendedExports . __getInt16ArrayView = getTypedArrayView . bind ( null , Int16Array , 1 ) ;
244
+ extendedExports . __getUint16Array = getTypedArray . bind ( null , Uint16Array , 1 ) ;
245
+ extendedExports . __getUint16ArrayView = getTypedArrayView . bind ( null , Uint16Array , 1 ) ;
246
+ extendedExports . __getInt32Array = getTypedArray . bind ( null , Int32Array , 2 ) ;
247
+ extendedExports . __getInt32ArrayView = getTypedArrayView . bind ( null , Int32Array , 2 ) ;
248
+ extendedExports . __getUint32Array = getTypedArray . bind ( null , Uint32Array , 2 ) ;
249
+ extendedExports . __getUint32ArrayView = getTypedArrayView . bind ( null , Uint32Array , 2 ) ;
250
250
if ( BIGINT ) {
251
- baseModule . __getInt64Array = getTypedArray . bind ( null , BigInt64Array , 3 ) ;
252
- baseModule . __getInt64ArrayView = getTypedArrayView . bind ( null , BigInt64Array , 3 ) ;
253
- baseModule . __getUint64Array = getTypedArray . bind ( null , BigUint64Array , 3 ) ;
254
- baseModule . __getUint64ArrayView = getTypedArrayView . bind ( null , BigUint64Array , 3 ) ;
251
+ extendedExports . __getInt64Array = getTypedArray . bind ( null , BigInt64Array , 3 ) ;
252
+ extendedExports . __getInt64ArrayView = getTypedArrayView . bind ( null , BigInt64Array , 3 ) ;
253
+ extendedExports . __getUint64Array = getTypedArray . bind ( null , BigUint64Array , 3 ) ;
254
+ extendedExports . __getUint64ArrayView = getTypedArrayView . bind ( null , BigUint64Array , 3 ) ;
255
255
}
256
- baseModule . __getFloat32Array = getTypedArray . bind ( null , Float32Array , 2 ) ;
257
- baseModule . __getFloat32ArrayView = getTypedArrayView . bind ( null , Float32Array , 2 ) ;
258
- baseModule . __getFloat64Array = getTypedArray . bind ( null , Float64Array , 3 ) ;
259
- baseModule . __getFloat64ArrayView = getTypedArrayView . bind ( null , Float64Array , 3 ) ;
256
+ extendedExports . __getFloat32Array = getTypedArray . bind ( null , Float32Array , 2 ) ;
257
+ extendedExports . __getFloat32ArrayView = getTypedArrayView . bind ( null , Float32Array , 2 ) ;
258
+ extendedExports . __getFloat64Array = getTypedArray . bind ( null , Float64Array , 3 ) ;
259
+ extendedExports . __getFloat64ArrayView = getTypedArrayView . bind ( null , Float64Array , 3 ) ;
260
260
261
261
/** Tests whether an object is an instance of the class represented by the specified base id. */
262
262
function __instanceof ( ptr , baseId ) {
263
263
const U32 = new Uint32Array ( memory . buffer ) ;
264
- var id = U32 [ ( ptr + ID_OFFSET ) >>> 2 ] ;
264
+ let id = U32 [ ( ptr + ID_OFFSET ) >>> 2 ] ;
265
265
if ( id <= U32 [ rttiBase >>> 2 ] ) {
266
266
do if ( id == baseId ) return true ;
267
267
while ( id = getBase ( id ) ) ;
268
268
}
269
269
return false ;
270
270
}
271
271
272
- baseModule . __instanceof = __instanceof ;
272
+ extendedExports . __instanceof = __instanceof ;
273
273
274
- // Pull basic exports to baseModule so code in preInstantiate can use them
275
- baseModule . memory = baseModule . memory || memory ;
276
- baseModule . table = baseModule . table || table ;
274
+ // Pull basic exports to extendedExports so code in preInstantiate can use them
275
+ extendedExports . memory = extendedExports . memory || memory ;
276
+ extendedExports . table = extendedExports . table || table ;
277
277
278
278
// Demangle exports and provide the usual utility on the prototype
279
- return demangle ( rawExports , baseModule ) ;
279
+ return demangle ( exports , extendedExports ) ;
280
280
}
281
281
282
- function isResponse ( o ) {
283
- return typeof Response !== "undefined" && o instanceof Response ;
282
+ function isResponse ( src ) {
283
+ return typeof Response !== "undefined" && src instanceof Response ;
284
+ }
285
+
286
+ function isModule ( src ) {
287
+ return src instanceof WebAssembly . Module ;
284
288
}
285
289
286
290
/** Asynchronously instantiates an AssemblyScript module from anything that can be instantiated. */
287
- async function instantiate ( source , imports ) {
291
+ async function instantiate ( source , imports = { } ) {
288
292
if ( isResponse ( source = await source ) ) return instantiateStreaming ( source , imports ) ;
289
- return postInstantiate (
290
- preInstantiate ( imports || ( imports = { } ) ) ,
291
- await WebAssembly . instantiate (
292
- source instanceof WebAssembly . Module
293
- ? source
294
- : await WebAssembly . compile ( source ) ,
295
- imports
296
- )
297
- ) ;
293
+ const module = isModule ( source ) ? source : await WebAssembly . compile ( source ) ;
294
+ const extended = preInstantiate ( imports ) ;
295
+ const instance = await WebAssembly . instantiate ( module , imports ) ;
296
+ const exports = postInstantiate ( extended , instance ) ;
297
+ return { module, instance, exports } ;
298
298
}
299
299
300
300
exports . instantiate = instantiate ;
301
301
302
302
/** Synchronously instantiates an AssemblyScript module from a WebAssembly.Module or binary buffer. */
303
- function instantiateSync ( source , imports ) {
304
- return postInstantiate (
305
- preInstantiate ( imports || ( imports = { } ) ) ,
306
- new WebAssembly . Instance (
307
- source instanceof WebAssembly . Module
308
- ? source
309
- : new WebAssembly . Module ( source ) ,
310
- imports
311
- )
312
- )
303
+ function instantiateSync ( source , imports = { } ) {
304
+ const module = isModule ( source ) ? source : new WebAssembly . Module ( source ) ;
305
+ const extended = preInstantiate ( imports ) ;
306
+ const instance = new WebAssembly . Instance ( module , imports ) ;
307
+ const exports = postInstantiate ( extended , instance ) ;
308
+ return { module, instance, exports } ;
313
309
}
314
310
315
311
exports . instantiateSync = instantiateSync ;
316
312
317
313
/** Asynchronously instantiates an AssemblyScript module from a response, i.e. as obtained by `fetch`. */
318
- async function instantiateStreaming ( source , imports ) {
314
+ async function instantiateStreaming ( source , imports = { } ) {
319
315
if ( ! WebAssembly . instantiateStreaming ) {
320
316
return instantiate (
321
317
isResponse ( source = await source )
@@ -324,25 +320,25 @@ async function instantiateStreaming(source, imports) {
324
320
imports
325
321
) ;
326
322
}
327
- return postInstantiate (
328
- preInstantiate ( imports || ( imports = { } ) ) ,
329
- ( await WebAssembly . instantiateStreaming ( source , imports ) ) . instance
330
- ) ;
323
+ const extended = preInstantiate ( imports ) ;
324
+ const result = await WebAssembly . instantiateStreaming ( source , imports ) ;
325
+ const exports = postInstantiate ( extended , result . instance ) ;
326
+ return { ... result , exports } ;
331
327
}
332
328
333
329
exports . instantiateStreaming = instantiateStreaming ;
334
330
335
331
/** Demangles an AssemblyScript module's exports to a friendly object structure. */
336
- function demangle ( exports , baseModule ) {
337
- var module = baseModule ? Object . create ( baseModule ) : { } ;
338
- var setArgumentsLength = exports [ "__argumentsLength" ]
339
- ? function ( length ) { exports [ "__argumentsLength" ] . value = length ; }
340
- : exports [ "__setArgumentsLength" ] || exports [ "__setargc" ] || function ( ) { } ;
332
+ function demangle ( exports , extendedExports = { } ) {
333
+ extendedExports = Object . create ( extendedExports ) ;
334
+ const setArgumentsLength = exports [ "__argumentsLength" ]
335
+ ? length => { exports [ "__argumentsLength" ] . value = length ; }
336
+ : exports [ "__setArgumentsLength" ] || exports [ "__setargc" ] || ( ( ) => { } ) ;
341
337
for ( let internalName in exports ) {
342
338
if ( ! Object . prototype . hasOwnProperty . call ( exports , internalName ) ) continue ;
343
339
const elem = exports [ internalName ] ;
344
340
let parts = internalName . split ( "." ) ;
345
- let curr = module ;
341
+ let curr = extendedExports ;
346
342
while ( parts . length > 1 ) {
347
343
let part = parts . shift ( ) ;
348
344
if ( ! Object . prototype . hasOwnProperty . call ( curr , part ) ) curr [ part ] = { } ;
@@ -351,10 +347,10 @@ function demangle(exports, baseModule) {
351
347
let name = parts [ 0 ] ;
352
348
let hash = name . indexOf ( "#" ) ;
353
349
if ( hash >= 0 ) {
354
- let className = name . substring ( 0 , hash ) ;
355
- let classElem = curr [ className ] ;
350
+ const className = name . substring ( 0 , hash ) ;
351
+ const classElem = curr [ className ] ;
356
352
if ( typeof classElem === "undefined" || ! classElem . prototype ) {
357
- let ctor = function ( ...args ) {
353
+ const ctor = function ( ...args ) {
358
354
return ctor . wrap ( ctor . prototype . constructor ( 0 , ...args ) ) ;
359
355
} ;
360
356
ctor . prototype = {
@@ -414,7 +410,7 @@ function demangle(exports, baseModule) {
414
410
}
415
411
}
416
412
}
417
- return module ;
413
+ return extendedExports ;
418
414
}
419
415
420
416
exports . demangle = demangle ;
0 commit comments