Projekt

Obecné

Profil

Stáhnout (1.3 KB) Statistiky
| Větev: | Revize:
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

    
6
  if (sectionMetadatas.length === 0) {
7
    throw new Error("Section metadata not found");
8
  }
9

    
10
  return sectionMetadatas.reverse().reduce(function (uint8Buffer, sectionMetadata) {
11
    var startsIncludingId = sectionMetadata.startOffset - 1;
12
    var ends = section === "start" ? sectionMetadata.size.loc.end.column + 1 : sectionMetadata.startOffset + sectionMetadata.size.value + 1;
13
    var delta = -(ends - startsIncludingId);
14
    /**
15
     * update AST
16
     */
17
    // Once we hit our section every that is after needs to be shifted by the delta
18

    
19
    var encounteredSection = false;
20
    traverse(ast, {
21
      SectionMetadata: function SectionMetadata(path) {
22
        if (path.node.section === section) {
23
          encounteredSection = true;
24
          return path.remove();
25
        }
26

    
27
        if (encounteredSection === true) {
28
          shiftSection(ast, path.node, delta);
29
        }
30
      }
31
    }); // replacement is nothing
32

    
33
    var replacement = [];
34
    return overrideBytesInBuffer(uint8Buffer, startsIncludingId, ends, replacement);
35
  }, uint8Buffer);
36
}
(3-3/4)