@@ -19,6 +19,8 @@ function compose(...fns) {
19
19
20
20
// Utility functions
21
21
const isGlobalImport = moduleImport => moduleImport . descr . type === "GlobalType" ;
22
+ const isFuncImport = moduleImport =>
23
+ moduleImport . descr . type === "FuncImportDescr" ;
22
24
const initFuncId = t . identifier ( "__webpack_init__" ) ;
23
25
24
26
// TODO replace with @callback
@@ -50,8 +52,8 @@ function getStartFuncIndex(ast) {
50
52
let startAtFuncIndex ;
51
53
52
54
t . traverse ( ast , {
53
- Start ( path ) {
54
- startAtFuncIndex = path . node . index ;
55
+ Start ( { node } ) {
56
+ startAtFuncIndex = node . index ;
55
57
}
56
58
} ) ;
57
59
@@ -78,6 +80,20 @@ function getImportedGlobals(ast) {
78
80
return importedGlobals ;
79
81
}
80
82
83
+ function getCountImportedFunc ( ast ) {
84
+ let count = 0 ;
85
+
86
+ t . traverse ( ast , {
87
+ ModuleImport ( { node } ) {
88
+ if ( isFuncImport ( node ) === true ) {
89
+ count ++ ;
90
+ }
91
+ }
92
+ } ) ;
93
+
94
+ return count ;
95
+ }
96
+
81
97
/**
82
98
* Get next type index
83
99
*
@@ -97,17 +113,24 @@ function getNextTypeIndex(ast) {
97
113
/**
98
114
* Get next func index
99
115
*
116
+ * The Func section metadata provide informations for implemented funcs
117
+ * in order to have the correct index we shift the index by number of external
118
+ * functions.
119
+ *
100
120
* @param {Object } ast - Module's AST
121
+ * @param {Number } countImportedFunc - number of imported funcs
101
122
* @returns {t.IndexLiteral } - index
102
123
*/
103
- function getNextFuncIndex ( ast ) {
124
+ function getNextFuncIndex ( ast , countImportedFunc ) {
104
125
const funcSectionMetadata = t . getSectionMetadata ( ast , "func" ) ;
105
126
106
127
if ( typeof funcSectionMetadata === "undefined" ) {
107
- return t . indexLiteral ( 0 ) ;
128
+ return t . indexLiteral ( 0 + countImportedFunc ) ;
108
129
}
109
130
110
- return t . indexLiteral ( funcSectionMetadata . vectorOfSize . value ) ;
131
+ const vectorOfSize = funcSectionMetadata . vectorOfSize . value ;
132
+
133
+ return t . indexLiteral ( vectorOfSize + countImportedFunc ) ;
111
134
}
112
135
113
136
/**
@@ -191,14 +214,19 @@ const addInitFunction = ({
191
214
192
215
const funcResults = [ ] ;
193
216
217
+ // Code section
194
218
const func = t . func ( initFuncId , funcParams , funcResults , funcBody ) ;
195
219
220
+ // Type section
196
221
const functype = t . typeInstructionFunc (
197
222
func . signature . params ,
198
223
func . signature . result
199
224
) ;
225
+
226
+ // Func section
200
227
const funcindex = t . indexInFuncSection ( nextTypeIndex ) ;
201
228
229
+ // Export section
202
230
const moduleExport = t . moduleExport ( initFuncId . value , "Func" , nextFuncIndex ) ;
203
231
204
232
return add ( bin , [ func , moduleExport , funcindex , functype ] ) ;
@@ -217,8 +245,9 @@ class WebAssemblyGenerator extends Generator {
217
245
218
246
const importedGlobals = getImportedGlobals ( ast ) ;
219
247
const funcSectionMetadata = t . getSectionMetadata ( ast , "func" ) ;
248
+ const countImportedFunc = getCountImportedFunc ( ast ) ;
220
249
const startAtFuncIndex = getStartFuncIndex ( ast ) ;
221
- const nextFuncIndex = getNextFuncIndex ( ast ) ;
250
+ const nextFuncIndex = getNextFuncIndex ( ast , countImportedFunc ) ;
222
251
const nextTypeIndex = getNextTypeIndex ( ast ) ;
223
252
224
253
const transform = compose (
0 commit comments