utils.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isAnonymous = isAnonymous;
  6. exports.getSectionMetadata = getSectionMetadata;
  7. exports.getSectionMetadatas = getSectionMetadatas;
  8. exports.sortSectionMetadata = sortSectionMetadata;
  9. exports.orderedInsertNode = orderedInsertNode;
  10. exports.assertHasLoc = assertHasLoc;
  11. exports.getEndOfSection = getEndOfSection;
  12. exports.shiftLoc = shiftLoc;
  13. exports.shiftSection = shiftSection;
  14. exports.signatureForOpcode = signatureForOpcode;
  15. exports.getUniqueNameGenerator = getUniqueNameGenerator;
  16. exports.getStartByteOffset = getStartByteOffset;
  17. exports.getEndByteOffset = getEndByteOffset;
  18. exports.getFunctionBeginingByteOffset = getFunctionBeginingByteOffset;
  19. exports.getEndBlockByteOffset = getEndBlockByteOffset;
  20. exports.getStartBlockByteOffset = getStartBlockByteOffset;
  21. var _signatures = require("./signatures");
  22. var _traverse = require("./traverse");
  23. var _helperWasmBytecode = _interopRequireWildcard(require("@webassemblyjs/helper-wasm-bytecode"));
  24. 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; } }
  25. function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  26. function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
  27. function _typeof(obj) { 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); }
  28. function isAnonymous(ident) {
  29. return ident.raw === "";
  30. }
  31. function getSectionMetadata(ast, name) {
  32. var section;
  33. (0, _traverse.traverse)(ast, {
  34. SectionMetadata: function (_SectionMetadata) {
  35. function SectionMetadata(_x) {
  36. return _SectionMetadata.apply(this, arguments);
  37. }
  38. SectionMetadata.toString = function () {
  39. return _SectionMetadata.toString();
  40. };
  41. return SectionMetadata;
  42. }(function (_ref) {
  43. var node = _ref.node;
  44. if (node.section === name) {
  45. section = node;
  46. }
  47. })
  48. });
  49. return section;
  50. }
  51. function getSectionMetadatas(ast, name) {
  52. var sections = [];
  53. (0, _traverse.traverse)(ast, {
  54. SectionMetadata: function (_SectionMetadata2) {
  55. function SectionMetadata(_x2) {
  56. return _SectionMetadata2.apply(this, arguments);
  57. }
  58. SectionMetadata.toString = function () {
  59. return _SectionMetadata2.toString();
  60. };
  61. return SectionMetadata;
  62. }(function (_ref2) {
  63. var node = _ref2.node;
  64. if (node.section === name) {
  65. sections.push(node);
  66. }
  67. })
  68. });
  69. return sections;
  70. }
  71. function sortSectionMetadata(m) {
  72. if (m.metadata == null) {
  73. console.warn("sortSectionMetadata: no metadata to sort");
  74. return;
  75. } // $FlowIgnore
  76. m.metadata.sections.sort(function (a, b) {
  77. var aId = _helperWasmBytecode.default.sections[a.section];
  78. var bId = _helperWasmBytecode.default.sections[b.section];
  79. if (typeof aId !== "number" || typeof bId !== "number") {
  80. throw new Error("Section id not found");
  81. }
  82. return aId - bId;
  83. });
  84. }
  85. function orderedInsertNode(m, n) {
  86. assertHasLoc(n);
  87. var didInsert = false;
  88. if (n.type === "ModuleExport") {
  89. m.fields.push(n);
  90. return;
  91. }
  92. m.fields = m.fields.reduce(function (acc, field) {
  93. var fieldEndCol = Infinity;
  94. if (field.loc != null) {
  95. // $FlowIgnore
  96. fieldEndCol = field.loc.end.column;
  97. } // $FlowIgnore: assertHasLoc ensures that
  98. if (didInsert === false && n.loc.start.column < fieldEndCol) {
  99. didInsert = true;
  100. acc.push(n);
  101. }
  102. acc.push(field);
  103. return acc;
  104. }, []); // Handles empty modules or n is the last element
  105. if (didInsert === false) {
  106. m.fields.push(n);
  107. }
  108. }
  109. function assertHasLoc(n) {
  110. if (n.loc == null || n.loc.start == null || n.loc.end == null) {
  111. throw new Error("Internal failure: node (".concat(JSON.stringify(n.type), ") has no location information"));
  112. }
  113. }
  114. function getEndOfSection(s) {
  115. assertHasLoc(s.size);
  116. return s.startOffset + s.size.value + ( // $FlowIgnore
  117. s.size.loc.end.column - s.size.loc.start.column);
  118. }
  119. function shiftLoc(node, delta) {
  120. // $FlowIgnore
  121. node.loc.start.column += delta; // $FlowIgnore
  122. node.loc.end.column += delta;
  123. }
  124. function shiftSection(ast, node, delta) {
  125. if (node.type !== "SectionMetadata") {
  126. throw new Error("Can not shift node " + JSON.stringify(node.type));
  127. }
  128. node.startOffset += delta;
  129. if (_typeof(node.size.loc) === "object") {
  130. shiftLoc(node.size, delta);
  131. } // Custom sections doesn't have vectorOfSize
  132. if (_typeof(node.vectorOfSize) === "object" && _typeof(node.vectorOfSize.loc) === "object") {
  133. shiftLoc(node.vectorOfSize, delta);
  134. }
  135. var sectionName = node.section; // shift node locations within that section
  136. (0, _traverse.traverse)(ast, {
  137. Node: function Node(_ref3) {
  138. var node = _ref3.node;
  139. var section = (0, _helperWasmBytecode.getSectionForNode)(node);
  140. if (section === sectionName && _typeof(node.loc) === "object") {
  141. shiftLoc(node, delta);
  142. }
  143. }
  144. });
  145. }
  146. function signatureForOpcode(object, name) {
  147. var opcodeName = name;
  148. if (object !== undefined && object !== "") {
  149. opcodeName = object + "." + name;
  150. }
  151. var sign = _signatures.signatures[opcodeName];
  152. if (sign == undefined) {
  153. // TODO: Uncomment this when br_table and others has been done
  154. //throw new Error("Invalid opcode: "+opcodeName);
  155. return [object, object];
  156. }
  157. return sign[0];
  158. }
  159. function getUniqueNameGenerator() {
  160. var inc = {};
  161. return function () {
  162. var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "temp";
  163. if (!(prefix in inc)) {
  164. inc[prefix] = 0;
  165. } else {
  166. inc[prefix] = inc[prefix] + 1;
  167. }
  168. return prefix + "_" + inc[prefix];
  169. };
  170. }
  171. function getStartByteOffset(n) {
  172. // $FlowIgnore
  173. if (typeof n.loc === "undefined" || typeof n.loc.start === "undefined") {
  174. throw new Error( // $FlowIgnore
  175. "Can not get byte offset without loc informations, node: " + String(n.id));
  176. }
  177. return n.loc.start.column;
  178. }
  179. function getEndByteOffset(n) {
  180. // $FlowIgnore
  181. if (typeof n.loc === "undefined" || typeof n.loc.end === "undefined") {
  182. throw new Error("Can not get byte offset without loc informations, node: " + n.type);
  183. }
  184. return n.loc.end.column;
  185. }
  186. function getFunctionBeginingByteOffset(n) {
  187. if (!(n.body.length > 0)) {
  188. throw new Error('n.body.length > 0' + " error: " + (undefined || "unknown"));
  189. }
  190. var _n$body = _slicedToArray(n.body, 1),
  191. firstInstruction = _n$body[0];
  192. return getStartByteOffset(firstInstruction);
  193. }
  194. function getEndBlockByteOffset(n) {
  195. // $FlowIgnore
  196. if (!(n.instr.length > 0 || n.body.length > 0)) {
  197. throw new Error('n.instr.length > 0 || n.body.length > 0' + " error: " + (undefined || "unknown"));
  198. }
  199. var lastInstruction;
  200. if (n.instr) {
  201. // $FlowIgnore
  202. lastInstruction = n.instr[n.instr.length - 1];
  203. }
  204. if (n.body) {
  205. // $FlowIgnore
  206. lastInstruction = n.body[n.body.length - 1];
  207. }
  208. if (!(_typeof(lastInstruction) === "object")) {
  209. throw new Error('typeof lastInstruction === "object"' + " error: " + (undefined || "unknown"));
  210. }
  211. // $FlowIgnore
  212. return getStartByteOffset(lastInstruction);
  213. }
  214. function getStartBlockByteOffset(n) {
  215. // $FlowIgnore
  216. if (!(n.instr.length > 0 || n.body.length > 0)) {
  217. throw new Error('n.instr.length > 0 || n.body.length > 0' + " error: " + (undefined || "unknown"));
  218. }
  219. var fistInstruction;
  220. if (n.instr) {
  221. // $FlowIgnore
  222. var _n$instr = _slicedToArray(n.instr, 1);
  223. fistInstruction = _n$instr[0];
  224. }
  225. if (n.body) {
  226. // $FlowIgnore
  227. var _n$body2 = _slicedToArray(n.body, 1);
  228. fistInstruction = _n$body2[0];
  229. }
  230. if (!(_typeof(fistInstruction) === "object")) {
  231. throw new Error('typeof fistInstruction === "object"' + " error: " + (undefined || "unknown"));
  232. }
  233. // $FlowIgnore
  234. return getStartByteOffset(fistInstruction);
  235. }