WebAssemblyGenerator.js 11 KB

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