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