WebAssemblyParser.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
  87. /** @type {Record<string, string> | undefined} */
  88. let jsIncompatibleExports = (buildMeta.jsIncompatibleExports = undefined);
  89. /** @type {TODO[]} */
  90. const importedGlobals = [];
  91. t.traverse(module, {
  92. ModuleExport({ node }) {
  93. const descriptor = node.descr;
  94. if (descriptor.exportType === "Func") {
  95. const funcIdx = descriptor.id.value;
  96. /** @type {t.FuncSignature} */
  97. const funcSignature = moduleContext.getFunction(funcIdx);
  98. const incompatibleType =
  99. getJsIncompatibleTypeOfFuncSignature(funcSignature);
  100. if (incompatibleType) {
  101. if (jsIncompatibleExports === undefined) {
  102. jsIncompatibleExports =
  103. /** @type {BuildMeta} */
  104. (state.module.buildMeta).jsIncompatibleExports = {};
  105. }
  106. jsIncompatibleExports[node.name] = incompatibleType;
  107. }
  108. }
  109. exports.push(node.name);
  110. if (node.descr && node.descr.exportType === "Global") {
  111. const refNode =
  112. importedGlobals[/** @type {TODO} */ (node.descr.id.value)];
  113. if (refNode) {
  114. const dep = new WebAssemblyExportImportedDependency(
  115. node.name,
  116. refNode.module,
  117. refNode.name,
  118. refNode.descr.valtype
  119. );
  120. state.module.addDependency(dep);
  121. }
  122. }
  123. },
  124. Global({ node }) {
  125. const init = node.init[0];
  126. let importNode = null;
  127. if (init.id === "get_global") {
  128. const globalIdx = init.args[0].value;
  129. if (globalIdx < importedGlobals.length) {
  130. importNode = importedGlobals[globalIdx];
  131. }
  132. }
  133. importedGlobals.push(importNode);
  134. },
  135. ModuleImport({ node }) {
  136. /** @type {false | string} */
  137. let onlyDirectImport = false;
  138. if (t.isMemory(node.descr) === true) {
  139. onlyDirectImport = "Memory";
  140. } else if (t.isTable(node.descr) === true) {
  141. onlyDirectImport = "Table";
  142. } else if (t.isFuncImportDescr(node.descr) === true) {
  143. const incompatibleType = getJsIncompatibleType(
  144. /** @type {t.Signature} */ (node.descr.signature)
  145. );
  146. if (incompatibleType) {
  147. onlyDirectImport = `Non-JS-compatible Func Signature (${incompatibleType})`;
  148. }
  149. } else if (t.isGlobalType(node.descr) === true) {
  150. const type = /** @type {string} */ (node.descr.valtype);
  151. if (!JS_COMPAT_TYPES.has(type)) {
  152. onlyDirectImport = `Non-JS-compatible Global Type (${type})`;
  153. }
  154. }
  155. const dep = new WebAssemblyImportDependency(
  156. node.module,
  157. node.name,
  158. node.descr,
  159. onlyDirectImport
  160. );
  161. state.module.addDependency(dep);
  162. if (t.isGlobalType(node.descr)) {
  163. importedGlobals.push(node);
  164. }
  165. }
  166. });
  167. state.module.addDependency(new StaticExportsDependency(exports, false));
  168. return state;
  169. }
  170. }
  171. module.exports = WebAssemblyParser;