source: trip-planner-front/node_modules/@webassemblyjs/wasm-edit/lib/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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