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