WXL
4 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
node_modules/webpack/lib/wasm-sync/WebAssemblyGenerator.js
@@ -11,16 +11,19 @@
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 */
@@ -30,15 +33,18 @@
/** @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
@@ -50,7 +56,7 @@
   );
/**
 * Removes the start instruction
 * Removes start func.
 * @param {object} state state
 * @param {AST} state.ast Module's ast
 * @returns {ArrayBufferTransform} transform
@@ -199,6 +205,7 @@
      // 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) {
@@ -251,6 +258,7 @@
   (bin) =>
      editWithAST(ast, bin, {
         /**
          * Processes the provided path.
          * @param {NodePath<ModuleExport>} path path
          */
         ModuleExport(path) {
@@ -270,11 +278,13 @@
         }
      });
/** @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 =
@@ -282,6 +292,7 @@
   (bin) =>
      editWithAST(ast, bin, {
         /**
          * Processes the provided path.
          * @param {NodePath<ModuleImport>} path path
          */
         ModuleImport(path) {
@@ -383,11 +394,11 @@
 * 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,
@@ -403,12 +414,14 @@
};
/**
 * 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) {
@@ -417,6 +430,7 @@
   }
   /**
    * Returns the source types available for this module.
    * @param {NormalModule} module fresh module
    * @returns {SourceTypes} available types (do not mutate)
    */
@@ -425,8 +439,9 @@
   }
   /**
    * 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) {
@@ -438,6 +453,7 @@
   }
   /**
    * 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
@@ -519,6 +535,7 @@
   }
   /**
    * 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
@@ -527,6 +544,17 @@
   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;