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