PageRenderTime 59ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/webpack/lib/wasm-sync/WebAssemblyGenerator.js

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