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.

35 lines
1.3 KiB

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