@@ -201,8 +201,15 @@ var LibraryEmVal = {
201
201
argsList += ( i !== 0 ?", " :"" ) + "arg" + i ; // 'arg0, arg1, ..., argn'
202
202
}
203
203
204
+ // The body of the generated function does not have access to enclosing
205
+ // scope where HEAPU64/HEAPU32/etc are defined, and we cannot pass them
206
+ // directly as arguments (like we do the Module object) since memory
207
+ // growth can cause them to be re-bound.
208
+ var getMemory = ( ) => { { { MEMORY64 ? "HEAPU64" : "HEAPU32" } } } ;
209
+
204
210
var functionBody =
205
- "return function emval_allocator_" + argCount + "(constructor, argTypes, args) {\n" ;
211
+ "return function emval_allocator_" + argCount + "(constructor, argTypes, args) {\n" +
212
+ " var {{{ MEMORY64 ? 'HEAPU64' : 'HEAPU32' }}} = getMemory();\n" ;
206
213
207
214
for ( var i = 0 ; i < argCount ; ++ i ) {
208
215
functionBody +=
@@ -217,8 +224,8 @@ var LibraryEmVal = {
217
224
"}\n" ;
218
225
219
226
/*jshint evil:true*/
220
- return ( new Function ( "requireRegisteredType" , "Module" , "valueToHandle" , "{{{ MEMORY64 ? " HEAPU64 " : "HEAPU32" } } } " , functionBody))(
221
- requireRegisteredType , Module , Emval . toHandle , { { { MEMORY64 ? "HEAPU64" : "HEAPU32" } } } ) ;
227
+ return ( new Function ( "requireRegisteredType" , "Module" , "valueToHandle" , "getMemory " , functionBody ) ) (
228
+ requireRegisteredType , Module , Emval . toHandle , getMemory ) ;
222
229
#endif
223
230
} ,
224
231
0 commit comments