@@ -44,17 +44,17 @@ const CHUNKSIZE = 1024;
44
44
function getStringImpl ( buffer , ptr ) {
45
45
const U32 = new Uint32Array ( buffer ) ;
46
46
const U16 = new Uint16Array ( buffer ) ;
47
- let length = U32 [ ( ptr + SIZE_OFFSET ) >>> 2 ] >>> 1 ;
47
+ let length = U32 [ ptr + SIZE_OFFSET >>> 2 ] >>> 1 ;
48
48
let offset = ptr >>> 1 ;
49
49
if ( length <= CHUNKSIZE ) return String . fromCharCode . apply ( String , U16 . subarray ( offset , offset + length ) ) ;
50
- const parts = [ ] ;
50
+ let parts = '' ;
51
51
do {
52
52
const last = U16 [ offset + CHUNKSIZE - 1 ] ;
53
53
const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE ;
54
- parts . push ( String . fromCharCode . apply ( String , U16 . subarray ( offset , offset += size ) ) ) ;
54
+ parts += String . fromCharCode . apply ( String , U16 . subarray ( offset , offset += size ) ) ;
55
55
length -= size ;
56
56
} while ( length > CHUNKSIZE ) ;
57
- return parts . join ( "" ) + String . fromCharCode . apply ( String , U16 . subarray ( offset , offset + length ) ) ;
57
+ return parts + String . fromCharCode . apply ( String , U16 . subarray ( offset , offset + length ) ) ;
58
58
}
59
59
60
60
/** Prepares the base module prior to instantiation. */
@@ -70,15 +70,13 @@ function preInstantiate(imports) {
70
70
const env = ( imports . env = imports . env || { } ) ;
71
71
env . abort = env . abort || function abort ( msg , file , line , colm ) {
72
72
const memory = extendedExports . memory || env . memory ; // prefer exported, otherwise try imported
73
- throw Error ( " abort: " + getString ( memory , msg ) + " at " + getString ( memory , file ) + ":" + line + ":" + colm ) ;
73
+ throw Error ( ` abort: ${ getString ( memory , msg ) } at ${ getString ( memory , file ) } : ${ line } : ${ colm } ` ) ;
74
74
} ;
75
- env . trace = env . trace || function trace ( msg , n ) {
75
+ env . trace = env . trace || function trace ( msg , n , ... args ) {
76
76
const memory = extendedExports . memory || env . memory ;
77
- console . log ( "trace: " + getString ( memory , msg ) + ( n ? " " : "" ) + Array . prototype . slice . call ( arguments , 2 , 2 + n ) . join ( ", " ) ) ;
78
- } ;
79
- env . seed = env . seed || function seed ( ) {
80
- return Date . now ( ) ;
77
+ console . log ( `trace: ${ getString ( memory , msg ) } ${ n ? " " : "" } ${ args . slice ( 0 , n ) . join ( ", " ) } ` ) ;
81
78
} ;
79
+ env . seed = env . seed || Date . now ;
82
80
imports . Math = imports . Math || Math ;
83
81
imports . Date = imports . Date || Date ;
84
82
@@ -98,15 +96,22 @@ function postInstantiate(extendedExports, instance) {
98
96
function getInfo ( id ) {
99
97
const U32 = new Uint32Array ( memory . buffer ) ;
100
98
const count = U32 [ rttiBase >>> 2 ] ;
101
- if ( ( id >>>= 0 ) >= count ) throw Error ( " invalid id: " + id ) ;
99
+ if ( ( id >>>= 0 ) >= count ) throw Error ( ` invalid id: ${ id } ` ) ;
102
100
return U32 [ ( rttiBase + 4 >>> 2 ) + id * 2 ] ;
103
101
}
104
102
103
+ /** Gets and validate runtime type info for the given id for array like objects */
104
+ function getArrayInfo ( id ) {
105
+ const info = getInfo ( id ) ;
106
+ if ( ! ( info & ( ARRAYBUFFERVIEW | ARRAY | STATICARRAY ) ) ) throw Error ( `not an array: ${ id } , flags=${ info } ` ) ;
107
+ return info ;
108
+ }
109
+
105
110
/** Gets the runtime base id for the given id. */
106
111
function getBase ( id ) {
107
112
const U32 = new Uint32Array ( memory . buffer ) ;
108
113
const count = U32 [ rttiBase >>> 2 ] ;
109
- if ( ( id >>>= 0 ) >= count ) throw Error ( " invalid id: " + id ) ;
114
+ if ( ( id >>>= 0 ) >= count ) throw Error ( ` invalid id: ${ id } ` ) ;
110
115
return U32 [ ( rttiBase + 4 >>> 2 ) + id * 2 + 1 ] ;
111
116
}
112
117
@@ -135,7 +140,7 @@ function postInstantiate(extendedExports, instance) {
135
140
function __getString ( ptr ) {
136
141
const buffer = memory . buffer ;
137
142
const id = new Uint32Array ( buffer ) [ ptr + ID_OFFSET >>> 2 ] ;
138
- if ( id !== STRING_ID ) throw Error ( " not a string: " + ptr ) ;
143
+ if ( id !== STRING_ID ) throw Error ( ` not a string: ${ ptr } ` ) ;
139
144
return getStringImpl ( buffer , ptr ) ;
140
145
}
141
146
@@ -157,13 +162,12 @@ function postInstantiate(extendedExports, instance) {
157
162
case 3 : return new ( signed ? BigInt64Array : BigUint64Array ) ( buffer ) ;
158
163
}
159
164
}
160
- throw Error ( " unsupported align: " + alignLog2 ) ;
165
+ throw Error ( ` unsupported align: ${ alignLog2 } ` ) ;
161
166
}
162
167
163
168
/** Allocates a new array in the module's memory and returns its retained pointer. */
164
169
function __allocArray ( id , values ) {
165
- const info = getInfo ( id ) ;
166
- if ( ! ( info & ( ARRAYBUFFERVIEW | ARRAY | STATICARRAY ) ) ) throw Error ( "not an array: " + id + ", flags= " + info ) ;
170
+ const info = getArrayInfo ( id ) ;
167
171
const align = getValueAlign ( info ) ;
168
172
const length = values . length ;
169
173
const buf = alloc ( length << align , info & STATICARRAY ? id : ARRAYBUFFER_ID ) ;
@@ -194,8 +198,7 @@ function postInstantiate(extendedExports, instance) {
194
198
function __getArrayView ( arr ) {
195
199
const U32 = new Uint32Array ( memory . buffer ) ;
196
200
const id = U32 [ arr + ID_OFFSET >>> 2 ] ;
197
- const info = getInfo ( id ) ;
198
- if ( ! ( info & ( ARRAYBUFFERVIEW | ARRAY | STATICARRAY ) ) ) throw Error ( "not an array: " + id + ", flags=" + info ) ;
201
+ const info = getArrayInfo ( id ) ;
199
202
const align = getValueAlign ( info ) ;
200
203
let buf = info & STATICARRAY
201
204
? arr
@@ -243,8 +246,8 @@ function postInstantiate(extendedExports, instance) {
243
246
244
247
/** Attach a set of get TypedArray and View functions to the exports. */
245
248
function attachTypedArrayFunctions ( ctor , name , align ) {
246
- extendedExports [ " __get" + name ] = getTypedArray . bind ( null , ctor , align ) ;
247
- extendedExports [ " __get" + name + " View" ] = getTypedArrayView . bind ( null , ctor , align ) ;
249
+ extendedExports [ ` __get${ name } ` ] = getTypedArray . bind ( null , ctor , align ) ;
250
+ extendedExports [ ` __get${ name } View` ] = getTypedArrayView . bind ( null , ctor , align ) ;
248
251
}
249
252
250
253
[
@@ -270,7 +273,7 @@ function postInstantiate(extendedExports, instance) {
270
273
/** Tests whether an object is an instance of the class represented by the specified base id. */
271
274
function __instanceof ( ptr , baseId ) {
272
275
const U32 = new Uint32Array ( memory . buffer ) ;
273
- let id = U32 [ ( ptr + ID_OFFSET ) >>> 2 ] ;
276
+ let id = U32 [ ptr + ID_OFFSET >>> 2 ] ;
274
277
if ( id <= U32 [ rttiBase >>> 2 ] ) {
275
278
do {
276
279
if ( id == baseId ) return true ;
@@ -365,9 +368,7 @@ function demangle(exports, extendedExports = {}) {
365
368
return ctor . wrap ( ctor . prototype . constructor ( 0 , ...args ) ) ;
366
369
} ;
367
370
ctor . prototype = {
368
- valueOf : function valueOf ( ) {
369
- return this [ THIS ] ;
370
- }
371
+ valueOf ( ) { return this [ THIS ] ; }
371
372
} ;
372
373
ctor . wrap = function ( thisValue ) {
373
374
return Object . create ( ctor . prototype , { [ THIS ] : { value : thisValue , writable : false } } ) ;
@@ -384,8 +385,8 @@ function demangle(exports, extendedExports = {}) {
384
385
let getter = exports [ internalName . replace ( "set:" , "get:" ) ] ;
385
386
let setter = exports [ internalName . replace ( "get:" , "set:" ) ] ;
386
387
Object . defineProperty ( curr , name , {
387
- get : function ( ) { return getter ( this [ THIS ] ) ; } ,
388
- set : function ( value ) { setter ( this [ THIS ] , value ) ; } ,
388
+ get ( ) { return getter ( this [ THIS ] ) ; } ,
389
+ set ( value ) { setter ( this [ THIS ] , value ) ; } ,
389
390
enumerable : true
390
391
} ) ;
391
392
}
0 commit comments