GeezerVison is showing github.com — Merge pull request #7254 from xtuc/feat-remove-extra-wasm-decodings · webpack/webpack@8d2f421
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: declarations.d.ts +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,19 +33,28 @@ declare module "chrome-trace-event" { | 33 | 33 | declare module "@webassemblyjs/ast" { | 34 | 34 | export function traverse( | 35 | 35 | 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 | + } | 37 | 41 | ); | 38 | | - export class Node { | 39 | | - index: number; | | 42 | + export class NodePath<T> { | | 43 | + node: T; | 40 | 44 | } | | 45 | + export class Node {} | 41 | 46 | export class Identifier extends Node { | 42 | 47 | value: string; | 43 | 48 | } | | 49 | + export class Start extends Node { | | 50 | + index: Identifier; | | 51 | + } | 44 | 52 | export class ModuleImport extends Node { | 45 | 53 | module: string; | 46 | 54 | descr: { | 47 | 55 | type: string; | 48 | 56 | valtype: string; | | 57 | + id: string; | 49 | 58 | }; | 50 | 59 | name: string; | 51 | 60 | } | |
Collapse file Copy file name to clipboardExpand all lines: lib/wasm/WebAssemblyGenerator.js +23-16Lines changed: 23 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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,35 +239,32 @@ 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 { | 236 | 246 | generate(module) { | 237 | 247 | const bin = module.originalSource().source(); | 238 | 248 |
| 239 | | - // FIXME(sven): this module is parsed twice, we could preserve the AST | 240 | | - // from wasm/WebAssemblyParser.js | 241 | 249 | const ast = decode(bin, { | 242 | 250 | ignoreDataSection: true, | 243 | 251 | ignoreCodeSection: true | 244 | 252 | }); | 245 | 253 |
| 246 | 254 | const importedGlobals = getImportedGlobals(ast); | 247 | | - const funcSectionMetadata = t.getSectionMetadata(ast, "func"); | 248 | 255 | const countImportedFunc = getCountImportedFunc(ast); | 249 | 256 | const startAtFuncIndex = getStartFuncIndex(ast); | 250 | 257 | const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc); | 251 | 258 | const nextTypeIndex = getNextTypeIndex(ast); | 252 | 259 |
| 253 | 260 | const transform = compose( | 254 | | - removeStartFunc({}), | | 261 | + removeStartFunc({ ast }), | 255 | 262 |
| 256 | | - rewriteImportedGlobals({}), | | 263 | + rewriteImportedGlobals({ ast }), | 257 | 264 |
| 258 | 265 | addInitFunction({ | | 266 | + ast, | 259 | 267 | importedGlobals, | 260 | | - funcSectionMetadata, | 261 | 268 | startAtFuncIndex, | 262 | 269 | nextFuncIndex, | 263 | 270 | nextTypeIndex | |
Collapse file Copy file name to clipboardExpand all lines: lib/wasm/WebAssemblyParser.js +12-13Lines changed: 12 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,17 +10,19 @@ const { decode } = require("@webassemblyjs/wasm-parser"); | 10 | 10 | const { Tapable } = require("tapable"); | 11 | 11 | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency"); | 12 | 12 |
| | 13 | +/** @typedef {import("../Module")} Module */ | | 14 | + | 13 | 15 | /** | 14 | | - * @param {t.ModuleImport} moduleImport the import | | 16 | + * @param {t.ModuleImport} n the import | 15 | 17 | * @returns {boolean} true, if a memory was imported | 16 | 18 | */ | 17 | | -const isMemoryImport = moduleImport => moduleImport.descr.type === "Memory"; | | 19 | +const isMemoryImport = n => n.descr.type === "Memory"; | 18 | 20 |
| 19 | 21 | /** | 20 | | - * @param {t.ModuleImport} moduleImport the import | | 22 | + * @param {t.ModuleImport} n the import | 21 | 23 | * @returns {boolean} true, if a table was imported | 22 | 24 | */ | 23 | | -const isTableImport = moduleImport => moduleImport.descr.type === "Table"; | | 25 | +const isTableImport = n => n.descr.type === "Table"; | 24 | 26 |
| 25 | 27 | const decoderOpts = { | 26 | 28 | ignoreCodeSection: true, | @@ -45,27 +47,24 @@ class WebAssemblyParser extends Tapable { | 45 | 47 | const exports = (state.module.buildMeta.providedExports = []); | 46 | 48 | t.traverse(ast, { | 47 | 49 | ModuleExport({ node }) { | 48 | | - const moduleExport = /** @type {t.ModuleExport} */ (node); | 49 | | - exports.push(moduleExport.name); | | 50 | + exports.push(node.name); | 50 | 51 | }, | 51 | 52 |
| 52 | 53 | ModuleImport({ node }) { | 53 | | - const moduleImport = /** @type {t.ModuleImport} */ (node); | 54 | | - | 55 | 54 | let onlyDirectImport = false; | 56 | 55 |
| 57 | | - if (isMemoryImport(moduleImport) === true) { | | 56 | + if (isMemoryImport(node) === true) { | 58 | 57 | onlyDirectImport = true; | 59 | 58 | } | 60 | 59 |
| 61 | | - if (isTableImport(moduleImport) === true) { | | 60 | + if (isTableImport(node) === true) { | 62 | 61 | onlyDirectImport = true; | 63 | 62 | } | 64 | 63 |
| 65 | 64 | const dep = new WebAssemblyImportDependency( | 66 | | - moduleImport.module, | 67 | | - moduleImport.name, | 68 | | - moduleImport.descr, | | 65 | + node.module, | | 66 | + node.name, | | 67 | + node.descr, | 69 | 68 | onlyDirectImport | 70 | 69 | ); | 71 | 70 |
| |
Collapse file Copy file name to clipboardExpand all lines: package.json +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | "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.", | 6 | 6 | "license": "MIT", | 7 | 7 | "dependencies": { | 8 | | - "@webassemblyjs/ast": "1.3.2", | 9 | | - "@webassemblyjs/wasm-edit": "1.3.2", | 10 | | - "@webassemblyjs/wasm-parser": "1.3.2", | | 8 | + "@webassemblyjs/ast": "1.4.2", | | 9 | + "@webassemblyjs/wasm-edit": "1.4.2", | | 10 | + "@webassemblyjs/wasm-parser": "1.4.2", | 11 | 11 | "acorn": "^5.0.0", | 12 | 12 | "acorn-dynamic-import": "^3.0.0", | 13 | 13 | "ajv": "^6.1.0", | |
|