WebAssemblyGenerator.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. /**
  24. * @typedef {(buf: 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. /**
  40. * Removes the start instruction
  41. *
  42. * @param {Object} state unused state
  43. * @returns {ArrayBufferTransform} transform
  44. */
  45. const removeStartFunc = state => bin => {
  46. return editWithAST(state.ast, bin, {
  47. Start(path) {
  48. path.remove();
  49. }
  50. });
  51. };
  52. /**
  53. * Get imported globals
  54. *
  55. * @param {Object} ast Module's AST
  56. * @returns {t.ModuleImport[]} - nodes
  57. */
  58. const getImportedGlobals = ast => {
  59. /** @type {t.ModuleImport[]} */
  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. /** @type {Array<t.Global>} */
  155. const newGlobals = [];
  156. bin = editWithAST(state.ast, bin, {
  157. ModuleImport(path) {
  158. if (t.isGlobalType(path.node.descr)) {
  159. const globalType = path.node.descr;
  160. globalType.mutability = "var";
  161. const init = [
  162. createDefaultInitForGlobal(globalType),
  163. t.instruction("end")
  164. ];
  165. newGlobals.push(t.global(globalType, init));
  166. path.remove();
  167. }
  168. },
  169. // in order to preserve non-imported global's order we need to re-inject
  170. // those as well
  171. Global(path) {
  172. const { node } = path;
  173. const [init] = node.init;
  174. if (init.id === "get_global") {
  175. node.globalType.mutability = "var";
  176. const initialGlobalIdx = init.args[0];
  177. node.init = [
  178. createDefaultInitForGlobal(node.globalType),
  179. t.instruction("end")
  180. ];
  181. additionalInitCode.push(
  182. /**
  183. * get_global in global initializer only works for imported globals.
  184. * They have the same indices as the init params, so use the
  185. * same index.
  186. */
  187. t.instruction("get_local", [initialGlobalIdx]),
  188. t.instruction("set_global", [t.indexLiteral(newGlobals.length)])
  189. );
  190. }
  191. newGlobals.push(node);
  192. path.remove();
  193. }
  194. });
  195. // Add global declaration instructions
  196. return addWithAST(state.ast, bin, newGlobals);
  197. };
  198. /**
  199. * Rewrite the export names
  200. * @param {Object} state state
  201. * @param {Object} state.ast Module's ast
  202. * @param {Module} state.module Module
  203. * @param {ModuleGraph} state.moduleGraph module graph
  204. * @param {Set<string>} state.externalExports Module
  205. * @param {RuntimeSpec} state.runtime runtime
  206. * @returns {ArrayBufferTransform} transform
  207. */
  208. const rewriteExportNames =
  209. ({ ast, moduleGraph, module, externalExports, runtime }) =>
  210. bin => {
  211. return editWithAST(ast, bin, {
  212. ModuleExport(path) {
  213. const isExternal = externalExports.has(path.node.name);
  214. if (isExternal) {
  215. path.remove();
  216. return;
  217. }
  218. const usedName = moduleGraph
  219. .getExportsInfo(module)
  220. .getUsedName(path.node.name, runtime);
  221. if (!usedName) {
  222. path.remove();
  223. return;
  224. }
  225. path.node.name = usedName;
  226. }
  227. });
  228. };
  229. /**
  230. * Mangle import names and modules
  231. * @param {Object} state state
  232. * @param {Object} state.ast Module's ast
  233. * @param {Map<string, UsedWasmDependency>} state.usedDependencyMap mappings to mangle names
  234. * @returns {ArrayBufferTransform} transform
  235. */
  236. const rewriteImports =
  237. ({ ast, usedDependencyMap }) =>
  238. bin => {
  239. return editWithAST(ast, bin, {
  240. ModuleImport(path) {
  241. const result = usedDependencyMap.get(
  242. path.node.module + ":" + path.node.name
  243. );
  244. if (result !== undefined) {
  245. path.node.module = result.module;
  246. path.node.name = result.name;
  247. }
  248. }
  249. });
  250. };
  251. /**
  252. * Add an init function.
  253. *
  254. * The init function fills the globals given input arguments.
  255. *
  256. * @param {Object} state transformation state
  257. * @param {Object} state.ast Module's ast
  258. * @param {t.Identifier} state.initFuncId identifier of the init function
  259. * @param {t.Index} state.startAtFuncOffset index of the start function
  260. * @param {t.ModuleImport[]} state.importedGlobals list of imported globals
  261. * @param {t.Instruction[]} state.additionalInitCode list of addition instructions for the init function
  262. * @param {t.Index} state.nextFuncIndex index of the next function
  263. * @param {t.Index} state.nextTypeIndex index of the next type
  264. * @returns {ArrayBufferTransform} transform
  265. */
  266. const addInitFunction =
  267. ({
  268. ast,
  269. initFuncId,
  270. startAtFuncOffset,
  271. importedGlobals,
  272. additionalInitCode,
  273. nextFuncIndex,
  274. nextTypeIndex
  275. }) =>
  276. bin => {
  277. const funcParams = importedGlobals.map(importedGlobal => {
  278. // used for debugging
  279. const id = t.identifier(
  280. `${importedGlobal.module}.${importedGlobal.name}`
  281. );
  282. return t.funcParam(
  283. /** @type {string} */ (importedGlobal.descr.valtype),
  284. id
  285. );
  286. });
  287. const funcBody = [];
  288. importedGlobals.forEach((importedGlobal, index) => {
  289. const args = [t.indexLiteral(index)];
  290. const body = [
  291. t.instruction("get_local", args),
  292. t.instruction("set_global", args)
  293. ];
  294. funcBody.push(...body);
  295. });
  296. if (typeof startAtFuncOffset === "number") {
  297. funcBody.push(
  298. t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset))
  299. );
  300. }
  301. for (const instr of additionalInitCode) {
  302. funcBody.push(instr);
  303. }
  304. funcBody.push(t.instruction("end"));
  305. /** @type {string[]} */
  306. const funcResults = [];
  307. // Code section
  308. const funcSignature = t.signature(funcParams, funcResults);
  309. const func = t.func(initFuncId, funcSignature, funcBody);
  310. // Type section
  311. const functype = t.typeInstruction(undefined, funcSignature);
  312. // Func section
  313. const funcindex = t.indexInFuncSection(nextTypeIndex);
  314. // Export section
  315. const moduleExport = t.moduleExport(
  316. initFuncId.value,
  317. t.moduleExportDescr("Func", nextFuncIndex)
  318. );
  319. return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
  320. };
  321. /**
  322. * Extract mangle mappings from module
  323. * @param {ModuleGraph} moduleGraph module graph
  324. * @param {Module} module current module
  325. * @param {boolean | undefined} mangle mangle imports
  326. * @returns {Map<string, UsedWasmDependency>} mappings to mangled names
  327. */
  328. const getUsedDependencyMap = (moduleGraph, module, mangle) => {
  329. /** @type {Map<string, UsedWasmDependency>} */
  330. const map = new Map();
  331. for (const usedDep of WebAssemblyUtils.getUsedDependencies(
  332. moduleGraph,
  333. module,
  334. mangle
  335. )) {
  336. const dep = usedDep.dependency;
  337. const request = dep.request;
  338. const exportName = dep.name;
  339. map.set(request + ":" + exportName, usedDep);
  340. }
  341. return map;
  342. };
  343. const TYPES = new Set(["webassembly"]);
  344. /**
  345. * @typedef {Object} WebAssemblyGeneratorOptions
  346. * @property {boolean} [mangleImports] mangle imports
  347. */
  348. class WebAssemblyGenerator extends Generator {
  349. /**
  350. * @param {WebAssemblyGeneratorOptions} options options
  351. */
  352. constructor(options) {
  353. super();
  354. this.options = options;
  355. }
  356. /**
  357. * @param {NormalModule} module fresh module
  358. * @returns {Set<string>} available types (do not mutate)
  359. */
  360. getTypes(module) {
  361. return TYPES;
  362. }
  363. /**
  364. * @param {NormalModule} module the module
  365. * @param {string=} type source type
  366. * @returns {number} estimate size of the module
  367. */
  368. getSize(module, type) {
  369. const originalSource = module.originalSource();
  370. if (!originalSource) {
  371. return 0;
  372. }
  373. return originalSource.size();
  374. }
  375. /**
  376. * @param {NormalModule} module module for which the code should be generated
  377. * @param {GenerateContext} generateContext context for generate
  378. * @returns {Source} generated code
  379. */
  380. generate(module, { moduleGraph, runtime }) {
  381. const bin = /** @type {Source} */ (module.originalSource()).source();
  382. const initFuncId = t.identifier("");
  383. // parse it
  384. const ast = decode(bin, {
  385. ignoreDataSection: true,
  386. ignoreCodeSection: true,
  387. ignoreCustomNameSection: true
  388. });
  389. const moduleContext = moduleContextFromModuleAST(ast.body[0]);
  390. const importedGlobals = getImportedGlobals(ast);
  391. const countImportedFunc = getCountImportedFunc(ast);
  392. const startAtFuncOffset = moduleContext.getStart();
  393. const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
  394. const nextTypeIndex = getNextTypeIndex(ast);
  395. const usedDependencyMap = getUsedDependencyMap(
  396. moduleGraph,
  397. module,
  398. this.options.mangleImports
  399. );
  400. const externalExports = new Set(
  401. module.dependencies
  402. .filter(d => d instanceof WebAssemblyExportImportedDependency)
  403. .map(d => {
  404. const wasmDep = /** @type {WebAssemblyExportImportedDependency} */ (
  405. d
  406. );
  407. return wasmDep.exportName;
  408. })
  409. );
  410. /** @type {t.Instruction[]} */
  411. const additionalInitCode = [];
  412. const transform = compose(
  413. rewriteExportNames({
  414. ast,
  415. moduleGraph,
  416. module,
  417. externalExports,
  418. runtime
  419. }),
  420. removeStartFunc({ ast }),
  421. rewriteImportedGlobals({ ast, additionalInitCode }),
  422. rewriteImports({
  423. ast,
  424. usedDependencyMap
  425. }),
  426. addInitFunction({
  427. ast,
  428. initFuncId,
  429. importedGlobals,
  430. additionalInitCode,
  431. startAtFuncOffset,
  432. nextFuncIndex,
  433. nextTypeIndex
  434. })
  435. );
  436. const newBin = transform(bin);
  437. const newBuf = Buffer.from(newBin);
  438. return new RawSource(newBuf);
  439. }
  440. }
  441. module.exports = WebAssemblyGenerator;