Skip to content

Commit d491fdc

Browse files
committed
feat: uses new APIs
1 parent 85b5ee3 commit d491fdc

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

declarations.d.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,28 @@ declare module "chrome-trace-event" {
3333
declare module "@webassemblyjs/ast" {
3434
export function traverse(
3535
ast: any,
36-
visitor: { [name: string]: (context: { node: Node }) => void }
36+
visitor: {
37+
ModuleImport?: (p: NodePath<ModuleImport>) => void;
38+
ModuleExport?: (p: NodePath<ModuleExport>) => void;
39+
Start?: (p: NodePath<Start>) => void;
40+
}
3741
);
38-
export class Node {
39-
index: number;
42+
export class NodePath<T> {
43+
node: T;
4044
}
45+
export class Node {}
4146
export class Identifier extends Node {
4247
value: string;
4348
}
49+
export class Start extends Node {
50+
index: Identifier;
51+
}
4452
export class ModuleImport extends Node {
4553
module: string;
4654
descr: {
4755
type: string;
4856
valtype: string;
57+
id: string;
4958
};
5059
name: string;
5160
}

lib/wasm/WebAssemblyGenerator.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const Generator = require("../Generator");
88
const { RawSource } = require("webpack-sources");
99

10-
const { edit, add } = require("@webassemblyjs/wasm-edit");
10+
const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
1111
const { decode } = require("@webassemblyjs/wasm-parser");
1212
const t = require("@webassemblyjs/ast");
1313

@@ -18,9 +18,19 @@ function compose(...fns) {
1818
}
1919

2020
// 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+
2434
const initFuncId = t.identifier("__webpack_init__");
2535

2636
// TODO replace with @callback
@@ -35,7 +45,7 @@ const initFuncId = t.identifier("__webpack_init__");
3545
* @returns {ArrayBufferTransform} transform
3646
*/
3747
const removeStartFunc = state => bin => {
38-
return edit(bin, {
48+
return editWithAST(state.ast, bin, {
3949
Start(path) {
4050
path.remove();
4151
}
@@ -149,7 +159,7 @@ function getNextFuncIndex(ast, countImportedFunc) {
149159
const rewriteImportedGlobals = state => bin => {
150160
const newGlobals = [];
151161

152-
bin = edit(bin, {
162+
bin = editWithAST(state.ast, bin, {
153163
ModuleImport(path) {
154164
if (isGlobalImport(path.node) === true) {
155165
const globalType = path.node.descr;
@@ -168,7 +178,7 @@ const rewriteImportedGlobals = state => bin => {
168178
});
169179

170180
// Add global declaration instructions
171-
return add(bin, newGlobals);
181+
return addWithAST(state.ast, bin, newGlobals);
172182
};
173183

174184
/**
@@ -177,17 +187,17 @@ const rewriteImportedGlobals = state => bin => {
177187
* The init function fills the globals given input arguments.
178188
*
179189
* @param {Object} state transformation state
190+
* @param {Object} state.ast - Module's ast
180191
* @param {t.IndexLiteral} state.startAtFuncIndex index of the start function
181192
* @param {t.ModuleImport[]} state.importedGlobals list of imported globals
182-
* @param {TODO} state.funcSectionMetadata ??
183193
* @param {t.IndexLiteral} state.nextFuncIndex index of the next function
184194
* @param {t.IndexLiteral} state.nextTypeIndex index of the next type
185195
* @returns {ArrayBufferTransform} transform
186196
*/
187197
const addInitFunction = ({
198+
ast,
188199
startAtFuncIndex,
189200
importedGlobals,
190-
funcSectionMetadata,
191201
nextFuncIndex,
192202
nextTypeIndex
193203
}) => bin => {
@@ -229,7 +239,7 @@ const addInitFunction = ({
229239
// Export section
230240
const moduleExport = t.moduleExport(initFuncId.value, "Func", nextFuncIndex);
231241

232-
return add(bin, [func, moduleExport, funcindex, functype]);
242+
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
233243
};
234244

235245
class WebAssemblyGenerator extends Generator {
@@ -244,20 +254,19 @@ class WebAssemblyGenerator extends Generator {
244254
});
245255

246256
const importedGlobals = getImportedGlobals(ast);
247-
const funcSectionMetadata = t.getSectionMetadata(ast, "func");
248257
const countImportedFunc = getCountImportedFunc(ast);
249258
const startAtFuncIndex = getStartFuncIndex(ast);
250259
const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
251260
const nextTypeIndex = getNextTypeIndex(ast);
252261

253262
const transform = compose(
254-
removeStartFunc({}),
263+
removeStartFunc({ ast }),
255264

256-
rewriteImportedGlobals({}),
265+
rewriteImportedGlobals({ ast }),
257266

258267
addInitFunction({
268+
ast,
259269
importedGlobals,
260-
funcSectionMetadata,
261270
startAtFuncIndex,
262271
nextFuncIndex,
263272
nextTypeIndex

lib/wasm/WebAssemblyParser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ const { Tapable } = require("tapable");
1111
const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
1212

1313
/**
14-
* @param {t.ModuleImport} moduleImport the import
14+
* @param {t.ModuleImport} n the import
1515
* @returns {boolean} true, if a memory was imported
1616
*/
17-
const isMemoryImport = moduleImport => moduleImport.descr.type === "Memory";
17+
const isMemoryImport = n => n.descr.type === "Memory";
1818

1919
/**
20-
* @param {t.ModuleImport} moduleImport the import
20+
* @param {t.ModuleImport} n the import
2121
* @returns {boolean} true, if a table was imported
2222
*/
23-
const isTableImport = moduleImport => moduleImport.descr.type === "Table";
23+
const isTableImport = n => n.descr.type === "Table";
2424

2525
const decoderOpts = {
2626
ignoreCodeSection: true,

0 commit comments

Comments
 (0)