diff --git a/declarations.d.ts b/declarations.d.ts index bab2f6008..0aa48818c 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -81,7 +81,7 @@ declare module "@webassemblyjs/ast" { export class TypeInstruction extends Node {} export class IndexInFuncSection extends Node {} export function indexLiteral(index: number): IndexLiteral; - export function numberLiteral(num: number): NumberLiteral; + export function numberLiteralFromRaw(num: number): NumberLiteral; export function global(globalType: string, nodes: Node[]): Global; export function identifier(indentifier: string): Identifier; export function funcParam(valType: string, id: Identifier): FuncParam; diff --git a/lib/wasm/WebAssemblyGenerator.js b/lib/wasm/WebAssemblyGenerator.js index ba9a232bc..7afb2648e 100644 --- a/lib/wasm/WebAssemblyGenerator.js +++ b/lib/wasm/WebAssemblyGenerator.js @@ -9,6 +9,7 @@ const Template = require("../Template"); const WebAssemblyUtils = require("./WebAssemblyUtils"); const { RawSource } = require("webpack-sources"); +const { shrinkPaddedLEB128 } = require("@webassemblyjs/wasm-opt"); const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit"); const { decode } = require("@webassemblyjs/wasm-parser"); const t = require("@webassemblyjs/ast"); @@ -16,6 +17,21 @@ const t = require("@webassemblyjs/ast"); /** @typedef {import("../Module")} Module */ /** @typedef {import("./WebAssemblyUtils").UsedWasmDependency} UsedWasmDependency */ +/** + * @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform + */ + +/** + * Run some preprocessing on the binary before wasm-edit + * + * @param {ArrayBuffer} ab - original binary + * @returns {ArrayBufferTransform} transform + */ +function preprocess(ab) { + const optBin = shrinkPaddedLEB128(new Uint8Array(ab)); + return optBin.buffer; +} + /** * @template T * @param {Function[]} fns transforms @@ -42,9 +58,6 @@ const isGlobalImport = n => n.descr.type === "GlobalType"; const isFuncImport = n => n.descr.type === "FuncImportDescr"; // TODO replace with @callback -/** - * @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform - */ /** * Removes the start instruction @@ -176,7 +189,7 @@ const rewriteImportedGlobals = state => bin => { newGlobals.push( t.global(globalType, [ - t.objectInstruction("const", "i32", [t.numberLiteral(0)]) + t.objectInstruction("const", "i32", [t.numberLiteralFromRaw(0)]) ]) ); @@ -315,7 +328,8 @@ const getUsedDependencyMap = module => { class WebAssemblyGenerator extends Generator { generate(module) { - const bin = module.originalSource().source(); + let bin = module.originalSource().source(); + bin = preprocess(bin); const initFuncId = t.identifier( Array.isArray(module.usedExports) diff --git a/package.json b/package.json index 09df29240..2cb009c11 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dependencies": { "@webassemblyjs/ast": "1.5.3", "@webassemblyjs/wasm-edit": "1.5.3", + "@webassemblyjs/wasm-opt": "1.5.3", "@webassemblyjs/wasm-parser": "1.5.3", "acorn": "^5.0.0", "acorn-dynamic-import": "^3.0.0",