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