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