utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isAnonymous = isAnonymous;
  6. exports.getSectionMetadata = getSectionMetadata;
  7. exports.sortSectionMetadata = sortSectionMetadata;
  8. exports.orderedInsertNode = orderedInsertNode;
  9. exports.assertHasLoc = assertHasLoc;
  10. exports.getEndOfSection = getEndOfSection;
  11. exports.shiftLoc = shiftLoc;
  12. exports.shiftSection = shiftSection;
  13. exports.signatureForOpcode = signatureForOpcode;
  14. exports.getUniqueNameGenerator = getUniqueNameGenerator;
  15. var _signatures = require("./signatures");
  16. var _traverse = require("./traverse");
  17. var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  19. 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); }
  20. var debug = require("debug")("webassemblyjs:ast:utils");
  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 sortSectionMetadata(m) {
  45. if (m.metadata == null) {
  46. console.warn("sortSectionMetadata: no metadata to sort");
  47. return;
  48. } // $FlowIgnore
  49. m.metadata.sections.sort(function (a, b) {
  50. var aId = _helperWasmBytecode.default.sections[a.section];
  51. var bId = _helperWasmBytecode.default.sections[b.section];
  52. if (typeof aId !== "number" || typeof bId !== "number") {
  53. throw new Error("Section id not found");
  54. }
  55. return aId > bId;
  56. });
  57. }
  58. function orderedInsertNode(m, n) {
  59. assertHasLoc(n);
  60. var didInsert = false;
  61. if (n.type === "ModuleExport") {
  62. m.fields.push(n);
  63. return;
  64. }
  65. m.fields = m.fields.reduce(function (acc, field) {
  66. var fieldEndCol = Infinity;
  67. if (field.loc != null) {
  68. // $FlowIgnore
  69. fieldEndCol = field.loc.end.column;
  70. } // $FlowIgnore: assertHasLoc ensures that
  71. if (didInsert === false && n.loc.start.column < fieldEndCol) {
  72. didInsert = true;
  73. acc.push(n);
  74. }
  75. acc.push(field);
  76. return acc;
  77. }, []); // Handles empty modules or n is the last element
  78. if (didInsert === false) {
  79. m.fields.push(n);
  80. }
  81. }
  82. function assertHasLoc(n) {
  83. if (n.loc == null || n.loc.start == null || n.loc.end == null) {
  84. throw new Error("Internal failure: node (".concat(JSON.stringify(n.type), ") has no location information"));
  85. }
  86. }
  87. function getEndOfSection(s) {
  88. assertHasLoc(s.size);
  89. return s.startOffset + s.size.value + ( // $FlowIgnore
  90. s.size.loc.end.column - s.size.loc.start.column);
  91. }
  92. function shiftLoc(node, delta) {
  93. // $FlowIgnore
  94. node.loc.start.column += delta; // $FlowIgnore
  95. node.loc.end.column += delta;
  96. }
  97. function shiftSection(ast, node, delta) {
  98. if (node.type !== "SectionMetadata") {
  99. throw new Error("Can not shift node " + JSON.stringify(node.type));
  100. }
  101. node.startOffset += delta;
  102. if (_typeof(node.size.loc) === "object") {
  103. shiftLoc(node.size, delta);
  104. } // Custom sections doesn't have vectorOfSize
  105. if (_typeof(node.vectorOfSize) === "object" && _typeof(node.vectorOfSize.loc) === "object") {
  106. shiftLoc(node.vectorOfSize, delta);
  107. }
  108. debug("shifted %s startOffset=%d", node.type, node.startOffset);
  109. var sectionName = node.section; // shift node locations within that section
  110. (0, _traverse.traverse)(ast, {
  111. Node: function Node(_ref2) {
  112. var node = _ref2.node;
  113. var section = _helperWasmBytecode.default.getSectionForNode(node);
  114. if (section === sectionName && _typeof(node.loc) === "object") {
  115. shiftLoc(node, delta);
  116. }
  117. }
  118. });
  119. }
  120. function signatureForOpcode(object, name) {
  121. var opcodeName = name;
  122. if (object !== undefined && object !== "") {
  123. opcodeName = object + "." + name;
  124. }
  125. var sign = _signatures.signatures[opcodeName];
  126. if (sign == undefined) {
  127. // TODO: Uncomment this when br_table and others has been done
  128. //throw new Error("Invalid opcode: "+opcodeName);
  129. return [object, object];
  130. }
  131. return sign[0];
  132. }
  133. function getUniqueNameGenerator() {
  134. var inc = {};
  135. return function () {
  136. var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "temp";
  137. if (!(prefix in inc)) {
  138. inc[prefix] = 0;
  139. } else {
  140. inc[prefix] = inc[prefix] + 1;
  141. }
  142. return prefix + "_" + inc[prefix];
  143. };
  144. }