utils.js 7.5 KB

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