mirror of https://github.com/webpack/webpack.git
feat: add missing preprocessing
This commit is contained in:
parent
98541fdb29
commit
6d8bc91a9b
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in New Issue