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.

256 lines
6.8 KiB

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