Skip to content

Commit 6d8bc91

Browse files
committed
feat: add missing preprocessing
1 parent 98541fd commit 6d8bc91

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

declarations.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ declare module "@webassemblyjs/ast" {
8181
export class TypeInstruction extends Node {}
8282
export class IndexInFuncSection extends Node {}
8383
export function indexLiteral(index: number): IndexLiteral;
84-
export function numberLiteral(num: number): NumberLiteral;
84+
export function numberLiteralFromRaw(num: number): NumberLiteral;
8585
export function global(globalType: string, nodes: Node[]): Global;
8686
export function identifier(indentifier: string): Identifier;
8787
export function funcParam(valType: string, id: Identifier): FuncParam;

lib/wasm/WebAssemblyGenerator.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,29 @@ const Template = require("../Template");
99
const WebAssemblyUtils = require("./WebAssemblyUtils");
1010
const { RawSource } = require("webpack-sources");
1111

12+
const { shrinkPaddedLEB128 } = require("@webassemblyjs/wasm-opt");
1213
const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
1314
const { decode } = require("@webassemblyjs/wasm-parser");
1415
const t = require("@webassemblyjs/ast");
1516

1617
/** @typedef {import("../Module")} Module */
1718
/** @typedef {import("./WebAssemblyUtils").UsedWasmDependency} UsedWasmDependency */
1819

20+
/**
21+
* @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
22+
*/
23+
24+
/**
25+
* Run some preprocessing on the binary before wasm-edit
26+
*
27+
* @param {ArrayBuffer} ab - original binary
28+
* @returns {ArrayBufferTransform} transform
29+
*/
30+
function preprocess(ab) {
31+
const optBin = shrinkPaddedLEB128(new Uint8Array(ab));
32+
return optBin.buffer;
33+
}
34+
1935
/**
2036
* @template T
2137
* @param {Function[]} fns transforms
@@ -42,9 +58,6 @@ const isGlobalImport = n => n.descr.type === "GlobalType";
4258
const isFuncImport = n => n.descr.type === "FuncImportDescr";
4359

4460
// TODO replace with @callback
45-
/**
46-
* @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
47-
*/
4861

4962
/**
5063
* Removes the start instruction
@@ -176,7 +189,7 @@ const rewriteImportedGlobals = state => bin => {
176189

177190
newGlobals.push(
178191
t.global(globalType, [
179-
t.objectInstruction("const", "i32", [t.numberLiteral(0)])
192+
t.objectInstruction("const", "i32", [t.numberLiteralFromRaw(0)])
180193
])
181194
);
182195

@@ -315,7 +328,8 @@ const getUsedDependencyMap = module => {
315328

316329
class WebAssemblyGenerator extends Generator {
317330
generate(module) {
318-
const bin = module.originalSource().source();
331+
let bin = module.originalSource().source();
332+
bin = preprocess(bin);
319333

320334
const initFuncId = t.identifier(
321335
Array.isArray(module.usedExports)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {
88
"@webassemblyjs/ast": "1.5.3",
99
"@webassemblyjs/wasm-edit": "1.5.3",
10+
"@webassemblyjs/wasm-opt": "1.5.3",
1011
"@webassemblyjs/wasm-parser": "1.5.3",
1112
"acorn": "^5.0.0",
1213
"acorn-dynamic-import": "^3.0.0",

0 commit comments

Comments
 (0)