webpack 4.8.2 → 4.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -52
- package/bin/webpack.js +128 -43
- package/lib/AmdMainTemplatePlugin.js +10 -0
- package/lib/AsyncDependencyToInitialChunkError.js +12 -2
- package/lib/BannerPlugin.js +115 -101
- package/lib/CaseSensitiveModulesWarning.js +20 -2
- package/lib/Chunk.js +1 -0
- package/lib/ChunkGroup.js +465 -465
- package/lib/ChunkRenderError.js +8 -0
- package/lib/ChunkTemplate.js +71 -71
- package/lib/Compilation.js +1 -1
- package/lib/Compiler.js +2 -1
- package/lib/ContextModule.js +8 -8
- package/lib/DllPlugin.js +3 -1
- package/lib/DllReferencePlugin.js +2 -1
- package/lib/Entrypoint.js +54 -54
- package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +115 -115
- package/lib/ExportPropertyMainTemplatePlugin.js +13 -0
- package/lib/Generator.js +52 -52
- package/lib/HotModuleReplacement.runtime.js +633 -633
- package/lib/JsonParser.js +2 -1
- package/lib/LibManifestPlugin.js +9 -0
- package/lib/LibraryTemplatePlugin.js +66 -33
- package/lib/MainTemplate.js +468 -468
- package/lib/Module.js +3 -3
- package/lib/ModuleDependencyError.js +12 -2
- package/lib/NormalModuleFactory.js +5 -3
- package/lib/Parser.js +27 -23
- package/lib/ProgressPlugin.js +1 -1
- package/lib/RecordIdsPlugin.js +3 -1
- package/lib/RuntimeTemplate.js +1 -1
- package/lib/SetVarMainTemplatePlugin.js +12 -0
- package/lib/SourceMapDevToolPlugin.js +11 -13
- package/lib/Template.js +289 -290
- package/lib/UmdMainTemplatePlugin.js +67 -32
- package/lib/WebpackError.js +8 -2
- package/lib/compareLocations.js +20 -0
- package/lib/debug/ProfilingPlugin.js +416 -416
- package/lib/dependencies/ContextDependencyHelpers.js +142 -142
- package/lib/dependencies/WebpackMissingModule.js +2 -2
- package/lib/optimize/RemoveEmptyChunksPlugin.js +42 -40
- package/lib/optimize/RuntimeChunkPlugin.js +9 -5
- package/lib/optimize/SplitChunksPlugin.js +195 -124
- package/lib/util/Queue.js +46 -46
- package/lib/util/SetHelpers.js +48 -48
- package/lib/util/SortableSet.js +106 -106
- package/lib/util/StackedSetMap.js +128 -128
- package/lib/util/cachedMerge.js +13 -0
- package/lib/util/identifier.js +5 -0
- package/lib/util/objectToMap.js +16 -16
- package/lib/wasm/WebAssemblyGenerator.js +280 -280
- package/lib/wasm/WebAssemblyParser.js +79 -79
- package/lib/web/JsonpMainTemplatePlugin.js +2 -2
- package/package.json +21 -17
- package/schemas/WebpackOptions.json +12 -1
- package/schemas/plugins/BannerPlugin.json +96 -85
- package/schemas/plugins/DllPlugin.json +4 -0
@@ -1,280 +1,280 @@
|
|
1
|
-
/*
|
2
|
-
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
-
Author Tobias Koppers @sokra
|
4
|
-
*/
|
5
|
-
"use strict";
|
6
|
-
|
7
|
-
const Generator = require("../Generator");
|
8
|
-
const { RawSource } = require("webpack-sources");
|
9
|
-
|
10
|
-
const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
|
11
|
-
const { decode } = require("@webassemblyjs/wasm-parser");
|
12
|
-
const t = require("@webassemblyjs/ast");
|
13
|
-
|
14
|
-
function compose(...fns) {
|
15
|
-
return fns.reverse().reduce((prevFn, nextFn) => {
|
16
|
-
return value => nextFn(prevFn(value));
|
17
|
-
}, value => value);
|
18
|
-
}
|
19
|
-
|
20
|
-
// Utility functions
|
21
|
-
|
22
|
-
/**
|
23
|
-
* @param {t.ModuleImport} n the import
|
24
|
-
* @returns {boolean} true, if a global was imported
|
25
|
-
*/
|
26
|
-
const isGlobalImport = n => n.descr.type === "GlobalType";
|
27
|
-
|
28
|
-
/**
|
29
|
-
* @param {t.ModuleImport} n the import
|
30
|
-
* @returns {boolean} true, if a func was imported
|
31
|
-
*/
|
32
|
-
const isFuncImport = n => n.descr.type === "FuncImportDescr";
|
33
|
-
|
34
|
-
const initFuncId = t.identifier("__webpack_init__");
|
35
|
-
|
36
|
-
// TODO replace with @callback
|
37
|
-
/**
|
38
|
-
* @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
|
39
|
-
*/
|
40
|
-
|
41
|
-
/**
|
42
|
-
* Removes the start instruction
|
43
|
-
*
|
44
|
-
* @param {Object} state - unused state
|
45
|
-
* @returns {ArrayBufferTransform} transform
|
46
|
-
*/
|
47
|
-
const removeStartFunc = state => bin => {
|
48
|
-
return editWithAST(state.ast, bin, {
|
49
|
-
Start(path) {
|
50
|
-
path.remove();
|
51
|
-
}
|
52
|
-
});
|
53
|
-
};
|
54
|
-
|
55
|
-
/**
|
56
|
-
* Retrieve the start function
|
57
|
-
*
|
58
|
-
* @param {Object} ast - Module's AST
|
59
|
-
* @returns {t.Identifier | undefined} - node if any
|
60
|
-
*/
|
61
|
-
function getStartFuncIndex(ast) {
|
62
|
-
let startAtFuncIndex;
|
63
|
-
|
64
|
-
t.traverse(ast, {
|
65
|
-
Start({ node }) {
|
66
|
-
startAtFuncIndex = node.index;
|
67
|
-
}
|
68
|
-
});
|
69
|
-
|
70
|
-
return startAtFuncIndex;
|
71
|
-
}
|
72
|
-
|
73
|
-
/**
|
74
|
-
* Get imported globals
|
75
|
-
*
|
76
|
-
* @param {Object} ast - Module's AST
|
77
|
-
* @returns {Array<t.ModuleImport>} - nodes
|
78
|
-
*/
|
79
|
-
function getImportedGlobals(ast) {
|
80
|
-
const importedGlobals = [];
|
81
|
-
|
82
|
-
t.traverse(ast, {
|
83
|
-
ModuleImport({ node }) {
|
84
|
-
if (isGlobalImport(node) === true) {
|
85
|
-
importedGlobals.push(node);
|
86
|
-
}
|
87
|
-
}
|
88
|
-
});
|
89
|
-
|
90
|
-
return importedGlobals;
|
91
|
-
}
|
92
|
-
|
93
|
-
function getCountImportedFunc(ast) {
|
94
|
-
let count = 0;
|
95
|
-
|
96
|
-
t.traverse(ast, {
|
97
|
-
ModuleImport({ node }) {
|
98
|
-
if (isFuncImport(node) === true) {
|
99
|
-
count++;
|
100
|
-
}
|
101
|
-
}
|
102
|
-
});
|
103
|
-
|
104
|
-
return count;
|
105
|
-
}
|
106
|
-
|
107
|
-
/**
|
108
|
-
* Get next type index
|
109
|
-
*
|
110
|
-
* @param {Object} ast - Module's AST
|
111
|
-
* @returns {t.IndexLiteral} - index
|
112
|
-
*/
|
113
|
-
function getNextTypeIndex(ast) {
|
114
|
-
const typeSectionMetadata = t.getSectionMetadata(ast, "type");
|
115
|
-
|
116
|
-
if (typeof typeSectionMetadata === "undefined") {
|
117
|
-
return t.indexLiteral(0);
|
118
|
-
}
|
119
|
-
|
120
|
-
return t.indexLiteral(typeSectionMetadata.vectorOfSize.value);
|
121
|
-
}
|
122
|
-
|
123
|
-
/**
|
124
|
-
* Get next func index
|
125
|
-
*
|
126
|
-
* The Func section metadata provide informations for implemented funcs
|
127
|
-
* in order to have the correct index we shift the index by number of external
|
128
|
-
* functions.
|
129
|
-
*
|
130
|
-
* @param {Object} ast - Module's AST
|
131
|
-
* @param {Number} countImportedFunc - number of imported funcs
|
132
|
-
* @returns {t.IndexLiteral} - index
|
133
|
-
*/
|
134
|
-
function getNextFuncIndex(ast, countImportedFunc) {
|
135
|
-
const funcSectionMetadata = t.getSectionMetadata(ast, "func");
|
136
|
-
|
137
|
-
if (typeof funcSectionMetadata === "undefined") {
|
138
|
-
return t.indexLiteral(0 + countImportedFunc);
|
139
|
-
}
|
140
|
-
|
141
|
-
const vectorOfSize = funcSectionMetadata.vectorOfSize.value;
|
142
|
-
|
143
|
-
return t.indexLiteral(vectorOfSize + countImportedFunc);
|
144
|
-
}
|
145
|
-
|
146
|
-
/**
|
147
|
-
* Rewrite the import globals:
|
148
|
-
* - removes the ModuleImport instruction
|
149
|
-
* - injects at the same offset a mutable global of the same time
|
150
|
-
*
|
151
|
-
* Since the imported globals are before the other global declarations, our
|
152
|
-
* indices will be preserved.
|
153
|
-
*
|
154
|
-
* Note that globals will become mutable.
|
155
|
-
*
|
156
|
-
* @param {Object} state - unused state
|
157
|
-
* @returns {ArrayBufferTransform} transform
|
158
|
-
*/
|
159
|
-
const rewriteImportedGlobals = state => bin => {
|
160
|
-
const newGlobals = [];
|
161
|
-
|
162
|
-
bin = editWithAST(state.ast, bin, {
|
163
|
-
ModuleImport(path) {
|
164
|
-
if (isGlobalImport(path.node) === true) {
|
165
|
-
const globalType = path.node.descr;
|
166
|
-
|
167
|
-
globalType.mutability = "var";
|
168
|
-
|
169
|
-
newGlobals.push(
|
170
|
-
t.global(globalType, [
|
171
|
-
t.objectInstruction("const", "i32", [t.numberLiteral(0)])
|
172
|
-
])
|
173
|
-
);
|
174
|
-
|
175
|
-
path.remove();
|
176
|
-
}
|
177
|
-
}
|
178
|
-
});
|
179
|
-
|
180
|
-
// Add global declaration instructions
|
181
|
-
return addWithAST(state.ast, bin, newGlobals);
|
182
|
-
};
|
183
|
-
|
184
|
-
/**
|
185
|
-
* Add an init function.
|
186
|
-
*
|
187
|
-
* The init function fills the globals given input arguments.
|
188
|
-
*
|
189
|
-
* @param {Object} state transformation state
|
190
|
-
* @param {Object} state.ast - Module's ast
|
191
|
-
* @param {t.IndexLiteral} state.startAtFuncIndex index of the start function
|
192
|
-
* @param {t.ModuleImport[]} state.importedGlobals list of imported globals
|
193
|
-
* @param {t.IndexLiteral} state.nextFuncIndex index of the next function
|
194
|
-
* @param {t.IndexLiteral} state.nextTypeIndex index of the next type
|
195
|
-
* @returns {ArrayBufferTransform} transform
|
196
|
-
*/
|
197
|
-
const addInitFunction = ({
|
198
|
-
ast,
|
199
|
-
startAtFuncIndex,
|
200
|
-
importedGlobals,
|
201
|
-
nextFuncIndex,
|
202
|
-
nextTypeIndex
|
203
|
-
}) => bin => {
|
204
|
-
const funcParams = importedGlobals.map(importedGlobal => {
|
205
|
-
// used for debugging
|
206
|
-
const id = t.identifier(`${importedGlobal.module}.${importedGlobal.name}`);
|
207
|
-
|
208
|
-
return t.funcParam(importedGlobal.descr.valtype, id);
|
209
|
-
});
|
210
|
-
|
211
|
-
const funcBody = importedGlobals.reduce((acc, importedGlobal, index) => {
|
212
|
-
const args = [t.indexLiteral(index)];
|
213
|
-
const body = [
|
214
|
-
t.instruction("get_local", args),
|
215
|
-
t.instruction("set_global", args)
|
216
|
-
];
|
217
|
-
|
218
|
-
return [...acc, ...body];
|
219
|
-
}, []);
|
220
|
-
|
221
|
-
if (typeof startAtFuncIndex !== "undefined") {
|
222
|
-
funcBody.push(t.callInstruction(startAtFuncIndex));
|
223
|
-
}
|
224
|
-
|
225
|
-
const funcResults = [];
|
226
|
-
|
227
|
-
// Code section
|
228
|
-
const func = t.func(initFuncId, funcParams, funcResults, funcBody);
|
229
|
-
|
230
|
-
// Type section
|
231
|
-
const functype = t.typeInstructionFunc(
|
232
|
-
func.signature.params,
|
233
|
-
func.signature.result
|
234
|
-
);
|
235
|
-
|
236
|
-
// Func section
|
237
|
-
const funcindex = t.indexInFuncSection(nextTypeIndex);
|
238
|
-
|
239
|
-
// Export section
|
240
|
-
const moduleExport = t.moduleExport(initFuncId.value, "Func", nextFuncIndex);
|
241
|
-
|
242
|
-
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
|
243
|
-
};
|
244
|
-
|
245
|
-
class WebAssemblyGenerator extends Generator {
|
246
|
-
generate(module) {
|
247
|
-
const bin = module.originalSource().source();
|
248
|
-
|
249
|
-
const ast = decode(bin, {
|
250
|
-
ignoreDataSection: true,
|
251
|
-
ignoreCodeSection: true
|
252
|
-
});
|
253
|
-
|
254
|
-
const importedGlobals = getImportedGlobals(ast);
|
255
|
-
const countImportedFunc = getCountImportedFunc(ast);
|
256
|
-
const startAtFuncIndex = getStartFuncIndex(ast);
|
257
|
-
const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
|
258
|
-
const nextTypeIndex = getNextTypeIndex(ast);
|
259
|
-
|
260
|
-
const transform = compose(
|
261
|
-
removeStartFunc({ ast }),
|
262
|
-
|
263
|
-
rewriteImportedGlobals({ ast }),
|
264
|
-
|
265
|
-
addInitFunction({
|
266
|
-
ast,
|
267
|
-
importedGlobals,
|
268
|
-
startAtFuncIndex,
|
269
|
-
nextFuncIndex,
|
270
|
-
nextTypeIndex
|
271
|
-
})
|
272
|
-
);
|
273
|
-
|
274
|
-
const newBin = transform(bin);
|
275
|
-
|
276
|
-
return new RawSource(newBin);
|
277
|
-
}
|
278
|
-
}
|
279
|
-
|
280
|
-
module.exports = WebAssemblyGenerator;
|
1
|
+
/*
|
2
|
+
MIT License http://www.opensource.org/licenses/mit-license.php
|
3
|
+
Author Tobias Koppers @sokra
|
4
|
+
*/
|
5
|
+
"use strict";
|
6
|
+
|
7
|
+
const Generator = require("../Generator");
|
8
|
+
const { RawSource } = require("webpack-sources");
|
9
|
+
|
10
|
+
const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
|
11
|
+
const { decode } = require("@webassemblyjs/wasm-parser");
|
12
|
+
const t = require("@webassemblyjs/ast");
|
13
|
+
|
14
|
+
function compose(...fns) {
|
15
|
+
return fns.reverse().reduce((prevFn, nextFn) => {
|
16
|
+
return value => nextFn(prevFn(value));
|
17
|
+
}, value => value);
|
18
|
+
}
|
19
|
+
|
20
|
+
// Utility functions
|
21
|
+
|
22
|
+
/**
|
23
|
+
* @param {t.ModuleImport} n the import
|
24
|
+
* @returns {boolean} true, if a global was imported
|
25
|
+
*/
|
26
|
+
const isGlobalImport = n => n.descr.type === "GlobalType";
|
27
|
+
|
28
|
+
/**
|
29
|
+
* @param {t.ModuleImport} n the import
|
30
|
+
* @returns {boolean} true, if a func was imported
|
31
|
+
*/
|
32
|
+
const isFuncImport = n => n.descr.type === "FuncImportDescr";
|
33
|
+
|
34
|
+
const initFuncId = t.identifier("__webpack_init__");
|
35
|
+
|
36
|
+
// TODO replace with @callback
|
37
|
+
/**
|
38
|
+
* @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
|
39
|
+
*/
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Removes the start instruction
|
43
|
+
*
|
44
|
+
* @param {Object} state - unused state
|
45
|
+
* @returns {ArrayBufferTransform} transform
|
46
|
+
*/
|
47
|
+
const removeStartFunc = state => bin => {
|
48
|
+
return editWithAST(state.ast, bin, {
|
49
|
+
Start(path) {
|
50
|
+
path.remove();
|
51
|
+
}
|
52
|
+
});
|
53
|
+
};
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Retrieve the start function
|
57
|
+
*
|
58
|
+
* @param {Object} ast - Module's AST
|
59
|
+
* @returns {t.Identifier | undefined} - node if any
|
60
|
+
*/
|
61
|
+
function getStartFuncIndex(ast) {
|
62
|
+
let startAtFuncIndex;
|
63
|
+
|
64
|
+
t.traverse(ast, {
|
65
|
+
Start({ node }) {
|
66
|
+
startAtFuncIndex = node.index;
|
67
|
+
}
|
68
|
+
});
|
69
|
+
|
70
|
+
return startAtFuncIndex;
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Get imported globals
|
75
|
+
*
|
76
|
+
* @param {Object} ast - Module's AST
|
77
|
+
* @returns {Array<t.ModuleImport>} - nodes
|
78
|
+
*/
|
79
|
+
function getImportedGlobals(ast) {
|
80
|
+
const importedGlobals = [];
|
81
|
+
|
82
|
+
t.traverse(ast, {
|
83
|
+
ModuleImport({ node }) {
|
84
|
+
if (isGlobalImport(node) === true) {
|
85
|
+
importedGlobals.push(node);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
});
|
89
|
+
|
90
|
+
return importedGlobals;
|
91
|
+
}
|
92
|
+
|
93
|
+
function getCountImportedFunc(ast) {
|
94
|
+
let count = 0;
|
95
|
+
|
96
|
+
t.traverse(ast, {
|
97
|
+
ModuleImport({ node }) {
|
98
|
+
if (isFuncImport(node) === true) {
|
99
|
+
count++;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
});
|
103
|
+
|
104
|
+
return count;
|
105
|
+
}
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Get next type index
|
109
|
+
*
|
110
|
+
* @param {Object} ast - Module's AST
|
111
|
+
* @returns {t.IndexLiteral} - index
|
112
|
+
*/
|
113
|
+
function getNextTypeIndex(ast) {
|
114
|
+
const typeSectionMetadata = t.getSectionMetadata(ast, "type");
|
115
|
+
|
116
|
+
if (typeof typeSectionMetadata === "undefined") {
|
117
|
+
return t.indexLiteral(0);
|
118
|
+
}
|
119
|
+
|
120
|
+
return t.indexLiteral(typeSectionMetadata.vectorOfSize.value);
|
121
|
+
}
|
122
|
+
|
123
|
+
/**
|
124
|
+
* Get next func index
|
125
|
+
*
|
126
|
+
* The Func section metadata provide informations for implemented funcs
|
127
|
+
* in order to have the correct index we shift the index by number of external
|
128
|
+
* functions.
|
129
|
+
*
|
130
|
+
* @param {Object} ast - Module's AST
|
131
|
+
* @param {Number} countImportedFunc - number of imported funcs
|
132
|
+
* @returns {t.IndexLiteral} - index
|
133
|
+
*/
|
134
|
+
function getNextFuncIndex(ast, countImportedFunc) {
|
135
|
+
const funcSectionMetadata = t.getSectionMetadata(ast, "func");
|
136
|
+
|
137
|
+
if (typeof funcSectionMetadata === "undefined") {
|
138
|
+
return t.indexLiteral(0 + countImportedFunc);
|
139
|
+
}
|
140
|
+
|
141
|
+
const vectorOfSize = funcSectionMetadata.vectorOfSize.value;
|
142
|
+
|
143
|
+
return t.indexLiteral(vectorOfSize + countImportedFunc);
|
144
|
+
}
|
145
|
+
|
146
|
+
/**
|
147
|
+
* Rewrite the import globals:
|
148
|
+
* - removes the ModuleImport instruction
|
149
|
+
* - injects at the same offset a mutable global of the same time
|
150
|
+
*
|
151
|
+
* Since the imported globals are before the other global declarations, our
|
152
|
+
* indices will be preserved.
|
153
|
+
*
|
154
|
+
* Note that globals will become mutable.
|
155
|
+
*
|
156
|
+
* @param {Object} state - unused state
|
157
|
+
* @returns {ArrayBufferTransform} transform
|
158
|
+
*/
|
159
|
+
const rewriteImportedGlobals = state => bin => {
|
160
|
+
const newGlobals = [];
|
161
|
+
|
162
|
+
bin = editWithAST(state.ast, bin, {
|
163
|
+
ModuleImport(path) {
|
164
|
+
if (isGlobalImport(path.node) === true) {
|
165
|
+
const globalType = path.node.descr;
|
166
|
+
|
167
|
+
globalType.mutability = "var";
|
168
|
+
|
169
|
+
newGlobals.push(
|
170
|
+
t.global(globalType, [
|
171
|
+
t.objectInstruction("const", "i32", [t.numberLiteral(0)])
|
172
|
+
])
|
173
|
+
);
|
174
|
+
|
175
|
+
path.remove();
|
176
|
+
}
|
177
|
+
}
|
178
|
+
});
|
179
|
+
|
180
|
+
// Add global declaration instructions
|
181
|
+
return addWithAST(state.ast, bin, newGlobals);
|
182
|
+
};
|
183
|
+
|
184
|
+
/**
|
185
|
+
* Add an init function.
|
186
|
+
*
|
187
|
+
* The init function fills the globals given input arguments.
|
188
|
+
*
|
189
|
+
* @param {Object} state transformation state
|
190
|
+
* @param {Object} state.ast - Module's ast
|
191
|
+
* @param {t.IndexLiteral} state.startAtFuncIndex index of the start function
|
192
|
+
* @param {t.ModuleImport[]} state.importedGlobals list of imported globals
|
193
|
+
* @param {t.IndexLiteral} state.nextFuncIndex index of the next function
|
194
|
+
* @param {t.IndexLiteral} state.nextTypeIndex index of the next type
|
195
|
+
* @returns {ArrayBufferTransform} transform
|
196
|
+
*/
|
197
|
+
const addInitFunction = ({
|
198
|
+
ast,
|
199
|
+
startAtFuncIndex,
|
200
|
+
importedGlobals,
|
201
|
+
nextFuncIndex,
|
202
|
+
nextTypeIndex
|
203
|
+
}) => bin => {
|
204
|
+
const funcParams = importedGlobals.map(importedGlobal => {
|
205
|
+
// used for debugging
|
206
|
+
const id = t.identifier(`${importedGlobal.module}.${importedGlobal.name}`);
|
207
|
+
|
208
|
+
return t.funcParam(importedGlobal.descr.valtype, id);
|
209
|
+
});
|
210
|
+
|
211
|
+
const funcBody = importedGlobals.reduce((acc, importedGlobal, index) => {
|
212
|
+
const args = [t.indexLiteral(index)];
|
213
|
+
const body = [
|
214
|
+
t.instruction("get_local", args),
|
215
|
+
t.instruction("set_global", args)
|
216
|
+
];
|
217
|
+
|
218
|
+
return [...acc, ...body];
|
219
|
+
}, []);
|
220
|
+
|
221
|
+
if (typeof startAtFuncIndex !== "undefined") {
|
222
|
+
funcBody.push(t.callInstruction(startAtFuncIndex));
|
223
|
+
}
|
224
|
+
|
225
|
+
const funcResults = [];
|
226
|
+
|
227
|
+
// Code section
|
228
|
+
const func = t.func(initFuncId, funcParams, funcResults, funcBody);
|
229
|
+
|
230
|
+
// Type section
|
231
|
+
const functype = t.typeInstructionFunc(
|
232
|
+
func.signature.params,
|
233
|
+
func.signature.result
|
234
|
+
);
|
235
|
+
|
236
|
+
// Func section
|
237
|
+
const funcindex = t.indexInFuncSection(nextTypeIndex);
|
238
|
+
|
239
|
+
// Export section
|
240
|
+
const moduleExport = t.moduleExport(initFuncId.value, "Func", nextFuncIndex);
|
241
|
+
|
242
|
+
return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
|
243
|
+
};
|
244
|
+
|
245
|
+
class WebAssemblyGenerator extends Generator {
|
246
|
+
generate(module) {
|
247
|
+
const bin = module.originalSource().source();
|
248
|
+
|
249
|
+
const ast = decode(bin, {
|
250
|
+
ignoreDataSection: true,
|
251
|
+
ignoreCodeSection: true
|
252
|
+
});
|
253
|
+
|
254
|
+
const importedGlobals = getImportedGlobals(ast);
|
255
|
+
const countImportedFunc = getCountImportedFunc(ast);
|
256
|
+
const startAtFuncIndex = getStartFuncIndex(ast);
|
257
|
+
const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
|
258
|
+
const nextTypeIndex = getNextTypeIndex(ast);
|
259
|
+
|
260
|
+
const transform = compose(
|
261
|
+
removeStartFunc({ ast }),
|
262
|
+
|
263
|
+
rewriteImportedGlobals({ ast }),
|
264
|
+
|
265
|
+
addInitFunction({
|
266
|
+
ast,
|
267
|
+
importedGlobals,
|
268
|
+
startAtFuncIndex,
|
269
|
+
nextFuncIndex,
|
270
|
+
nextTypeIndex
|
271
|
+
})
|
272
|
+
);
|
273
|
+
|
274
|
+
const newBin = transform(bin);
|
275
|
+
|
276
|
+
return new RawSource(newBin);
|
277
|
+
}
|
278
|
+
}
|
279
|
+
|
280
|
+
module.exports = WebAssemblyGenerator;
|