WebAssemblyParser.js 5.5 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("../Module")} Module */
  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"]);
  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 {{}=} options parser options
  59. */
  60. constructor(options) {
  61. super();
  62. this.hooks = Object.freeze({});
  63. this.options = options;
  64. }
  65. /**
  66. * @param {string | Buffer | PreparsedAst} source the source to parse
  67. * @param {ParserState} state the parser state
  68. * @returns {ParserState} the parser state
  69. */
  70. parse(source, state) {
  71. if (!Buffer.isBuffer(source)) {
  72. throw new Error("WebAssemblyParser input must be a Buffer");
  73. }
  74. // flag it as ESM
  75. /** @type {BuildInfo} */
  76. (state.module.buildInfo).strict = true;
  77. /** @type {BuildMeta} */
  78. (state.module.buildMeta).exportsType = "namespace";
  79. // parse it
  80. const program = decode(source, decoderOpts);
  81. const module = program.body[0];
  82. const moduleContext = moduleContextFromModuleAST(module);
  83. // extract imports and exports
  84. /** @type {string[]} */
  85. const exports = [];
  86. let jsIncompatibleExports = (state.module.buildMeta.jsIncompatibleExports =
  87. undefined);
  88. /** @type {TODO[]} */
  89. const importedGlobals = [];
  90. t.traverse(module, {
  91. ModuleExport({ node }) {
  92. const descriptor = node.descr;
  93. if (descriptor.exportType === "Func") {
  94. const funcIdx = descriptor.id.value;
  95. /** @type {t.FuncSignature} */
  96. const funcSignature = moduleContext.getFunction(funcIdx);
  97. const incompatibleType =
  98. getJsIncompatibleTypeOfFuncSignature(funcSignature);
  99. if (incompatibleType) {
  100. if (jsIncompatibleExports === undefined) {
  101. jsIncompatibleExports =
  102. state.module.buildMeta.jsIncompatibleExports = {};
  103. }
  104. jsIncompatibleExports[node.name] = incompatibleType;
  105. }
  106. }
  107. exports.push(node.name);
  108. if (node.descr && node.descr.exportType === "Global") {
  109. const refNode = importedGlobals[node.descr.id.value];
  110. if (refNode) {
  111. const dep = new WebAssemblyExportImportedDependency(
  112. node.name,
  113. refNode.module,
  114. refNode.name,
  115. refNode.descr.valtype
  116. );
  117. state.module.addDependency(dep);
  118. }
  119. }
  120. },
  121. Global({ node }) {
  122. const init = node.init[0];
  123. let importNode = null;
  124. if (init.id === "get_global") {
  125. const globalIdx = init.args[0].value;
  126. if (globalIdx < importedGlobals.length) {
  127. importNode = importedGlobals[globalIdx];
  128. }
  129. }
  130. importedGlobals.push(importNode);
  131. },
  132. ModuleImport({ node }) {
  133. /** @type {false | string} */
  134. let onlyDirectImport = false;
  135. if (t.isMemory(node.descr) === true) {
  136. onlyDirectImport = "Memory";
  137. } else if (t.isTable(node.descr) === true) {
  138. onlyDirectImport = "Table";
  139. } else if (t.isFuncImportDescr(node.descr) === true) {
  140. const incompatibleType = getJsIncompatibleType(
  141. /** @type {t.Signature} */ (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;