| | |
| | | const { decode } = require("@webassemblyjs/wasm-parser"); |
| | | const { RawSource } = require("webpack-sources"); |
| | | const Generator = require("../Generator"); |
| | | const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypesConstants"); |
| | | const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypeConstants"); |
| | | const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency"); |
| | | const WebAssemblyUtils = require("./WebAssemblyUtils"); |
| | | |
| | | /** @typedef {import("webpack-sources").Source} Source */ |
| | | /** @typedef {import("../Generator").GenerateContext} GenerateContext */ |
| | | /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */ |
| | | /** @typedef {import("../Module")} Module */ |
| | | /** @typedef {import("../Module").SourceType} SourceType */ |
| | | /** @typedef {import("../Module").SourceTypes} SourceTypes */ |
| | | /** @typedef {import("../ModuleGraph")} ModuleGraph */ |
| | | /** @typedef {import("../NormalModule")} NormalModule */ |
| | | /** @typedef {import("../util/Hash")} Hash */ |
| | | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ |
| | | /** @typedef {import("./WebAssemblyUtils").UsedWasmDependency} UsedWasmDependency */ |
| | | /** @typedef {import("@webassemblyjs/ast").Instruction} Instruction */ |
| | |
| | | /** @typedef {import("@webassemblyjs/ast").AST} AST */ |
| | | /** @typedef {import("@webassemblyjs/ast").GlobalType} GlobalType */ |
| | | /** |
| | | * Defines the node path type used by this module. |
| | | * @template T |
| | | * @typedef {import("@webassemblyjs/ast").NodePath<T>} NodePath |
| | | */ |
| | | |
| | | /** |
| | | * Defines the array buffer transform type used by this module. |
| | | * @typedef {(buf: ArrayBuffer) => ArrayBuffer} ArrayBufferTransform |
| | | */ |
| | | |
| | | /** |
| | | * Returns composed transform. |
| | | * @template T |
| | | * @param {((prev: ArrayBuffer) => ArrayBuffer)[]} fns transforms |
| | | * @returns {(buf: ArrayBuffer) => ArrayBuffer} composed transform |
| | |
| | | ); |
| | | |
| | | /** |
| | | * Removes the start instruction |
| | | * Removes start func. |
| | | * @param {object} state state |
| | | * @param {AST} state.ast Module's ast |
| | | * @returns {ArrayBufferTransform} transform |
| | |
| | | // in order to preserve non-imported global's order we need to re-inject |
| | | // those as well |
| | | /** |
| | | * Processes the provided path. |
| | | * @param {NodePath<Global>} path path |
| | | */ |
| | | Global(path) { |
| | |
| | | (bin) => |
| | | editWithAST(ast, bin, { |
| | | /** |
| | | * Processes the provided path. |
| | | * @param {NodePath<ModuleExport>} path path |
| | | */ |
| | | ModuleExport(path) { |
| | |
| | | } |
| | | }); |
| | | |
| | | /** @typedef {Map<string, UsedWasmDependency>} Mapping */ |
| | | |
| | | /** |
| | | * Mangle import names and modules |
| | | * @param {object} state state |
| | | * @param {AST} state.ast Module's ast |
| | | * @param {Map<string, UsedWasmDependency>} state.usedDependencyMap mappings to mangle names |
| | | * @param {Mapping} state.usedDependencyMap mappings to mangle names |
| | | * @returns {ArrayBufferTransform} transform |
| | | */ |
| | | const rewriteImports = |
| | |
| | | (bin) => |
| | | editWithAST(ast, bin, { |
| | | /** |
| | | * Processes the provided path. |
| | | * @param {NodePath<ModuleImport>} path path |
| | | */ |
| | | ModuleImport(path) { |
| | |
| | | * Extract mangle mappings from module |
| | | * @param {ModuleGraph} moduleGraph module graph |
| | | * @param {Module} module current module |
| | | * @param {boolean | undefined} mangle mangle imports |
| | | * @returns {Map<string, UsedWasmDependency>} mappings to mangled names |
| | | * @param {boolean=} mangle mangle imports |
| | | * @returns {Mapping} mappings to mangled names |
| | | */ |
| | | const getUsedDependencyMap = (moduleGraph, module, mangle) => { |
| | | /** @type {Map<string, UsedWasmDependency>} */ |
| | | /** @type {Mapping} */ |
| | | const map = new Map(); |
| | | for (const usedDep of WebAssemblyUtils.getUsedDependencies( |
| | | moduleGraph, |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Represents the web assembly generator runtime component. |
| | | * @typedef {object} WebAssemblyGeneratorOptions |
| | | * @property {boolean=} mangleImports mangle imports |
| | | */ |
| | | |
| | | class WebAssemblyGenerator extends Generator { |
| | | /** |
| | | * Creates an instance of WebAssemblyGenerator. |
| | | * @param {WebAssemblyGeneratorOptions} options options |
| | | */ |
| | | constructor(options) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the source types available for this module. |
| | | * @param {NormalModule} module fresh module |
| | | * @returns {SourceTypes} available types (do not mutate) |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the estimated size for the requested source type. |
| | | * @param {NormalModule} module the module |
| | | * @param {string=} type source type |
| | | * @param {SourceType=} type source type |
| | | * @returns {number} estimate size of the module |
| | | */ |
| | | getSize(module, type) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Generates generated code for this runtime module. |
| | | * @param {NormalModule} module module for which the code should be generated |
| | | * @param {GenerateContext} generateContext context for generate |
| | | * @returns {Source | null} generated code |
| | |
| | | } |
| | | |
| | | /** |
| | | * Generates fallback output for the provided error condition. |
| | | * @param {Error} error the error |
| | | * @param {NormalModule} module module for which the code should be generated |
| | | * @param {GenerateContext} generateContext context for generate |
| | |
| | | generateError(error, module, generateContext) { |
| | | return new RawSource(error.message); |
| | | } |
| | | |
| | | /** |
| | | * Updates the hash with the data contributed by this instance. |
| | | * @param {Hash} hash hash that will be modified |
| | | * @param {UpdateHashContext} updateHashContext context for updating hash |
| | | */ |
| | | updateHash(hash, updateHashContext) { |
| | | if (this.options.mangleImports) { |
| | | hash.update("mangle-imports"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | module.exports = WebAssemblyGenerator; |