@@ -13,6 +13,9 @@ const { shrinkPaddedLEB128 } = require("@webassemblyjs/wasm-opt");
13
13
const { editWithAST, addWithAST } = require ( "@webassemblyjs/wasm-edit" ) ;
14
14
const { decode } = require ( "@webassemblyjs/wasm-parser" ) ;
15
15
const t = require ( "@webassemblyjs/ast" ) ;
16
+ const {
17
+ moduleContextFromModuleAST
18
+ } = require ( "@webassemblyjs/helper-module-context" ) ;
16
19
17
20
const WebAssemblyExportImportedDependency = require ( "../dependencies/WebAssemblyExportImportedDependency" ) ;
18
21
@@ -45,20 +48,6 @@ function compose(...fns) {
45
48
} , value => value ) ;
46
49
}
47
50
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
-
62
51
// TODO replace with @callback
63
52
64
53
/**
@@ -75,24 +64,6 @@ const removeStartFunc = state => bin => {
75
64
} ) ;
76
65
} ;
77
66
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
-
96
67
/**
97
68
* Get imported globals
98
69
*
@@ -104,7 +75,7 @@ function getImportedGlobals(ast) {
104
75
105
76
t . traverse ( ast , {
106
77
ModuleImport ( { node } ) {
107
- if ( isGlobalImport ( node ) === true ) {
78
+ if ( t . isGlobalType ( node . descr ) === true ) {
108
79
importedGlobals . push ( node ) ;
109
80
}
110
81
}
@@ -118,7 +89,7 @@ function getCountImportedFunc(ast) {
118
89
119
90
t . traverse ( ast , {
120
91
ModuleImport ( { node } ) {
121
- if ( isFuncImport ( node ) === true ) {
92
+ if ( t . isFuncImportDescr ( node . descr ) === true ) {
122
93
count ++ ;
123
94
}
124
95
}
@@ -206,7 +177,7 @@ const rewriteImportedGlobals = state => bin => {
206
177
207
178
bin = editWithAST ( state . ast , bin , {
208
179
ModuleImport ( path ) {
209
- if ( isGlobalImport ( path . node ) === true ) {
180
+ if ( t . isGlobalType ( path . node . descr ) === true ) {
210
181
const globalType = path . node . descr ;
211
182
212
183
globalType . mutability = "var" ;
@@ -309,7 +280,7 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
309
280
* @param {Object } state transformation state
310
281
* @param {Object } state.ast - Module's ast
311
282
* @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
313
284
* @param {t.ModuleImport[] } state.importedGlobals list of imported globals
314
285
* @param {t.Instruction[] } state.additionalInitCode list of addition instructions for the init function
315
286
* @param {t.Index } state.nextFuncIndex index of the next function
@@ -319,7 +290,7 @@ const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
319
290
const addInitFunction = ( {
320
291
ast,
321
292
initFuncId,
322
- startAtFuncIndex ,
293
+ startAtFuncOffset ,
323
294
importedGlobals,
324
295
additionalInitCode,
325
296
nextFuncIndex,
@@ -342,8 +313,8 @@ const addInitFunction = ({
342
313
return [ ...acc , ...body ] ;
343
314
} , [ ] ) ;
344
315
345
- if ( typeof startAtFuncIndex !== "undefined ") {
346
- funcBody . push ( t . callInstruction ( startAtFuncIndex ) ) ;
316
+ if ( typeof startAtFuncOffset === "number ") {
317
+ funcBody . push ( t . callInstruction ( t . numberLiteralFromRaw ( startAtFuncOffset ) ) ) ;
347
318
}
348
319
349
320
for ( const instr of additionalInitCode ) {
@@ -405,15 +376,18 @@ class WebAssemblyGenerator extends Generator {
405
376
: "__webpack_init__"
406
377
) ;
407
378
379
+ // parse it
408
380
const ast = decode ( bin , {
409
381
ignoreDataSection : true ,
410
382
ignoreCodeSection : true ,
411
383
ignoreCustomNameSection : true
412
384
} ) ;
413
385
386
+ const moduleContext = moduleContextFromModuleAST ( ast . body [ 0 ] ) ;
387
+
414
388
const importedGlobals = getImportedGlobals ( ast ) ;
415
389
const countImportedFunc = getCountImportedFunc ( ast ) ;
416
- const startAtFuncIndex = getStartFuncIndex ( ast ) ;
390
+ const startAtFuncOffset = moduleContext . getStart ( ) ;
417
391
const nextFuncIndex = getNextFuncIndex ( ast , countImportedFunc ) ;
418
392
const nextTypeIndex = getNextTypeIndex ( ast ) ;
419
393
@@ -451,7 +425,7 @@ class WebAssemblyGenerator extends Generator {
451
425
initFuncId,
452
426
importedGlobals,
453
427
additionalInitCode,
454
- startAtFuncIndex ,
428
+ startAtFuncOffset ,
455
429
nextFuncIndex,
456
430
nextTypeIndex
457
431
} )
0 commit comments