source: trip-planner-front/node_modules/@webassemblyjs/wasm-edit/esm/apply.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.1 KB
Line 
1function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
2
3function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
4
5import { encodeNode } from "@webassemblyjs/wasm-gen";
6import { encodeU32 } from "@webassemblyjs/wasm-gen/lib/encoder";
7import { isFunc, isGlobal, assertHasLoc, orderedInsertNode, getSectionMetadata, traverse, getEndOfSection } from "@webassemblyjs/ast";
8import { resizeSectionByteSize, resizeSectionVecSize, createEmptySection, removeSections } from "@webassemblyjs/helper-wasm-section";
9import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer";
10import { getSectionForNode } from "@webassemblyjs/helper-wasm-bytecode";
11
12function shiftLocNodeByDelta(node, delta) {
13 assertHasLoc(node); // $FlowIgnore: assertHasLoc ensures that
14
15 node.loc.start.column += delta; // $FlowIgnore: assertHasLoc ensures that
16
17 node.loc.end.column += delta;
18}
19
20function applyUpdate(ast, uint8Buffer, _ref) {
21 var _ref2 = _slicedToArray(_ref, 2),
22 oldNode = _ref2[0],
23 newNode = _ref2[1];
24
25 var deltaElements = 0;
26 assertHasLoc(oldNode);
27 var sectionName = getSectionForNode(newNode);
28 var replacementByteArray = encodeNode(newNode);
29 /**
30 * Replace new node as bytes
31 */
32
33 uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
34 oldNode.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
35 oldNode.loc.end.column, replacementByteArray);
36 /**
37 * Update function body size if needed
38 */
39
40 if (sectionName === "code") {
41 // Find the parent func
42 traverse(ast, {
43 Func: function Func(_ref3) {
44 var node = _ref3.node;
45 var funcHasThisIntr = node.body.find(function (n) {
46 return n === newNode;
47 }) !== undefined; // Update func's body size if needed
48
49 if (funcHasThisIntr === true) {
50 // These are the old functions locations informations
51 assertHasLoc(node);
52 var oldNodeSize = encodeNode(oldNode).length;
53 var bodySizeDeltaBytes = replacementByteArray.length - oldNodeSize;
54
55 if (bodySizeDeltaBytes !== 0) {
56 var newValue = node.metadata.bodySize + bodySizeDeltaBytes;
57 var newByteArray = encodeU32(newValue); // function body size byte
58 // FIXME(sven): only handles one byte u32
59
60 var start = node.loc.start.column;
61 var end = start + 1;
62 uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newByteArray);
63 }
64 }
65 }
66 });
67 }
68 /**
69 * Update section size
70 */
71
72
73 var deltaBytes = replacementByteArray.length - ( // $FlowIgnore: assertHasLoc ensures that
74 oldNode.loc.end.column - oldNode.loc.start.column); // Init location informations
75
76 newNode.loc = {
77 start: {
78 line: -1,
79 column: -1
80 },
81 end: {
82 line: -1,
83 column: -1
84 }
85 }; // Update new node end position
86 // $FlowIgnore: assertHasLoc ensures that
87
88 newNode.loc.start.column = oldNode.loc.start.column; // $FlowIgnore: assertHasLoc ensures that
89
90 newNode.loc.end.column = // $FlowIgnore: assertHasLoc ensures that
91 oldNode.loc.start.column + replacementByteArray.length;
92 return {
93 uint8Buffer: uint8Buffer,
94 deltaBytes: deltaBytes,
95 deltaElements: deltaElements
96 };
97}
98
99function applyDelete(ast, uint8Buffer, node) {
100 var deltaElements = -1; // since we removed an element
101
102 assertHasLoc(node);
103 var sectionName = getSectionForNode(node);
104
105 if (sectionName === "start") {
106 var sectionMetadata = getSectionMetadata(ast, "start");
107 /**
108 * The start section only contains one element,
109 * we need to remove the whole section
110 */
111
112 uint8Buffer = removeSections(ast, uint8Buffer, "start");
113
114 var _deltaBytes = -(sectionMetadata.size.value + 1);
115 /* section id */
116
117
118 return {
119 uint8Buffer: uint8Buffer,
120 deltaBytes: _deltaBytes,
121 deltaElements: deltaElements
122 };
123 } // replacement is nothing
124
125
126 var replacement = [];
127 uint8Buffer = overrideBytesInBuffer(uint8Buffer, // $FlowIgnore: assertHasLoc ensures that
128 node.loc.start.column, // $FlowIgnore: assertHasLoc ensures that
129 node.loc.end.column, replacement);
130 /**
131 * Update section
132 */
133 // $FlowIgnore: assertHasLoc ensures that
134
135 var deltaBytes = -(node.loc.end.column - node.loc.start.column);
136 return {
137 uint8Buffer: uint8Buffer,
138 deltaBytes: deltaBytes,
139 deltaElements: deltaElements
140 };
141}
142
143function applyAdd(ast, uint8Buffer, node) {
144 var deltaElements = +1; // since we added an element
145
146 var sectionName = getSectionForNode(node);
147 var sectionMetadata = getSectionMetadata(ast, sectionName); // Section doesn't exists, we create an empty one
148
149 if (typeof sectionMetadata === "undefined") {
150 var res = createEmptySection(ast, uint8Buffer, sectionName);
151 uint8Buffer = res.uint8Buffer;
152 sectionMetadata = res.sectionMetadata;
153 }
154 /**
155 * check that the expressions were ended
156 */
157
158
159 if (isFunc(node)) {
160 // $FlowIgnore
161 var body = node.body;
162
163 if (body.length === 0 || body[body.length - 1].id !== "end") {
164 throw new Error("expressions must be ended");
165 }
166 }
167
168 if (isGlobal(node)) {
169 // $FlowIgnore
170 var body = node.init;
171
172 if (body.length === 0 || body[body.length - 1].id !== "end") {
173 throw new Error("expressions must be ended");
174 }
175 }
176 /**
177 * Add nodes
178 */
179
180
181 var newByteArray = encodeNode(node); // The size of the section doesn't include the storage of the size itself
182 // we need to manually add it here
183
184 var start = getEndOfSection(sectionMetadata);
185 var end = start;
186 /**
187 * Update section
188 */
189
190 var deltaBytes = newByteArray.length;
191 uint8Buffer = overrideBytesInBuffer(uint8Buffer, start, end, newByteArray);
192 node.loc = {
193 start: {
194 line: -1,
195 column: start
196 },
197 end: {
198 line: -1,
199 column: start + deltaBytes
200 }
201 }; // for func add the additional metadata in the AST
202
203 if (node.type === "Func") {
204 // the size is the first byte
205 // FIXME(sven): handle LEB128 correctly here
206 var bodySize = newByteArray[0];
207 node.metadata = {
208 bodySize: bodySize
209 };
210 }
211
212 if (node.type !== "IndexInFuncSection") {
213 orderedInsertNode(ast.body[0], node);
214 }
215
216 return {
217 uint8Buffer: uint8Buffer,
218 deltaBytes: deltaBytes,
219 deltaElements: deltaElements
220 };
221}
222
223export function applyOperations(ast, uint8Buffer, ops) {
224 ops.forEach(function (op) {
225 var state;
226 var sectionName;
227
228 switch (op.kind) {
229 case "update":
230 state = applyUpdate(ast, uint8Buffer, [op.oldNode, op.node]);
231 sectionName = getSectionForNode(op.node);
232 break;
233
234 case "delete":
235 state = applyDelete(ast, uint8Buffer, op.node);
236 sectionName = getSectionForNode(op.node);
237 break;
238
239 case "add":
240 state = applyAdd(ast, uint8Buffer, op.node);
241 sectionName = getSectionForNode(op.node);
242 break;
243
244 default:
245 throw new Error("Unknown operation");
246 }
247 /**
248 * Resize section vec size.
249 * If the length of the LEB-encoded size changes, this can change
250 * the byte length of the section and the offset for nodes in the
251 * section. So we do this first before resizing section byte size
252 * or shifting following operations' nodes.
253 */
254
255
256 if (state.deltaElements !== 0 && sectionName !== "start") {
257 var oldBufferLength = state.uint8Buffer.length;
258 state.uint8Buffer = resizeSectionVecSize(ast, state.uint8Buffer, sectionName, state.deltaElements); // Infer bytes added/removed by comparing buffer lengths
259
260 state.deltaBytes += state.uint8Buffer.length - oldBufferLength;
261 }
262 /**
263 * Resize section byte size.
264 * If the length of the LEB-encoded size changes, this can change
265 * the offset for nodes in the section. So we do this before
266 * shifting following operations' nodes.
267 */
268
269
270 if (state.deltaBytes !== 0 && sectionName !== "start") {
271 var _oldBufferLength = state.uint8Buffer.length;
272 state.uint8Buffer = resizeSectionByteSize(ast, state.uint8Buffer, sectionName, state.deltaBytes); // Infer bytes added/removed by comparing buffer lengths
273
274 state.deltaBytes += state.uint8Buffer.length - _oldBufferLength;
275 }
276 /**
277 * Shift following operation's nodes
278 */
279
280
281 if (state.deltaBytes !== 0) {
282 ops.forEach(function (op) {
283 // We don't need to handle add ops, they are positioning independent
284 switch (op.kind) {
285 case "update":
286 shiftLocNodeByDelta(op.oldNode, state.deltaBytes);
287 break;
288
289 case "delete":
290 shiftLocNodeByDelta(op.node, state.deltaBytes);
291 break;
292 }
293 });
294 }
295
296 uint8Buffer = state.uint8Buffer;
297 });
298 return uint8Buffer;
299}
Note: See TracBrowser for help on using the repository browser.