1 | import { encodeU32 } from "@webassemblyjs/wasm-gen";
|
---|
2 | import { getSectionMetadata, traverse, shiftSection } from "@webassemblyjs/ast";
|
---|
3 | import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer";
|
---|
4 | export function resizeSectionByteSize(ast, uint8Buffer, section, deltaBytes) {
|
---|
5 | var sectionMetadata = getSectionMetadata(ast, section);
|
---|
6 |
|
---|
7 | if (typeof sectionMetadata === "undefined") {
|
---|
8 | throw new Error("Section metadata not found");
|
---|
9 | }
|
---|
10 |
|
---|
11 | if (typeof sectionMetadata.size.loc === "undefined") {
|
---|
12 | throw new Error("SectionMetadata " + section + " has no loc");
|
---|
13 | } // keep old node location to be overriden
|
---|
14 |
|
---|
15 |
|
---|
16 | var start = sectionMetadata.size.loc.start.column;
|
---|
17 | var end = sectionMetadata.size.loc.end.column;
|
---|
18 | var newSectionSize = sectionMetadata.size.value + deltaBytes;
|
---|
19 | var newBytes = encodeU32(newSectionSize);
|
---|
20 | /**
|
---|
21 | * update AST
|
---|
22 | */
|
---|
23 |
|
---|
24 | sectionMetadata.size.value = newSectionSize;
|
---|
25 | var oldu32EncodedLen = end - start;
|
---|
26 | var newu32EncodedLen = newBytes.length; // the new u32 has a different encoded length
|
---|
27 |
|
---|
28 | if (newu32EncodedLen !== oldu32EncodedLen) {
|
---|
29 | var deltaInSizeEncoding = newu32EncodedLen - oldu32EncodedLen;
|
---|
30 | sectionMetadata.size.loc.end.column = start + newu32EncodedLen;
|
---|
31 | deltaBytes += deltaInSizeEncoding; // move the vec size pointer size the section size is now smaller
|
---|
32 |
|
---|
33 | sectionMetadata.vectorOfSize.loc.start.column += deltaInSizeEncoding;
|
---|
34 | sectionMetadata.vectorOfSize.loc.end.column += deltaInSizeEncoding;
|
---|
35 | } // Once we hit our section every that is after needs to be shifted by the delta
|
---|
36 |
|
---|
37 |
|
---|
38 | var encounteredSection = false;
|
---|
39 | traverse(ast, {
|
---|
40 | SectionMetadata: function SectionMetadata(path) {
|
---|
41 | if (path.node.section === section) {
|
---|
42 | encounteredSection = true;
|
---|
43 | return;
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (encounteredSection === true) {
|
---|
47 | shiftSection(ast, path.node, deltaBytes);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | });
|
---|
51 | return overrideBytesInBuffer(uint8Buffer, start, end, newBytes);
|
---|
52 | }
|
---|
53 | export function resizeSectionVecSize(ast, uint8Buffer, section, deltaElements) {
|
---|
54 | var sectionMetadata = getSectionMetadata(ast, section);
|
---|
55 |
|
---|
56 | if (typeof sectionMetadata === "undefined") {
|
---|
57 | throw new Error("Section metadata not found");
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (typeof sectionMetadata.vectorOfSize.loc === "undefined") {
|
---|
61 | throw new Error("SectionMetadata " + section + " has no loc");
|
---|
62 | } // Section has no vector
|
---|
63 |
|
---|
64 |
|
---|
65 | if (sectionMetadata.vectorOfSize.value === -1) {
|
---|
66 | return uint8Buffer;
|
---|
67 | } // keep old node location to be overriden
|
---|
68 |
|
---|
69 |
|
---|
70 | var start = sectionMetadata.vectorOfSize.loc.start.column;
|
---|
71 | var end = sectionMetadata.vectorOfSize.loc.end.column;
|
---|
72 | var newValue = sectionMetadata.vectorOfSize.value + deltaElements;
|
---|
73 | var newBytes = encodeU32(newValue); // Update AST
|
---|
74 |
|
---|
75 | sectionMetadata.vectorOfSize.value = newValue;
|
---|
76 | sectionMetadata.vectorOfSize.loc.end.column = start + newBytes.length;
|
---|
77 | return overrideBytesInBuffer(uint8Buffer, start, end, newBytes);
|
---|
78 | } |
---|