7
7
const Generator = require ( "../Generator" ) ;
8
8
const { RawSource } = require ( "webpack-sources" ) ;
9
9
10
- const { edit , add } = require ( "@webassemblyjs/wasm-edit" ) ;
10
+ const { editWithAST , addWithAST } = require ( "@webassemblyjs/wasm-edit" ) ;
11
11
const { decode } = require ( "@webassemblyjs/wasm-parser" ) ;
12
12
const t = require ( "@webassemblyjs/ast" ) ;
13
13
@@ -18,9 +18,19 @@ function compose(...fns) {
18
18
}
19
19
20
20
// Utility functions
21
- const isGlobalImport = moduleImport => moduleImport . descr . type === "GlobalType" ;
22
- const isFuncImport = moduleImport =>
23
- moduleImport . descr . type === "FuncImportDescr" ;
21
+
22
+ /**
23
+ * @param {t.ModuleImport } n the import
24
+ * @returns {boolean } true, if a global was imported
25
+ */
26
+ const isGlobalImport = n => n . descr . type === "GlobalType" ;
27
+
28
+ /**
29
+ * @param {t.ModuleImport } n the import
30
+ * @returns {boolean } true, if a func was imported
31
+ */
32
+ const isFuncImport = n => n . descr . type === "FuncImportDescr" ;
33
+
24
34
const initFuncId = t . identifier ( "__webpack_init__" ) ;
25
35
26
36
// TODO replace with @callback
@@ -35,7 +45,7 @@ const initFuncId = t.identifier("__webpack_init__");
35
45
* @returns {ArrayBufferTransform } transform
36
46
*/
37
47
const removeStartFunc = state => bin => {
38
- return edit ( bin , {
48
+ return editWithAST ( state . ast , bin , {
39
49
Start ( path ) {
40
50
path . remove ( ) ;
41
51
}
@@ -149,7 +159,7 @@ function getNextFuncIndex(ast, countImportedFunc) {
149
159
const rewriteImportedGlobals = state => bin => {
150
160
const newGlobals = [ ] ;
151
161
152
- bin = edit ( bin , {
162
+ bin = editWithAST ( state . ast , bin , {
153
163
ModuleImport ( path ) {
154
164
if ( isGlobalImport ( path . node ) === true ) {
155
165
const globalType = path . node . descr ;
@@ -168,7 +178,7 @@ const rewriteImportedGlobals = state => bin => {
168
178
} ) ;
169
179
170
180
// Add global declaration instructions
171
- return add ( bin , newGlobals ) ;
181
+ return addWithAST ( state . ast , bin , newGlobals ) ;
172
182
} ;
173
183
174
184
/**
@@ -177,17 +187,17 @@ const rewriteImportedGlobals = state => bin => {
177
187
* The init function fills the globals given input arguments.
178
188
*
179
189
* @param {Object } state transformation state
190
+ * @param {Object } state.ast - Module's ast
180
191
* @param {t.IndexLiteral } state.startAtFuncIndex index of the start function
181
192
* @param {t.ModuleImport[] } state.importedGlobals list of imported globals
182
- * @param {TODO } state.funcSectionMetadata ??
183
193
* @param {t.IndexLiteral } state.nextFuncIndex index of the next function
184
194
* @param {t.IndexLiteral } state.nextTypeIndex index of the next type
185
195
* @returns {ArrayBufferTransform } transform
186
196
*/
187
197
const addInitFunction = ( {
198
+ ast,
188
199
startAtFuncIndex,
189
200
importedGlobals,
190
- funcSectionMetadata,
191
201
nextFuncIndex,
192
202
nextTypeIndex
193
203
} ) => bin => {
@@ -229,7 +239,7 @@ const addInitFunction = ({
229
239
// Export section
230
240
const moduleExport = t . moduleExport ( initFuncId . value , "Func" , nextFuncIndex ) ;
231
241
232
- return add ( bin , [ func , moduleExport , funcindex , functype ] ) ;
242
+ return addWithAST ( ast , bin , [ func , moduleExport , funcindex , functype ] ) ;
233
243
} ;
234
244
235
245
class WebAssemblyGenerator extends Generator {
@@ -244,20 +254,19 @@ class WebAssemblyGenerator extends Generator {
244
254
} ) ;
245
255
246
256
const importedGlobals = getImportedGlobals ( ast ) ;
247
- const funcSectionMetadata = t . getSectionMetadata ( ast , "func" ) ;
248
257
const countImportedFunc = getCountImportedFunc ( ast ) ;
249
258
const startAtFuncIndex = getStartFuncIndex ( ast ) ;
250
259
const nextFuncIndex = getNextFuncIndex ( ast , countImportedFunc ) ;
251
260
const nextTypeIndex = getNextTypeIndex ( ast ) ;
252
261
253
262
const transform = compose (
254
- removeStartFunc ( { } ) ,
263
+ removeStartFunc ( { ast } ) ,
255
264
256
- rewriteImportedGlobals ( { } ) ,
265
+ rewriteImportedGlobals ( { ast } ) ,
257
266
258
267
addInitFunction ( {
268
+ ast,
259
269
importedGlobals,
260
- funcSectionMetadata,
261
270
startAtFuncIndex,
262
271
nextFuncIndex,
263
272
nextTypeIndex
0 commit comments