You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import * as decoder from "./decoder";
  2. import * as t from "@webassemblyjs/ast";
  3. /**
  4. * TODO(sven): I added initial props, but we should rather fix
  5. * https://github.com/xtuc/webassemblyjs/issues/405
  6. */
  7. var defaultDecoderOpts = {
  8. dump: false,
  9. ignoreCodeSection: false,
  10. ignoreDataSection: false,
  11. ignoreCustomNameSection: false
  12. }; // traverses the AST, locating function name metadata, which is then
  13. // used to update index-based identifiers with function names
  14. function restoreFunctionNames(ast) {
  15. var functionNames = [];
  16. t.traverse(ast, {
  17. FunctionNameMetadata: function FunctionNameMetadata(_ref) {
  18. var node = _ref.node;
  19. functionNames.push({
  20. name: node.value,
  21. index: node.index
  22. });
  23. }
  24. });
  25. if (functionNames.length === 0) {
  26. return;
  27. }
  28. t.traverse(ast, {
  29. Func: function (_Func) {
  30. function Func(_x) {
  31. return _Func.apply(this, arguments);
  32. }
  33. Func.toString = function () {
  34. return _Func.toString();
  35. };
  36. return Func;
  37. }(function (_ref2) {
  38. var node = _ref2.node;
  39. // $FlowIgnore
  40. var nodeName = node.name;
  41. var indexBasedFunctionName = nodeName.value;
  42. var index = Number(indexBasedFunctionName.replace("func_", ""));
  43. var functionName = functionNames.find(function (f) {
  44. return f.index === index;
  45. });
  46. if (functionName) {
  47. var oldValue = nodeName.value;
  48. nodeName.value = functionName.name;
  49. nodeName.numeric = oldValue; // $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. var oldValue = node.index;
  115. node.index = t.identifier(functionName.name);
  116. node.numeric = oldValue; // $FlowIgnore
  117. delete node.raw;
  118. }
  119. })
  120. });
  121. }
  122. function restoreLocalNames(ast) {
  123. var localNames = [];
  124. t.traverse(ast, {
  125. LocalNameMetadata: function LocalNameMetadata(_ref5) {
  126. var node = _ref5.node;
  127. localNames.push({
  128. name: node.value,
  129. localIndex: node.localIndex,
  130. functionIndex: node.functionIndex
  131. });
  132. }
  133. });
  134. if (localNames.length === 0) {
  135. return;
  136. }
  137. t.traverse(ast, {
  138. Func: function (_Func2) {
  139. function Func(_x5) {
  140. return _Func2.apply(this, arguments);
  141. }
  142. Func.toString = function () {
  143. return _Func2.toString();
  144. };
  145. return Func;
  146. }(function (_ref6) {
  147. var node = _ref6.node;
  148. var signature = node.signature;
  149. if (signature.type !== "Signature") {
  150. return;
  151. } // $FlowIgnore
  152. var nodeName = node.name;
  153. var indexBasedFunctionName = nodeName.value;
  154. var functionIndex = Number(indexBasedFunctionName.replace("func_", ""));
  155. signature.params.forEach(function (param, paramIndex) {
  156. var paramName = localNames.find(function (f) {
  157. return f.localIndex === paramIndex && f.functionIndex === functionIndex;
  158. });
  159. if (paramName && paramName.name !== "") {
  160. param.id = paramName.name;
  161. }
  162. });
  163. })
  164. });
  165. }
  166. function restoreModuleName(ast) {
  167. t.traverse(ast, {
  168. ModuleNameMetadata: function (_ModuleNameMetadata) {
  169. function ModuleNameMetadata(_x6) {
  170. return _ModuleNameMetadata.apply(this, arguments);
  171. }
  172. ModuleNameMetadata.toString = function () {
  173. return _ModuleNameMetadata.toString();
  174. };
  175. return ModuleNameMetadata;
  176. }(function (moduleNameMetadataPath) {
  177. // update module
  178. t.traverse(ast, {
  179. Module: function (_Module) {
  180. function Module(_x7) {
  181. return _Module.apply(this, arguments);
  182. }
  183. Module.toString = function () {
  184. return _Module.toString();
  185. };
  186. return Module;
  187. }(function (_ref7) {
  188. var node = _ref7.node;
  189. var name = moduleNameMetadataPath.node.value; // compatiblity with wast-parser
  190. if (name === "") {
  191. name = null;
  192. }
  193. node.id = name;
  194. })
  195. });
  196. })
  197. });
  198. }
  199. export function decode(buf, customOpts) {
  200. var opts = Object.assign({}, defaultDecoderOpts, customOpts);
  201. var ast = decoder.decode(buf, opts);
  202. if (opts.ignoreCustomNameSection === false) {
  203. restoreFunctionNames(ast);
  204. restoreLocalNames(ast);
  205. restoreModuleName(ast);
  206. }
  207. return ast;
  208. }