WebAssemblyGenerator.js 13 KB

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