utils.js 8.4 KB

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