index.js 6.5 KB

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