Spaces:
Sleeping
Sleeping
File size: 3,885 Bytes
4cadbaf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.edit = edit;
exports.editWithAST = editWithAST;
exports.add = add;
exports.addWithAST = addWithAST;
var _wasmParser = require("@webassemblyjs/wasm-parser");
var _ast = require("@webassemblyjs/ast");
var _clone = require("@webassemblyjs/ast/lib/clone");
var _wasmOpt = require("@webassemblyjs/wasm-opt");
var _helperWasmBytecode = _interopRequireWildcard(require("@webassemblyjs/helper-wasm-bytecode"));
var _apply = require("./apply");
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function hashNode(node) {
return JSON.stringify(node);
}
function preprocess(ab) {
var optBin = (0, _wasmOpt.shrinkPaddedLEB128)(new Uint8Array(ab));
return optBin.buffer;
}
function sortBySectionOrder(nodes) {
var originalOrder = new Map();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _node = _step.value;
originalOrder.set(_node, originalOrder.size);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
nodes.sort(function (a, b) {
var sectionA = (0, _helperWasmBytecode.getSectionForNode)(a);
var sectionB = (0, _helperWasmBytecode.getSectionForNode)(b);
var aId = _helperWasmBytecode.default.sections[sectionA];
var bId = _helperWasmBytecode.default.sections[sectionB];
if (typeof aId !== "number" || typeof bId !== "number") {
throw new Error("Section id not found");
}
if (aId === bId) {
// $FlowIgnore originalOrder is filled for all nodes
return originalOrder.get(a) - originalOrder.get(b);
}
return aId - bId;
});
}
function edit(ab, visitors) {
ab = preprocess(ab);
var ast = (0, _wasmParser.decode)(ab);
return editWithAST(ast, ab, visitors);
}
function editWithAST(ast, ab, visitors) {
var operations = [];
var uint8Buffer = new Uint8Array(ab);
var nodeBefore;
function before(type, path) {
nodeBefore = (0, _clone.cloneNode)(path.node);
}
function after(type, path) {
if (path.node._deleted === true) {
operations.push({
kind: "delete",
node: path.node
}); // $FlowIgnore
} else if (hashNode(nodeBefore) !== hashNode(path.node)) {
operations.push({
kind: "update",
oldNode: nodeBefore,
node: path.node
});
}
}
(0, _ast.traverse)(ast, visitors, before, after);
uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
return uint8Buffer.buffer;
}
function add(ab, newNodes) {
ab = preprocess(ab);
var ast = (0, _wasmParser.decode)(ab);
return addWithAST(ast, ab, newNodes);
}
function addWithAST(ast, ab, newNodes) {
// Sort nodes by insertion order
sortBySectionOrder(newNodes);
var uint8Buffer = new Uint8Array(ab); // Map node into operations
var operations = newNodes.map(function (n) {
return {
kind: "add",
node: n
};
});
uint8Buffer = (0, _apply.applyOperations)(ast, uint8Buffer, operations);
return uint8Buffer.buffer;
} |