WebAssemblyUtils.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("../Template");
  7. const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
  8. /** @typedef {import("../Module")} Module */
  9. /** @typedef {Object} UsedWasmDependency
  10. * @property {WebAssemblyImportDependency} dependency the dependency
  11. * @property {string} name the export name
  12. * @property {string} module the module name
  13. */
  14. const MANGLED_MODULE = "a";
  15. /**
  16. * @param {Module} module the module
  17. * @param {boolean} mangle mangle module and export names
  18. * @returns {UsedWasmDependency[]} used dependencies and (mangled) name
  19. */
  20. const getUsedDependencies = (module, mangle) => {
  21. /** @type {UsedWasmDependency[]} */
  22. const array = [];
  23. let importIndex = 0;
  24. for (const dep of module.dependencies) {
  25. if (dep instanceof WebAssemblyImportDependency) {
  26. if (dep.description.type === "GlobalType" || dep.module === null) {
  27. continue;
  28. }
  29. const exportName = dep.name;
  30. // TODO add the following 3 lines when removing of ModuleExport is possible
  31. // const importedModule = dep.module;
  32. // const usedName = importedModule && importedModule.isUsed(exportName);
  33. // if (usedName !== false) {
  34. if (mangle) {
  35. array.push({
  36. dependency: dep,
  37. name: Template.numberToIdentifer(importIndex++),
  38. module: MANGLED_MODULE
  39. });
  40. } else {
  41. array.push({
  42. dependency: dep,
  43. name: exportName,
  44. module: dep.request
  45. });
  46. }
  47. }
  48. }
  49. return array;
  50. };
  51. exports.getUsedDependencies = getUsedDependencies;
  52. exports.MANGLED_MODULE = MANGLED_MODULE;