utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. var _signatures = require("./signatures");
  17. var _traverse = require("./traverse");
  18. var _helperWasmBytecode = _interopRequireWildcard(require("@webassemblyjs/helper-wasm-bytecode"));
  19. 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; } }
  20. 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); }
  21. function isAnonymous(ident) {
  22. return ident.raw === "";
  23. }
  24. function getSectionMetadata(ast, name) {
  25. var section;
  26. (0, _traverse.traverse)(ast, {
  27. SectionMetadata: function (_SectionMetadata) {
  28. function SectionMetadata(_x) {
  29. return _SectionMetadata.apply(this, arguments);
  30. }
  31. SectionMetadata.toString = function () {
  32. return _SectionMetadata.toString();
  33. };
  34. return SectionMetadata;
  35. }(function (_ref) {
  36. var node = _ref.node;
  37. if (node.section === name) {
  38. section = node;
  39. }
  40. })
  41. });
  42. return section;
  43. }
  44. function getSectionMetadatas(ast, name) {
  45. var sections = [];
  46. (0, _traverse.traverse)(ast, {
  47. SectionMetadata: function (_SectionMetadata2) {
  48. function SectionMetadata(_x2) {
  49. return _SectionMetadata2.apply(this, arguments);
  50. }
  51. SectionMetadata.toString = function () {
  52. return _SectionMetadata2.toString();
  53. };
  54. return SectionMetadata;
  55. }(function (_ref2) {
  56. var node = _ref2.node;
  57. if (node.section === name) {
  58. sections.push(node);
  59. }
  60. })
  61. });
  62. return sections;
  63. }
  64. function sortSectionMetadata(m) {
  65. if (m.metadata == null) {
  66. console.warn("sortSectionMetadata: no metadata to sort");
  67. return;
  68. } // $FlowIgnore
  69. m.metadata.sections.sort(function (a, b) {
  70. var aId = _helperWasmBytecode.default.sections[a.section];
  71. var bId = _helperWasmBytecode.default.sections[b.section];
  72. if (typeof aId !== "number" || typeof bId !== "number") {
  73. throw new Error("Section id not found");
  74. }
  75. return aId - bId;
  76. });
  77. }
  78. function orderedInsertNode(m, n) {
  79. assertHasLoc(n);
  80. var didInsert = false;
  81. if (n.type === "ModuleExport") {
  82. m.fields.push(n);
  83. return;
  84. }
  85. m.fields = m.fields.reduce(function (acc, field) {
  86. var fieldEndCol = Infinity;
  87. if (field.loc != null) {
  88. // $FlowIgnore
  89. fieldEndCol = field.loc.end.column;
  90. } // $FlowIgnore: assertHasLoc ensures that
  91. if (didInsert === false && n.loc.start.column < fieldEndCol) {
  92. didInsert = true;
  93. acc.push(n);
  94. }
  95. acc.push(field);
  96. return acc;
  97. }, []); // Handles empty modules or n is the last element
  98. if (didInsert === false) {
  99. m.fields.push(n);
  100. }
  101. }
  102. function assertHasLoc(n) {
  103. if (n.loc == null || n.loc.start == null || n.loc.end == null) {
  104. throw new Error("Internal failure: node (".concat(JSON.stringify(n.type), ") has no location information"));
  105. }
  106. }
  107. function getEndOfSection(s) {
  108. assertHasLoc(s.size);
  109. return s.startOffset + s.size.value + ( // $FlowIgnore
  110. s.size.loc.end.column - s.size.loc.start.column);
  111. }
  112. function shiftLoc(node, delta) {
  113. // $FlowIgnore
  114. node.loc.start.column += delta; // $FlowIgnore
  115. node.loc.end.column += delta;
  116. }
  117. function shiftSection(ast, node, delta) {
  118. if (node.type !== "SectionMetadata") {
  119. throw new Error("Can not shift node " + JSON.stringify(node.type));
  120. }
  121. node.startOffset += delta;
  122. if (_typeof(node.size.loc) === "object") {
  123. shiftLoc(node.size, delta);
  124. } // Custom sections doesn't have vectorOfSize
  125. if (_typeof(node.vectorOfSize) === "object" && _typeof(node.vectorOfSize.loc) === "object") {
  126. shiftLoc(node.vectorOfSize, delta);
  127. }
  128. var sectionName = node.section; // shift node locations within that section
  129. (0, _traverse.traverse)(ast, {
  130. Node: function Node(_ref3) {
  131. var node = _ref3.node;
  132. var section = (0, _helperWasmBytecode.getSectionForNode)(node);
  133. if (section === sectionName && _typeof(node.loc) === "object") {
  134. shiftLoc(node, delta);
  135. }
  136. }
  137. });
  138. }
  139. function signatureForOpcode(object, name) {
  140. var opcodeName = name;
  141. if (object !== undefined && object !== "") {
  142. opcodeName = object + "." + name;
  143. }
  144. var sign = _signatures.signatures[opcodeName];
  145. if (sign == undefined) {
  146. // TODO: Uncomment this when br_table and others has been done
  147. //throw new Error("Invalid opcode: "+opcodeName);
  148. return [object, object];
  149. }
  150. return sign[0];
  151. }
  152. function getUniqueNameGenerator() {
  153. var inc = {};
  154. return function () {
  155. var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "temp";
  156. if (!(prefix in inc)) {
  157. inc[prefix] = 0;
  158. } else {
  159. inc[prefix] = inc[prefix] + 1;
  160. }
  161. return prefix + "_" + inc[prefix];
  162. };
  163. }