utils.js 10 KB

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