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