source: imaps-frontend/node_modules/@webassemblyjs/wasm-edit/lib/index.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 6.0 KB
Line 
1"use strict";
2
3function _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
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.edit = edit;
9exports.editWithAST = editWithAST;
10exports.add = add;
11exports.addWithAST = addWithAST;
12
13var _wasmParser = require("@webassemblyjs/wasm-parser");
14
15var _ast = require("@webassemblyjs/ast");
16
17var _clone = require("@webassemblyjs/ast/lib/clone");
18
19var _wasmOpt = require("@webassemblyjs/wasm-opt");
20
21var _helperWasmBytecode = _interopRequireWildcard(require("@webassemblyjs/helper-wasm-bytecode"));
22
23var _apply = require("./apply");
24
25function _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
27function _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
29function _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
31function _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
33function _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
35function hashNode(node) {
36 return JSON.stringify(node);
37}
38
39function preprocess(ab) {
40 var optBin = (0, _wasmOpt.shrinkPaddedLEB128)(new Uint8Array(ab));
41 return optBin.buffer;
42}
43
44function 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
80function edit(ab, visitors) {
81 ab = preprocess(ab);
82 var ast = (0, _wasmParser.decode)(ab);
83 return editWithAST(ast, ab, visitors);
84}
85
86function 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
115function add(ab, newNodes) {
116 ab = preprocess(ab);
117 var ast = (0, _wasmParser.decode)(ab);
118 return addWithAST(ast, ab, newNodes);
119}
120
121function 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}
Note: See TracBrowser for help on using the repository browser.