feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -13,6 +13,7 @@ const WebAssemblyUtils = require("./WebAssemblyUtils");
/** @typedef {import("@webassemblyjs/ast").Signature} Signature */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */
@@ -89,7 +90,8 @@ const generateImportObject = (
const instanceVar = `m${waitForInstances.size}`;
waitForInstances.set(
instanceVar,
chunkGraph.getModuleId(/** @type {Module} */ (importedModule))
/** @type {ModuleId} */
(chunkGraph.getModuleId(/** @type {Module} */ (importedModule)))
);
properties.push({
module,
@@ -100,7 +102,7 @@ const generateImportObject = (
const params =
/** @type {Signature} */
(description.signature).params.map(
(param, k) => "p" + k + param.valtype
(param, k) => `p${k}${param.valtype}`
);
const mod = `${RuntimeGlobals.moduleCache}[${JSON.stringify(
@@ -121,7 +123,7 @@ const generateImportObject = (
module,
name,
value: Template.asString([
modCode + `function(${params}) {`,
`${modCode}function(${params}) {`,
Template.indent([
`if(${cache} === undefined) ${cache} = ${modExports};`,
`return ${cache}[${JSON.stringify(usedName)}](${params});`
@@ -154,15 +156,15 @@ const generateImportObject = (
importObject = [
"return {",
Template.indent([
Array.from(propertiesByModule, ([module, list]) => {
return Template.asString([
Array.from(propertiesByModule, ([module, list]) =>
Template.asString([
`${JSON.stringify(module)}: {`,
Template.indent([
list.map(p => `${JSON.stringify(p.name)}: ${p.value}`).join(",\n")
]),
"}"
]);
}).join(",\n")
])
).join(",\n")
]),
"};"
];
@@ -200,13 +202,12 @@ const generateImportObject = (
]),
"},"
]);
} else {
return Template.asString([
`${moduleIdStringified}: function() {`,
Template.indent(importObject),
"},"
]);
}
return Template.asString([
`${moduleIdStringified}: function() {`,
Template.indent(importObject),
"},"
]);
};
/**
@@ -250,15 +251,15 @@ class WasmChunkLoadingRuntimeModule extends RuntimeModule {
const { mangleImports } = this;
/** @type {string[]} */
const declarations = [];
const importObjects = wasmModules.map(module => {
return generateImportObject(
const importObjects = wasmModules.map(module =>
generateImportObject(
chunkGraph,
module,
mangleImports,
declarations,
chunk.runtime
);
});
)
);
const chunkModuleIdMap = chunkGraph.getChunkModuleIdMap(chunk, m =>
m.type.startsWith("webassembly")
);
@@ -331,7 +332,7 @@ class WasmChunkLoadingRuntimeModule extends RuntimeModule {
`${fn}.wasm = function(chunkId, promises) {`,
Template.indent([
"",
`var wasmModules = wasmModuleMap[chunkId] || [];`,
"var wasmModules = wasmModuleMap[chunkId] || [];",
"",
"wasmModules.forEach(function(wasmModuleId, idx) {",
Template.indent([
@@ -342,7 +343,7 @@ class WasmChunkLoadingRuntimeModule extends RuntimeModule {
Template.indent(["promises.push(installedWasmModuleData);"]),
"else {",
Template.indent([
`var importObject = wasmImportObjects[wasmModuleId]();`,
"var importObject = wasmImportObjects[wasmModuleId]();",
`var req = ${this.generateLoadBinaryCode(wasmModuleSrcPath)};`,
"var promise;",
this.supportsStreaming

View File

@@ -5,14 +5,14 @@
"use strict";
const { RawSource } = require("webpack-sources");
const Generator = require("../Generator");
const WebAssemblyUtils = require("./WebAssemblyUtils");
const t = require("@webassemblyjs/ast");
const { moduleContextFromModuleAST } = require("@webassemblyjs/ast");
const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
const { decode } = require("@webassemblyjs/wasm-parser");
const { RawSource } = require("webpack-sources");
const Generator = require("../Generator");
const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypesConstants");
const WebAssemblyUtils = require("./WebAssemblyUtils");
const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
@@ -20,6 +20,7 @@ const WebAssemblyExportImportedDependency = require("../dependencies/WebAssembly
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").SourceTypes} SourceTypes */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
@@ -43,33 +44,27 @@ const WebAssemblyExportImportedDependency = require("../dependencies/WebAssembly
* @param {((prev: ArrayBuffer) => ArrayBuffer)[]} fns transforms
* @returns {Function} composed transform
*/
const compose = (...fns) => {
return fns.reduce(
(prevFn, nextFn) => {
return value => nextFn(prevFn(value));
},
const compose = (...fns) =>
fns.reduce(
(prevFn, nextFn) => value => nextFn(prevFn(value)),
value => value
);
};
/**
* Removes the start instruction
*
* @param {object} state state
* @param {object} state.ast Module's ast
* @returns {ArrayBufferTransform} transform
*/
const removeStartFunc = state => bin => {
return editWithAST(state.ast, bin, {
const removeStartFunc = state => bin =>
editWithAST(state.ast, bin, {
Start(path) {
path.remove();
}
});
};
/**
* Get imported globals
*
* @param {object} ast Module's AST
* @returns {t.ModuleImport[]} - nodes
*/
@@ -90,7 +85,6 @@ const getImportedGlobals = ast => {
/**
* Get the count for imported func
*
* @param {object} ast Module's AST
* @returns {number} - count
*/
@@ -110,7 +104,6 @@ const getCountImportedFunc = ast => {
/**
* Get next type index
*
* @param {object} ast Module's AST
* @returns {t.Index} - index
*/
@@ -126,11 +119,9 @@ const getNextTypeIndex = ast => {
/**
* Get next func index
*
* The Func section metadata provide information for implemented funcs
* in order to have the correct index we shift the index by number of external
* functions.
*
* @param {object} ast Module's AST
* @param {number} countImportedFunc number of imported funcs
* @returns {t.Index} - index
@@ -163,9 +154,8 @@ const createDefaultInitForGlobal = globalType => {
return t.objectInstruction("const", globalType.valtype, [
t.floatLiteral(66, false, false, "66")
]);
} else {
throw new Error("unknown type: " + globalType.valtype);
}
throw new Error(`unknown type: ${globalType.valtype}`);
};
/**
@@ -177,7 +167,6 @@ const createDefaultInitForGlobal = globalType => {
* indices will be preserved.
*
* Note that globals will become mutable.
*
* @param {object} state transformation state
* @param {object} state.ast Module's ast
* @param {t.Instruction[]} state.additionalInitCode list of addition instructions for the init function
@@ -258,8 +247,8 @@ const rewriteImportedGlobals = state => bin => {
*/
const rewriteExportNames =
({ ast, moduleGraph, module, externalExports, runtime }) =>
bin => {
return editWithAST(ast, bin, {
bin =>
editWithAST(ast, bin, {
/**
* @param {NodePath<ModuleExport>} path path
*/
@@ -279,7 +268,6 @@ const rewriteExportNames =
path.node.name = /** @type {string} */ (usedName);
}
});
};
/**
* Mangle import names and modules
@@ -290,14 +278,14 @@ const rewriteExportNames =
*/
const rewriteImports =
({ ast, usedDependencyMap }) =>
bin => {
return editWithAST(ast, bin, {
bin =>
editWithAST(ast, bin, {
/**
* @param {NodePath<ModuleImport>} path path
*/
ModuleImport(path) {
const result = usedDependencyMap.get(
path.node.module + ":" + path.node.name
`${path.node.module}:${path.node.name}`
);
if (result !== undefined) {
@@ -306,13 +294,11 @@ const rewriteImports =
}
}
});
};
/**
* Add an init function.
*
* The init function fills the globals given input arguments.
*
* @param {object} state transformation state
* @param {object} state.ast Module's ast
* @param {t.Identifier} state.initFuncId identifier of the init function
@@ -348,7 +334,7 @@ const addInitFunction =
/** @type {Instruction[]} */
const funcBody = [];
importedGlobals.forEach((importedGlobal, index) => {
for (const [index, _importedGlobal] of importedGlobals.entries()) {
const args = [t.indexLiteral(index)];
const body = [
t.instruction("get_local", args),
@@ -356,7 +342,7 @@ const addInitFunction =
];
funcBody.push(...body);
});
}
if (typeof startAtFuncOffset === "number") {
funcBody.push(
@@ -410,13 +396,11 @@ const getUsedDependencyMap = (moduleGraph, module, mangle) => {
const dep = usedDep.dependency;
const request = dep.request;
const exportName = dep.name;
map.set(request + ":" + exportName, usedDep);
map.set(`${request}:${exportName}`, usedDep);
}
return map;
};
const TYPES = new Set(["webassembly"]);
/**
* @typedef {object} WebAssemblyGeneratorOptions
* @property {boolean} [mangleImports] mangle imports
@@ -433,10 +417,10 @@ class WebAssemblyGenerator extends Generator {
/**
* @param {NormalModule} module fresh module
* @returns {Set<string>} available types (do not mutate)
* @returns {SourceTypes} available types (do not mutate)
*/
getTypes(module) {
return TYPES;
return WEBASSEMBLY_TYPES;
}
/**
@@ -455,7 +439,7 @@ class WebAssemblyGenerator extends Generator {
/**
* @param {NormalModule} module module for which the code should be generated
* @param {GenerateContext} generateContext context for generate
* @returns {Source} generated code
* @returns {Source | null} generated code
*/
generate(module, { moduleGraph, runtime }) {
const bin = /** @type {Source} */ (module.originalSource()).source();

View File

@@ -9,6 +9,7 @@ const { RawSource } = require("webpack-sources");
const { UsageState } = require("../ExportsInfo");
const Generator = require("../Generator");
const InitFragment = require("../InitFragment");
const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypesConstants");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const ModuleDependency = require("../dependencies/ModuleDependency");
@@ -20,18 +21,17 @@ const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDe
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").SourceTypes} SourceTypes */
/** @typedef {import("../NormalModule")} NormalModule */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
const TYPES = new Set(["webassembly"]);
class WebAssemblyJavascriptGenerator extends Generator {
/**
* @param {NormalModule} module fresh module
* @returns {Set<string>} available types (do not mutate)
* @returns {SourceTypes} available types (do not mutate)
*/
getTypes(module) {
return TYPES;
return WEBASSEMBLY_TYPES;
}
/**
@@ -46,7 +46,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
/**
* @param {NormalModule} module module for which the code should be generated
* @param {GenerateContext} generateContext context for generate
* @returns {Source} generated code
* @returns {Source | null} generated code
*/
generate(module, generateContext) {
const {
@@ -201,7 +201,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
copyAllExports
? `${module.moduleArgument}.exports = wasmExports;`
: "for(var name in wasmExports) " +
`if(name) ` +
"if(name) " +
`${module.exportsArgument}[name] = wasmExports[name];`,
"// exec imports from WebAssembly module (for esm order)",
importsCode,

View File

@@ -62,5 +62,5 @@ const getUsedDependencies = (moduleGraph, module, mangle) => {
return array;
};
exports.getUsedDependencies = getUsedDependencies;
exports.MANGLED_MODULE = MANGLED_MODULE;
module.exports.getUsedDependencies = getUsedDependencies;
module.exports.MANGLED_MODULE = MANGLED_MODULE;