WebAssemblyParser.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 { moduleContextFromModuleAST } = require("@webassemblyjs/ast");
  8. const { decode } = require("@webassemblyjs/wasm-parser");
  9. const Parser = require("../Parser");
  10. const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
  11. const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency");
  12. const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency");
  13. /** @typedef {import("@webassemblyjs/ast").ModuleImport} ModuleImport */
  14. /** @typedef {import("../Module").BuildInfo} BuildInfo */
  15. /** @typedef {import("../Module").BuildMeta} BuildMeta */
  16. /** @typedef {import("../Parser").ParserState} ParserState */
  17. /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
  18. const JS_COMPAT_TYPES = new Set(["i32", "i64", "f32", "f64", "externref"]);
  19. /**
  20. * @param {t.Signature} signature the func signature
  21. * @returns {null | string} the type incompatible with js types
  22. */
  23. const getJsIncompatibleType = (signature) => {
  24. for (const param of signature.params) {
  25. if (!JS_COMPAT_TYPES.has(param.valtype)) {
  26. return `${param.valtype} as parameter`;
  27. }
  28. }
  29. for (const type of signature.results) {
  30. if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
  31. }
  32. return null;
  33. };
  34. /**
  35. * TODO why are there two different Signature types?
  36. * @param {t.FuncSignature} signature the func signature
  37. * @returns {null | string} the type incompatible with js types
  38. */
  39. const getJsIncompatibleTypeOfFuncSignature = (signature) => {
  40. for (const param of signature.args) {
  41. if (!JS_COMPAT_TYPES.has(param)) {
  42. return `${param} as parameter`;
  43. }
  44. }
  45. for (const type of signature.result) {
  46. if (!JS_COMPAT_TYPES.has(type)) return `${type} as result`;
  47. }
  48. return null;
  49. };
  50. const decoderOpts = {
  51. ignoreCodeSection: true,
  52. ignoreDataSection: true,
  53. // this will avoid having to lookup with identifiers in the ModuleContext
  54. ignoreCustomNameSection: true
  55. };
  56. class WebAssemblyParser extends Parser {
  57. /**
  58. * @param {string | Buffer | PreparsedAst} source the source to parse
  59. * @param {ParserState} state the parser state
  60. * @returns {ParserState} the parser state
  61. */
  62. parse(source, state) {
  63. if (!Buffer.isBuffer(source)) {
  64. throw new Error("WebAssemblyParser input must be a Buffer");
  65. }
  66. // flag it as ESM
  67. /** @type {BuildInfo} */
  68. (state.module.buildInfo).strict = true;
  69. /** @type {BuildMeta} */
  70. (state.module.buildMeta).exportsType = "namespace";
  71. // parse it
  72. const program = decode(source, decoderOpts);
  73. const module = program.body[0];
  74. const moduleContext = moduleContextFromModuleAST(module);
  75. // extract imports and exports
  76. /** @type {string[]} */
  77. const exports = [];
  78. const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
  79. /** @type {Record<string, string> | undefined} */
  80. let jsIncompatibleExports = (buildMeta.jsIncompatibleExports = undefined);
  81. /** @typedef {ModuleImport | null} ImportNode */
  82. /** @type {ImportNode[]} */
  83. const importedGlobals = [];
  84. t.traverse(module, {
  85. ModuleExport({ node }) {
  86. const descriptor = node.descr;
  87. if (descriptor.exportType === "Func") {
  88. const funcIdx = descriptor.id.value;
  89. /** @type {t.FuncSignature} */
  90. const funcSignature = moduleContext.getFunction(funcIdx);
  91. const incompatibleType =
  92. getJsIncompatibleTypeOfFuncSignature(funcSignature);
  93. if (incompatibleType) {
  94. if (jsIncompatibleExports === undefined) {
  95. jsIncompatibleExports =
  96. /** @type {BuildMeta} */
  97. (state.module.buildMeta).jsIncompatibleExports = {};
  98. }
  99. jsIncompatibleExports[node.name] = incompatibleType;
  100. }
  101. }
  102. exports.push(node.name);
  103. if (node.descr && node.descr.exportType === "Global") {
  104. const refNode = importedGlobals[node.descr.id.value];
  105. if (refNode) {
  106. const dep = new WebAssemblyExportImportedDependency(
  107. node.name,
  108. refNode.module,
  109. refNode.name,
  110. /** @type {string} */
  111. (refNode.descr.valtype)
  112. );
  113. state.module.addDependency(dep);
  114. }
  115. }
  116. },
  117. Global({ node }) {
  118. const init = node.init[0];
  119. /** @type {ImportNode} */
  120. let importNode = null;
  121. if (init.id === "get_global") {
  122. const globalIdx = init.args[0].value;
  123. if (globalIdx < importedGlobals.length) {
  124. importNode = importedGlobals[globalIdx];
  125. }
  126. }
  127. importedGlobals.push(importNode);
  128. },
  129. ModuleImport({ node }) {
  130. /** @type {false | string} */
  131. let onlyDirectImport = false;
  132. if (t.isMemory(node.descr) === true) {
  133. onlyDirectImport = "Memory";
  134. } else if (t.isTable(node.descr) === true) {
  135. onlyDirectImport = "Table";
  136. } else if (t.isFuncImportDescr(node.descr) === true) {
  137. const incompatibleType = getJsIncompatibleType(
  138. /** @type {t.Signature} */
  139. (node.descr.signature)
  140. );
  141. if (incompatibleType) {
  142. onlyDirectImport = `Non-JS-compatible Func Signature (${incompatibleType})`;
  143. }
  144. } else if (t.isGlobalType(node.descr) === true) {
  145. const type = /** @type {string} */ (node.descr.valtype);
  146. if (!JS_COMPAT_TYPES.has(type)) {
  147. onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
  148. }
  149. }
  150. const dep = new WebAssemblyImportDependency(
  151. node.module,
  152. node.name,
  153. node.descr,
  154. onlyDirectImport
  155. );
  156. state.module.addDependency(dep);
  157. if (t.isGlobalType(node.descr)) {
  158. importedGlobals.push(node);
  159. }
  160. }
  161. });
  162. state.module.addDependency(new StaticExportsDependency(exports, false));
  163. return state;
  164. }
  165. }
  166. module.exports = WebAssemblyParser;