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("../ModuleSourceTypesConstants");
|
---|
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("../Dependency")} Dependency */
|
---|
21 | /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
|
---|
22 | /** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
---|
23 | /** @typedef {import("../Module")} Module */
|
---|
24 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
---|
25 | /** @typedef {import("../NormalModule")} NormalModule */
|
---|
26 | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
---|
27 |
|
---|
28 | class WebAssemblyJavascriptGenerator extends Generator {
|
---|
29 | /**
|
---|
30 | * @param {NormalModule} module fresh module
|
---|
31 | * @returns {SourceTypes} available types (do not mutate)
|
---|
32 | */
|
---|
33 | getTypes(module) {
|
---|
34 | return WEBASSEMBLY_TYPES;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * @param {NormalModule} module the module
|
---|
39 | * @param {string=} 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 | * @param {NormalModule} module module for which the code should be generated
|
---|
48 | * @param {GenerateContext} generateContext context for generate
|
---|
49 | * @returns {Source | null} generated code
|
---|
50 | */
|
---|
51 | generate(module, generateContext) {
|
---|
52 | const {
|
---|
53 | runtimeTemplate,
|
---|
54 | moduleGraph,
|
---|
55 | chunkGraph,
|
---|
56 | runtimeRequirements,
|
---|
57 | runtime
|
---|
58 | } = generateContext;
|
---|
59 | /** @type {InitFragment<InitFragment<string>>[]} */
|
---|
60 | const initFragments = [];
|
---|
61 |
|
---|
62 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
---|
63 |
|
---|
64 | let needExportsCopy = false;
|
---|
65 | const importedModules = new Map();
|
---|
66 | const initParams = [];
|
---|
67 | let index = 0;
|
---|
68 | for (const dep of module.dependencies) {
|
---|
69 | const moduleDep =
|
---|
70 | dep && dep instanceof ModuleDependency ? dep : undefined;
|
---|
71 | if (moduleGraph.getModule(dep)) {
|
---|
72 | let importData = importedModules.get(moduleGraph.getModule(dep));
|
---|
73 | if (importData === undefined) {
|
---|
74 | importedModules.set(
|
---|
75 | moduleGraph.getModule(dep),
|
---|
76 | (importData = {
|
---|
77 | importVar: `m${index}`,
|
---|
78 | index,
|
---|
79 | request: (moduleDep && moduleDep.userRequest) || undefined,
|
---|
80 | names: new Set(),
|
---|
81 | reexports: []
|
---|
82 | })
|
---|
83 | );
|
---|
84 | index++;
|
---|
85 | }
|
---|
86 | if (dep instanceof WebAssemblyImportDependency) {
|
---|
87 | importData.names.add(dep.name);
|
---|
88 | if (dep.description.type === "GlobalType") {
|
---|
89 | const exportName = dep.name;
|
---|
90 | const importedModule = moduleGraph.getModule(dep);
|
---|
91 |
|
---|
92 | if (importedModule) {
|
---|
93 | const usedName = moduleGraph
|
---|
94 | .getExportsInfo(importedModule)
|
---|
95 | .getUsedName(exportName, runtime);
|
---|
96 | if (usedName) {
|
---|
97 | initParams.push(
|
---|
98 | runtimeTemplate.exportFromImport({
|
---|
99 | moduleGraph,
|
---|
100 | module: importedModule,
|
---|
101 | request: dep.request,
|
---|
102 | importVar: importData.importVar,
|
---|
103 | originModule: module,
|
---|
104 | exportName: dep.name,
|
---|
105 | asiSafe: true,
|
---|
106 | isCall: false,
|
---|
107 | callContext: null,
|
---|
108 | defaultInterop: true,
|
---|
109 | initFragments,
|
---|
110 | runtime,
|
---|
111 | runtimeRequirements
|
---|
112 | })
|
---|
113 | );
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | if (dep instanceof WebAssemblyExportImportedDependency) {
|
---|
119 | importData.names.add(dep.name);
|
---|
120 | const usedName = moduleGraph
|
---|
121 | .getExportsInfo(module)
|
---|
122 | .getUsedName(dep.exportName, runtime);
|
---|
123 | if (usedName) {
|
---|
124 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
125 | const exportProp = `${module.exportsArgument}[${JSON.stringify(
|
---|
126 | usedName
|
---|
127 | )}]`;
|
---|
128 | const defineStatement = Template.asString([
|
---|
129 | `${exportProp} = ${runtimeTemplate.exportFromImport({
|
---|
130 | moduleGraph,
|
---|
131 | module: /** @type {Module} */ (moduleGraph.getModule(dep)),
|
---|
132 | request: dep.request,
|
---|
133 | importVar: importData.importVar,
|
---|
134 | originModule: module,
|
---|
135 | exportName: dep.name,
|
---|
136 | asiSafe: true,
|
---|
137 | isCall: false,
|
---|
138 | callContext: null,
|
---|
139 | defaultInterop: true,
|
---|
140 | initFragments,
|
---|
141 | runtime,
|
---|
142 | runtimeRequirements
|
---|
143 | })};`,
|
---|
144 | `if(WebAssembly.Global) ${exportProp} = ` +
|
---|
145 | `new WebAssembly.Global({ value: ${JSON.stringify(
|
---|
146 | dep.valueType
|
---|
147 | )} }, ${exportProp});`
|
---|
148 | ]);
|
---|
149 | importData.reexports.push(defineStatement);
|
---|
150 | needExportsCopy = true;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | const importsCode = Template.asString(
|
---|
156 | Array.from(
|
---|
157 | importedModules,
|
---|
158 | ([module, { importVar, request, reexports }]) => {
|
---|
159 | const importStatement = runtimeTemplate.importStatement({
|
---|
160 | module,
|
---|
161 | chunkGraph,
|
---|
162 | request,
|
---|
163 | importVar,
|
---|
164 | originModule: module,
|
---|
165 | runtimeRequirements
|
---|
166 | });
|
---|
167 | return importStatement[0] + importStatement[1] + reexports.join("\n");
|
---|
168 | }
|
---|
169 | )
|
---|
170 | );
|
---|
171 |
|
---|
172 | const copyAllExports =
|
---|
173 | exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused &&
|
---|
174 | !needExportsCopy;
|
---|
175 |
|
---|
176 | // need these globals
|
---|
177 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
178 | runtimeRequirements.add(RuntimeGlobals.moduleId);
|
---|
179 | runtimeRequirements.add(RuntimeGlobals.wasmInstances);
|
---|
180 | if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused) {
|
---|
181 | runtimeRequirements.add(RuntimeGlobals.makeNamespaceObject);
|
---|
182 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
183 | }
|
---|
184 | if (!copyAllExports) {
|
---|
185 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
186 | }
|
---|
187 |
|
---|
188 | // create source
|
---|
189 | const source = new RawSource(
|
---|
190 | [
|
---|
191 | '"use strict";',
|
---|
192 | "// Instantiate WebAssembly module",
|
---|
193 | `var wasmExports = ${RuntimeGlobals.wasmInstances}[${module.moduleArgument}.id];`,
|
---|
194 |
|
---|
195 | exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused
|
---|
196 | ? `${RuntimeGlobals.makeNamespaceObject}(${module.exportsArgument});`
|
---|
197 | : "",
|
---|
198 |
|
---|
199 | // this must be before import for circular dependencies
|
---|
200 | "// export exports from WebAssembly module",
|
---|
201 | copyAllExports
|
---|
202 | ? `${module.moduleArgument}.exports = wasmExports;`
|
---|
203 | : "for(var name in wasmExports) " +
|
---|
204 | "if(name) " +
|
---|
205 | `${module.exportsArgument}[name] = wasmExports[name];`,
|
---|
206 | "// exec imports from WebAssembly module (for esm order)",
|
---|
207 | importsCode,
|
---|
208 | "",
|
---|
209 | "// exec wasm module",
|
---|
210 | `wasmExports[""](${initParams.join(", ")})`
|
---|
211 | ].join("\n")
|
---|
212 | );
|
---|
213 | return InitFragment.addToSource(source, initFragments, generateContext);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | module.exports = WebAssemblyJavascriptGenerator;
|
---|