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