@@ -13,6 +13,9 @@ const { shrinkPaddedLEB128 } = require("@webassemblyjs/wasm-opt");
1313const { editWithAST, addWithAST } = require ( "@webassemblyjs/wasm-edit" ) ;
1414const { decode } = require ( "@webassemblyjs/wasm-parser" ) ;
1515const t = require ( "@webassemblyjs/ast" ) ;
16+ const {
17+ moduleContextFromModuleAST
18+ } = require ( "@webassemblyjs/helper-module-context" ) ;
1619
1720const WebAssemblyExportImportedDependency = require ( "../dependencies/WebAssemblyExportImportedDependency" ) ;
1821
@@ -45,20 +48,6 @@ function compose(...fns) {
4548 } , value => value ) ;
4649}
4750
48- // Utility functions
49-
50- /**
51- * @param {t.ModuleImport } n the import
52- * @returns {boolean } true, if a global was imported
53- */
54- const isGlobalImport = n => n . descr . type === "GlobalType" ;
55-
56- /**
57- * @param {t.ModuleImport } n the import
58- * @returns {boolean } true, if a func was imported
59- */
60- const isFuncImport = n => n . descr . type === "FuncImportDescr" ;
61-
6251// TODO replace with @callback
6352
6453/**
@@ -75,24 +64,6 @@ const removeStartFunc = state => bin => {
7564 } ) ;
7665} ;
7766
78- /**
79- * Retrieve the start function
80- *
81- * @param {Object } ast - Module's AST
82- * @returns {t.Identifier | undefined } - node if any
83- */
84- function getStartFuncIndex ( ast ) {
85- let startAtFuncIndex ;
86-
87- t . traverse ( ast , {
88- Start ( { node } ) {
89- startAtFuncIndex = node . index ;
90- }
91- } ) ;
92-
93- return startAtFuncIndex ;
94- }
95-
9667/**
9768 * Get imported globals
9869 *
@@ -104,7 +75,7 @@ function getImportedGlobals(ast) {
10475
10576 t . traverse ( ast , {
10677 ModuleImport ( { node } ) {
107- if ( isGlobalImport ( node ) === true ) {
78+ if ( t . isGlobalType ( node . descr ) === true ) {
10879 importedGlobals . push ( node ) ;
10980 }
11081 }
@@ -118,7 +89,7 @@ function getCountImportedFunc(ast) {
11889
11990 t . traverse ( ast , {
12091 ModuleImport ( { node } ) {
121- if ( isFuncImport ( node ) === true ) {
92+ if ( t . isFuncImportDescr ( node . descr ) === true ) {
12293 count ++ ;
12394 }
12495 }
@@ -206,7 +177,7 @@ const rewriteImportedGlobals = state => bin => {
206177
207178 bin = editWithAST ( state . ast , bin , {
208179 ModuleImport ( path ) {
209- if ( isGlobalImport ( path . node ) === true ) {
180+ if ( t . isGlobalType ( path . node . descr ) === true ) {
210181 const globalType = path . node . descr ;
211182
212183 globalType . mutability = "var" ;
@@ -309,7 +280,7 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
309280 * @param {Object } state transformation state
310281 * @param {Object } state.ast - Module's ast
311282 * @param {t.Identifier } state.initFuncId identifier of the init function
312- * @param {t.Index } state.startAtFuncIndex index of the start function
283+ * @param {t.Index } state.startAtFuncOffset index of the start function
313284 * @param {t.ModuleImport[] } state.importedGlobals list of imported globals
314285 * @param {t.Instruction[] } state.additionalInitCode list of addition instructions for the init function
315286 * @param {t.Index } state.nextFuncIndex index of the next function
@@ -319,7 +290,7 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
319290const addInitFunction = ( {
320291 ast,
321292 initFuncId,
322- startAtFuncIndex ,
293+ startAtFuncOffset ,
323294 importedGlobals,
324295 additionalInitCode,
325296 nextFuncIndex,
@@ -342,8 +313,8 @@ const addInitFunction = ({
342313 return [ ...acc , ...body ] ;
343314 } , [ ] ) ;
344315
345- if ( typeof startAtFuncIndex !== "undefined ") {
346- funcBody . push ( t . callInstruction ( startAtFuncIndex ) ) ;
316+ if ( typeof startAtFuncOffset === "number ") {
317+ funcBody . push ( t . callInstruction ( t . numberLiteralFromRaw ( startAtFuncOffset ) ) ) ;
347318 }
348319
349320 for ( const instr of additionalInitCode ) {
@@ -405,15 +376,18 @@ class WebAssemblyGenerator extends Generator {
405376 : "__webpack_init__"
406377 ) ;
407378
379+ // parse it
408380 const ast = decode ( bin , {
409381 ignoreDataSection : true ,
410382 ignoreCodeSection : true ,
411383 ignoreCustomNameSection : true
412384 } ) ;
413385
386+ const moduleContext = moduleContextFromModuleAST ( ast . body [ 0 ] ) ;
387+
414388 const importedGlobals = getImportedGlobals ( ast ) ;
415389 const countImportedFunc = getCountImportedFunc ( ast ) ;
416- const startAtFuncIndex = getStartFuncIndex ( ast ) ;
390+ const startAtFuncOffset = moduleContext . getStart ( ) ;
417391 const nextFuncIndex = getNextFuncIndex ( ast , countImportedFunc ) ;
418392 const nextTypeIndex = getNextTypeIndex ( ast ) ;
419393
@@ -451,7 +425,7 @@ class WebAssemblyGenerator extends Generator {
451425 initFuncId,
452426 importedGlobals,
453427 additionalInitCode,
454- startAtFuncIndex ,
428+ startAtFuncOffset ,
455429 nextFuncIndex,
456430 nextTypeIndex
457431 } )
0 commit comments