Skip to content

Commit 94c0f0b

Browse files
authored
Merge pull request webpack#7490 from xtuc/feat-wasm-some-cleanups
wasm some cleanups
2 parents 956c0b1 + 0288128 commit 94c0f0b

File tree

5 files changed

+128
-172
lines changed

5 files changed

+128
-172
lines changed

declarations.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ declare module "@webassemblyjs/ast" {
139139
args: string[];
140140
result: string[];
141141
}
142+
143+
// Node matcher
144+
export function isGlobalType(n: Node): boolean;
145+
export function isTable(n: Node): boolean;
146+
export function isMemory(n: Node): boolean;
147+
export function isFuncImportDescr(n: Node): boolean;
142148
}
143149

144150
/**

lib/wasm/WebAssemblyGenerator.js

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const { shrinkPaddedLEB128 } = require("@webassemblyjs/wasm-opt");
1313
const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
1414
const { decode } = require("@webassemblyjs/wasm-parser");
1515
const t = require("@webassemblyjs/ast");
16+
const {
17+
moduleContextFromModuleAST
18+
} = require("@webassemblyjs/helper-module-context");
1619

1720
const 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 => {
319290
const 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
})

lib/wasm/WebAssemblyParser.js

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,6 @@ const WebAssemblyExportImportedDependency = require("../dependencies/WebAssembly
1616

1717
/** @typedef {import("../Module")} Module */
1818

19-
/**
20-
* @param {t.ModuleImport} n the import
21-
* @returns {boolean} true, if a memory was imported
22-
*/
23-
const isMemoryImport = n => n.descr.type === "Memory";
24-
25-
/**
26-
* @param {t.ModuleImport} n the import
27-
* @returns {boolean} true, if a table was imported
28-
*/
29-
const isTableImport = n => n.descr.type === "Table";
30-
31-
/**
32-
* @param {t.ModuleImport} n the import
33-
* @returns {boolean} true, if a func was imported
34-
*/
35-
const isFuncImport = n => n.descr.type === "FuncImportDescr";
36-
37-
/**
38-
* @param {t.ModuleImport} n the import
39-
* @returns {boolean} true, if a global was imported
40-
*/
41-
const isGlobalImport = n => n.descr.type === "GlobalType";
42-
4319
const JS_COMPAT_TYPES = new Set(["i32", "f32", "f64"]);
4420

4521
/**
@@ -160,16 +136,16 @@ class WebAssemblyParser extends Tapable {
160136
/** @type {false | string} */
161137
let onlyDirectImport = false;
162138

163-
if (isMemoryImport(node) === true) {
139+
if (t.isMemory(node.descr) === true) {
164140
onlyDirectImport = "Memory";
165-
} else if (isTableImport(node) === true) {
141+
} else if (t.isTable(node.descr) === true) {
166142
onlyDirectImport = "Table";
167-
} else if (isFuncImport(node) === true) {
143+
} else if (t.isFuncImportDescr(node.descr) === true) {
168144
const incompatibleType = getJsIncompatibleType(node.descr.signature);
169145
if (incompatibleType) {
170146
onlyDirectImport = `Non-JS-compatible Func Sigurature (${incompatibleType})`;
171147
}
172-
} else if (isGlobalImport(node) === true) {
148+
} else if (t.isGlobalType(node.descr) === true) {
173149
const type = node.descr.valtype;
174150
if (!JS_COMPAT_TYPES.has(type)) {
175151
onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
@@ -185,7 +161,7 @@ class WebAssemblyParser extends Tapable {
185161

186162
state.module.addDependency(dep);
187163

188-
if (isGlobalImport(node)) {
164+
if (t.isGlobalType(node.descr)) {
189165
importedGlobals.push(node);
190166
}
191167
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
66
"license": "MIT",
77
"dependencies": {
8-
"@webassemblyjs/ast": "1.5.10",
9-
"@webassemblyjs/helper-module-context": "1.5.10",
10-
"@webassemblyjs/wasm-edit": "1.5.10",
11-
"@webassemblyjs/wasm-opt": "1.5.10",
12-
"@webassemblyjs/wasm-parser": "1.5.10",
8+
"@webassemblyjs/ast": "1.5.11",
9+
"@webassemblyjs/helper-module-context": "1.5.11",
10+
"@webassemblyjs/wasm-edit": "1.5.11",
11+
"@webassemblyjs/wasm-opt": "1.5.11",
12+
"@webassemblyjs/wasm-parser": "1.5.11",
1313
"acorn": "^5.0.0",
1414
"acorn-dynamic-import": "^3.0.0",
1515
"ajv": "^6.1.0",

0 commit comments

Comments
 (0)