1 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
---|
2 |
|
---|
3 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
---|
4 |
|
---|
5 | 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); }
|
---|
6 |
|
---|
7 | 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; }
|
---|
8 |
|
---|
9 | function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
---|
10 |
|
---|
11 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
---|
12 |
|
---|
13 | 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); }
|
---|
14 |
|
---|
15 | import { signatures } from "./signatures";
|
---|
16 | import { traverse } from "./traverse";
|
---|
17 | import constants from "@webassemblyjs/helper-wasm-bytecode";
|
---|
18 | import { getSectionForNode } from "@webassemblyjs/helper-wasm-bytecode";
|
---|
19 | export function isAnonymous(ident) {
|
---|
20 | return ident.raw === "";
|
---|
21 | }
|
---|
22 | export function getSectionMetadata(ast, name) {
|
---|
23 | var section;
|
---|
24 | traverse(ast, {
|
---|
25 | SectionMetadata: function (_SectionMetadata) {
|
---|
26 | function SectionMetadata(_x) {
|
---|
27 | return _SectionMetadata.apply(this, arguments);
|
---|
28 | }
|
---|
29 |
|
---|
30 | SectionMetadata.toString = function () {
|
---|
31 | return _SectionMetadata.toString();
|
---|
32 | };
|
---|
33 |
|
---|
34 | return SectionMetadata;
|
---|
35 | }(function (_ref) {
|
---|
36 | var node = _ref.node;
|
---|
37 |
|
---|
38 | if (node.section === name) {
|
---|
39 | section = node;
|
---|
40 | }
|
---|
41 | })
|
---|
42 | });
|
---|
43 | return section;
|
---|
44 | }
|
---|
45 | export function getSectionMetadatas(ast, name) {
|
---|
46 | var sections = [];
|
---|
47 | traverse(ast, {
|
---|
48 | SectionMetadata: function (_SectionMetadata2) {
|
---|
49 | function SectionMetadata(_x2) {
|
---|
50 | return _SectionMetadata2.apply(this, arguments);
|
---|
51 | }
|
---|
52 |
|
---|
53 | SectionMetadata.toString = function () {
|
---|
54 | return _SectionMetadata2.toString();
|
---|
55 | };
|
---|
56 |
|
---|
57 | return SectionMetadata;
|
---|
58 | }(function (_ref2) {
|
---|
59 | var node = _ref2.node;
|
---|
60 |
|
---|
61 | if (node.section === name) {
|
---|
62 | sections.push(node);
|
---|
63 | }
|
---|
64 | })
|
---|
65 | });
|
---|
66 | return sections;
|
---|
67 | }
|
---|
68 | export function sortSectionMetadata(m) {
|
---|
69 | if (m.metadata == null) {
|
---|
70 | console.warn("sortSectionMetadata: no metadata to sort");
|
---|
71 | return;
|
---|
72 | } // $FlowIgnore
|
---|
73 |
|
---|
74 |
|
---|
75 | m.metadata.sections.sort(function (a, b) {
|
---|
76 | var aId = constants.sections[a.section];
|
---|
77 | var bId = constants.sections[b.section];
|
---|
78 |
|
---|
79 | if (typeof aId !== "number" || typeof bId !== "number") {
|
---|
80 | throw new Error("Section id not found");
|
---|
81 | }
|
---|
82 |
|
---|
83 | return aId - bId;
|
---|
84 | });
|
---|
85 | }
|
---|
86 | export function orderedInsertNode(m, n) {
|
---|
87 | assertHasLoc(n);
|
---|
88 | var didInsert = false;
|
---|
89 |
|
---|
90 | if (n.type === "ModuleExport") {
|
---|
91 | m.fields.push(n);
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | m.fields = m.fields.reduce(function (acc, field) {
|
---|
96 | var fieldEndCol = Infinity;
|
---|
97 |
|
---|
98 | if (field.loc != null) {
|
---|
99 | // $FlowIgnore
|
---|
100 | fieldEndCol = field.loc.end.column;
|
---|
101 | } // $FlowIgnore: assertHasLoc ensures that
|
---|
102 |
|
---|
103 |
|
---|
104 | if (didInsert === false && n.loc.start.column < fieldEndCol) {
|
---|
105 | didInsert = true;
|
---|
106 | acc.push(n);
|
---|
107 | }
|
---|
108 |
|
---|
109 | acc.push(field);
|
---|
110 | return acc;
|
---|
111 | }, []); // Handles empty modules or n is the last element
|
---|
112 |
|
---|
113 | if (didInsert === false) {
|
---|
114 | m.fields.push(n);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | export function assertHasLoc(n) {
|
---|
118 | if (n.loc == null || n.loc.start == null || n.loc.end == null) {
|
---|
119 | throw new Error("Internal failure: node (".concat(JSON.stringify(n.type), ") has no location information"));
|
---|
120 | }
|
---|
121 | }
|
---|
122 | export function getEndOfSection(s) {
|
---|
123 | assertHasLoc(s.size);
|
---|
124 | return s.startOffset + s.size.value + (s.size.loc.end.column - s.size.loc.start.column);
|
---|
125 | }
|
---|
126 | export function shiftLoc(node, delta) {
|
---|
127 | // $FlowIgnore
|
---|
128 | node.loc.start.column += delta; // $FlowIgnore
|
---|
129 |
|
---|
130 | node.loc.end.column += delta;
|
---|
131 | }
|
---|
132 | export function shiftSection(ast, node, delta) {
|
---|
133 | if (node.type !== "SectionMetadata") {
|
---|
134 | throw new Error("Can not shift node " + JSON.stringify(node.type));
|
---|
135 | }
|
---|
136 |
|
---|
137 | node.startOffset += delta;
|
---|
138 |
|
---|
139 | if (_typeof(node.size.loc) === "object") {
|
---|
140 | shiftLoc(node.size, delta);
|
---|
141 | } // Custom sections doesn't have vectorOfSize
|
---|
142 |
|
---|
143 |
|
---|
144 | if (_typeof(node.vectorOfSize) === "object" && _typeof(node.vectorOfSize.loc) === "object") {
|
---|
145 | shiftLoc(node.vectorOfSize, delta);
|
---|
146 | }
|
---|
147 |
|
---|
148 | var sectionName = node.section; // shift node locations within that section
|
---|
149 |
|
---|
150 | traverse(ast, {
|
---|
151 | Node: function Node(_ref3) {
|
---|
152 | var node = _ref3.node;
|
---|
153 | var section = getSectionForNode(node);
|
---|
154 |
|
---|
155 | if (section === sectionName && _typeof(node.loc) === "object") {
|
---|
156 | shiftLoc(node, delta);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | });
|
---|
160 | }
|
---|
161 | export function signatureForOpcode(object, name) {
|
---|
162 | var opcodeName = name;
|
---|
163 |
|
---|
164 | if (object !== undefined && object !== "") {
|
---|
165 | opcodeName = object + "." + name;
|
---|
166 | }
|
---|
167 |
|
---|
168 | var sign = signatures[opcodeName];
|
---|
169 |
|
---|
170 | if (sign == undefined) {
|
---|
171 | // TODO: Uncomment this when br_table and others has been done
|
---|
172 | //throw new Error("Invalid opcode: "+opcodeName);
|
---|
173 | return [object, object];
|
---|
174 | }
|
---|
175 |
|
---|
176 | return sign[0];
|
---|
177 | }
|
---|
178 | export function getUniqueNameGenerator() {
|
---|
179 | var inc = {};
|
---|
180 | return function () {
|
---|
181 | var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "temp";
|
---|
182 |
|
---|
183 | if (!(prefix in inc)) {
|
---|
184 | inc[prefix] = 0;
|
---|
185 | } else {
|
---|
186 | inc[prefix] = inc[prefix] + 1;
|
---|
187 | }
|
---|
188 |
|
---|
189 | return prefix + "_" + inc[prefix];
|
---|
190 | };
|
---|
191 | }
|
---|
192 | export function getStartByteOffset(n) {
|
---|
193 | // $FlowIgnore
|
---|
194 | if (typeof n.loc === "undefined" || typeof n.loc.start === "undefined") {
|
---|
195 | throw new Error( // $FlowIgnore
|
---|
196 | "Can not get byte offset without loc informations, node: " + String(n.id));
|
---|
197 | }
|
---|
198 |
|
---|
199 | return n.loc.start.column;
|
---|
200 | }
|
---|
201 | export function getEndByteOffset(n) {
|
---|
202 | // $FlowIgnore
|
---|
203 | if (typeof n.loc === "undefined" || typeof n.loc.end === "undefined") {
|
---|
204 | throw new Error("Can not get byte offset without loc informations, node: " + n.type);
|
---|
205 | }
|
---|
206 |
|
---|
207 | return n.loc.end.column;
|
---|
208 | }
|
---|
209 | export function getFunctionBeginingByteOffset(n) {
|
---|
210 | if (!(n.body.length > 0)) {
|
---|
211 | throw new Error('n.body.length > 0' + " error: " + (undefined || "unknown"));
|
---|
212 | }
|
---|
213 |
|
---|
214 | var _n$body = _slicedToArray(n.body, 1),
|
---|
215 | firstInstruction = _n$body[0];
|
---|
216 |
|
---|
217 | return getStartByteOffset(firstInstruction);
|
---|
218 | }
|
---|
219 | export function getEndBlockByteOffset(n) {
|
---|
220 | // $FlowIgnore
|
---|
221 | if (!(n.instr.length > 0 || n.body.length > 0)) {
|
---|
222 | throw new Error('n.instr.length > 0 || n.body.length > 0' + " error: " + (undefined || "unknown"));
|
---|
223 | }
|
---|
224 |
|
---|
225 | var lastInstruction;
|
---|
226 |
|
---|
227 | if (n.instr) {
|
---|
228 | // $FlowIgnore
|
---|
229 | lastInstruction = n.instr[n.instr.length - 1];
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (n.body) {
|
---|
233 | // $FlowIgnore
|
---|
234 | lastInstruction = n.body[n.body.length - 1];
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (!(_typeof(lastInstruction) === "object")) {
|
---|
238 | throw new Error('typeof lastInstruction === "object"' + " error: " + (undefined || "unknown"));
|
---|
239 | }
|
---|
240 |
|
---|
241 | // $FlowIgnore
|
---|
242 | return getStartByteOffset(lastInstruction);
|
---|
243 | }
|
---|
244 | export function getStartBlockByteOffset(n) {
|
---|
245 | // $FlowIgnore
|
---|
246 | if (!(n.instr.length > 0 || n.body.length > 0)) {
|
---|
247 | throw new Error('n.instr.length > 0 || n.body.length > 0' + " error: " + (undefined || "unknown"));
|
---|
248 | }
|
---|
249 |
|
---|
250 | var fistInstruction;
|
---|
251 |
|
---|
252 | if (n.instr) {
|
---|
253 | // $FlowIgnore
|
---|
254 | var _n$instr = _slicedToArray(n.instr, 1);
|
---|
255 |
|
---|
256 | fistInstruction = _n$instr[0];
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (n.body) {
|
---|
260 | // $FlowIgnore
|
---|
261 | var _n$body2 = _slicedToArray(n.body, 1);
|
---|
262 |
|
---|
263 | fistInstruction = _n$body2[0];
|
---|
264 | }
|
---|
265 |
|
---|
266 | if (!(_typeof(fistInstruction) === "object")) {
|
---|
267 | throw new Error('typeof fistInstruction === "object"' + " error: " + (undefined || "unknown"));
|
---|
268 | }
|
---|
269 |
|
---|
270 | // $FlowIgnore
|
---|
271 | return getStartByteOffset(fistInstruction);
|
---|
272 | } |
---|