@@ -178,6 +178,7 @@ function getNextFuncIndex(ast, countImportedFunc) {
178178 * @returns {ArrayBufferTransform } transform
179179 */
180180const rewriteImportedGlobals = state => bin => {
181+ const additionalInitCode = state . additionalInitCode ;
181182 const newGlobals = [ ] ;
182183
183184 bin = editWithAST ( state . ast , bin , {
@@ -212,29 +213,7 @@ const rewriteImportedGlobals = state => bin => {
212213 // in order to preserve non-imported global's order we need to re-inject
213214 // those as well
214215 Global ( path ) {
215- newGlobals . push ( path . node ) ;
216- path . remove ( ) ;
217- }
218- } ) ;
219-
220- // Add global declaration instructions
221- return addWithAST ( state . ast , bin , newGlobals ) ;
222- } ;
223-
224- const rewriteGlobalsReferingImportedGlobals = state => bin => {
225- const additionalInitCode = [ ] ;
226-
227- // Track global index in the module
228- let globalidx = 0 ;
229-
230- bin = editWithAST ( state . ast , bin , {
231- ModuleImport ( { node } ) {
232- if ( isGlobalImport ( node ) === true ) {
233- globalidx ++ ;
234- }
235- } ,
236-
237- Global ( { node } ) {
216+ const { node } = path ;
238217 const [ init ] = node . init ;
239218
240219 if ( init . id === "get_global" ) {
@@ -256,28 +235,18 @@ const rewriteGlobalsReferingImportedGlobals = state => bin => {
256235 * same index.
257236 */
258237 t . instruction ( "get_local" , [ initialGlobalidx ] ) ,
259- t . instruction ( "set_global" , [ t . indexLiteral ( globalidx ) ] )
238+ t . instruction ( "set_global" , [ t . indexLiteral ( newGlobals . length ) ] )
260239 ) ;
261240 }
262241
263- globalidx ++ ;
242+ newGlobals . push ( node ) ;
243+
244+ path . remove ( ) ;
264245 }
265246 } ) ;
266247
267- // Update the init with our additional runtime code
268- if ( additionalInitCode . length > 0 ) {
269- bin = editWithAST ( state . ast , bin , {
270- Func ( { node } ) {
271- if ( node . name . value === state . initFuncId . value ) {
272- const newBody = [ ...node . body , ...additionalInitCode ] ;
273-
274- node . body = newBody ;
275- }
276- }
277- } ) ;
278- }
279-
280- return bin ;
248+ // Add global declaration instructions
249+ return addWithAST ( state . ast , bin , newGlobals ) ;
281250} ;
282251
283252/**
@@ -333,6 +302,7 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
333302 * @param {t.Identifier } state.initFuncId identifier of the init function
334303 * @param {t.IndexLiteral } state.startAtFuncIndex index of the start function
335304 * @param {t.ModuleImport[] } state.importedGlobals list of imported globals
305+ * @param {t.Instruction[] } state.additionalInitCode list of addition instructions for the init function
336306 * @param {t.IndexLiteral } state.nextFuncIndex index of the next function
337307 * @param {t.IndexLiteral } state.nextTypeIndex index of the next type
338308 * @returns {ArrayBufferTransform } transform
@@ -342,6 +312,7 @@ const addInitFunction = ({
342312 initFuncId,
343313 startAtFuncIndex,
344314 importedGlobals,
315+ additionalInitCode,
345316 nextFuncIndex,
346317 nextTypeIndex
347318} ) => bin => {
@@ -366,6 +337,10 @@ const addInitFunction = ({
366337 funcBody . push ( t . callInstruction ( startAtFuncIndex ) ) ;
367338 }
368339
340+ for ( const instr of additionalInitCode ) {
341+ funcBody . push ( instr ) ;
342+ }
343+
369344 const funcResults = [ ] ;
370345
371346 // Code section
@@ -429,6 +404,9 @@ class WebAssemblyGenerator extends Generator {
429404
430405 const usedDependencyMap = getUsedDependencyMap ( module ) ;
431406
407+ /** @type {t.Instruction[] } */
408+ const additionalInitCode = [ ] ;
409+
432410 const transform = compose (
433411 rewriteExportNames ( {
434412 ast,
@@ -437,7 +415,7 @@ class WebAssemblyGenerator extends Generator {
437415
438416 removeStartFunc ( { ast } ) ,
439417
440- rewriteImportedGlobals ( { ast } ) ,
418+ rewriteImportedGlobals ( { ast, additionalInitCode } ) ,
441419
442420 rewriteImports ( {
443421 ast,
@@ -448,12 +426,11 @@ class WebAssemblyGenerator extends Generator {
448426 ast,
449427 initFuncId,
450428 importedGlobals,
429+ additionalInitCode,
451430 startAtFuncIndex,
452431 nextFuncIndex,
453432 nextTypeIndex
454- } ) ,
455-
456- rewriteGlobalsReferingImportedGlobals ( { ast, initFuncId } )
433+ } )
457434 ) ;
458435
459436 const newBin = transform ( bin ) ;
0 commit comments