You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

458 lines
12 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Generator = require("../Generator");
  7. const Template = require("../Template");
  8. const WebAssemblyUtils = require("./WebAssemblyUtils");
  9. const { RawSource } = require("webpack-sources");
  10. const { editWithAST, addWithAST } = require("@webassemblyjs/wasm-edit");
  11. const { decode } = require("@webassemblyjs/wasm-parser");
  12. const t = require("@webassemblyjs/ast");
  13. const {
  14. moduleContextFromModuleAST
  15. } = require("@webassemblyjs/helper-module-context");
  16. const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
  17. /** @typedef {import("../Module")} Module */
  18. /** @typedef {import("./WebAssemblyUtils").UsedWasmDependency} UsedWasmDependency */
  19. /** @typedef {import("../NormalModule")} NormalModule */
  20. /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
  21. /** @typedef {import("webpack-sources").Source} Source */
  22. /** @typedef {import("../Dependency").DependencyTemplate} DependencyTemplate */
  23. /**
  24. * @typedef {(ArrayBuffer) => ArrayBuffer} ArrayBufferTransform
  25. */
  26. /**
  27. * @template T
  28. * @param {Function[]} fns transforms
  29. * @returns {Function} composed transform
  30. */
  31. const compose = (...fns) => {
  32. return fns.reduce(
  33. (prevFn, nextFn) => {
  34. return value => nextFn(prevFn(value));
  35. },
  36. value => value
  37. );
  38. };
  39. // TODO replace with @callback
  40. /**
  41. * Removes the start instruction
  42. *
  43. * @param {Object} state unused state
  44. * @returns {ArrayBufferTransform} transform
  45. */
  46. const removeStartFunc = state => bin => {
  47. return editWithAST(state.ast, bin, {
  48. Start(path) {
  49. path.remove();
  50. }
  51. });
  52. };
  53. /**
  54. * Get imported globals
  55. *
  56. * @param {Object} ast Module's AST
  57. * @returns {Array<t.ModuleImport>} - nodes
  58. */
  59. const getImportedGlobals = ast => {
  60. const importedGlobals = [];
  61. t.traverse(ast, {
  62. ModuleImport({ node }) {
  63. if (t.isGlobalType(node.descr)) {
  64. importedGlobals.push(node);
  65. }
  66. }
  67. });
  68. return importedGlobals;
  69. };
  70. /**
  71. * Get the count for imported func
  72. *
  73. * @param {Object} ast Module's AST
  74. * @returns {Number} - count
  75. */
  76. const getCountImportedFunc = ast => {
  77. let count = 0;
  78. t.traverse(ast, {
  79. ModuleImport({ node }) {
  80. if (t.isFuncImportDescr(node.descr)) {
  81. count++;
  82. }
  83. }
  84. });
  85. return count;
  86. };
  87. /**
  88. * Get next type index
  89. *
  90. * @param {Object} ast Module's AST
  91. * @returns {t.Index} - index
  92. */
  93. const getNextTypeIndex = ast => {
  94. const typeSectionMetadata = t.getSectionMetadata(ast, "type");
  95. if (typeSectionMetadata === undefined) {
  96. return t.indexLiteral(0);
  97. }
  98. return t.indexLiteral(typeSectionMetadata.vectorOfSize.value);
  99. };
  100. /**
  101. * Get next func index
  102. *
  103. * The Func section metadata provide informations for implemented funcs
  104. * in order to have the correct index we shift the index by number of external
  105. * functions.
  106. *
  107. * @param {Object} ast Module's AST
  108. * @param {Number} countImportedFunc number of imported funcs
  109. * @returns {t.Index} - index
  110. */
  111. const getNextFuncIndex = (ast, countImportedFunc) => {
  112. const funcSectionMetadata = t.getSectionMetadata(ast, "func");
  113. if (funcSectionMetadata === undefined) {
  114. return t.indexLiteral(0 + countImportedFunc);
  115. }
  116. const vectorOfSize = funcSectionMetadata.vectorOfSize.value;
  117. return t.indexLiteral(vectorOfSize + countImportedFunc);
  118. };
  119. /**
  120. * Creates an init instruction for a global type
  121. * @param {t.GlobalType} globalType the global type
  122. * @returns {t.Instruction} init expression
  123. */
  124. const createDefaultInitForGlobal = globalType => {
  125. if (globalType.valtype[0] === "i") {
  126. // create NumberLiteral global initializer
  127. return t.objectInstruction("const", globalType.valtype, [
  128. t.numberLiteralFromRaw(66)
  129. ]);
  130. } else if (globalType.valtype[0] === "f") {
  131. // create FloatLiteral global initializer
  132. return t.objectInstruction("const", globalType.valtype, [
  133. t.floatLiteral(66, false, false, "66")
  134. ]);
  135. } else {
  136. throw new Error("unknown type: " + globalType.valtype);
  137. }
  138. };
  139. /**
  140. * Rewrite the import globals:
  141. * - removes the ModuleImport instruction
  142. * - injects at the same offset a mutable global of the same type
  143. *
  144. * Since the imported globals are before the other global declarations, our
  145. * indices will be preserved.
  146. *
  147. * Note that globals will become mutable.
  148. *
  149. * @param {Object} state unused state
  150. * @returns {ArrayBufferTransform} transform
  151. */
  152. const rewriteImportedGlobals = state => bin => {
  153. const additionalInitCode = state.additionalInitCode;
  154. const newGlobals = [];
  155. bin = editWithAST(state.ast, bin, {
  156. ModuleImport(path) {
  157. if (t.isGlobalType(path.node.descr)) {
  158. const globalType = path.node.descr;
  159. globalType.mutability = "var";
  160. const init = [
  161. createDefaultInitForGlobal(globalType),
  162. t.instruction("end")
  163. ];
  164. newGlobals.push(t.global(globalType, init));
  165. path.remove();
  166. }
  167. },
  168. // in order to preserve non-imported global's order we need to re-inject
  169. // those as well
  170. Global(path) {
  171. const { node } = path;
  172. const [init] = node.init;
  173. if (init.id === "get_global") {
  174. node.globalType.mutability = "var";
  175. const initialGlobalidx = init.args[0];
  176. node.init = [
  177. createDefaultInitForGlobal(node.globalType),
  178. t.instruction("end")
  179. ];
  180. additionalInitCode.push(
  181. /**
  182. * get_global in global initializer only works for imported globals.
  183. * They have the same indices as the init params, so use the
  184. * same index.
  185. */
  186. t.instruction("get_local", [initialGlobalidx]),
  187. t.instruction("set_global", [t.indexLiteral(newGlobals.length)])
  188. );
  189. }
  190. newGlobals.push(node);
  191. path.remove();
  192. }
  193. });
  194. // Add global declaration instructions
  195. return addWithAST(state.ast, bin, newGlobals);
  196. };
  197. /**
  198. * Rewrite the export names
  199. * @param {Object} state state
  200. * @param {Object} state.ast Module's ast
  201. * @param {Module} state.module Module
  202. * @param {Set<string>} state.externalExports Module
  203. * @returns {ArrayBufferTransform} transform
  204. */
  205. const rewriteExportNames = ({ ast, module, externalExports }) => bin => {
  206. return editWithAST(ast, bin, {
  207. ModuleExport(path) {
  208. const isExternal = externalExports.has(path.node.name);
  209. if (isExternal) {
  210. path.remove();
  211. return;
  212. }
  213. const usedName = module.isUsed(path.node.name);
  214. if (!usedName) {
  215. path.remove();
  216. return;
  217. }
  218. path.node.name = usedName;
  219. }
  220. });
  221. };
  222. /**
  223. * Mangle import names and modules
  224. * @param {Object} state state
  225. * @param {Object} state.ast Module's ast
  226. * @param {Map<string, UsedWasmDependency>} state.usedDependencyMap mappings to mangle names
  227. * @returns {ArrayBufferTransform} transform
  228. */
  229. const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
  230. return editWithAST(ast, bin, {
  231. ModuleImport(path) {
  232. const result = usedDependencyMap.get(
  233. path.node.module + ":" + path.node.name
  234. );
  235. if (result !== undefined) {
  236. path.node.module = result.module;
  237. path.node.name = result.name;
  238. }
  239. }
  240. });
  241. };
  242. /**
  243. * Add an init function.
  244. *
  245. * The init function fills the globals given input arguments.
  246. *
  247. * @param {Object} state transformation state
  248. * @param {Object} state.ast Module's ast
  249. * @param {t.Identifier} state.initFuncId identifier of the init function
  250. * @param {t.Index} state.startAtFuncOffset index of the start function
  251. * @param {t.ModuleImport[]} state.importedGlobals list of imported globals
  252. * @param {t.Instruction[]} state.additionalInitCode list of addition instructions for the init function
  253. * @param {t.Index} state.nextFuncIndex index of the next function
  254. * @param {t.Index} state.nextTypeIndex index of the next type
  255. * @returns {ArrayBufferTransform} transform
  256. */
  257. const addInitFunction = ({
  258. ast,
  259. initFuncId,
  260. startAtFuncOffset,
  261. importedGlobals,
  262. additionalInitCode,
  263. nextFuncIndex,
  264. nextTypeIndex
  265. }) => bin => {
  266. const funcParams = importedGlobals.map(importedGlobal => {
  267. // used for debugging
  268. const id = t.identifier(`${importedGlobal.module}.${importedGlobal.name}`);
  269. return t.funcParam(importedGlobal.descr.valtype, id);
  270. });
  271. const funcBody = importedGlobals.reduce((acc, importedGlobal, index) => {
  272. const args = [t.indexLiteral(index)];
  273. const body = [
  274. t.instruction("get_local", args),
  275. t.instruction("set_global", args)
  276. ];
  277. return [...acc, ...body];
  278. }, []);
  279. if (typeof startAtFuncOffset === "number") {
  280. funcBody.push(t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)));
  281. }
  282. for (const instr of additionalInitCode) {
  283. funcBody.push(instr);
  284. }
  285. funcBody.push(t.instruction("end"));
  286. const funcResults = [];
  287. // Code section
  288. const funcSignature = t.signature(funcParams, funcResults);
  289. const func = t.func(initFuncId, funcSignature, funcBody);
  290. // Type section
  291. const functype = t.typeInstruction(undefined, funcSignature);
  292. // Func section
  293. const funcindex = t.indexInFuncSection(nextTypeIndex);
  294. // Export section
  295. const moduleExport = t.moduleExport(
  296. initFuncId.value,
  297. t.moduleExportDescr("Func", nextFuncIndex)
  298. );
  299. return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
  300. };
  301. /**
  302. * Extract mangle mappings from module
  303. * @param {Module} module current module
  304. * @param {boolean} mangle mangle imports
  305. * @returns {Map<string, UsedWasmDependency>} mappings to mangled names
  306. */
  307. const getUsedDependencyMap = (module, mangle) => {
  308. /** @type {Map<string, UsedWasmDependency>} */
  309. const map = new Map();
  310. for (const usedDep of WebAssemblyUtils.getUsedDependencies(module, mangle)) {
  311. const dep = usedDep.dependency;
  312. const request = dep.request;
  313. const exportName = dep.name;
  314. map.set(request + ":" + exportName, usedDep);
  315. }
  316. return map;
  317. };
  318. class WebAssemblyGenerator extends Generator {
  319. constructor(options) {
  320. super();
  321. this.options = options;
  322. }
  323. /**
  324. * @param {NormalModule} module module for which the code should be generated
  325. * @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
  326. * @param {RuntimeTemplate} runtimeTemplate the runtime template
  327. * @param {string} type which kind of code should be generated
  328. * @returns {Source} generated code
  329. */
  330. generate(module, dependencyTemplates, runtimeTemplate, type) {
  331. let bin = module.originalSource().source();
  332. const initFuncId = t.identifier(
  333. Array.isArray(module.usedExports)
  334. ? Template.numberToIdentifer(module.usedExports.length)
  335. : "__webpack_init__"
  336. );
  337. // parse it
  338. const ast = decode(bin, {
  339. ignoreDataSection: true,
  340. ignoreCodeSection: true,
  341. ignoreCustomNameSection: true
  342. });
  343. const moduleContext = moduleContextFromModuleAST(ast.body[0]);
  344. const importedGlobals = getImportedGlobals(ast);
  345. const countImportedFunc = getCountImportedFunc(ast);
  346. const startAtFuncOffset = moduleContext.getStart();
  347. const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
  348. const nextTypeIndex = getNextTypeIndex(ast);
  349. const usedDependencyMap = getUsedDependencyMap(
  350. module,
  351. this.options.mangleImports
  352. );
  353. const externalExports = new Set(
  354. module.dependencies
  355. .filter(d => d instanceof WebAssemblyExportImportedDependency)
  356. .map(d => {
  357. const wasmDep = /** @type {WebAssemblyExportImportedDependency} */ (d);
  358. return wasmDep.exportName;
  359. })
  360. );
  361. /** @type {t.Instruction[]} */
  362. const additionalInitCode = [];
  363. const transform = compose(
  364. rewriteExportNames({
  365. ast,
  366. module,
  367. externalExports
  368. }),
  369. removeStartFunc({ ast }),
  370. rewriteImportedGlobals({ ast, additionalInitCode }),
  371. rewriteImports({
  372. ast,
  373. usedDependencyMap
  374. }),
  375. addInitFunction({
  376. ast,
  377. initFuncId,
  378. importedGlobals,
  379. additionalInitCode,
  380. startAtFuncOffset,
  381. nextFuncIndex,
  382. nextTypeIndex
  383. })
  384. );
  385. const newBin = transform(bin);
  386. return new RawSource(newBin);
  387. }
  388. }
  389. module.exports = WebAssemblyGenerator;