index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.decode = decode;
  7. var decoder = _interopRequireWildcard(require("./decoder"));
  8. var t = _interopRequireWildcard(require("@webassemblyjs/ast"));
  9. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  10. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  11. /**
  12. * TODO(sven): I added initial props, but we should rather fix
  13. * https://github.com/xtuc/webassemblyjs/issues/405
  14. */
  15. var defaultDecoderOpts = {
  16. dump: false,
  17. ignoreCodeSection: false,
  18. ignoreDataSection: false,
  19. ignoreCustomNameSection: false
  20. }; // traverses the AST, locating function name metadata, which is then
  21. // used to update index-based identifiers with function names
  22. function restoreFunctionNames(ast) {
  23. var functionNames = [];
  24. t.traverse(ast, {
  25. FunctionNameMetadata: function FunctionNameMetadata(_ref) {
  26. var node = _ref.node;
  27. functionNames.push({
  28. name: node.value,
  29. index: node.index
  30. });
  31. }
  32. });
  33. if (functionNames.length === 0) {
  34. return;
  35. }
  36. t.traverse(ast, {
  37. Func: function (_Func) {
  38. function Func(_x) {
  39. return _Func.apply(this, arguments);
  40. }
  41. Func.toString = function () {
  42. return _Func.toString();
  43. };
  44. return Func;
  45. }(function (_ref2) {
  46. var node = _ref2.node;
  47. // $FlowIgnore
  48. var nodeName = node.name;
  49. var indexBasedFunctionName = nodeName.value;
  50. var index = Number(indexBasedFunctionName.replace("func_", ""));
  51. var functionName = functionNames.find(function (f) {
  52. return f.index === index;
  53. });
  54. if (functionName) {
  55. var oldValue = nodeName.value;
  56. nodeName.value = functionName.name; // $FlowIgnore
  57. nodeName.numeric = oldValue; // $FlowIgnore
  58. delete nodeName.raw;
  59. }
  60. }),
  61. // Also update the reference in the export
  62. ModuleExport: function (_ModuleExport) {
  63. function ModuleExport(_x2) {
  64. return _ModuleExport.apply(this, arguments);
  65. }
  66. ModuleExport.toString = function () {
  67. return _ModuleExport.toString();
  68. };
  69. return ModuleExport;
  70. }(function (_ref3) {
  71. var node = _ref3.node;
  72. if (node.descr.exportType === "Func") {
  73. // $FlowIgnore
  74. var nodeName = node.descr.id;
  75. var index = nodeName.value;
  76. var functionName = functionNames.find(function (f) {
  77. return f.index === index;
  78. });
  79. if (functionName) {
  80. node.descr.id = t.identifier(functionName.name);
  81. }
  82. }
  83. }),
  84. ModuleImport: function (_ModuleImport) {
  85. function ModuleImport(_x3) {
  86. return _ModuleImport.apply(this, arguments);
  87. }
  88. ModuleImport.toString = function () {
  89. return _ModuleImport.toString();
  90. };
  91. return ModuleImport;
  92. }(function (_ref4) {
  93. var node = _ref4.node;
  94. if (node.descr.type === "FuncImportDescr") {
  95. // $FlowIgnore
  96. var indexBasedFunctionName = node.descr.id;
  97. var index = Number(indexBasedFunctionName.replace("func_", ""));
  98. var functionName = functionNames.find(function (f) {
  99. return f.index === index;
  100. });
  101. if (functionName) {
  102. // $FlowIgnore
  103. node.descr.id = t.identifier(functionName.name);
  104. }
  105. }
  106. }),
  107. CallInstruction: function (_CallInstruction) {
  108. function CallInstruction(_x4) {
  109. return _CallInstruction.apply(this, arguments);
  110. }
  111. CallInstruction.toString = function () {
  112. return _CallInstruction.toString();
  113. };
  114. return CallInstruction;
  115. }(function (nodePath) {
  116. var node = nodePath.node;
  117. var index = node.index.value;
  118. var functionName = functionNames.find(function (f) {
  119. return f.index === index;
  120. });
  121. if (functionName) {
  122. var oldValue = node.index;
  123. node.index = t.identifier(functionName.name);
  124. node.numeric = oldValue; // $FlowIgnore
  125. delete node.raw;
  126. }
  127. })
  128. });
  129. }
  130. function restoreLocalNames(ast) {
  131. var localNames = [];
  132. t.traverse(ast, {
  133. LocalNameMetadata: function LocalNameMetadata(_ref5) {
  134. var node = _ref5.node;
  135. localNames.push({
  136. name: node.value,
  137. localIndex: node.localIndex,
  138. functionIndex: node.functionIndex
  139. });
  140. }
  141. });
  142. if (localNames.length === 0) {
  143. return;
  144. }
  145. t.traverse(ast, {
  146. Func: function (_Func2) {
  147. function Func(_x5) {
  148. return _Func2.apply(this, arguments);
  149. }
  150. Func.toString = function () {
  151. return _Func2.toString();
  152. };
  153. return Func;
  154. }(function (_ref6) {
  155. var node = _ref6.node;
  156. var signature = node.signature;
  157. if (signature.type !== "Signature") {
  158. return;
  159. } // $FlowIgnore
  160. var nodeName = node.name;
  161. var indexBasedFunctionName = nodeName.value;
  162. var functionIndex = Number(indexBasedFunctionName.replace("func_", ""));
  163. signature.params.forEach(function (param, paramIndex) {
  164. var paramName = localNames.find(function (f) {
  165. return f.localIndex === paramIndex && f.functionIndex === functionIndex;
  166. });
  167. if (paramName && paramName.name !== "") {
  168. param.id = paramName.name;
  169. }
  170. });
  171. })
  172. });
  173. }
  174. function restoreModuleName(ast) {
  175. t.traverse(ast, {
  176. ModuleNameMetadata: function (_ModuleNameMetadata) {
  177. function ModuleNameMetadata(_x6) {
  178. return _ModuleNameMetadata.apply(this, arguments);
  179. }
  180. ModuleNameMetadata.toString = function () {
  181. return _ModuleNameMetadata.toString();
  182. };
  183. return ModuleNameMetadata;
  184. }(function (moduleNameMetadataPath) {
  185. // update module
  186. t.traverse(ast, {
  187. Module: function (_Module) {
  188. function Module(_x7) {
  189. return _Module.apply(this, arguments);
  190. }
  191. Module.toString = function () {
  192. return _Module.toString();
  193. };
  194. return Module;
  195. }(function (_ref7) {
  196. var node = _ref7.node;
  197. var name = moduleNameMetadataPath.node.value; // compatiblity with wast-parser
  198. if (name === "") {
  199. name = null;
  200. }
  201. node.id = name;
  202. })
  203. });
  204. })
  205. });
  206. }
  207. function decode(buf, customOpts) {
  208. var opts = Object.assign({}, defaultDecoderOpts, customOpts);
  209. var ast = decoder.decode(buf, opts);
  210. if (opts.ignoreCustomNameSection === false) {
  211. restoreFunctionNames(ast);
  212. restoreLocalNames(ast);
  213. restoreModuleName(ast);
  214. }
  215. return ast;
  216. }