feat: add missing preprocessing

This commit is contained in:
Sven SAULEAU 2018-05-14 13:00:19 +02:00
parent 98541fdb29
commit 6d8bc91a9b
No known key found for this signature in database
GPG Key ID: F5464AC83B687AD1
3 changed files with 21 additions and 6 deletions

2
declarations.d.ts vendored
View File

@ -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;

View File

@ -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)

View File

@ -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",