utils.js 4.7 KB

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