1 | import { traverse, shiftSection } from "@webassemblyjs/ast";
|
---|
2 | import { encodeU32 } from "@webassemblyjs/wasm-gen/lib/encoder";
|
---|
3 | import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer";
|
---|
4 |
|
---|
5 | function shiftFollowingSections(ast, _ref, deltaInSizeEncoding) {
|
---|
6 | var section = _ref.section;
|
---|
7 | // Once we hit our section every that is after needs to be shifted by the delta
|
---|
8 | var encounteredSection = false;
|
---|
9 | traverse(ast, {
|
---|
10 | SectionMetadata: function SectionMetadata(path) {
|
---|
11 | if (path.node.section === section) {
|
---|
12 | encounteredSection = true;
|
---|
13 | return;
|
---|
14 | }
|
---|
15 |
|
---|
16 | if (encounteredSection === true) {
|
---|
17 | shiftSection(ast, path.node, deltaInSizeEncoding);
|
---|
18 | }
|
---|
19 | }
|
---|
20 | });
|
---|
21 | }
|
---|
22 |
|
---|
23 | export function shrinkPaddedLEB128(ast, uint8Buffer) {
|
---|
24 | traverse(ast, {
|
---|
25 | SectionMetadata: function SectionMetadata(_ref2) {
|
---|
26 | var node = _ref2.node;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Section size
|
---|
30 | */
|
---|
31 | {
|
---|
32 | var newu32Encoded = encodeU32(node.size.value);
|
---|
33 | var newu32EncodedLen = newu32Encoded.length;
|
---|
34 | var start = node.size.loc.start.column;
|
---|
35 | var end = node.size.loc.end.column;
|
---|
36 | var oldu32EncodedLen = end - start;
|
---|
37 |
|
---|
38 | if (newu32EncodedLen !== oldu32EncodedLen) {
|
---|
39 | var deltaInSizeEncoding = oldu32EncodedLen - newu32EncodedLen;
|
---|
40 | uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newu32Encoded);
|
---|
41 | shiftFollowingSections(ast, node, -deltaInSizeEncoding);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | });
|
---|
46 | return uint8Buffer;
|
---|
47 | } |
---|