WebAssemblyGenerator.js 15 KB

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