@@ -81,16 +81,16 @@ function postInstantiate(extendedExports, instance) {
8181 const exports = instance . exports ;
8282 const memory = exports . memory ;
8383 const table = exports . table ;
84- const new_ = exports [ "__new" ] ;
85- const retain = exports [ "__retain" ] ;
86- const rttiBase = exports [ "__rtti_base" ] || ~ 0 ; // oob if not present
84+ const __new = exports [ "__new" ] ;
85+ const __retain = exports [ "__retain" ] ;
86+ const __rtti_base = exports [ "__rtti_base" ] || ~ 0 ; // oob if not present
8787
8888 /** Gets the runtime type info for the given id. */
8989 function getInfo ( id ) {
9090 const U32 = new Uint32Array ( memory . buffer ) ;
91- const count = U32 [ rttiBase >>> 2 ] ;
91+ const count = U32 [ __rtti_base >>> 2 ] ;
9292 if ( ( id >>>= 0 ) >= count ) throw Error ( `invalid id: ${ id } ` ) ;
93- return U32 [ ( rttiBase + 4 >>> 2 ) + id * 2 ] ;
93+ return U32 [ ( __rtti_base + 4 >>> 2 ) + id * 2 ] ;
9494 }
9595
9696 /** Gets and validate runtime type info for the given id for array like objects */
@@ -103,9 +103,9 @@ function postInstantiate(extendedExports, instance) {
103103 /** Gets the runtime base id for the given id. */
104104 function getBase ( id ) {
105105 const U32 = new Uint32Array ( memory . buffer ) ;
106- const count = U32 [ rttiBase >>> 2 ] ;
106+ const count = U32 [ __rtti_base >>> 2 ] ;
107107 if ( ( id >>>= 0 ) >= count ) throw Error ( `invalid id: ${ id } ` ) ;
108- return U32 [ ( rttiBase + 4 >>> 2 ) + id * 2 + 1 ] ;
108+ return U32 [ ( __rtti_base + 4 >>> 2 ) + id * 2 + 1 ] ;
109109 }
110110
111111 /** Gets the runtime alignment of a collection's values. */
@@ -120,8 +120,9 @@ function postInstantiate(extendedExports, instance) {
120120
121121 /** Allocates a new string in the module's memory and returns its retained pointer. */
122122 function __newString ( str ) {
123+ if ( str == null ) return 0 ;
123124 const length = str . length ;
124- const ptr = new_ ( length << 1 , STRING_ID ) ;
125+ const ptr = __new ( length << 1 , STRING_ID ) ;
125126 const U16 = new Uint16Array ( memory . buffer ) ;
126127 for ( var i = 0 , p = ptr >>> 1 ; i < length ; ++ i ) U16 [ p + i ] = str . charCodeAt ( i ) ;
127128 return ptr ;
@@ -131,6 +132,7 @@ function postInstantiate(extendedExports, instance) {
131132
132133 /** Reads a string from the module's memory by its pointer. */
133134 function __getString ( ptr ) {
135+ if ( ! ptr ) return null ;
134136 const buffer = memory . buffer ;
135137 const id = new Uint32Array ( buffer ) [ ptr + ID_OFFSET >>> 2 ] ;
136138 if ( id !== STRING_ID ) throw Error ( `not a string: ${ ptr } ` ) ;
@@ -163,22 +165,22 @@ function postInstantiate(extendedExports, instance) {
163165 const info = getArrayInfo ( id ) ;
164166 const align = getValueAlign ( info ) ;
165167 const length = values . length ;
166- const buf = new_ ( length << align , info & STATICARRAY ? id : ARRAYBUFFER_ID ) ;
168+ const buf = __new ( length << align , info & STATICARRAY ? id : ARRAYBUFFER_ID ) ;
167169 let result ;
168170 if ( info & STATICARRAY ) {
169171 result = buf ;
170172 } else {
171- const arr = new_ ( info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE , id ) ;
173+ const arr = __new ( info & ARRAY ? ARRAY_SIZE : ARRAYBUFFERVIEW_SIZE , id ) ;
172174 const U32 = new Uint32Array ( memory . buffer ) ;
173- U32 [ arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2 ] = retain ( buf ) ;
175+ U32 [ arr + ARRAYBUFFERVIEW_BUFFER_OFFSET >>> 2 ] = __retain ( buf ) ;
174176 U32 [ arr + ARRAYBUFFERVIEW_DATASTART_OFFSET >>> 2 ] = buf ;
175177 U32 [ arr + ARRAYBUFFERVIEW_DATALENGTH_OFFSET >>> 2 ] = length << align ;
176178 if ( info & ARRAY ) U32 [ arr + ARRAY_LENGTH_OFFSET >>> 2 ] = length ;
177179 result = arr ;
178180 }
179181 const view = getView ( align , info & VAL_SIGNED , info & VAL_FLOAT ) ;
180182 if ( info & VAL_MANAGED ) {
181- for ( let i = 0 ; i < length ; ++ i ) view [ ( buf >>> align ) + i ] = retain ( values [ i ] ) ;
183+ for ( let i = 0 ; i < length ; ++ i ) view [ ( buf >>> align ) + i ] = __retain ( values [ i ] ) ;
182184 } else {
183185 view . set ( values , buf >>> align ) ;
184186 }
@@ -267,7 +269,7 @@ function postInstantiate(extendedExports, instance) {
267269 function __instanceof ( ptr , baseId ) {
268270 const U32 = new Uint32Array ( memory . buffer ) ;
269271 let id = U32 [ ptr + ID_OFFSET >>> 2 ] ;
270- if ( id <= U32 [ rttiBase >>> 2 ] ) {
272+ if ( id <= U32 [ __rtti_base >>> 2 ] ) {
271273 do {
272274 if ( id == baseId ) return true ;
273275 id = getBase ( id ) ;
@@ -331,7 +333,6 @@ export async function instantiateStreaming(source, imports = {}) {
331333
332334/** Demangles an AssemblyScript module's exports to a friendly object structure. */
333335export function demangle ( exports , extendedExports = { } ) {
334- extendedExports = Object . create ( extendedExports ) ;
335336 const setArgumentsLength = exports [ "__argumentsLength" ]
336337 ? length => { exports [ "__argumentsLength" ] . value = length ; }
337338 : exports [ "__setArgumentsLength" ] || exports [ "__setargc" ] || ( ( ) => { /* nop */ } ) ;
0 commit comments