WasmDis.js (103339B)
1 "use strict"; 2 var __extends = (this && this.__extends) || (function () { 3 var extendStatics = function (d, b) { 4 extendStatics = Object.setPrototypeOf || 5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || 6 function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; 7 return extendStatics(d, b); 8 }; 9 return function (d, b) { 10 if (typeof b !== "function" && b !== null) 11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); 12 extendStatics(d, b); 13 function __() { this.constructor = d; } 14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 15 }; 16 })(); 17 Object.defineProperty(exports, "__esModule", { value: true }); 18 exports.DevToolsNameGenerator = exports.DevToolsNameResolver = exports.NameSectionReader = exports.WasmDisassembler = exports.LabelMode = exports.NumericNameResolver = exports.DefaultNameResolver = void 0; 19 /* Copyright 2016 Mozilla Foundation 20 * 21 * Licensed under the Apache License, Version 2.0 (the "License"); 22 * you may not use this file except in compliance with the License. 23 * You may obtain a copy of the License at 24 * 25 * http://www.apache.org/licenses/LICENSE-2.0 26 * 27 * Unless required by applicable law or agreed to in writing, software 28 * distributed under the License is distributed on an "AS IS" BASIS, 29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 30 * See the License for the specific language governing permissions and 31 * limitations under the License. 32 */ 33 var WasmParser_js_1 = require("./WasmParser.js"); 34 var NAME_SECTION_NAME = "name"; 35 var INVALID_NAME_SYMBOLS_REGEX = /[^0-9A-Za-z!#$%&'*+.:<=>?@^_`|~\/\-]/; 36 var INVALID_NAME_SYMBOLS_REGEX_GLOBAL = new RegExp(INVALID_NAME_SYMBOLS_REGEX.source, "g"); 37 function formatFloat32(n) { 38 if (n === 0) 39 return 1 / n < 0 ? "-0.0" : "0.0"; 40 if (isFinite(n)) 41 return n.toString(); 42 if (!isNaN(n)) 43 return n < 0 ? "-inf" : "inf"; 44 var view = new DataView(new ArrayBuffer(8)); 45 view.setFloat32(0, n, true); 46 var data = view.getInt32(0, true); 47 var payload = data & 0x7fffff; 48 var canonicalBits = 4194304; // 0x800..0 49 if (data > 0 && payload === canonicalBits) 50 return "nan"; 51 // canonical NaN; 52 else if (payload === canonicalBits) 53 return "-nan"; 54 return (data < 0 ? "-" : "+") + "nan:0x" + payload.toString(16); 55 } 56 function formatFloat64(n) { 57 if (n === 0) 58 return 1 / n < 0 ? "-0.0" : "0.0"; 59 if (isFinite(n)) 60 return n.toString(); 61 if (!isNaN(n)) 62 return n < 0 ? "-inf" : "inf"; 63 var view = new DataView(new ArrayBuffer(8)); 64 view.setFloat64(0, n, true); 65 var data1 = view.getUint32(0, true); 66 var data2 = view.getInt32(4, true); 67 var payload = data1 + (data2 & 0xfffff) * 4294967296; 68 var canonicalBits = 524288 * 4294967296; // 0x800..0 69 if (data2 > 0 && payload === canonicalBits) 70 return "nan"; 71 // canonical NaN; 72 else if (payload === canonicalBits) 73 return "-nan"; 74 return (data2 < 0 ? "-" : "+") + "nan:0x" + payload.toString(16); 75 } 76 function formatI32Array(bytes, count) { 77 var dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); 78 var result = []; 79 for (var i = 0; i < count; i++) 80 result.push("0x".concat(formatHex(dv.getInt32(i << 2, true), 8))); 81 return result.join(" "); 82 } 83 function formatI8Array(bytes, count) { 84 var dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); 85 var result = []; 86 for (var i = 0; i < count; i++) 87 result.push("".concat(dv.getInt8(i))); 88 return result.join(" "); 89 } 90 function memoryAddressToString(address, code) { 91 var defaultAlignFlags; 92 switch (code) { 93 case 1036288 /* OperatorCode.v128_load */: 94 case 1036289 /* OperatorCode.i16x8_load8x8_s */: 95 case 1036290 /* OperatorCode.i16x8_load8x8_u */: 96 case 1036291 /* OperatorCode.i32x4_load16x4_s */: 97 case 1036292 /* OperatorCode.i32x4_load16x4_u */: 98 case 1036293 /* OperatorCode.i64x2_load32x2_s */: 99 case 1036294 /* OperatorCode.i64x2_load32x2_u */: 100 case 1036295 /* OperatorCode.v8x16_load_splat */: 101 case 1036296 /* OperatorCode.v16x8_load_splat */: 102 case 1036297 /* OperatorCode.v32x4_load_splat */: 103 case 1036298 /* OperatorCode.v64x2_load_splat */: 104 case 1036299 /* OperatorCode.v128_store */: 105 defaultAlignFlags = 4; 106 break; 107 case 41 /* OperatorCode.i64_load */: 108 case 55 /* OperatorCode.i64_store */: 109 case 43 /* OperatorCode.f64_load */: 110 case 57 /* OperatorCode.f64_store */: 111 case 65026 /* OperatorCode.memory_atomic_wait64 */: 112 case 65041 /* OperatorCode.i64_atomic_load */: 113 case 65048 /* OperatorCode.i64_atomic_store */: 114 case 65055 /* OperatorCode.i64_atomic_rmw_add */: 115 case 65062 /* OperatorCode.i64_atomic_rmw_sub */: 116 case 65069 /* OperatorCode.i64_atomic_rmw_and */: 117 case 65076 /* OperatorCode.i64_atomic_rmw_or */: 118 case 65083 /* OperatorCode.i64_atomic_rmw_xor */: 119 case 65090 /* OperatorCode.i64_atomic_rmw_xchg */: 120 case 65097 /* OperatorCode.i64_atomic_rmw_cmpxchg */: 121 case 1036381 /* OperatorCode.v128_load64_zero */: 122 case 1036375 /* OperatorCode.v128_load64_lane */: 123 case 1036379 /* OperatorCode.v128_store64_lane */: 124 defaultAlignFlags = 3; 125 break; 126 case 40 /* OperatorCode.i32_load */: 127 case 52 /* OperatorCode.i64_load32_s */: 128 case 53 /* OperatorCode.i64_load32_u */: 129 case 54 /* OperatorCode.i32_store */: 130 case 62 /* OperatorCode.i64_store32 */: 131 case 42 /* OperatorCode.f32_load */: 132 case 56 /* OperatorCode.f32_store */: 133 case 65024 /* OperatorCode.memory_atomic_notify */: 134 case 65025 /* OperatorCode.memory_atomic_wait32 */: 135 case 65040 /* OperatorCode.i32_atomic_load */: 136 case 65046 /* OperatorCode.i64_atomic_load32_u */: 137 case 65047 /* OperatorCode.i32_atomic_store */: 138 case 65053 /* OperatorCode.i64_atomic_store32 */: 139 case 65054 /* OperatorCode.i32_atomic_rmw_add */: 140 case 65060 /* OperatorCode.i64_atomic_rmw32_add_u */: 141 case 65061 /* OperatorCode.i32_atomic_rmw_sub */: 142 case 65067 /* OperatorCode.i64_atomic_rmw32_sub_u */: 143 case 65068 /* OperatorCode.i32_atomic_rmw_and */: 144 case 65074 /* OperatorCode.i64_atomic_rmw32_and_u */: 145 case 65075 /* OperatorCode.i32_atomic_rmw_or */: 146 case 65081 /* OperatorCode.i64_atomic_rmw32_or_u */: 147 case 65082 /* OperatorCode.i32_atomic_rmw_xor */: 148 case 65088 /* OperatorCode.i64_atomic_rmw32_xor_u */: 149 case 65089 /* OperatorCode.i32_atomic_rmw_xchg */: 150 case 65095 /* OperatorCode.i64_atomic_rmw32_xchg_u */: 151 case 65096 /* OperatorCode.i32_atomic_rmw_cmpxchg */: 152 case 65102 /* OperatorCode.i64_atomic_rmw32_cmpxchg_u */: 153 case 1036380 /* OperatorCode.v128_load32_zero */: 154 case 1036374 /* OperatorCode.v128_load32_lane */: 155 case 1036378 /* OperatorCode.v128_store32_lane */: 156 defaultAlignFlags = 2; 157 break; 158 case 46 /* OperatorCode.i32_load16_s */: 159 case 47 /* OperatorCode.i32_load16_u */: 160 case 50 /* OperatorCode.i64_load16_s */: 161 case 51 /* OperatorCode.i64_load16_u */: 162 case 59 /* OperatorCode.i32_store16 */: 163 case 61 /* OperatorCode.i64_store16 */: 164 case 65043 /* OperatorCode.i32_atomic_load16_u */: 165 case 65045 /* OperatorCode.i64_atomic_load16_u */: 166 case 65050 /* OperatorCode.i32_atomic_store16 */: 167 case 65052 /* OperatorCode.i64_atomic_store16 */: 168 case 65057 /* OperatorCode.i32_atomic_rmw16_add_u */: 169 case 65059 /* OperatorCode.i64_atomic_rmw16_add_u */: 170 case 65064 /* OperatorCode.i32_atomic_rmw16_sub_u */: 171 case 65066 /* OperatorCode.i64_atomic_rmw16_sub_u */: 172 case 65071 /* OperatorCode.i32_atomic_rmw16_and_u */: 173 case 65073 /* OperatorCode.i64_atomic_rmw16_and_u */: 174 case 65078 /* OperatorCode.i32_atomic_rmw16_or_u */: 175 case 65080 /* OperatorCode.i64_atomic_rmw16_or_u */: 176 case 65085 /* OperatorCode.i32_atomic_rmw16_xor_u */: 177 case 65087 /* OperatorCode.i64_atomic_rmw16_xor_u */: 178 case 65092 /* OperatorCode.i32_atomic_rmw16_xchg_u */: 179 case 65094 /* OperatorCode.i64_atomic_rmw16_xchg_u */: 180 case 65099 /* OperatorCode.i32_atomic_rmw16_cmpxchg_u */: 181 case 65101 /* OperatorCode.i64_atomic_rmw16_cmpxchg_u */: 182 case 1036373 /* OperatorCode.v128_load16_lane */: 183 case 1036377 /* OperatorCode.v128_store16_lane */: 184 defaultAlignFlags = 1; 185 break; 186 case 44 /* OperatorCode.i32_load8_s */: 187 case 45 /* OperatorCode.i32_load8_u */: 188 case 48 /* OperatorCode.i64_load8_s */: 189 case 49 /* OperatorCode.i64_load8_u */: 190 case 58 /* OperatorCode.i32_store8 */: 191 case 60 /* OperatorCode.i64_store8 */: 192 case 65042 /* OperatorCode.i32_atomic_load8_u */: 193 case 65044 /* OperatorCode.i64_atomic_load8_u */: 194 case 65049 /* OperatorCode.i32_atomic_store8 */: 195 case 65051 /* OperatorCode.i64_atomic_store8 */: 196 case 65056 /* OperatorCode.i32_atomic_rmw8_add_u */: 197 case 65058 /* OperatorCode.i64_atomic_rmw8_add_u */: 198 case 65063 /* OperatorCode.i32_atomic_rmw8_sub_u */: 199 case 65065 /* OperatorCode.i64_atomic_rmw8_sub_u */: 200 case 65070 /* OperatorCode.i32_atomic_rmw8_and_u */: 201 case 65072 /* OperatorCode.i64_atomic_rmw8_and_u */: 202 case 65077 /* OperatorCode.i32_atomic_rmw8_or_u */: 203 case 65079 /* OperatorCode.i64_atomic_rmw8_or_u */: 204 case 65084 /* OperatorCode.i32_atomic_rmw8_xor_u */: 205 case 65086 /* OperatorCode.i64_atomic_rmw8_xor_u */: 206 case 65091 /* OperatorCode.i32_atomic_rmw8_xchg_u */: 207 case 65093 /* OperatorCode.i64_atomic_rmw8_xchg_u */: 208 case 65098 /* OperatorCode.i32_atomic_rmw8_cmpxchg_u */: 209 case 65100 /* OperatorCode.i64_atomic_rmw8_cmpxchg_u */: 210 case 1036372 /* OperatorCode.v128_load8_lane */: 211 case 1036376 /* OperatorCode.v128_store8_lane */: 212 defaultAlignFlags = 0; 213 break; 214 } 215 if (address.flags == defaultAlignFlags) 216 // hide default flags 217 return !address.offset ? null : "offset=".concat(address.offset); 218 if (!address.offset) 219 // hide default offset 220 return "align=".concat(1 << address.flags); 221 return "offset=".concat(address.offset | 0, " align=").concat(1 << address.flags); 222 } 223 function limitsToString(limits) { 224 return (limits.initial + (limits.maximum !== undefined ? " " + limits.maximum : "")); 225 } 226 var paddingCache = ["0", "00", "000"]; 227 function formatHex(n, width) { 228 var s = (n >>> 0).toString(16).toUpperCase(); 229 if (width === undefined || s.length >= width) 230 return s; 231 var paddingIndex = width - s.length - 1; 232 while (paddingIndex >= paddingCache.length) 233 paddingCache.push(paddingCache[paddingCache.length - 1] + "0"); 234 return paddingCache[paddingIndex] + s; 235 } 236 var IndentIncrement = " "; 237 function isValidName(name) { 238 return !INVALID_NAME_SYMBOLS_REGEX.test(name); 239 } 240 var DefaultNameResolver = /** @class */ (function () { 241 function DefaultNameResolver() { 242 } 243 DefaultNameResolver.prototype.getTypeName = function (index, isRef) { 244 return "$type" + index; 245 }; 246 DefaultNameResolver.prototype.getTableName = function (index, isRef) { 247 return "$table" + index; 248 }; 249 DefaultNameResolver.prototype.getMemoryName = function (index, isRef) { 250 return "$memory" + index; 251 }; 252 DefaultNameResolver.prototype.getGlobalName = function (index, isRef) { 253 return "$global" + index; 254 }; 255 DefaultNameResolver.prototype.getElementName = function (index, isRef) { 256 return "$elem".concat(index); 257 }; 258 DefaultNameResolver.prototype.getTagName = function (index, isRef) { 259 return "$tag".concat(index); 260 }; 261 DefaultNameResolver.prototype.getFunctionName = function (index, isImport, isRef) { 262 return (isImport ? "$import" : "$func") + index; 263 }; 264 DefaultNameResolver.prototype.getVariableName = function (funcIndex, index, isRef) { 265 return "$var" + index; 266 }; 267 DefaultNameResolver.prototype.getFieldName = function (typeIndex, index, isRef) { 268 return "$field" + index; 269 }; 270 DefaultNameResolver.prototype.getLabel = function (index) { 271 return "$label" + index; 272 }; 273 return DefaultNameResolver; 274 }()); 275 exports.DefaultNameResolver = DefaultNameResolver; 276 var EMPTY_STRING_ARRAY = []; 277 var DevToolsExportMetadata = /** @class */ (function () { 278 function DevToolsExportMetadata(functionExportNames, globalExportNames, memoryExportNames, tableExportNames, eventExportNames) { 279 this._functionExportNames = functionExportNames; 280 this._globalExportNames = globalExportNames; 281 this._memoryExportNames = memoryExportNames; 282 this._tableExportNames = tableExportNames; 283 this._eventExportNames = eventExportNames; 284 } 285 DevToolsExportMetadata.prototype.getFunctionExportNames = function (index) { 286 var _a; 287 return (_a = this._functionExportNames[index]) !== null && _a !== void 0 ? _a : EMPTY_STRING_ARRAY; 288 }; 289 DevToolsExportMetadata.prototype.getGlobalExportNames = function (index) { 290 var _a; 291 return (_a = this._globalExportNames[index]) !== null && _a !== void 0 ? _a : EMPTY_STRING_ARRAY; 292 }; 293 DevToolsExportMetadata.prototype.getMemoryExportNames = function (index) { 294 var _a; 295 return (_a = this._memoryExportNames[index]) !== null && _a !== void 0 ? _a : EMPTY_STRING_ARRAY; 296 }; 297 DevToolsExportMetadata.prototype.getTableExportNames = function (index) { 298 var _a; 299 return (_a = this._tableExportNames[index]) !== null && _a !== void 0 ? _a : EMPTY_STRING_ARRAY; 300 }; 301 DevToolsExportMetadata.prototype.getTagExportNames = function (index) { 302 var _a; 303 return (_a = this._eventExportNames[index]) !== null && _a !== void 0 ? _a : EMPTY_STRING_ARRAY; 304 }; 305 return DevToolsExportMetadata; 306 }()); 307 var NumericNameResolver = /** @class */ (function () { 308 function NumericNameResolver() { 309 } 310 NumericNameResolver.prototype.getTypeName = function (index, isRef) { 311 return isRef ? "" + index : "(;".concat(index, ";)"); 312 }; 313 NumericNameResolver.prototype.getTableName = function (index, isRef) { 314 return isRef ? "" + index : "(;".concat(index, ";)"); 315 }; 316 NumericNameResolver.prototype.getMemoryName = function (index, isRef) { 317 return isRef ? "" + index : "(;".concat(index, ";)"); 318 }; 319 NumericNameResolver.prototype.getGlobalName = function (index, isRef) { 320 return isRef ? "" + index : "(;".concat(index, ";)"); 321 }; 322 NumericNameResolver.prototype.getElementName = function (index, isRef) { 323 return isRef ? "" + index : "(;".concat(index, ";)"); 324 }; 325 NumericNameResolver.prototype.getTagName = function (index, isRef) { 326 return isRef ? "" + index : "(;".concat(index, ";)"); 327 }; 328 NumericNameResolver.prototype.getFunctionName = function (index, isImport, isRef) { 329 return isRef ? "" + index : "(;".concat(index, ";)"); 330 }; 331 NumericNameResolver.prototype.getVariableName = function (funcIndex, index, isRef) { 332 return isRef ? "" + index : "(;".concat(index, ";)"); 333 }; 334 NumericNameResolver.prototype.getFieldName = function (typeIndex, index, isRef) { 335 return isRef ? "" : index + "(;".concat(index, ";)"); 336 }; 337 NumericNameResolver.prototype.getLabel = function (index) { 338 return null; 339 }; 340 return NumericNameResolver; 341 }()); 342 exports.NumericNameResolver = NumericNameResolver; 343 var LabelMode; 344 (function (LabelMode) { 345 LabelMode[LabelMode["Depth"] = 0] = "Depth"; 346 LabelMode[LabelMode["WhenUsed"] = 1] = "WhenUsed"; 347 LabelMode[LabelMode["Always"] = 2] = "Always"; 348 })(LabelMode = exports.LabelMode || (exports.LabelMode = {})); 349 var WasmDisassembler = /** @class */ (function () { 350 function WasmDisassembler() { 351 this._skipTypes = true; 352 this._exportMetadata = null; 353 this._lines = []; 354 this._offsets = []; 355 this._buffer = ""; 356 this._indent = null; 357 this._indentLevel = 0; 358 this._addOffsets = false; 359 this._done = false; 360 this._currentPosition = 0; 361 this._nameResolver = new DefaultNameResolver(); 362 this._labelMode = LabelMode.WhenUsed; 363 this._functionBodyOffsets = []; 364 this._currentFunctionBodyOffset = 0; 365 this._currentSectionId = -1 /* SectionCode.Unknown */; 366 this._logFirstInstruction = false; 367 this._reset(); 368 } 369 WasmDisassembler.prototype._reset = function () { 370 this._types = []; 371 this._funcIndex = 0; 372 this._funcTypes = []; 373 this._importCount = 0; 374 this._globalCount = 0; 375 this._memoryCount = 0; 376 this._eventCount = 0; 377 this._tableCount = 0; 378 this._elementCount = 0; 379 this._expression = []; 380 this._backrefLabels = null; 381 this._labelIndex = 0; 382 }; 383 Object.defineProperty(WasmDisassembler.prototype, "addOffsets", { 384 get: function () { 385 return this._addOffsets; 386 }, 387 set: function (value) { 388 if (this._currentPosition) 389 throw new Error("Cannot switch addOffsets during processing."); 390 this._addOffsets = value; 391 }, 392 enumerable: false, 393 configurable: true 394 }); 395 Object.defineProperty(WasmDisassembler.prototype, "skipTypes", { 396 get: function () { 397 return this._skipTypes; 398 }, 399 set: function (skipTypes) { 400 if (this._currentPosition) 401 throw new Error("Cannot switch skipTypes during processing."); 402 this._skipTypes = skipTypes; 403 }, 404 enumerable: false, 405 configurable: true 406 }); 407 Object.defineProperty(WasmDisassembler.prototype, "labelMode", { 408 get: function () { 409 return this._labelMode; 410 }, 411 set: function (value) { 412 if (this._currentPosition) 413 throw new Error("Cannot switch labelMode during processing."); 414 this._labelMode = value; 415 }, 416 enumerable: false, 417 configurable: true 418 }); 419 Object.defineProperty(WasmDisassembler.prototype, "exportMetadata", { 420 get: function () { 421 return this._exportMetadata; 422 }, 423 set: function (exportMetadata) { 424 if (this._currentPosition) 425 throw new Error("Cannot switch exportMetadata during processing."); 426 this._exportMetadata = exportMetadata; 427 }, 428 enumerable: false, 429 configurable: true 430 }); 431 Object.defineProperty(WasmDisassembler.prototype, "nameResolver", { 432 get: function () { 433 return this._nameResolver; 434 }, 435 set: function (resolver) { 436 if (this._currentPosition) 437 throw new Error("Cannot switch nameResolver during processing."); 438 this._nameResolver = resolver; 439 }, 440 enumerable: false, 441 configurable: true 442 }); 443 WasmDisassembler.prototype.appendBuffer = function (s) { 444 this._buffer += s; 445 }; 446 WasmDisassembler.prototype.newLine = function () { 447 if (this.addOffsets) 448 this._offsets.push(this._currentPosition); 449 this._lines.push(this._buffer); 450 this._buffer = ""; 451 }; 452 WasmDisassembler.prototype.logStartOfFunctionBodyOffset = function () { 453 if (this.addOffsets) { 454 this._currentFunctionBodyOffset = this._currentPosition; 455 } 456 }; 457 WasmDisassembler.prototype.logEndOfFunctionBodyOffset = function () { 458 if (this.addOffsets) { 459 this._functionBodyOffsets.push({ 460 start: this._currentFunctionBodyOffset, 461 end: this._currentPosition, 462 }); 463 } 464 }; 465 WasmDisassembler.prototype.typeIndexToString = function (typeIndex) { 466 if (typeIndex >= 0) 467 return this._nameResolver.getTypeName(typeIndex, true); 468 switch (typeIndex) { 469 case -16 /* TypeKind.funcref */: 470 return "func"; 471 case -17 /* TypeKind.externref */: 472 return "extern"; 473 case -18 /* TypeKind.anyref */: 474 return "any"; 475 case -19 /* TypeKind.eqref */: 476 return "eq"; 477 case -20 /* TypeKind.i31ref */: 478 return "i31"; 479 case -23 /* TypeKind.exnref */: 480 return "exnref"; 481 case -21 /* TypeKind.structref */: 482 return "struct"; 483 case -22 /* TypeKind.arrayref */: 484 return "array"; 485 case -13 /* TypeKind.nullfuncref */: 486 return "nofunc"; 487 case -14 /* TypeKind.nullexternref */: 488 return "noextern"; 489 case -15 /* TypeKind.nullref */: 490 return "none"; 491 case -12 /* TypeKind.nullexnref */: 492 return "noexnref"; 493 } 494 }; 495 WasmDisassembler.prototype.refTypeToString = function (typeIndex, nullable) { 496 return this.typeToString(new WasmParser_js_1.RefType(nullable ? -29 /* TypeKind.ref_null */ : -28 /* TypeKind.ref */, typeIndex)); 497 }; 498 WasmDisassembler.prototype.typeToString = function (type) { 499 switch (type.kind) { 500 case -1 /* TypeKind.i32 */: 501 return "i32"; 502 case -2 /* TypeKind.i64 */: 503 return "i64"; 504 case -3 /* TypeKind.f32 */: 505 return "f32"; 506 case -4 /* TypeKind.f64 */: 507 return "f64"; 508 case -5 /* TypeKind.v128 */: 509 return "v128"; 510 case -8 /* TypeKind.i8 */: 511 return "i8"; 512 case -9 /* TypeKind.i16 */: 513 return "i16"; 514 case -16 /* TypeKind.funcref */: 515 return "funcref"; 516 case -17 /* TypeKind.externref */: 517 return "externref"; 518 case -23 /* TypeKind.exnref */: 519 return "exnref"; 520 case -18 /* TypeKind.anyref */: 521 return "anyref"; 522 case -19 /* TypeKind.eqref */: 523 return "eqref"; 524 case -20 /* TypeKind.i31ref */: 525 return "i31ref"; 526 case -21 /* TypeKind.structref */: 527 return "structref"; 528 case -22 /* TypeKind.arrayref */: 529 return "arrayref"; 530 case -13 /* TypeKind.nullfuncref */: 531 return "nullfuncref"; 532 case -14 /* TypeKind.nullexternref */: 533 return "nullexternref"; 534 case -12 /* TypeKind.nullexnref */: 535 return "nullexnref"; 536 case -15 /* TypeKind.nullref */: 537 return "nullref"; 538 case -28 /* TypeKind.ref */: 539 return "(ref ".concat(this.typeIndexToString(type.ref_index), ")"); 540 case -29 /* TypeKind.ref_null */: 541 return "(ref null ".concat(this.typeIndexToString(type.ref_index), ")"); 542 default: 543 throw new Error("Unexpected type ".concat(JSON.stringify(type))); 544 } 545 }; 546 WasmDisassembler.prototype.maybeMut = function (type, mutability) { 547 return mutability ? "(mut ".concat(type, ")") : type; 548 }; 549 WasmDisassembler.prototype.globalTypeToString = function (type) { 550 var typeStr = this.typeToString(type.contentType); 551 return this.maybeMut(typeStr, !!type.mutability); 552 }; 553 WasmDisassembler.prototype.printFuncType = function (typeIndex) { 554 var type = this._types[typeIndex]; 555 if (type.params.length > 0) { 556 this.appendBuffer(" (param"); 557 for (var i = 0; i < type.params.length; i++) { 558 this.appendBuffer(" "); 559 this.appendBuffer(this.typeToString(type.params[i])); 560 } 561 this.appendBuffer(")"); 562 } 563 if (type.returns.length > 0) { 564 this.appendBuffer(" (result"); 565 for (var i = 0; i < type.returns.length; i++) { 566 this.appendBuffer(" "); 567 this.appendBuffer(this.typeToString(type.returns[i])); 568 } 569 this.appendBuffer(")"); 570 } 571 }; 572 WasmDisassembler.prototype.printStructType = function (typeIndex) { 573 var type = this._types[typeIndex]; 574 if (type.fields.length === 0) 575 return; 576 for (var i = 0; i < type.fields.length; i++) { 577 var fieldType = this.maybeMut(this.typeToString(type.fields[i]), type.mutabilities[i]); 578 var fieldName = this._nameResolver.getFieldName(typeIndex, i, false); 579 this.appendBuffer(" (field ".concat(fieldName, " ").concat(fieldType, ")")); 580 } 581 }; 582 WasmDisassembler.prototype.printArrayType = function (typeIndex) { 583 var type = this._types[typeIndex]; 584 this.appendBuffer(" (field "); 585 this.appendBuffer(this.maybeMut(this.typeToString(type.elementType), type.mutability)); 586 }; 587 WasmDisassembler.prototype.printBlockType = function (type) { 588 if (type.kind === -64 /* TypeKind.empty_block_type */) { 589 return; 590 } 591 if (type.kind === 0 /* TypeKind.unspecified */) { 592 if (this._types[type.index].form == -32 /* TypeKind.func */) { 593 return this.printFuncType(type.index); 594 } 595 else { 596 // Encoding error. 597 this.appendBuffer(" (type ".concat(type.index, ")")); 598 return; 599 } 600 } 601 this.appendBuffer(" (result "); 602 this.appendBuffer(this.typeToString(type)); 603 this.appendBuffer(")"); 604 }; 605 WasmDisassembler.prototype.printString = function (b) { 606 this.appendBuffer('"'); 607 for (var i = 0; i < b.length; i++) { 608 var byte = b[i]; 609 if (byte < 0x20 || 610 byte >= 0x7f || 611 byte == /* " */ 0x22 || 612 byte == /* \ */ 0x5c) { 613 this.appendBuffer("\\" + (byte >> 4).toString(16) + (byte & 15).toString(16)); 614 } 615 else { 616 this.appendBuffer(String.fromCharCode(byte)); 617 } 618 } 619 this.appendBuffer('"'); 620 }; 621 WasmDisassembler.prototype.printExpression = function (expression) { 622 for (var _i = 0, expression_1 = expression; _i < expression_1.length; _i++) { 623 var operator = expression_1[_i]; 624 this.appendBuffer("("); 625 this.printOperator(operator); 626 this.appendBuffer(")"); 627 } 628 }; 629 // extraDepthOffset is used by "delegate" instructions. 630 WasmDisassembler.prototype.useLabel = function (depth, extraDepthOffset) { 631 if (extraDepthOffset === void 0) { extraDepthOffset = 0; } 632 if (!this._backrefLabels) { 633 return "" + depth; 634 } 635 var i = this._backrefLabels.length - depth - 1 - extraDepthOffset; 636 if (i < 0) { 637 return "" + depth; 638 } 639 var backrefLabel = this._backrefLabels[i]; 640 if (!backrefLabel.useLabel) { 641 backrefLabel.useLabel = true; 642 backrefLabel.label = this._nameResolver.getLabel(this._labelIndex); 643 var line = this._lines[backrefLabel.line]; 644 this._lines[backrefLabel.line] = 645 line.substring(0, backrefLabel.position) + 646 " " + 647 backrefLabel.label + 648 line.substring(backrefLabel.position); 649 this._labelIndex++; 650 } 651 return backrefLabel.label || "" + depth; 652 }; 653 WasmDisassembler.prototype.printOperator = function (operator) { 654 var code = operator.code; 655 this.appendBuffer(WasmParser_js_1.OperatorCodeNames[code]); 656 switch (code) { 657 case 2 /* OperatorCode.block */: 658 case 3 /* OperatorCode.loop */: 659 case 4 /* OperatorCode.if */: 660 case 6 /* OperatorCode.try */: 661 case 31 /* OperatorCode.try_table */: 662 if (this._labelMode !== LabelMode.Depth) { 663 var backrefLabel_1 = { 664 line: this._lines.length, 665 position: this._buffer.length, 666 useLabel: false, 667 label: null, 668 }; 669 if (this._labelMode === LabelMode.Always) { 670 backrefLabel_1.useLabel = true; 671 backrefLabel_1.label = this._nameResolver.getLabel(this._labelIndex++); 672 if (backrefLabel_1.label) { 673 this.appendBuffer(" "); 674 this.appendBuffer(backrefLabel_1.label); 675 } 676 } 677 this._backrefLabels.push(backrefLabel_1); 678 } 679 this.printBlockType(operator.blockType); 680 if (operator.tryTable) { 681 for (var i = 0; i < operator.tryTable.length; i++) { 682 this.appendBuffer(" ("); 683 switch (operator.tryTable[i].kind) { 684 case WasmParser_js_1.CatchHandlerKind.Catch: 685 this.appendBuffer("catch "); 686 break; 687 case WasmParser_js_1.CatchHandlerKind.CatchRef: 688 this.appendBuffer("catch_ref "); 689 break; 690 case WasmParser_js_1.CatchHandlerKind.CatchAll: 691 this.appendBuffer("catch_all "); 692 break; 693 case WasmParser_js_1.CatchHandlerKind.CatchAllRef: 694 this.appendBuffer("catch_all_ref "); 695 break; 696 } 697 if (operator.tryTable[i].tagIndex != null) { 698 var tagName = this._nameResolver.getTagName(operator.tryTable[i].tagIndex, true); 699 this.appendBuffer("".concat(tagName, " ")); 700 } 701 this.appendBuffer(this.useLabel(operator.tryTable[i].depth + 1)); 702 this.appendBuffer(")"); 703 } 704 } 705 break; 706 case 11 /* OperatorCode.end */: 707 if (this._labelMode === LabelMode.Depth) { 708 break; 709 } 710 var backrefLabel = this._backrefLabels.pop(); 711 if (backrefLabel.label) { 712 this.appendBuffer(" "); 713 this.appendBuffer(backrefLabel.label); 714 } 715 break; 716 case 12 /* OperatorCode.br */: 717 case 13 /* OperatorCode.br_if */: 718 case 213 /* OperatorCode.br_on_null */: 719 case 214 /* OperatorCode.br_on_non_null */: 720 this.appendBuffer(" "); 721 this.appendBuffer(this.useLabel(operator.brDepth)); 722 break; 723 case 64280 /* OperatorCode.br_on_cast */: 724 case 64281 /* OperatorCode.br_on_cast_fail */: 725 this.appendBuffer(" flags=" + operator.literal); 726 this.appendBuffer(" "); 727 this.appendBuffer(this.typeIndexToString(operator.srcType)); 728 this.appendBuffer(" "); 729 this.appendBuffer(this.typeIndexToString(operator.refType)); 730 this.appendBuffer(" "); 731 this.appendBuffer(this.useLabel(operator.brDepth)); 732 break; 733 case 14 /* OperatorCode.br_table */: 734 for (var i = 0; i < operator.brTable.length; i++) { 735 this.appendBuffer(" "); 736 this.appendBuffer(this.useLabel(operator.brTable[i])); 737 } 738 break; 739 case 9 /* OperatorCode.rethrow */: 740 this.appendBuffer(" "); 741 this.appendBuffer(this.useLabel(operator.relativeDepth)); 742 break; 743 case 24 /* OperatorCode.delegate */: 744 this.appendBuffer(" "); 745 this.appendBuffer(this.useLabel(operator.relativeDepth, 1)); 746 break; 747 case 7 /* OperatorCode.catch */: 748 case 8 /* OperatorCode.throw */: 749 var tagName = this._nameResolver.getTagName(operator.tagIndex, true); 750 this.appendBuffer(" ".concat(tagName)); 751 break; 752 case 208 /* OperatorCode.ref_null */: 753 this.appendBuffer(" "); 754 this.appendBuffer(this.typeIndexToString(operator.refType)); 755 break; 756 case 16 /* OperatorCode.call */: 757 case 18 /* OperatorCode.return_call */: 758 case 210 /* OperatorCode.ref_func */: 759 var funcName = this._nameResolver.getFunctionName(operator.funcIndex, operator.funcIndex < this._importCount, true); 760 this.appendBuffer(" ".concat(funcName)); 761 break; 762 case 17 /* OperatorCode.call_indirect */: 763 case 19 /* OperatorCode.return_call_indirect */: 764 this.printFuncType(operator.typeIndex); 765 break; 766 case 28 /* OperatorCode.select_with_type */: { 767 var selectType = this.typeToString(operator.selectType); 768 this.appendBuffer(" ".concat(selectType)); 769 break; 770 } 771 case 32 /* OperatorCode.local_get */: 772 case 33 /* OperatorCode.local_set */: 773 case 34 /* OperatorCode.local_tee */: 774 var paramName = this._nameResolver.getVariableName(this._funcIndex, operator.localIndex, true); 775 this.appendBuffer(" ".concat(paramName)); 776 break; 777 case 35 /* OperatorCode.global_get */: 778 case 36 /* OperatorCode.global_set */: 779 var globalName = this._nameResolver.getGlobalName(operator.globalIndex, true); 780 this.appendBuffer(" ".concat(globalName)); 781 break; 782 case 40 /* OperatorCode.i32_load */: 783 case 41 /* OperatorCode.i64_load */: 784 case 42 /* OperatorCode.f32_load */: 785 case 43 /* OperatorCode.f64_load */: 786 case 44 /* OperatorCode.i32_load8_s */: 787 case 45 /* OperatorCode.i32_load8_u */: 788 case 46 /* OperatorCode.i32_load16_s */: 789 case 47 /* OperatorCode.i32_load16_u */: 790 case 48 /* OperatorCode.i64_load8_s */: 791 case 49 /* OperatorCode.i64_load8_u */: 792 case 50 /* OperatorCode.i64_load16_s */: 793 case 51 /* OperatorCode.i64_load16_u */: 794 case 52 /* OperatorCode.i64_load32_s */: 795 case 53 /* OperatorCode.i64_load32_u */: 796 case 54 /* OperatorCode.i32_store */: 797 case 55 /* OperatorCode.i64_store */: 798 case 56 /* OperatorCode.f32_store */: 799 case 57 /* OperatorCode.f64_store */: 800 case 58 /* OperatorCode.i32_store8 */: 801 case 59 /* OperatorCode.i32_store16 */: 802 case 60 /* OperatorCode.i64_store8 */: 803 case 61 /* OperatorCode.i64_store16 */: 804 case 62 /* OperatorCode.i64_store32 */: 805 case 65024 /* OperatorCode.memory_atomic_notify */: 806 case 65025 /* OperatorCode.memory_atomic_wait32 */: 807 case 65026 /* OperatorCode.memory_atomic_wait64 */: 808 case 65040 /* OperatorCode.i32_atomic_load */: 809 case 65041 /* OperatorCode.i64_atomic_load */: 810 case 65042 /* OperatorCode.i32_atomic_load8_u */: 811 case 65043 /* OperatorCode.i32_atomic_load16_u */: 812 case 65044 /* OperatorCode.i64_atomic_load8_u */: 813 case 65045 /* OperatorCode.i64_atomic_load16_u */: 814 case 65046 /* OperatorCode.i64_atomic_load32_u */: 815 case 65047 /* OperatorCode.i32_atomic_store */: 816 case 65048 /* OperatorCode.i64_atomic_store */: 817 case 65049 /* OperatorCode.i32_atomic_store8 */: 818 case 65050 /* OperatorCode.i32_atomic_store16 */: 819 case 65051 /* OperatorCode.i64_atomic_store8 */: 820 case 65052 /* OperatorCode.i64_atomic_store16 */: 821 case 65053 /* OperatorCode.i64_atomic_store32 */: 822 case 65054 /* OperatorCode.i32_atomic_rmw_add */: 823 case 65055 /* OperatorCode.i64_atomic_rmw_add */: 824 case 65056 /* OperatorCode.i32_atomic_rmw8_add_u */: 825 case 65057 /* OperatorCode.i32_atomic_rmw16_add_u */: 826 case 65058 /* OperatorCode.i64_atomic_rmw8_add_u */: 827 case 65059 /* OperatorCode.i64_atomic_rmw16_add_u */: 828 case 65060 /* OperatorCode.i64_atomic_rmw32_add_u */: 829 case 65061 /* OperatorCode.i32_atomic_rmw_sub */: 830 case 65062 /* OperatorCode.i64_atomic_rmw_sub */: 831 case 65063 /* OperatorCode.i32_atomic_rmw8_sub_u */: 832 case 65064 /* OperatorCode.i32_atomic_rmw16_sub_u */: 833 case 65065 /* OperatorCode.i64_atomic_rmw8_sub_u */: 834 case 65066 /* OperatorCode.i64_atomic_rmw16_sub_u */: 835 case 65067 /* OperatorCode.i64_atomic_rmw32_sub_u */: 836 case 65068 /* OperatorCode.i32_atomic_rmw_and */: 837 case 65069 /* OperatorCode.i64_atomic_rmw_and */: 838 case 65070 /* OperatorCode.i32_atomic_rmw8_and_u */: 839 case 65071 /* OperatorCode.i32_atomic_rmw16_and_u */: 840 case 65072 /* OperatorCode.i64_atomic_rmw8_and_u */: 841 case 65073 /* OperatorCode.i64_atomic_rmw16_and_u */: 842 case 65074 /* OperatorCode.i64_atomic_rmw32_and_u */: 843 case 65075 /* OperatorCode.i32_atomic_rmw_or */: 844 case 65076 /* OperatorCode.i64_atomic_rmw_or */: 845 case 65077 /* OperatorCode.i32_atomic_rmw8_or_u */: 846 case 65078 /* OperatorCode.i32_atomic_rmw16_or_u */: 847 case 65079 /* OperatorCode.i64_atomic_rmw8_or_u */: 848 case 65080 /* OperatorCode.i64_atomic_rmw16_or_u */: 849 case 65081 /* OperatorCode.i64_atomic_rmw32_or_u */: 850 case 65082 /* OperatorCode.i32_atomic_rmw_xor */: 851 case 65083 /* OperatorCode.i64_atomic_rmw_xor */: 852 case 65084 /* OperatorCode.i32_atomic_rmw8_xor_u */: 853 case 65085 /* OperatorCode.i32_atomic_rmw16_xor_u */: 854 case 65086 /* OperatorCode.i64_atomic_rmw8_xor_u */: 855 case 65087 /* OperatorCode.i64_atomic_rmw16_xor_u */: 856 case 65088 /* OperatorCode.i64_atomic_rmw32_xor_u */: 857 case 65089 /* OperatorCode.i32_atomic_rmw_xchg */: 858 case 65090 /* OperatorCode.i64_atomic_rmw_xchg */: 859 case 65091 /* OperatorCode.i32_atomic_rmw8_xchg_u */: 860 case 65092 /* OperatorCode.i32_atomic_rmw16_xchg_u */: 861 case 65093 /* OperatorCode.i64_atomic_rmw8_xchg_u */: 862 case 65094 /* OperatorCode.i64_atomic_rmw16_xchg_u */: 863 case 65095 /* OperatorCode.i64_atomic_rmw32_xchg_u */: 864 case 65096 /* OperatorCode.i32_atomic_rmw_cmpxchg */: 865 case 65097 /* OperatorCode.i64_atomic_rmw_cmpxchg */: 866 case 65098 /* OperatorCode.i32_atomic_rmw8_cmpxchg_u */: 867 case 65099 /* OperatorCode.i32_atomic_rmw16_cmpxchg_u */: 868 case 65100 /* OperatorCode.i64_atomic_rmw8_cmpxchg_u */: 869 case 65101 /* OperatorCode.i64_atomic_rmw16_cmpxchg_u */: 870 case 65102 /* OperatorCode.i64_atomic_rmw32_cmpxchg_u */: 871 case 1036288 /* OperatorCode.v128_load */: 872 case 1036289 /* OperatorCode.i16x8_load8x8_s */: 873 case 1036290 /* OperatorCode.i16x8_load8x8_u */: 874 case 1036291 /* OperatorCode.i32x4_load16x4_s */: 875 case 1036292 /* OperatorCode.i32x4_load16x4_u */: 876 case 1036293 /* OperatorCode.i64x2_load32x2_s */: 877 case 1036294 /* OperatorCode.i64x2_load32x2_u */: 878 case 1036295 /* OperatorCode.v8x16_load_splat */: 879 case 1036296 /* OperatorCode.v16x8_load_splat */: 880 case 1036297 /* OperatorCode.v32x4_load_splat */: 881 case 1036298 /* OperatorCode.v64x2_load_splat */: 882 case 1036299 /* OperatorCode.v128_store */: 883 case 1036380 /* OperatorCode.v128_load32_zero */: 884 case 1036381 /* OperatorCode.v128_load64_zero */: 885 var memoryAddress = memoryAddressToString(operator.memoryAddress, operator.code); 886 if (memoryAddress !== null) { 887 this.appendBuffer(" "); 888 this.appendBuffer(memoryAddress); 889 } 890 break; 891 case 63 /* OperatorCode.memory_size */: 892 case 64 /* OperatorCode.memory_grow */: 893 break; 894 case 65 /* OperatorCode.i32_const */: 895 this.appendBuffer(" ".concat(operator.literal.toString())); 896 break; 897 case 66 /* OperatorCode.i64_const */: 898 this.appendBuffer(" ".concat(operator.literal.toString())); 899 break; 900 case 67 /* OperatorCode.f32_const */: 901 this.appendBuffer(" ".concat(formatFloat32(operator.literal))); 902 break; 903 case 68 /* OperatorCode.f64_const */: 904 this.appendBuffer(" ".concat(formatFloat64(operator.literal))); 905 break; 906 case 1036300 /* OperatorCode.v128_const */: 907 this.appendBuffer(" i32x4 ".concat(formatI32Array(operator.literal, 4))); 908 break; 909 case 1036301 /* OperatorCode.i8x16_shuffle */: 910 this.appendBuffer(" ".concat(formatI8Array(operator.lines, 16))); 911 break; 912 case 1036309 /* OperatorCode.i8x16_extract_lane_s */: 913 case 1036310 /* OperatorCode.i8x16_extract_lane_u */: 914 case 1036311 /* OperatorCode.i8x16_replace_lane */: 915 case 1036312 /* OperatorCode.i16x8_extract_lane_s */: 916 case 1036313 /* OperatorCode.i16x8_extract_lane_u */: 917 case 1036314 /* OperatorCode.i16x8_replace_lane */: 918 case 1036315 /* OperatorCode.i32x4_extract_lane */: 919 case 1036316 /* OperatorCode.i32x4_replace_lane */: 920 case 1036319 /* OperatorCode.f32x4_extract_lane */: 921 case 1036320 /* OperatorCode.f32x4_replace_lane */: 922 case 1036317 /* OperatorCode.i64x2_extract_lane */: 923 case 1036318 /* OperatorCode.i64x2_replace_lane */: 924 case 1036321 /* OperatorCode.f64x2_extract_lane */: 925 case 1036322 /* OperatorCode.f64x2_replace_lane */: 926 this.appendBuffer(" ".concat(operator.lineIndex)); 927 break; 928 case 1036372 /* OperatorCode.v128_load8_lane */: 929 case 1036373 /* OperatorCode.v128_load16_lane */: 930 case 1036374 /* OperatorCode.v128_load32_lane */: 931 case 1036375 /* OperatorCode.v128_load64_lane */: 932 case 1036376 /* OperatorCode.v128_store8_lane */: 933 case 1036377 /* OperatorCode.v128_store16_lane */: 934 case 1036378 /* OperatorCode.v128_store32_lane */: 935 case 1036379 /* OperatorCode.v128_store64_lane */: 936 var memoryAddress = memoryAddressToString(operator.memoryAddress, operator.code); 937 if (memoryAddress !== null) { 938 this.appendBuffer(" "); 939 this.appendBuffer(memoryAddress); 940 } 941 this.appendBuffer(" ".concat(operator.lineIndex)); 942 break; 943 case 64520 /* OperatorCode.memory_init */: 944 case 64521 /* OperatorCode.data_drop */: 945 this.appendBuffer(" ".concat(operator.segmentIndex)); 946 break; 947 case 64525 /* OperatorCode.elem_drop */: 948 var elementName = this._nameResolver.getElementName(operator.segmentIndex, true); 949 this.appendBuffer(" ".concat(elementName)); 950 break; 951 case 38 /* OperatorCode.table_set */: 952 case 37 /* OperatorCode.table_get */: 953 case 64529 /* OperatorCode.table_fill */: { 954 var tableName = this._nameResolver.getTableName(operator.tableIndex, true); 955 this.appendBuffer(" ".concat(tableName)); 956 break; 957 } 958 case 64526 /* OperatorCode.table_copy */: { 959 // Table index might be omitted and defaults to 0. 960 if (operator.tableIndex !== 0 || operator.destinationIndex !== 0) { 961 var tableName = this._nameResolver.getTableName(operator.tableIndex, true); 962 var destinationName = this._nameResolver.getTableName(operator.destinationIndex, true); 963 this.appendBuffer(" ".concat(destinationName, " ").concat(tableName)); 964 } 965 break; 966 } 967 case 64524 /* OperatorCode.table_init */: { 968 // Table index might be omitted and defaults to 0. 969 if (operator.tableIndex !== 0) { 970 var tableName = this._nameResolver.getTableName(operator.tableIndex, true); 971 this.appendBuffer(" ".concat(tableName)); 972 } 973 var elementName_1 = this._nameResolver.getElementName(operator.segmentIndex, true); 974 this.appendBuffer(" ".concat(elementName_1)); 975 break; 976 } 977 case 64258 /* OperatorCode.struct_get */: 978 case 64259 /* OperatorCode.struct_get_s */: 979 case 64260 /* OperatorCode.struct_get_u */: 980 case 64261 /* OperatorCode.struct_set */: { 981 var refType = this.typeIndexToString(operator.refType); 982 var fieldName = this._nameResolver.getFieldName(operator.refType, operator.fieldIndex, true); 983 this.appendBuffer(" ".concat(refType, " ").concat(fieldName)); 984 break; 985 } 986 case 64278 /* OperatorCode.ref_cast */: 987 case 64276 /* OperatorCode.ref_test */: { 988 var refType = this.refTypeToString(operator.refType, false); 989 this.appendBuffer(" ".concat(refType)); 990 break; 991 } 992 case 64279 /* OperatorCode.ref_cast_null */: 993 case 64277 /* OperatorCode.ref_test_null */: { 994 var refType = this.refTypeToString(operator.refType, true); 995 this.appendBuffer(" ".concat(refType)); 996 break; 997 } 998 case 64257 /* OperatorCode.struct_new_default */: 999 case 64256 /* OperatorCode.struct_new */: 1000 case 64263 /* OperatorCode.array_new_default */: 1001 case 64262 /* OperatorCode.array_new */: 1002 case 64267 /* OperatorCode.array_get */: 1003 case 64268 /* OperatorCode.array_get_s */: 1004 case 64269 /* OperatorCode.array_get_u */: 1005 case 64270 /* OperatorCode.array_set */: { 1006 var refType = this.typeIndexToString(operator.refType); 1007 this.appendBuffer(" ".concat(refType)); 1008 break; 1009 } 1010 case 64272 /* OperatorCode.array_fill */: { 1011 var dstType = this.typeIndexToString(operator.refType); 1012 this.appendBuffer(" ".concat(dstType)); 1013 break; 1014 } 1015 case 64273 /* OperatorCode.array_copy */: { 1016 var dstType = this.typeIndexToString(operator.refType); 1017 var srcType = this.typeIndexToString(operator.srcType); 1018 this.appendBuffer(" ".concat(dstType, " ").concat(srcType)); 1019 break; 1020 } 1021 case 64264 /* OperatorCode.array_new_fixed */: { 1022 var refType = this.typeIndexToString(operator.refType); 1023 var length_1 = operator.len; 1024 this.appendBuffer(" ".concat(refType, " ").concat(length_1)); 1025 break; 1026 } 1027 } 1028 }; 1029 WasmDisassembler.prototype.printImportSource = function (info) { 1030 this.printString(info.module); 1031 this.appendBuffer(" "); 1032 this.printString(info.field); 1033 }; 1034 WasmDisassembler.prototype.increaseIndent = function () { 1035 this._indent += IndentIncrement; 1036 this._indentLevel++; 1037 }; 1038 WasmDisassembler.prototype.decreaseIndent = function () { 1039 this._indent = this._indent.slice(0, -IndentIncrement.length); 1040 this._indentLevel--; 1041 }; 1042 WasmDisassembler.prototype.disassemble = function (reader) { 1043 var _this = this; 1044 var done = this.disassembleChunk(reader); 1045 if (!done) 1046 return null; 1047 var lines = this._lines; 1048 if (this._addOffsets) { 1049 lines = lines.map(function (line, index) { 1050 var position = formatHex(_this._offsets[index], 4); 1051 return line + " ;; @" + position; 1052 }); 1053 } 1054 lines.push(""); // we need '\n' after last line 1055 var result = lines.join("\n"); 1056 this._lines.length = 0; 1057 this._offsets.length = 0; 1058 this._functionBodyOffsets.length = 0; 1059 return result; 1060 }; 1061 WasmDisassembler.prototype.getResult = function () { 1062 var linesReady = this._lines.length; 1063 if (this._backrefLabels && this._labelMode === LabelMode.WhenUsed) { 1064 this._backrefLabels.some(function (backrefLabel) { 1065 if (backrefLabel.useLabel) 1066 return false; 1067 linesReady = backrefLabel.line; 1068 return true; 1069 }); 1070 } 1071 if (linesReady === 0) { 1072 return { 1073 lines: [], 1074 offsets: this._addOffsets ? [] : undefined, 1075 done: this._done, 1076 functionBodyOffsets: this._addOffsets ? [] : undefined, 1077 }; 1078 } 1079 if (linesReady === this._lines.length) { 1080 var result_1 = { 1081 lines: this._lines, 1082 offsets: this._addOffsets ? this._offsets : undefined, 1083 done: this._done, 1084 functionBodyOffsets: this._addOffsets 1085 ? this._functionBodyOffsets 1086 : undefined, 1087 }; 1088 this._lines = []; 1089 if (this._addOffsets) { 1090 this._offsets = []; 1091 this._functionBodyOffsets = []; 1092 } 1093 return result_1; 1094 } 1095 var result = { 1096 lines: this._lines.splice(0, linesReady), 1097 offsets: this._addOffsets 1098 ? this._offsets.splice(0, linesReady) 1099 : undefined, 1100 done: false, 1101 functionBodyOffsets: this._addOffsets 1102 ? this._functionBodyOffsets 1103 : undefined, 1104 }; 1105 if (this._backrefLabels) { 1106 this._backrefLabels.forEach(function (backrefLabel) { 1107 backrefLabel.line -= linesReady; 1108 }); 1109 } 1110 return result; 1111 }; 1112 WasmDisassembler.prototype.disassembleChunk = function (reader, offsetInModule) { 1113 var _this = this; 1114 if (offsetInModule === void 0) { offsetInModule = 0; } 1115 if (this._done) 1116 throw new Error("Invalid state: disassembly process was already finished."); 1117 while (true) { 1118 this._currentPosition = reader.position + offsetInModule; 1119 if (!reader.read()) 1120 return false; 1121 switch (reader.state) { 1122 case 2 /* BinaryReaderState.END_WASM */: 1123 this.appendBuffer(")"); 1124 this.newLine(); 1125 this._reset(); 1126 if (!reader.hasMoreBytes()) { 1127 this._done = true; 1128 return true; 1129 } 1130 break; 1131 case -1 /* BinaryReaderState.ERROR */: 1132 throw reader.error; 1133 case 1 /* BinaryReaderState.BEGIN_WASM */: 1134 this.appendBuffer("(module"); 1135 this.newLine(); 1136 break; 1137 case 4 /* BinaryReaderState.END_SECTION */: 1138 this._currentSectionId = -1 /* SectionCode.Unknown */; 1139 break; 1140 case 3 /* BinaryReaderState.BEGIN_SECTION */: 1141 var sectionInfo = reader.result; 1142 switch (sectionInfo.id) { 1143 case 1 /* SectionCode.Type */: 1144 case 2 /* SectionCode.Import */: 1145 case 7 /* SectionCode.Export */: 1146 case 6 /* SectionCode.Global */: 1147 case 3 /* SectionCode.Function */: 1148 case 8 /* SectionCode.Start */: 1149 case 10 /* SectionCode.Code */: 1150 case 5 /* SectionCode.Memory */: 1151 case 11 /* SectionCode.Data */: 1152 case 4 /* SectionCode.Table */: 1153 case 9 /* SectionCode.Element */: 1154 case 13 /* SectionCode.Tag */: 1155 this._currentSectionId = sectionInfo.id; 1156 this._indent = " "; 1157 this._indentLevel = 0; 1158 break; // reading known section; 1159 default: 1160 reader.skipSection(); 1161 break; 1162 } 1163 break; 1164 case 15 /* BinaryReaderState.MEMORY_SECTION_ENTRY */: 1165 var memoryInfo = reader.result; 1166 var memoryIndex = this._memoryCount++; 1167 var memoryName = this._nameResolver.getMemoryName(memoryIndex, false); 1168 this.appendBuffer(" (memory ".concat(memoryName)); 1169 if (this._exportMetadata !== null) { 1170 for (var _i = 0, _a = this._exportMetadata.getMemoryExportNames(memoryIndex); _i < _a.length; _i++) { 1171 var exportName = _a[_i]; 1172 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1173 } 1174 } 1175 this.appendBuffer(" ".concat(limitsToString(memoryInfo.limits))); 1176 if (memoryInfo.shared) { 1177 this.appendBuffer(" shared"); 1178 } 1179 this.appendBuffer(")"); 1180 this.newLine(); 1181 break; 1182 case 23 /* BinaryReaderState.TAG_SECTION_ENTRY */: 1183 var tagInfo = reader.result; 1184 var tagIndex = this._eventCount++; 1185 var tagName = this._nameResolver.getTagName(tagIndex, false); 1186 this.appendBuffer(" (tag ".concat(tagName)); 1187 if (this._exportMetadata !== null) { 1188 for (var _b = 0, _c = this._exportMetadata.getTagExportNames(tagIndex); _b < _c.length; _b++) { 1189 var exportName = _c[_b]; 1190 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1191 } 1192 } 1193 this.printFuncType(tagInfo.typeIndex); 1194 this.appendBuffer(")"); 1195 this.newLine(); 1196 break; 1197 case 14 /* BinaryReaderState.TABLE_SECTION_ENTRY */: 1198 var tableInfo = reader.result; 1199 var tableIndex = this._tableCount++; 1200 var tableName = this._nameResolver.getTableName(tableIndex, false); 1201 this.appendBuffer(" (table ".concat(tableName)); 1202 if (this._exportMetadata !== null) { 1203 for (var _d = 0, _e = this._exportMetadata.getTableExportNames(tableIndex); _d < _e.length; _d++) { 1204 var exportName = _e[_d]; 1205 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1206 } 1207 } 1208 this.appendBuffer(" ".concat(limitsToString(tableInfo.limits), " ").concat(this.typeToString(tableInfo.elementType), ")")); 1209 this.newLine(); 1210 break; 1211 case 17 /* BinaryReaderState.EXPORT_SECTION_ENTRY */: 1212 // Skip printing exports here when we have export metadata 1213 // which we can use to print export information inline. 1214 if (this._exportMetadata === null) { 1215 var exportInfo = reader.result; 1216 this.appendBuffer(" (export "); 1217 this.printString(exportInfo.field); 1218 this.appendBuffer(" "); 1219 switch (exportInfo.kind) { 1220 case 0 /* ExternalKind.Function */: 1221 var funcName = this._nameResolver.getFunctionName(exportInfo.index, exportInfo.index < this._importCount, true); 1222 this.appendBuffer("(func ".concat(funcName, ")")); 1223 break; 1224 case 1 /* ExternalKind.Table */: 1225 var tableName = this._nameResolver.getTableName(exportInfo.index, true); 1226 this.appendBuffer("(table ".concat(tableName, ")")); 1227 break; 1228 case 2 /* ExternalKind.Memory */: 1229 var memoryName = this._nameResolver.getMemoryName(exportInfo.index, true); 1230 this.appendBuffer("(memory ".concat(memoryName, ")")); 1231 break; 1232 case 3 /* ExternalKind.Global */: 1233 var globalName = this._nameResolver.getGlobalName(exportInfo.index, true); 1234 this.appendBuffer("(global ".concat(globalName, ")")); 1235 break; 1236 case 4 /* ExternalKind.Tag */: 1237 var tagName = this._nameResolver.getTagName(exportInfo.index, true); 1238 this.appendBuffer("(tag ".concat(tagName, ")")); 1239 break; 1240 default: 1241 throw new Error("Unsupported export ".concat(exportInfo.kind)); 1242 } 1243 this.appendBuffer(")"); 1244 this.newLine(); 1245 } 1246 break; 1247 case 12 /* BinaryReaderState.IMPORT_SECTION_ENTRY */: 1248 var importInfo = reader.result; 1249 switch (importInfo.kind) { 1250 case 0 /* ExternalKind.Function */: 1251 this._importCount++; 1252 var funcIndex = this._funcIndex++; 1253 var funcName = this._nameResolver.getFunctionName(funcIndex, true, false); 1254 this.appendBuffer(" (func ".concat(funcName)); 1255 if (this._exportMetadata !== null) { 1256 for (var _f = 0, _g = this._exportMetadata.getFunctionExportNames(funcIndex); _f < _g.length; _f++) { 1257 var exportName = _g[_f]; 1258 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1259 } 1260 } 1261 this.appendBuffer(" (import "); 1262 this.printImportSource(importInfo); 1263 this.appendBuffer(")"); 1264 this.printFuncType(importInfo.funcTypeIndex); 1265 this.appendBuffer(")"); 1266 break; 1267 case 3 /* ExternalKind.Global */: 1268 var globalImportInfo = importInfo.type; 1269 var globalIndex = this._globalCount++; 1270 var globalName = this._nameResolver.getGlobalName(globalIndex, false); 1271 this.appendBuffer(" (global ".concat(globalName)); 1272 if (this._exportMetadata !== null) { 1273 for (var _h = 0, _j = this._exportMetadata.getGlobalExportNames(globalIndex); _h < _j.length; _h++) { 1274 var exportName = _j[_h]; 1275 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1276 } 1277 } 1278 this.appendBuffer(" (import "); 1279 this.printImportSource(importInfo); 1280 this.appendBuffer(") ".concat(this.globalTypeToString(globalImportInfo), ")")); 1281 break; 1282 case 2 /* ExternalKind.Memory */: 1283 var memoryImportInfo = importInfo.type; 1284 var memoryIndex = this._memoryCount++; 1285 var memoryName = this._nameResolver.getMemoryName(memoryIndex, false); 1286 this.appendBuffer(" (memory ".concat(memoryName)); 1287 if (this._exportMetadata !== null) { 1288 for (var _k = 0, _l = this._exportMetadata.getMemoryExportNames(memoryIndex); _k < _l.length; _k++) { 1289 var exportName = _l[_k]; 1290 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1291 } 1292 } 1293 this.appendBuffer(" (import "); 1294 this.printImportSource(importInfo); 1295 this.appendBuffer(") ".concat(limitsToString(memoryImportInfo.limits))); 1296 if (memoryImportInfo.shared) { 1297 this.appendBuffer(" shared"); 1298 } 1299 this.appendBuffer(")"); 1300 break; 1301 case 1 /* ExternalKind.Table */: 1302 var tableImportInfo = importInfo.type; 1303 var tableIndex = this._tableCount++; 1304 var tableName = this._nameResolver.getTableName(tableIndex, false); 1305 this.appendBuffer(" (table ".concat(tableName)); 1306 if (this._exportMetadata !== null) { 1307 for (var _m = 0, _o = this._exportMetadata.getTableExportNames(tableIndex); _m < _o.length; _m++) { 1308 var exportName = _o[_m]; 1309 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1310 } 1311 } 1312 this.appendBuffer(" (import "); 1313 this.printImportSource(importInfo); 1314 this.appendBuffer(") ".concat(limitsToString(tableImportInfo.limits), " ").concat(this.typeToString(tableImportInfo.elementType), ")")); 1315 break; 1316 case 4 /* ExternalKind.Tag */: 1317 var eventImportInfo = importInfo.type; 1318 var tagIndex = this._eventCount++; 1319 var tagName = this._nameResolver.getTagName(tagIndex, false); 1320 this.appendBuffer(" (tag ".concat(tagName)); 1321 if (this._exportMetadata !== null) { 1322 for (var _p = 0, _q = this._exportMetadata.getTagExportNames(tagIndex); _p < _q.length; _p++) { 1323 var exportName = _q[_p]; 1324 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1325 } 1326 } 1327 this.appendBuffer(" (import "); 1328 this.printImportSource(importInfo); 1329 this.appendBuffer(")"); 1330 this.printFuncType(eventImportInfo.typeIndex); 1331 this.appendBuffer(")"); 1332 break; 1333 default: 1334 throw new Error("NYI other import types: ".concat(importInfo.kind)); 1335 } 1336 this.newLine(); 1337 break; 1338 case 33 /* BinaryReaderState.BEGIN_ELEMENT_SECTION_ENTRY */: 1339 var elementSegment = reader.result; 1340 var elementIndex = this._elementCount++; 1341 var elementName = this._nameResolver.getElementName(elementIndex, false); 1342 this.appendBuffer(" (elem ".concat(elementName)); 1343 switch (elementSegment.mode) { 1344 case 0 /* ElementMode.Active */: 1345 if (elementSegment.tableIndex !== 0) { 1346 var tableName_1 = this._nameResolver.getTableName(elementSegment.tableIndex, false); 1347 this.appendBuffer(" (table ".concat(tableName_1, ")")); 1348 } 1349 break; 1350 case 1 /* ElementMode.Passive */: 1351 break; 1352 case 2 /* ElementMode.Declarative */: 1353 this.appendBuffer(" declare"); 1354 break; 1355 } 1356 break; 1357 case 35 /* BinaryReaderState.END_ELEMENT_SECTION_ENTRY */: 1358 this.appendBuffer(")"); 1359 this.newLine(); 1360 break; 1361 case 34 /* BinaryReaderState.ELEMENT_SECTION_ENTRY_BODY */: 1362 var elementSegmentBody = reader.result; 1363 this.appendBuffer(" ".concat(this.typeToString(elementSegmentBody.elementType))); 1364 break; 1365 case 39 /* BinaryReaderState.BEGIN_GLOBAL_SECTION_ENTRY */: 1366 var globalInfo = reader.result; 1367 var globalIndex = this._globalCount++; 1368 var globalName = this._nameResolver.getGlobalName(globalIndex, false); 1369 this.appendBuffer(" (global ".concat(globalName)); 1370 if (this._exportMetadata !== null) { 1371 for (var _r = 0, _s = this._exportMetadata.getGlobalExportNames(globalIndex); _r < _s.length; _r++) { 1372 var exportName = _s[_r]; 1373 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1374 } 1375 } 1376 this.appendBuffer(" ".concat(this.globalTypeToString(globalInfo.type))); 1377 break; 1378 case 40 /* BinaryReaderState.END_GLOBAL_SECTION_ENTRY */: 1379 this.appendBuffer(")"); 1380 this.newLine(); 1381 break; 1382 case 11 /* BinaryReaderState.TYPE_SECTION_ENTRY */: 1383 var typeEntry = reader.result; 1384 var typeIndex = this._types.length; 1385 this._types.push(typeEntry); 1386 if (!this._skipTypes) { 1387 var typeName = this._nameResolver.getTypeName(typeIndex, false); 1388 var superTypeName = undefined; 1389 if (typeEntry.supertypes !== undefined) { 1390 superTypeName = typeEntry.supertypes 1391 .map(function (ty) { return _this.typeIndexToString(ty); }) 1392 .join("+"); 1393 } 1394 this.appendBuffer(this._indent); 1395 this.appendBuffer("(type ".concat(typeName, " ")); 1396 var subtype = typeEntry.supertypes || typeEntry.final; 1397 if (subtype) { 1398 this.appendBuffer("(sub "); 1399 if (typeEntry.final) 1400 this.appendBuffer("final "); 1401 if (typeEntry.supertypes) { 1402 this.appendBuffer(typeEntry.supertypes 1403 .map(function (ty) { return _this.typeIndexToString(ty); }) 1404 .join(" ")); 1405 this.appendBuffer(" "); 1406 } 1407 } 1408 if (typeEntry.form === -32 /* TypeKind.func */) { 1409 this.appendBuffer("(func"); 1410 this.printFuncType(typeIndex); 1411 this.appendBuffer(")"); 1412 } 1413 else if (typeEntry.form === -33 /* TypeKind.struct */) { 1414 this.appendBuffer("(struct"); 1415 this.printStructType(typeIndex); 1416 this.appendBuffer(")"); 1417 } 1418 else if (typeEntry.form === -34 /* TypeKind.array */) { 1419 this.appendBuffer("(array"); 1420 this.printArrayType(typeIndex); 1421 this.appendBuffer(")"); 1422 } 1423 else { 1424 throw new Error("Unknown type form: ".concat(typeEntry.form)); 1425 } 1426 if (subtype) { 1427 this.appendBuffer(")"); 1428 } 1429 this.appendBuffer(")"); 1430 this.newLine(); 1431 } 1432 break; 1433 case 22 /* BinaryReaderState.START_SECTION_ENTRY */: 1434 var startEntry = reader.result; 1435 var funcName = this._nameResolver.getFunctionName(startEntry.index, startEntry.index < this._importCount, true); 1436 this.appendBuffer(" (start ".concat(funcName, ")")); 1437 this.newLine(); 1438 break; 1439 case 36 /* BinaryReaderState.BEGIN_DATA_SECTION_ENTRY */: 1440 this.appendBuffer(" (data"); 1441 break; 1442 case 37 /* BinaryReaderState.DATA_SECTION_ENTRY_BODY */: 1443 var body = reader.result; 1444 this.appendBuffer(" "); 1445 this.printString(body.data); 1446 break; 1447 case 38 /* BinaryReaderState.END_DATA_SECTION_ENTRY */: 1448 this.appendBuffer(")"); 1449 this.newLine(); 1450 break; 1451 case 25 /* BinaryReaderState.BEGIN_INIT_EXPRESSION_BODY */: 1452 case 44 /* BinaryReaderState.BEGIN_OFFSET_EXPRESSION_BODY */: 1453 this._expression = []; 1454 break; 1455 case 26 /* BinaryReaderState.INIT_EXPRESSION_OPERATOR */: 1456 case 45 /* BinaryReaderState.OFFSET_EXPRESSION_OPERATOR */: 1457 var operator = reader.result; 1458 if (operator.code !== 11 /* OperatorCode.end */) { 1459 this._expression.push(operator); 1460 } 1461 break; 1462 case 46 /* BinaryReaderState.END_OFFSET_EXPRESSION_BODY */: 1463 if (this._expression.length > 1) { 1464 this.appendBuffer(" (offset "); 1465 this.printExpression(this._expression); 1466 this.appendBuffer(")"); 1467 } 1468 else { 1469 this.appendBuffer(" "); 1470 this.printExpression(this._expression); 1471 } 1472 this._expression = []; 1473 break; 1474 case 27 /* BinaryReaderState.END_INIT_EXPRESSION_BODY */: 1475 if (this._expression.length > 1 && 1476 this._currentSectionId === 9 /* SectionCode.Element */) { 1477 this.appendBuffer(" (item "); 1478 this.printExpression(this._expression); 1479 this.appendBuffer(")"); 1480 } 1481 else { 1482 this.appendBuffer(" "); 1483 this.printExpression(this._expression); 1484 } 1485 this._expression = []; 1486 break; 1487 case 13 /* BinaryReaderState.FUNCTION_SECTION_ENTRY */: 1488 this._funcTypes.push(reader.result.typeIndex); 1489 break; 1490 case 28 /* BinaryReaderState.BEGIN_FUNCTION_BODY */: 1491 var func = reader.result; 1492 var type = this._types[this._funcTypes[this._funcIndex - this._importCount]]; 1493 this.appendBuffer(" (func "); 1494 this.appendBuffer(this._nameResolver.getFunctionName(this._funcIndex, false, false)); 1495 if (this._exportMetadata !== null) { 1496 for (var _t = 0, _u = this._exportMetadata.getFunctionExportNames(this._funcIndex); _t < _u.length; _t++) { 1497 var exportName = _u[_t]; 1498 this.appendBuffer(" (export ".concat(JSON.stringify(exportName), ")")); 1499 } 1500 } 1501 for (var i = 0; i < type.params.length; i++) { 1502 var paramName = this._nameResolver.getVariableName(this._funcIndex, i, false); 1503 this.appendBuffer(" (param ".concat(paramName, " ").concat(this.typeToString(type.params[i]), ")")); 1504 } 1505 for (var i = 0; i < type.returns.length; i++) { 1506 this.appendBuffer(" (result ".concat(this.typeToString(type.returns[i]), ")")); 1507 } 1508 this.newLine(); 1509 var localIndex = type.params.length; 1510 if (func.locals.length > 0) { 1511 this.appendBuffer(" "); 1512 for (var _v = 0, _w = func.locals; _v < _w.length; _v++) { 1513 var l = _w[_v]; 1514 for (var i = 0; i < l.count; i++) { 1515 var paramName = this._nameResolver.getVariableName(this._funcIndex, localIndex++, false); 1516 this.appendBuffer(" (local ".concat(paramName, " ").concat(this.typeToString(l.type), ")")); 1517 } 1518 } 1519 this.newLine(); 1520 } 1521 this._indent = " "; 1522 this._indentLevel = 0; 1523 this._labelIndex = 0; 1524 this._backrefLabels = this._labelMode === LabelMode.Depth ? null : []; 1525 this._logFirstInstruction = true; 1526 break; 1527 case 30 /* BinaryReaderState.CODE_OPERATOR */: 1528 if (this._logFirstInstruction) { 1529 this.logStartOfFunctionBodyOffset(); 1530 this._logFirstInstruction = false; 1531 } 1532 var operator = reader.result; 1533 if (operator.code == 11 /* OperatorCode.end */ && this._indentLevel == 0) { 1534 // reached of the function, closing function body 1535 this.appendBuffer(" )"); 1536 this.newLine(); 1537 break; 1538 } 1539 switch (operator.code) { 1540 case 11 /* OperatorCode.end */: 1541 case 5 /* OperatorCode.else */: 1542 case 7 /* OperatorCode.catch */: 1543 case 25 /* OperatorCode.catch_all */: 1544 case 24 /* OperatorCode.delegate */: 1545 this.decreaseIndent(); 1546 break; 1547 } 1548 this.appendBuffer(this._indent); 1549 this.printOperator(operator); 1550 this.newLine(); 1551 switch (operator.code) { 1552 case 4 /* OperatorCode.if */: 1553 case 2 /* OperatorCode.block */: 1554 case 3 /* OperatorCode.loop */: 1555 case 5 /* OperatorCode.else */: 1556 case 6 /* OperatorCode.try */: 1557 case 31 /* OperatorCode.try_table */: 1558 case 7 /* OperatorCode.catch */: 1559 case 25 /* OperatorCode.catch_all */: 1560 this.increaseIndent(); 1561 break; 1562 } 1563 break; 1564 case 31 /* BinaryReaderState.END_FUNCTION_BODY */: 1565 this._funcIndex++; 1566 this._backrefLabels = null; 1567 this.logEndOfFunctionBodyOffset(); 1568 // See case BinaryReaderState.CODE_OPERATOR for closing of body 1569 break; 1570 case 47 /* BinaryReaderState.BEGIN_REC_GROUP */: 1571 if (!this._skipTypes) { 1572 this.appendBuffer(" (rec"); 1573 this.newLine(); 1574 this.increaseIndent(); 1575 } 1576 break; 1577 case 48 /* BinaryReaderState.END_REC_GROUP */: 1578 if (!this._skipTypes) { 1579 this.decreaseIndent(); 1580 this.appendBuffer(" )"); 1581 this.newLine(); 1582 } 1583 break; 1584 default: 1585 throw new Error("Expectected state: ".concat(reader.state)); 1586 } 1587 } 1588 }; 1589 return WasmDisassembler; 1590 }()); 1591 exports.WasmDisassembler = WasmDisassembler; 1592 var UNKNOWN_FUNCTION_PREFIX = "unknown"; 1593 var NameSectionNameResolver = /** @class */ (function (_super) { 1594 __extends(NameSectionNameResolver, _super); 1595 function NameSectionNameResolver(functionNames, localNames, tagNames, typeNames, tableNames, memoryNames, globalNames, fieldNames) { 1596 var _this = _super.call(this) || this; 1597 _this._functionNames = functionNames; 1598 _this._localNames = localNames; 1599 _this._tagNames = tagNames; 1600 _this._typeNames = typeNames; 1601 _this._tableNames = tableNames; 1602 _this._memoryNames = memoryNames; 1603 _this._globalNames = globalNames; 1604 _this._fieldNames = fieldNames; 1605 return _this; 1606 } 1607 NameSectionNameResolver.prototype.getTypeName = function (index, isRef) { 1608 var name = this._typeNames[index]; 1609 if (!name) 1610 return _super.prototype.getTypeName.call(this, index, isRef); 1611 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1612 }; 1613 NameSectionNameResolver.prototype.getTableName = function (index, isRef) { 1614 var name = this._tableNames[index]; 1615 if (!name) 1616 return _super.prototype.getTableName.call(this, index, isRef); 1617 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1618 }; 1619 NameSectionNameResolver.prototype.getMemoryName = function (index, isRef) { 1620 var name = this._memoryNames[index]; 1621 if (!name) 1622 return _super.prototype.getMemoryName.call(this, index, isRef); 1623 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1624 }; 1625 NameSectionNameResolver.prototype.getGlobalName = function (index, isRef) { 1626 var name = this._globalNames[index]; 1627 if (!name) 1628 return _super.prototype.getGlobalName.call(this, index, isRef); 1629 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1630 }; 1631 NameSectionNameResolver.prototype.getTagName = function (index, isRef) { 1632 var name = this._tagNames[index]; 1633 if (!name) 1634 return _super.prototype.getTagName.call(this, index, isRef); 1635 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1636 }; 1637 NameSectionNameResolver.prototype.getFunctionName = function (index, isImport, isRef) { 1638 var name = this._functionNames[index]; 1639 if (!name) 1640 return "$".concat(UNKNOWN_FUNCTION_PREFIX).concat(index); 1641 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1642 }; 1643 NameSectionNameResolver.prototype.getVariableName = function (funcIndex, index, isRef) { 1644 var name = this._localNames[funcIndex] && this._localNames[funcIndex][index]; 1645 if (!name) 1646 return _super.prototype.getVariableName.call(this, funcIndex, index, isRef); 1647 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1648 }; 1649 NameSectionNameResolver.prototype.getFieldName = function (typeIndex, index, isRef) { 1650 var name = this._fieldNames[typeIndex] && this._fieldNames[typeIndex][index]; 1651 if (!name) 1652 return _super.prototype.getFieldName.call(this, typeIndex, index, isRef); 1653 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1654 }; 1655 return NameSectionNameResolver; 1656 }(DefaultNameResolver)); 1657 var NameSectionReader = /** @class */ (function () { 1658 function NameSectionReader() { 1659 this._done = false; 1660 this._functionsCount = 0; 1661 this._functionImportsCount = 0; 1662 this._functionNames = null; 1663 this._functionLocalNames = null; 1664 this._tagNames = null; 1665 this._typeNames = null; 1666 this._tableNames = null; 1667 this._memoryNames = null; 1668 this._globalNames = null; 1669 this._fieldNames = null; 1670 this._hasNames = false; 1671 } 1672 NameSectionReader.prototype.read = function (reader) { 1673 var _this = this; 1674 if (this._done) 1675 throw new Error("Invalid state: disassembly process was already finished."); 1676 while (true) { 1677 if (!reader.read()) 1678 return false; 1679 switch (reader.state) { 1680 case 2 /* BinaryReaderState.END_WASM */: 1681 if (!reader.hasMoreBytes()) { 1682 this._done = true; 1683 return true; 1684 } 1685 break; 1686 case -1 /* BinaryReaderState.ERROR */: 1687 throw reader.error; 1688 case 1 /* BinaryReaderState.BEGIN_WASM */: 1689 this._functionsCount = 0; 1690 this._functionImportsCount = 0; 1691 this._functionNames = []; 1692 this._functionLocalNames = []; 1693 this._tagNames = []; 1694 this._typeNames = []; 1695 this._tableNames = []; 1696 this._memoryNames = []; 1697 this._globalNames = []; 1698 this._fieldNames = []; 1699 this._hasNames = false; 1700 break; 1701 case 4 /* BinaryReaderState.END_SECTION */: 1702 break; 1703 case 3 /* BinaryReaderState.BEGIN_SECTION */: 1704 var sectionInfo = reader.result; 1705 if (sectionInfo.id === 0 /* SectionCode.Custom */ && 1706 (0, WasmParser_js_1.bytesToString)(sectionInfo.name) === NAME_SECTION_NAME) { 1707 break; 1708 } 1709 if (sectionInfo.id === 3 /* SectionCode.Function */ || 1710 sectionInfo.id === 2 /* SectionCode.Import */) { 1711 break; 1712 } 1713 reader.skipSection(); 1714 break; 1715 case 12 /* BinaryReaderState.IMPORT_SECTION_ENTRY */: 1716 var importInfo = reader.result; 1717 if (importInfo.kind === 0 /* ExternalKind.Function */) 1718 this._functionImportsCount++; 1719 break; 1720 case 13 /* BinaryReaderState.FUNCTION_SECTION_ENTRY */: 1721 this._functionsCount++; 1722 break; 1723 case 19 /* BinaryReaderState.NAME_SECTION_ENTRY */: 1724 var nameInfo = reader.result; 1725 if (nameInfo.type === 1 /* NameType.Function */) { 1726 var names = nameInfo.names; 1727 names.forEach(function (_a) { 1728 var index = _a.index, name = _a.name; 1729 _this._functionNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1730 }); 1731 this._hasNames = true; 1732 } 1733 else if (nameInfo.type === 2 /* NameType.Local */) { 1734 var funcs = nameInfo.funcs; 1735 funcs.forEach(function (_a) { 1736 var index = _a.index, locals = _a.locals; 1737 var localNames = (_this._functionLocalNames[index] = []); 1738 locals.forEach(function (_a) { 1739 var index = _a.index, name = _a.name; 1740 localNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1741 }); 1742 }); 1743 this._hasNames = true; 1744 } 1745 else if (nameInfo.type === 11 /* NameType.Tag */) { 1746 var names = nameInfo.names; 1747 names.forEach(function (_a) { 1748 var index = _a.index, name = _a.name; 1749 _this._tagNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1750 }); 1751 this._hasNames = true; 1752 } 1753 else if (nameInfo.type === 4 /* NameType.Type */) { 1754 var names = nameInfo.names; 1755 names.forEach(function (_a) { 1756 var index = _a.index, name = _a.name; 1757 _this._typeNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1758 }); 1759 this._hasNames = true; 1760 } 1761 else if (nameInfo.type === 5 /* NameType.Table */) { 1762 var names = nameInfo.names; 1763 names.forEach(function (_a) { 1764 var index = _a.index, name = _a.name; 1765 _this._tableNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1766 }); 1767 this._hasNames = true; 1768 } 1769 else if (nameInfo.type === 6 /* NameType.Memory */) { 1770 var names = nameInfo.names; 1771 names.forEach(function (_a) { 1772 var index = _a.index, name = _a.name; 1773 _this._memoryNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1774 }); 1775 this._hasNames = true; 1776 } 1777 else if (nameInfo.type === 7 /* NameType.Global */) { 1778 var names = nameInfo.names; 1779 names.forEach(function (_a) { 1780 var index = _a.index, name = _a.name; 1781 _this._globalNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1782 }); 1783 this._hasNames = true; 1784 } 1785 else if (nameInfo.type === 10 /* NameType.Field */) { 1786 var types = nameInfo.types; 1787 types.forEach(function (_a) { 1788 var index = _a.index, fields = _a.fields; 1789 var fieldNames = (_this._fieldNames[index] = []); 1790 fields.forEach(function (_a) { 1791 var index = _a.index, name = _a.name; 1792 fieldNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1793 }); 1794 }); 1795 } 1796 break; 1797 default: 1798 throw new Error("Expectected state: ".concat(reader.state)); 1799 } 1800 } 1801 }; 1802 NameSectionReader.prototype.hasValidNames = function () { 1803 return this._hasNames; 1804 }; 1805 NameSectionReader.prototype.getNameResolver = function () { 1806 if (!this.hasValidNames()) 1807 throw new Error("Has no valid name section"); 1808 // Fix bad names. 1809 var functionNamesLength = this._functionImportsCount + this._functionsCount; 1810 var functionNames = this._functionNames.slice(0, functionNamesLength); 1811 var usedNameAt = Object.create(null); 1812 for (var i = 0; i < functionNames.length; i++) { 1813 var name_1 = functionNames[i]; 1814 if (!name_1) 1815 continue; 1816 var goodName = !(name_1 in usedNameAt) && 1817 isValidName(name_1) && 1818 name_1.indexOf(UNKNOWN_FUNCTION_PREFIX) !== 0; 1819 if (!goodName) { 1820 if (usedNameAt[name_1] >= 0) { 1821 // Remove all non-unique names. 1822 functionNames[usedNameAt[name_1]] = null; 1823 usedNameAt[name_1] = -1; 1824 } 1825 functionNames[i] = null; 1826 continue; 1827 } 1828 usedNameAt[name_1] = i; 1829 } 1830 return new NameSectionNameResolver(functionNames, this._functionLocalNames, this._tagNames, this._typeNames, this._tableNames, this._memoryNames, this._globalNames, this._fieldNames); 1831 }; 1832 return NameSectionReader; 1833 }()); 1834 exports.NameSectionReader = NameSectionReader; 1835 var DevToolsNameResolver = /** @class */ (function (_super) { 1836 __extends(DevToolsNameResolver, _super); 1837 function DevToolsNameResolver(functionNames, localNames, tagNames, typeNames, tableNames, memoryNames, globalNames, fieldNames) { 1838 return _super.call(this, functionNames, localNames, tagNames, typeNames, tableNames, memoryNames, globalNames, fieldNames) || this; 1839 } 1840 DevToolsNameResolver.prototype.getFunctionName = function (index, isImport, isRef) { 1841 var name = this._functionNames[index]; 1842 if (!name) 1843 return isImport ? "$import".concat(index) : "$func".concat(index); 1844 return isRef ? "$".concat(name) : "$".concat(name, " (;").concat(index, ";)"); 1845 }; 1846 return DevToolsNameResolver; 1847 }(NameSectionNameResolver)); 1848 exports.DevToolsNameResolver = DevToolsNameResolver; 1849 var DevToolsNameGenerator = /** @class */ (function () { 1850 function DevToolsNameGenerator() { 1851 this._done = false; 1852 this._functionImportsCount = 0; 1853 this._memoryImportsCount = 0; 1854 this._tableImportsCount = 0; 1855 this._globalImportsCount = 0; 1856 this._tagImportsCount = 0; 1857 this._functionNames = null; 1858 this._functionLocalNames = null; 1859 this._tagNames = null; 1860 this._memoryNames = null; 1861 this._typeNames = null; 1862 this._tableNames = null; 1863 this._globalNames = null; 1864 this._fieldNames = null; 1865 this._functionExportNames = null; 1866 this._globalExportNames = null; 1867 this._memoryExportNames = null; 1868 this._tableExportNames = null; 1869 this._tagExportNames = null; 1870 } 1871 DevToolsNameGenerator.prototype._addExportName = function (exportNames, index, name) { 1872 var names = exportNames[index]; 1873 if (names) { 1874 names.push(name); 1875 } 1876 else { 1877 exportNames[index] = [name]; 1878 } 1879 }; 1880 DevToolsNameGenerator.prototype._setName = function (names, index, name, isNameSectionName) { 1881 if (!name) 1882 return; 1883 if (isNameSectionName) { 1884 if (!isValidName(name)) 1885 return; 1886 names[index] = name; 1887 } 1888 else if (!names[index]) { 1889 names[index] = name.replace(INVALID_NAME_SYMBOLS_REGEX_GLOBAL, "_"); 1890 } 1891 }; 1892 DevToolsNameGenerator.prototype.read = function (reader) { 1893 var _this = this; 1894 if (this._done) 1895 throw new Error("Invalid state: disassembly process was already finished."); 1896 while (true) { 1897 if (!reader.read()) 1898 return false; 1899 switch (reader.state) { 1900 case 2 /* BinaryReaderState.END_WASM */: 1901 if (!reader.hasMoreBytes()) { 1902 this._done = true; 1903 return true; 1904 } 1905 break; 1906 case -1 /* BinaryReaderState.ERROR */: 1907 throw reader.error; 1908 case 1 /* BinaryReaderState.BEGIN_WASM */: 1909 this._functionImportsCount = 0; 1910 this._memoryImportsCount = 0; 1911 this._tableImportsCount = 0; 1912 this._globalImportsCount = 0; 1913 this._tagImportsCount = 0; 1914 this._functionNames = []; 1915 this._functionLocalNames = []; 1916 this._tagNames = []; 1917 this._memoryNames = []; 1918 this._typeNames = []; 1919 this._tableNames = []; 1920 this._globalNames = []; 1921 this._fieldNames = []; 1922 this._functionExportNames = []; 1923 this._globalExportNames = []; 1924 this._memoryExportNames = []; 1925 this._tableExportNames = []; 1926 this._tagExportNames = []; 1927 break; 1928 case 4 /* BinaryReaderState.END_SECTION */: 1929 break; 1930 case 3 /* BinaryReaderState.BEGIN_SECTION */: 1931 var sectionInfo = reader.result; 1932 if (sectionInfo.id === 0 /* SectionCode.Custom */ && 1933 (0, WasmParser_js_1.bytesToString)(sectionInfo.name) === NAME_SECTION_NAME) { 1934 break; 1935 } 1936 switch (sectionInfo.id) { 1937 case 2 /* SectionCode.Import */: 1938 case 7 /* SectionCode.Export */: 1939 break; // reading known section; 1940 default: 1941 reader.skipSection(); 1942 break; 1943 } 1944 break; 1945 case 12 /* BinaryReaderState.IMPORT_SECTION_ENTRY */: 1946 var importInfo = reader.result; 1947 var importName = "".concat((0, WasmParser_js_1.bytesToString)(importInfo.module), ".").concat((0, WasmParser_js_1.bytesToString)(importInfo.field)); 1948 switch (importInfo.kind) { 1949 case 0 /* ExternalKind.Function */: 1950 this._setName(this._functionNames, this._functionImportsCount++, importName, false); 1951 break; 1952 case 1 /* ExternalKind.Table */: 1953 this._setName(this._tableNames, this._tableImportsCount++, importName, false); 1954 break; 1955 case 2 /* ExternalKind.Memory */: 1956 this._setName(this._memoryNames, this._memoryImportsCount++, importName, false); 1957 break; 1958 case 3 /* ExternalKind.Global */: 1959 this._setName(this._globalNames, this._globalImportsCount++, importName, false); 1960 break; 1961 case 4 /* ExternalKind.Tag */: 1962 this._setName(this._tagNames, this._tagImportsCount++, importName, false); 1963 default: 1964 throw new Error("Unsupported export ".concat(importInfo.kind)); 1965 } 1966 break; 1967 case 19 /* BinaryReaderState.NAME_SECTION_ENTRY */: 1968 var nameInfo = reader.result; 1969 if (nameInfo.type === 1 /* NameType.Function */) { 1970 var names = nameInfo.names; 1971 names.forEach(function (_a) { 1972 var index = _a.index, name = _a.name; 1973 _this._setName(_this._functionNames, index, (0, WasmParser_js_1.bytesToString)(name), true); 1974 }); 1975 } 1976 else if (nameInfo.type === 2 /* NameType.Local */) { 1977 var funcs = nameInfo.funcs; 1978 funcs.forEach(function (_a) { 1979 var index = _a.index, locals = _a.locals; 1980 var localNames = (_this._functionLocalNames[index] = []); 1981 locals.forEach(function (_a) { 1982 var index = _a.index, name = _a.name; 1983 localNames[index] = (0, WasmParser_js_1.bytesToString)(name); 1984 }); 1985 }); 1986 } 1987 else if (nameInfo.type === 11 /* NameType.Tag */) { 1988 var names = nameInfo.names; 1989 names.forEach(function (_a) { 1990 var index = _a.index, name = _a.name; 1991 _this._setName(_this._tagNames, index, (0, WasmParser_js_1.bytesToString)(name), true); 1992 }); 1993 } 1994 else if (nameInfo.type === 4 /* NameType.Type */) { 1995 var names = nameInfo.names; 1996 names.forEach(function (_a) { 1997 var index = _a.index, name = _a.name; 1998 _this._setName(_this._typeNames, index, (0, WasmParser_js_1.bytesToString)(name), true); 1999 }); 2000 } 2001 else if (nameInfo.type === 5 /* NameType.Table */) { 2002 var names = nameInfo.names; 2003 names.forEach(function (_a) { 2004 var index = _a.index, name = _a.name; 2005 _this._setName(_this._tableNames, index, (0, WasmParser_js_1.bytesToString)(name), true); 2006 }); 2007 } 2008 else if (nameInfo.type === 6 /* NameType.Memory */) { 2009 var names = nameInfo.names; 2010 names.forEach(function (_a) { 2011 var index = _a.index, name = _a.name; 2012 _this._setName(_this._memoryNames, index, (0, WasmParser_js_1.bytesToString)(name), true); 2013 }); 2014 } 2015 else if (nameInfo.type === 7 /* NameType.Global */) { 2016 var names = nameInfo.names; 2017 names.forEach(function (_a) { 2018 var index = _a.index, name = _a.name; 2019 _this._setName(_this._globalNames, index, (0, WasmParser_js_1.bytesToString)(name), true); 2020 }); 2021 } 2022 else if (nameInfo.type === 10 /* NameType.Field */) { 2023 var types = nameInfo.types; 2024 types.forEach(function (_a) { 2025 var index = _a.index, fields = _a.fields; 2026 var fieldNames = (_this._fieldNames[index] = []); 2027 fields.forEach(function (_a) { 2028 var index = _a.index, name = _a.name; 2029 fieldNames[index] = (0, WasmParser_js_1.bytesToString)(name); 2030 }); 2031 }); 2032 } 2033 break; 2034 case 17 /* BinaryReaderState.EXPORT_SECTION_ENTRY */: 2035 var exportInfo = reader.result; 2036 var exportName = (0, WasmParser_js_1.bytesToString)(exportInfo.field); 2037 switch (exportInfo.kind) { 2038 case 0 /* ExternalKind.Function */: 2039 this._addExportName(this._functionExportNames, exportInfo.index, exportName); 2040 this._setName(this._functionNames, exportInfo.index, exportName, false); 2041 break; 2042 case 3 /* ExternalKind.Global */: 2043 this._addExportName(this._globalExportNames, exportInfo.index, exportName); 2044 this._setName(this._globalNames, exportInfo.index, exportName, false); 2045 break; 2046 case 2 /* ExternalKind.Memory */: 2047 this._addExportName(this._memoryExportNames, exportInfo.index, exportName); 2048 this._setName(this._memoryNames, exportInfo.index, exportName, false); 2049 break; 2050 case 1 /* ExternalKind.Table */: 2051 this._addExportName(this._tableExportNames, exportInfo.index, exportName); 2052 this._setName(this._tableNames, exportInfo.index, exportName, false); 2053 break; 2054 case 4 /* ExternalKind.Tag */: 2055 this._addExportName(this._tagExportNames, exportInfo.index, exportName); 2056 this._setName(this._tagNames, exportInfo.index, exportName, false); 2057 break; 2058 default: 2059 throw new Error("Unsupported export ".concat(exportInfo.kind)); 2060 } 2061 break; 2062 default: 2063 throw new Error("Expectected state: ".concat(reader.state)); 2064 } 2065 } 2066 }; 2067 DevToolsNameGenerator.prototype.getExportMetadata = function () { 2068 return new DevToolsExportMetadata(this._functionExportNames, this._globalExportNames, this._memoryExportNames, this._tableExportNames, this._tagExportNames); 2069 }; 2070 DevToolsNameGenerator.prototype.getNameResolver = function () { 2071 return new DevToolsNameResolver(this._functionNames, this._functionLocalNames, this._tagNames, this._typeNames, this._tableNames, this._memoryNames, this._globalNames, this._fieldNames); 2072 }; 2073 return DevToolsNameGenerator; 2074 }()); 2075 exports.DevToolsNameGenerator = DevToolsNameGenerator;