| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { RawSource } = require("webpack-sources");
|
|---|
| 9 | const { UsageState } = require("../ExportsInfo");
|
|---|
| 10 | const Generator = require("../Generator");
|
|---|
| 11 | const InitFragment = require("../InitFragment");
|
|---|
| 12 | const { WEBASSEMBLY_TYPES } = require("../ModuleSourceTypeConstants");
|
|---|
| 13 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 14 | const Template = require("../Template");
|
|---|
| 15 | const ModuleDependency = require("../dependencies/ModuleDependency");
|
|---|
| 16 | const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
|
|---|
| 17 | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
|
|---|
| 18 |
|
|---|
| 19 | /** @typedef {import("webpack-sources").Source} Source */
|
|---|
| 20 | /** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
|---|
| 21 | /** @typedef {import("../Module")} Module */
|
|---|
| 22 | /** @typedef {import("../Module").SourceType} SourceType */
|
|---|
| 23 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
|---|
| 24 | /** @typedef {import("../NormalModule")} NormalModule */
|
|---|
| 25 |
|
|---|
| 26 | class WebAssemblyJavascriptGenerator extends Generator {
|
|---|
| 27 | /**
|
|---|
| 28 | * Returns the source types available for this module.
|
|---|
| 29 | * @param {NormalModule} module fresh module
|
|---|
| 30 | * @returns {SourceTypes} available types (do not mutate)
|
|---|
| 31 | */
|
|---|
| 32 | getTypes(module) {
|
|---|
| 33 | return WEBASSEMBLY_TYPES;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Returns the estimated size for the requested source type.
|
|---|
| 38 | * @param {NormalModule} module the module
|
|---|
| 39 | * @param {SourceType=} type source type
|
|---|
| 40 | * @returns {number} estimate size of the module
|
|---|
| 41 | */
|
|---|
| 42 | getSize(module, type) {
|
|---|
| 43 | return 95 + module.dependencies.length * 5;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Generates generated code for this runtime module.
|
|---|
| 48 | * @param {NormalModule} module module for which the code should be generated
|
|---|
| 49 | * @param {GenerateContext} generateContext context for generate
|
|---|
| 50 | * @returns {Source | null} generated code
|
|---|
| 51 | */
|
|---|
| 52 | generate(module, generateContext) {
|
|---|
| 53 | const {
|
|---|
| 54 | runtimeTemplate,
|
|---|
| 55 | moduleGraph,
|
|---|
| 56 | chunkGraph,
|
|---|
| 57 | runtimeRequirements,
|
|---|
| 58 | runtime
|
|---|
| 59 | } = generateContext;
|
|---|
| 60 | /** @type {InitFragment<GenerateContext>[]} */
|
|---|
| 61 | const initFragments = [];
|
|---|
| 62 |
|
|---|
| 63 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
|---|
| 64 |
|
|---|
| 65 | let needExportsCopy = false;
|
|---|
| 66 | /** @typedef {{ dependency: ModuleDependency | undefined, importVar: string, index: number, request: string | undefined, names: Set<string>, reexports: string[] }} ImportData */
|
|---|
| 67 | /** @type {Map<Module, ImportData>} */
|
|---|
| 68 | const importedModules = new Map();
|
|---|
| 69 | /** @type {string[]} */
|
|---|
| 70 | const initParams = [];
|
|---|
| 71 | let index = 0;
|
|---|
| 72 | for (const dep of module.dependencies) {
|
|---|
| 73 | const moduleDep =
|
|---|
| 74 | dep && dep instanceof ModuleDependency ? dep : undefined;
|
|---|
| 75 | const mod = moduleGraph.getModule(dep);
|
|---|
| 76 | if (mod) {
|
|---|
| 77 | let importData = importedModules.get(mod);
|
|---|
| 78 | if (importData === undefined) {
|
|---|
| 79 | importedModules.set(
|
|---|
| 80 | mod,
|
|---|
| 81 | (importData = {
|
|---|
| 82 | dependency: moduleDep,
|
|---|
| 83 | importVar: `m${index}`,
|
|---|
| 84 | index,
|
|---|
| 85 | request: (moduleDep && moduleDep.userRequest) || undefined,
|
|---|
| 86 | names: new Set(),
|
|---|
| 87 | reexports: []
|
|---|
| 88 | })
|
|---|
| 89 | );
|
|---|
| 90 | index++;
|
|---|
| 91 | }
|
|---|
| 92 | if (dep instanceof WebAssemblyImportDependency) {
|
|---|
| 93 | importData.names.add(dep.name);
|
|---|
| 94 | if (dep.description.type === "GlobalType") {
|
|---|
| 95 | const exportName = dep.name;
|
|---|
| 96 | const importedModule = moduleGraph.getModule(dep);
|
|---|
| 97 |
|
|---|
| 98 | if (importedModule) {
|
|---|
| 99 | const usedName = moduleGraph
|
|---|
| 100 | .getExportsInfo(importedModule)
|
|---|
| 101 | .getUsedName(exportName, runtime);
|
|---|
| 102 | if (usedName) {
|
|---|
| 103 | initParams.push(
|
|---|
| 104 | runtimeTemplate.exportFromImport({
|
|---|
| 105 | moduleGraph,
|
|---|
| 106 | chunkGraph,
|
|---|
| 107 | module: importedModule,
|
|---|
| 108 | request: dep.request,
|
|---|
| 109 | importVar: importData.importVar,
|
|---|
| 110 | originModule: module,
|
|---|
| 111 | exportName: dep.name,
|
|---|
| 112 | asiSafe: true,
|
|---|
| 113 | isCall: false,
|
|---|
| 114 | callContext: null,
|
|---|
| 115 | defaultInterop: true,
|
|---|
| 116 | initFragments,
|
|---|
| 117 | runtime,
|
|---|
| 118 | runtimeRequirements,
|
|---|
| 119 | dependency: dep
|
|---|
| 120 | })
|
|---|
| 121 | );
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | if (dep instanceof WebAssemblyExportImportedDependency) {
|
|---|
| 127 | importData.names.add(dep.name);
|
|---|
| 128 | const usedName = moduleGraph
|
|---|
| 129 | .getExportsInfo(module)
|
|---|
| 130 | .getUsedName(dep.exportName, runtime);
|
|---|
| 131 | if (usedName) {
|
|---|
| 132 | runtimeRequirements.add(RuntimeGlobals.exports);
|
|---|
| 133 | const exportProp = `${module.exportsArgument}[${JSON.stringify(
|
|---|
| 134 | usedName
|
|---|
| 135 | )}]`;
|
|---|
| 136 | const defineStatement = Template.asString([
|
|---|
| 137 | `${exportProp} = ${runtimeTemplate.exportFromImport({
|
|---|
| 138 | moduleGraph,
|
|---|
| 139 | module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
|---|
| 140 | chunkGraph,
|
|---|
| 141 | request: dep.request,
|
|---|
| 142 | importVar: importData.importVar,
|
|---|
| 143 | originModule: module,
|
|---|
| 144 | exportName: dep.name,
|
|---|
| 145 | asiSafe: true,
|
|---|
| 146 | isCall: false,
|
|---|
| 147 | callContext: null,
|
|---|
| 148 | defaultInterop: true,
|
|---|
| 149 | initFragments,
|
|---|
| 150 | runtime,
|
|---|
| 151 | runtimeRequirements,
|
|---|
| 152 | dependency: dep
|
|---|
| 153 | })};`,
|
|---|
| 154 | `if(WebAssembly.Global) ${exportProp} = ` +
|
|---|
| 155 | `new WebAssembly.Global({ value: ${JSON.stringify(
|
|---|
| 156 | dep.valueType
|
|---|
| 157 | )} }, ${exportProp});`
|
|---|
| 158 | ]);
|
|---|
| 159 | importData.reexports.push(defineStatement);
|
|---|
| 160 | needExportsCopy = true;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | const importsCode = Template.asString(
|
|---|
| 166 | Array.from(
|
|---|
| 167 | importedModules,
|
|---|
| 168 | ([module, { importVar, request, reexports, dependency }]) => {
|
|---|
| 169 | const importStatement = runtimeTemplate.importStatement({
|
|---|
| 170 | module,
|
|---|
| 171 | moduleGraph,
|
|---|
| 172 | chunkGraph,
|
|---|
| 173 | request,
|
|---|
| 174 | importVar,
|
|---|
| 175 | originModule: module,
|
|---|
| 176 | runtimeRequirements,
|
|---|
| 177 | dependency
|
|---|
| 178 | });
|
|---|
| 179 | return importStatement[0] + importStatement[1] + reexports.join("\n");
|
|---|
| 180 | }
|
|---|
| 181 | )
|
|---|
| 182 | );
|
|---|
| 183 |
|
|---|
| 184 | const copyAllExports =
|
|---|
| 185 | exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused &&
|
|---|
| 186 | !needExportsCopy;
|
|---|
| 187 |
|
|---|
| 188 | // need these globals
|
|---|
| 189 | runtimeRequirements.add(RuntimeGlobals.module);
|
|---|
| 190 | runtimeRequirements.add(RuntimeGlobals.moduleId);
|
|---|
| 191 | runtimeRequirements.add(RuntimeGlobals.wasmInstances);
|
|---|
| 192 | if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused) {
|
|---|
| 193 | runtimeRequirements.add(RuntimeGlobals.makeNamespaceObject);
|
|---|
| 194 | runtimeRequirements.add(RuntimeGlobals.exports);
|
|---|
| 195 | }
|
|---|
| 196 | if (!copyAllExports) {
|
|---|
| 197 | runtimeRequirements.add(RuntimeGlobals.exports);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | // create source
|
|---|
| 201 | const source = new RawSource(
|
|---|
| 202 | [
|
|---|
| 203 | '"use strict";',
|
|---|
| 204 | "// Instantiate WebAssembly module",
|
|---|
| 205 | `var wasmExports = ${RuntimeGlobals.wasmInstances}[${module.moduleArgument}.id];`,
|
|---|
| 206 |
|
|---|
| 207 | exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused
|
|---|
| 208 | ? `${RuntimeGlobals.makeNamespaceObject}(${module.exportsArgument});`
|
|---|
| 209 | : "",
|
|---|
| 210 |
|
|---|
| 211 | // this must be before import for circular dependencies
|
|---|
| 212 | "// export exports from WebAssembly module",
|
|---|
| 213 | copyAllExports
|
|---|
| 214 | ? `${module.moduleArgument}.exports = wasmExports;`
|
|---|
| 215 | : "for(var name in wasmExports) " +
|
|---|
| 216 | "if(name) " +
|
|---|
| 217 | `${module.exportsArgument}[name] = wasmExports[name];`,
|
|---|
| 218 | "// exec imports from WebAssembly module (for esm order)",
|
|---|
| 219 | importsCode,
|
|---|
| 220 | "",
|
|---|
| 221 | "// exec wasm module",
|
|---|
| 222 | `wasmExports[""](${initParams.join(", ")})`
|
|---|
| 223 | ].join("\n")
|
|---|
| 224 | );
|
|---|
| 225 | return InitFragment.addToSource(source, initFragments, generateContext);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * Generates fallback output for the provided error condition.
|
|---|
| 230 | * @param {Error} error the error
|
|---|
| 231 | * @param {NormalModule} module module for which the code should be generated
|
|---|
| 232 | * @param {GenerateContext} generateContext context for generate
|
|---|
| 233 | * @returns {Source | null} generated code
|
|---|
| 234 | */
|
|---|
| 235 | generateError(error, module, generateContext) {
|
|---|
| 236 | return new RawSource(`throw new Error(${JSON.stringify(error.message)});`);
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | module.exports = WebAssemblyJavascriptGenerator;
|
|---|