WebAssemblyParser.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const t = require("@webassemblyjs/ast");
  7. const { decode } = require("@webassemblyjs/wasm-parser");
  8. const {
  9. moduleContextFromModuleAST
  10. } = require("@webassemblyjs/helper-module-context");
  11. const { Tapable } = require("tapable");
  12. const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
  13. const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
  14. /** @typedef {import("../Module")} Module */
  15. const JS_COMPAT_TYPES = new Set(["i32", "f32", "f64"]);
  16. /**
  17. * @param {t.Signature} signature the func signature
  18. * @returns {null | string} the type incompatible with js types
  19. */
  20. const getJsIncompatibleType = signature => {
  21. for (const param of signature.params) {
  22. if (!JS_COMPAT_TYPES.has(param.valtype)) {
  23. return `${param.valtype} as parameter`;
  24. }
  25. }
  26. for (const type of signature.results) {
  27. if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
  28. }
  29. return null;
  30. };
  31. /**
  32. * TODO why are there two different Signature types?
  33. * @param {t.FuncSignature} signature the func signature
  34. * @returns {null | string} the type incompatible with js types
  35. */
  36. const getJsIncompatibleTypeOfFuncSignature = signature => {
  37. for (const param of signature.args) {
  38. if (!JS_COMPAT_TYPES.has(param)) {
  39. return `${param} as parameter`;
  40. }
  41. }
  42. for (const type of signature.result) {
  43. if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
  44. }
  45. return null;
  46. };
  47. const decoderOpts = {
  48. ignoreCodeSection: true,
  49. ignoreDataSection: true,
  50. // this will avoid having to lookup with identifiers in the ModuleContext
  51. ignoreCustomNameSection: true
  52. };
  53. class WebAssemblyParser extends Tapable {
  54. constructor(options) {
  55. super();
  56. this.hooks = {};
  57. this.options = options;
  58. }
  59. parse(binary, state) {
  60. // flag it as ESM
  61. state.module.buildMeta.exportsType = "namespace";
  62. // parse it
  63. const program = decode(binary, decoderOpts);
  64. const module = program.body[0];
  65. const moduleContext = moduleContextFromModuleAST(module);
  66. // extract imports and exports
  67. const exports = (state.module.buildMeta.providedExports = []);
  68. const jsIncompatibleExports = (state.module.buildMeta.jsIncompatibleExports = []);
  69. const importedGlobals = [];
  70. t.traverse(module, {
  71. ModuleExport({ node }) {
  72. const descriptor = node.descr;
  73. if (descriptor.exportType === "Func") {
  74. const funcidx = descriptor.id.value;
  75. /** @type {t.FuncSignature} */
  76. const funcSignature = moduleContext.getFunction(funcidx);
  77. const incompatibleType = getJsIncompatibleTypeOfFuncSignature(
  78. funcSignature
  79. );
  80. if (incompatibleType) {
  81. jsIncompatibleExports[node.name] = incompatibleType;
  82. }
  83. }
  84. exports.push(node.name);
  85. if (node.descr && node.descr.exportType === "Global") {
  86. const refNode = importedGlobals[node.descr.id.value];
  87. if (refNode) {
  88. const dep = new WebAssemblyExportImportedDependency(
  89. node.name,
  90. refNode.module,
  91. refNode.name,
  92. refNode.descr.valtype
  93. );
  94. state.module.addDependency(dep);
  95. }
  96. }
  97. },
  98. Global({ node }) {
  99. const init = node.init[0];
  100. let importNode = null;
  101. if (init.id === "get_global") {
  102. const globalIdx = init.args[0].value;
  103. if (globalIdx < importedGlobals.length) {
  104. importNode = importedGlobals[globalIdx];
  105. }
  106. }
  107. importedGlobals.push(importNode);
  108. },
  109. ModuleImport({ node }) {
  110. /** @type {false | string} */
  111. let onlyDirectImport = false;
  112. if (t.isMemory(node.descr) === true) {
  113. onlyDirectImport = "Memory";
  114. } else if (t.isTable(node.descr) === true) {
  115. onlyDirectImport = "Table";
  116. } else if (t.isFuncImportDescr(node.descr) === true) {
  117. const incompatibleType = getJsIncompatibleType(node.descr.signature);
  118. if (incompatibleType) {
  119. onlyDirectImport = `Non-JS-compatible Func Sigurature (${incompatibleType})`;
  120. }
  121. } else if (t.isGlobalType(node.descr) === true) {
  122. const type = node.descr.valtype;
  123. if (!JS_COMPAT_TYPES.has(type)) {
  124. onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
  125. }
  126. }
  127. const dep = new WebAssemblyImportDependency(
  128. node.module,
  129. node.name,
  130. node.descr,
  131. onlyDirectImport
  132. );
  133. state.module.addDependency(dep);
  134. if (t.isGlobalType(node.descr)) {
  135. importedGlobals.push(node);
  136. }
  137. }
  138. });
  139. return state;
  140. }
  141. }
  142. module.exports = WebAssemblyParser;