WebAssemblyGenerator.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 = createDefaultInitForGlobal(globalType);
  155. newGlobals.push(t.global(globalType, [init]));
  156. path.remove();
  157. }
  158. },
  159. // in order to preserve non-imported global's order we need to re-inject
  160. // those as well
  161. Global(path) {
  162. const { node } = path;
  163. const [init] = node.init;
  164. if (init.id === "get_global") {
  165. node.globalType.mutability = "var";
  166. const initialGlobalidx = init.args[0];
  167. node.init = [createDefaultInitForGlobal(node.globalType)];
  168. additionalInitCode.push(
  169. /**
  170. * get_global in global initilizer only work for imported globals.
  171. * They have the same indices than the init params, so use the
  172. * same index.
  173. */
  174. t.instruction("get_local", [initialGlobalidx]),
  175. t.instruction("set_global", [t.indexLiteral(newGlobals.length)])
  176. );
  177. }
  178. newGlobals.push(node);
  179. path.remove();
  180. }
  181. });
  182. // Add global declaration instructions
  183. return addWithAST(state.ast, bin, newGlobals);
  184. };
  185. /**
  186. * Rewrite the export names
  187. * @param {Object} state state
  188. * @param {Object} state.ast Module's ast
  189. * @param {Module} state.module Module
  190. * @param {Set<string>} state.externalExports Module
  191. * @returns {ArrayBufferTransform} transform
  192. */
  193. const rewriteExportNames = ({ ast, module, externalExports }) => bin => {
  194. return editWithAST(ast, bin, {
  195. ModuleExport(path) {
  196. const isExternal = externalExports.has(path.node.name);
  197. if (isExternal) {
  198. path.remove();
  199. return;
  200. }
  201. const usedName = module.isUsed(path.node.name);
  202. if (!usedName) {
  203. path.remove();
  204. return;
  205. }
  206. path.node.name = usedName;
  207. }
  208. });
  209. };
  210. /**
  211. * Mangle import names and modules
  212. * @param {Object} state state
  213. * @param {Object} state.ast Module's ast
  214. * @param {Map<string, UsedWasmDependency>} state.usedDependencyMap mappings to mangle names
  215. * @returns {ArrayBufferTransform} transform
  216. */
  217. const rewriteImports = ({ ast, usedDependencyMap }) => bin => {
  218. return editWithAST(ast, bin, {
  219. ModuleImport(path) {
  220. const result = usedDependencyMap.get(
  221. path.node.module + ":" + path.node.name
  222. );
  223. if (result !== undefined) {
  224. path.node.module = result.module;
  225. path.node.name = result.name;
  226. }
  227. }
  228. });
  229. };
  230. /**
  231. * Add an init function.
  232. *
  233. * The init function fills the globals given input arguments.
  234. *
  235. * @param {Object} state transformation state
  236. * @param {Object} state.ast - Module's ast
  237. * @param {t.Identifier} state.initFuncId identifier of the init function
  238. * @param {t.Index} state.startAtFuncOffset index of the start function
  239. * @param {t.ModuleImport[]} state.importedGlobals list of imported globals
  240. * @param {t.Instruction[]} state.additionalInitCode list of addition instructions for the init function
  241. * @param {t.Index} state.nextFuncIndex index of the next function
  242. * @param {t.Index} state.nextTypeIndex index of the next type
  243. * @returns {ArrayBufferTransform} transform
  244. */
  245. const addInitFunction = ({
  246. ast,
  247. initFuncId,
  248. startAtFuncOffset,
  249. importedGlobals,
  250. additionalInitCode,
  251. nextFuncIndex,
  252. nextTypeIndex
  253. }) => bin => {
  254. const funcParams = importedGlobals.map(importedGlobal => {
  255. // used for debugging
  256. const id = t.identifier(`${importedGlobal.module}.${importedGlobal.name}`);
  257. return t.funcParam(importedGlobal.descr.valtype, id);
  258. });
  259. const funcBody = importedGlobals.reduce((acc, importedGlobal, index) => {
  260. const args = [t.indexLiteral(index)];
  261. const body = [
  262. t.instruction("get_local", args),
  263. t.instruction("set_global", args)
  264. ];
  265. return [...acc, ...body];
  266. }, []);
  267. if (typeof startAtFuncOffset === "number") {
  268. funcBody.push(t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)));
  269. }
  270. for (const instr of additionalInitCode) {
  271. funcBody.push(instr);
  272. }
  273. const funcResults = [];
  274. // Code section
  275. const funcSignature = t.signature(funcParams, funcResults);
  276. const func = t.func(initFuncId, funcSignature, funcBody);
  277. // Type section
  278. const functype = t.typeInstruction(undefined, funcSignature);
  279. // Func section
  280. const funcindex = t.indexInFuncSection(nextTypeIndex);
  281. // Export section
  282. const moduleExport = t.moduleExport(
  283. initFuncId.value,
  284. t.moduleExportDescr("Func", nextFuncIndex)
  285. );
  286. return addWithAST(ast, bin, [func, moduleExport, funcindex, functype]);
  287. };
  288. /**
  289. * Extract mangle mappings from module
  290. * @param {Module} module current module
  291. * @param {boolean} mangle mangle imports
  292. * @returns {Map<string, UsedWasmDependency>} mappings to mangled names
  293. */
  294. const getUsedDependencyMap = (module, mangle) => {
  295. /** @type {Map<string, UsedWasmDependency>} */
  296. const map = new Map();
  297. for (const usedDep of WebAssemblyUtils.getUsedDependencies(module, mangle)) {
  298. const dep = usedDep.dependency;
  299. const request = dep.request;
  300. const exportName = dep.name;
  301. map.set(request + ":" + exportName, usedDep);
  302. }
  303. return map;
  304. };
  305. class WebAssemblyGenerator extends Generator {
  306. constructor(options) {
  307. super();
  308. this.options = options;
  309. }
  310. /**
  311. * @param {NormalModule} module module for which the code should be generated
  312. * @param {Map<Function, DependencyTemplate>} dependencyTemplates mapping from dependencies to templates
  313. * @param {RuntimeTemplate} runtimeTemplate the runtime template
  314. * @param {string} type which kind of code should be generated
  315. * @returns {Source} generated code
  316. */
  317. generate(module, dependencyTemplates, runtimeTemplate, type) {
  318. let bin = module.originalSource().source();
  319. const initFuncId = t.identifier(
  320. Array.isArray(module.usedExports)
  321. ? Template.numberToIdentifer(module.usedExports.length)
  322. : "__webpack_init__"
  323. );
  324. // parse it
  325. const ast = decode(bin, {
  326. ignoreDataSection: true,
  327. ignoreCodeSection: true,
  328. ignoreCustomNameSection: true
  329. });
  330. const moduleContext = moduleContextFromModuleAST(ast.body[0]);
  331. const importedGlobals = getImportedGlobals(ast);
  332. const countImportedFunc = getCountImportedFunc(ast);
  333. const startAtFuncOffset = moduleContext.getStart();
  334. const nextFuncIndex = getNextFuncIndex(ast, countImportedFunc);
  335. const nextTypeIndex = getNextTypeIndex(ast);
  336. const usedDependencyMap = getUsedDependencyMap(
  337. module,
  338. this.options.mangleImports
  339. );
  340. const externalExports = new Set(
  341. module.dependencies
  342. .filter(d => d instanceof WebAssemblyExportImportedDependency)
  343. .map(d => {
  344. const wasmDep = /** @type {WebAssemblyExportImportedDependency} */ (d);
  345. return wasmDep.exportName;
  346. })
  347. );
  348. /** @type {t.Instruction[]} */
  349. const additionalInitCode = [];
  350. const transform = compose(
  351. rewriteExportNames({
  352. ast,
  353. module,
  354. externalExports
  355. }),
  356. removeStartFunc({ ast }),
  357. rewriteImportedGlobals({ ast, additionalInitCode }),
  358. rewriteImports({
  359. ast,
  360. usedDependencyMap
  361. }),
  362. addInitFunction({
  363. ast,
  364. initFuncId,
  365. importedGlobals,
  366. additionalInitCode,
  367. startAtFuncOffset,
  368. nextFuncIndex,
  369. nextTypeIndex
  370. })
  371. );
  372. const newBin = transform(bin);
  373. return new RawSource(newBin);
  374. }
  375. }
  376. module.exports = WebAssemblyGenerator;