WebAssemblyGenerator.js 13 KB

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