index.js 5.9 KB

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