You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.removeSections = removeSections;
  6. var _ast = require("@webassemblyjs/ast");
  7. var _helperBuffer = require("@webassemblyjs/helper-buffer");
  8. function removeSections(ast, uint8Buffer, section) {
  9. var sectionMetadatas = (0, _ast.getSectionMetadatas)(ast, section);
  10. if (sectionMetadatas.length === 0) {
  11. throw new Error("Section metadata not found");
  12. }
  13. return sectionMetadatas.reverse().reduce(function (uint8Buffer, sectionMetadata) {
  14. var startsIncludingId = sectionMetadata.startOffset - 1;
  15. var ends = section === "start" ? sectionMetadata.size.loc.end.column + 1 : sectionMetadata.startOffset + sectionMetadata.size.value + 1;
  16. var delta = -(ends - startsIncludingId);
  17. /**
  18. * update AST
  19. */
  20. // Once we hit our section every that is after needs to be shifted by the delta
  21. var encounteredSection = false;
  22. (0, _ast.traverse)(ast, {
  23. SectionMetadata: function SectionMetadata(path) {
  24. if (path.node.section === section) {
  25. encounteredSection = true;
  26. return path.remove();
  27. }
  28. if (encounteredSection === true) {
  29. (0, _ast.shiftSection)(ast, path.node, delta);
  30. }
  31. }
  32. }); // replacement is nothing
  33. var replacement = [];
  34. return (0, _helperBuffer.overrideBytesInBuffer)(uint8Buffer, startsIncludingId, ends, replacement);
  35. }, uint8Buffer);
  36. }