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