1 | function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
---|
2 |
|
---|
3 | 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); }
|
---|
4 |
|
---|
5 | 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; }
|
---|
6 |
|
---|
7 | import { decode } from "@webassemblyjs/wasm-parser";
|
---|
8 | import { traverse } from "@webassemblyjs/ast";
|
---|
9 | import { cloneNode } from "@webassemblyjs/ast/lib/clone";
|
---|
10 | import { shrinkPaddedLEB128 } from "@webassemblyjs/wasm-opt";
|
---|
11 | import { getSectionForNode } from "@webassemblyjs/helper-wasm-bytecode";
|
---|
12 | import constants from "@webassemblyjs/helper-wasm-bytecode";
|
---|
13 | import { applyOperations } from "./apply";
|
---|
14 |
|
---|
15 | function hashNode(node) {
|
---|
16 | return JSON.stringify(node);
|
---|
17 | }
|
---|
18 |
|
---|
19 | function preprocess(ab) {
|
---|
20 | var optBin = shrinkPaddedLEB128(new Uint8Array(ab));
|
---|
21 | return optBin.buffer;
|
---|
22 | }
|
---|
23 |
|
---|
24 | function sortBySectionOrder(nodes) {
|
---|
25 | var originalOrder = new Map();
|
---|
26 |
|
---|
27 | var _iterator = _createForOfIteratorHelper(nodes),
|
---|
28 | _step;
|
---|
29 |
|
---|
30 | try {
|
---|
31 | for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
---|
32 | var node = _step.value;
|
---|
33 | originalOrder.set(node, originalOrder.size);
|
---|
34 | }
|
---|
35 | } catch (err) {
|
---|
36 | _iterator.e(err);
|
---|
37 | } finally {
|
---|
38 | _iterator.f();
|
---|
39 | }
|
---|
40 |
|
---|
41 | nodes.sort(function (a, b) {
|
---|
42 | var sectionA = getSectionForNode(a);
|
---|
43 | var sectionB = getSectionForNode(b);
|
---|
44 | var aId = constants.sections[sectionA];
|
---|
45 | var bId = constants.sections[sectionB];
|
---|
46 |
|
---|
47 | if (typeof aId !== "number" || typeof bId !== "number") {
|
---|
48 | throw new Error("Section id not found");
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (aId === bId) {
|
---|
52 | // $FlowIgnore originalOrder is filled for all nodes
|
---|
53 | return originalOrder.get(a) - originalOrder.get(b);
|
---|
54 | }
|
---|
55 |
|
---|
56 | return aId - bId;
|
---|
57 | });
|
---|
58 | }
|
---|
59 |
|
---|
60 | export function edit(ab, visitors) {
|
---|
61 | ab = preprocess(ab);
|
---|
62 | var ast = decode(ab);
|
---|
63 | return editWithAST(ast, ab, visitors);
|
---|
64 | }
|
---|
65 | export function editWithAST(ast, ab, visitors) {
|
---|
66 | var operations = [];
|
---|
67 | var uint8Buffer = new Uint8Array(ab);
|
---|
68 | var nodeBefore;
|
---|
69 |
|
---|
70 | function before(type, path) {
|
---|
71 | nodeBefore = cloneNode(path.node);
|
---|
72 | }
|
---|
73 |
|
---|
74 | function after(type, path) {
|
---|
75 | if (path.node._deleted === true) {
|
---|
76 | operations.push({
|
---|
77 | kind: "delete",
|
---|
78 | node: path.node
|
---|
79 | }); // $FlowIgnore
|
---|
80 | } else if (hashNode(nodeBefore) !== hashNode(path.node)) {
|
---|
81 | operations.push({
|
---|
82 | kind: "update",
|
---|
83 | oldNode: nodeBefore,
|
---|
84 | node: path.node
|
---|
85 | });
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | traverse(ast, visitors, before, after);
|
---|
90 | uint8Buffer = applyOperations(ast, uint8Buffer, operations);
|
---|
91 | return uint8Buffer.buffer;
|
---|
92 | }
|
---|
93 | export function add(ab, newNodes) {
|
---|
94 | ab = preprocess(ab);
|
---|
95 | var ast = decode(ab);
|
---|
96 | return addWithAST(ast, ab, newNodes);
|
---|
97 | }
|
---|
98 | export function addWithAST(ast, ab, newNodes) {
|
---|
99 | // Sort nodes by insertion order
|
---|
100 | sortBySectionOrder(newNodes);
|
---|
101 | var uint8Buffer = new Uint8Array(ab); // Map node into operations
|
---|
102 |
|
---|
103 | var operations = newNodes.map(function (n) {
|
---|
104 | return {
|
---|
105 | kind: "add",
|
---|
106 | node: n
|
---|
107 | };
|
---|
108 | });
|
---|
109 | uint8Buffer = applyOperations(ast, uint8Buffer, operations);
|
---|
110 | return uint8Buffer.buffer;
|
---|
111 | } |
---|