WebAssemblyParser.js 5.7 KB

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