Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 | 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 1649x 1649x 1649x 1649x 1649x 1649x 1649x 41x 41x 1643x 1643x 41x 41x 1659x 1659x 1659x 41x 41x 41x 41x 174x 174x 174x 174x 174x 174x 41x 41x 4x 4x 41x 41x 41x 41x 2408x 118x 118x 118x 2408x 2408x 2408x 2178x 2408x 230x 230x 230x 2408x 41x 41x 41x 41x 82x 82x 82x 82x 82x 82x 41x 41x 1302x 1302x 1302x 1302x 1302x 649x 1302x 653x 653x 1302x 41x 41x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 2x 2x 32x 41x 41x 573x 573x 41x 41x 573x 541x 541x 541x 541x 541x 541x 541x 573x 573x 41x 41x 2402x 2402x 2402x 2402x 2402x 2402x 2341x 2341x 163x 163x 2341x 2178x 2178x 2178x 2402x 61x 61x 2402x 2402x 2402x 2402x 2402x 41x 41x 2402x 2402x 2402x 2402x 2402x 2402x 41x 41x 51x 541x 541x 541x 541x 541x 541x 541x 541x 51x 51x 51x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 200x 51x 51x 41x 41x 649x 649x 649x 649x 649x 649x 649x 12x 12x 637x 637x 637x 637x 637x 649x 92x 92x 545x 545x 545x 545x 545x 545x 545x 649x 41x 41x 41x 41x 92x 92x 92x 92x 41x 41x 73x 73x 73x 73x 73x 73x 73x 41x 41x 67x 67x 41x 41x 25x 25x 25x 41x 41x 92x 92x 92x 92x 92x 92x 92x 92x 41x 41x 92x 92x 41x 41x 105x 105x 105x 105x 105x 105x 105x 105x 105x 29x 29x 29x 105x 92x 92x 105x 105x 105x 105x 105x 105x 105x 105x 105x 76x 76x 76x 76x 76x 67x 67x 67x 67x 67x 67x 67x 67x 67x 105x 29x 29x 105x 105x 29x 29x 105x 105x 105x 105x 41x 41x 105x 105x 105x 105x 105x 105x 39x 39x 105x 105x 105x 105x 105x 29x 29x 29x 105x 105x 105x 105x 105x 105x 105x 105x 105x 105x 105x 41x 41x 211x 211x 211x 211x 211x 211x 211x 41x 41x 41x 41x 73x 73x 73x 73x 73x 73x 73x 6x 6x 6x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 73x 41x 41x 41x 41x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 41x 41x 41x 41x 5709x 5709x 5709x 5709x 5292x 38x 5292x 5254x 5254x 5254x 4918x 4918x 4918x 4918x 5254x 5292x 5709x 5709x 5709x 5709x 5709x 5709x 5709x 5709x 5709x 38x 38x 38x 38x 5709x 5671x 417x 417x 5671x 5671x 5709x 41x 41x 335x 335x 41x 41x 39x 39x 41x 41x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 41x 41x 274x 274x 41x 41x 306x 306x 306x 41x 41x 3584x 3584x 3584x 41x 41x 608x 608x 608x 41x 41x 608x 608x 608x 41x 41x 57x 57x 57x 41x 41x 57x 57x 57x 41x 41x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 156x 154x 154x 156x 2x 2x 2x 156x 1653x 1653x 1653x 1653x 1653x 10x 10x 1653x 1643x 1643x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 1653x 41x 41x 1633x 1633x 1633x 1633x 1633x 1633x 1633x 1633x 1633x 1633x 1633x 1633x 1633x 41x 41x 2874x 2874x 2874x 2874x 41x 41x 41x 41x 2778x 2778x 41x 41x 1966x 1966x 1966x 41x 41x 1966x 1966x 1966x 41x 41x 118x 116x 118x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 118x 41x 41x 27716x 162x 162x 162x 27716x 27554x 27554x 27554x 27716x 41x 41x 102x 102x 102x 102x 41x 41x 102x 102x 102x 41x 41x 4952x 4952x 4952x 4952x 41x 41x 41x 372x 372x 215x 215x 157x 372x 41x 41x 325x 132x 128x 128x 132x 325x 41x 41x 16931x 16931x 16931x 41x 41x 105x 105x 41x 41x 177x 4x 176x 173x 173x 173x 173x 177x 41x 41x 3661x 3661x 3661x 41x 41x 41x 41x 1855x 1855x 1855x 41x 41x 38110x 38110x 38110x 41x 41x 2533x 2533x 41x 41x 828x 828x 828x 41x 41x 754x 754x 754x 41x 41x 763x 763x 763x 41x 41x 14684x 4677x 14684x 10007x 10007x 14684x 41x 41x 241x 52x 52x 241x 189x 189x 241x 41x 41x 30x 11x 11x 11x 11x 11x 11x 30x 41x 41x 446x 44x 39x 44x 2x 5x 3x 3x 446x 402x 402x 446x 41x 41x 1375x 1375x 1375x 41x 41x 57x 57x 57x 57x 57x 41x 41x 368x 368x 368x 368x 360x 360x 368x 8x 8x 8x 8x 368x 368x 41x 41x 4x 4x 41x 41x 105750x 105750x 41x 41x 814x 814x 41x 41x 1432x 6x 6x 6x 6x 6x 6x 6x 6x 1432x 1432x 6x 6x 1432x 1432x 174x 174x 82x 174x 92x 92x 174x 1432x 41x 41x 7634x 7634x 41x 41x 8828x 8828x 41x 41x 4141x 4141x 41x 41x 11033x 11033x 41x | import { Instance } from "./Instance.js"; import { Communicator } from "./Communicator.js"; import { Ice as Ice_Version } from "./Version.js"; const { EncodingVersion } = Ice_Version; import { FormatType } from "./FormatType.js"; import { InitializationException, MarshalException } from "./LocalExceptions.js"; import { Buffer } from "./Buffer.js"; import { Ice as Ice_Identity } from "./Identity.js"; const { Identity } = Ice_Identity; import { Encoding_1_0, Protocol } from "./Protocol.js"; import { SliceType } from "./SliceType.js"; import { Value } from "./Value.js"; import { OptionalFormat } from "./OptionalFormat.js"; class WriteEncaps { constructor() { this.start = 0; this.format = null; this.encoding = null; this.encoding_1_0 = false; this.encoder = null; this.next = null; } reset() { this.encoder = null; } setEncoding(encoding) { this.encoding = encoding; this.encoding_1_0 = encoding.equals(Encoding_1_0); } } class EncapsEncoder { constructor(stream, encaps) { this._stream = stream; this._encaps = encaps; this._marshaledMap = new Map(); // Map<Value, int>; this._typeIdMap = null; // Lazy initialized. Map<String, int> this._typeIdIndex = 0; } writeOptional() { return false; } writePendingValues() { return undefined; } registerTypeId(typeId) { if (this._typeIdMap === null) { // Lazy initialization this._typeIdMap = new Map(); // Map<String, int> } const p = this._typeIdMap.get(typeId); if (p !== undefined) { return p; } else { this._typeIdMap.set(typeId, ++this._typeIdIndex); return -1; } } } class EncapsEncoder10 extends EncapsEncoder { constructor(stream, encaps) { super(stream, encaps); this._sliceType = SliceType.NoSlice; this._writeSlice = 0; // Position of the slice data members this._valueIdIndex = 0; this._toBeMarshaledMap = new Map(); // Map<Value, Integer>(); } writeValue(v) { console.assert(v !== undefined); // // Object references are encoded as a negative integer in 1.0. // if (v !== null && v !== undefined) { this._stream.writeInt(-this.registerValue(v)); } else { this._stream.writeInt(0); } } writeException(v) { console.assert(v !== null && v !== undefined); // // User exception with the 1.0 encoding start with a boolean // flag that indicates whether or not the exception uses // classes. // // This allows reading the pending instances even if some part of // the exception was sliced. // const usesClasses = v._usesClasses(); this._stream.writeBool(usesClasses); v._write(this._stream); if (usesClasses) { this.writePendingValues(); } } startInstance(sliceType) { this._sliceType = sliceType; } endInstance() { if (this._sliceType === SliceType.ValueSlice) { // // Write the Object slice. // this.startSlice(Value.ice_staticId(), -1, true); this._stream.writeSize(0); // For compatibility with the old AFM. this.endSlice(); } this._sliceType = SliceType.NoSlice; } startSlice(typeId) { // // For instance slices, encode a boolean to indicate how the type ID // is encoded and the type ID either as a string or index. For // exception slices, always encode the type ID as a string. // if (this._sliceType === SliceType.ValueSlice) { const index = this.registerTypeId(typeId); if (index < 0) { this._stream.writeBool(false); this._stream.writeString(typeId); } else { this._stream.writeBool(true); this._stream.writeSize(index); } } else { this._stream.writeString(typeId); } this._stream.writeInt(0); // Placeholder for the slice length. this._writeSlice = this._stream.pos; } endSlice() { // // Write the slice length. // const sz = this._stream.pos - this._writeSlice + 4; this._stream.rewriteInt(sz, this._writeSlice - 4); } writePendingValues() { const writeCB = (value, key) => { // // Ask the instance to marshal itself. Any new class // instances that are triggered by the classes marshaled // are added to toBeMarshaledMap. // this._stream.writeInt(value); key.ice_preMarshal(); key._iceWrite(this._stream); }; while (this._toBeMarshaledMap.size > 0) { // // Consider the to be marshaled instances as marshaled now, // this is necessary to avoid adding again the "to be // marshaled instances" into _toBeMarshaledMap while writing // instances. // this._toBeMarshaledMap.forEach((value, key) => this._marshaledMap.set(key, value)); const savedMap = this._toBeMarshaledMap; this._toBeMarshaledMap = new Map(); // Map<Value, int>(); this._stream.writeSize(savedMap.size); savedMap.forEach(writeCB); } this._stream.writeSize(0); // Zero marker indicates end of sequence of sequences of instances. } registerValue(v) { console.assert(v !== null); // // Look for this instance in the to-be-marshaled map. // let p = this._toBeMarshaledMap.get(v); if (p !== undefined) { return p; } // // Didn't find it, try the marshaled map next. // p = this._marshaledMap.get(v); if (p !== undefined) { return p; } // // We haven't seen this instance previously, create a new // index, and insert it into the to-be-marshaled map. // this._toBeMarshaledMap.set(v, ++this._valueIdIndex); return this._valueIdIndex; } } class EncapsEncoder11 extends EncapsEncoder { constructor(stream, encaps) { super(stream, encaps); this._current = null; this._valueIdIndex = 1; } writeValue(v) { console.assert(v !== undefined); if (v === null || v === undefined) { this._stream.writeSize(0); } else if (this._current !== null && this._encaps.format === FormatType.SlicedFormat) { if (this._current.indirectionTable === null) { // Lazy initialization this._current.indirectionTable = []; // Value[] this._current.indirectionMap = new Map(); // Map<Value, int> } // // If writing an instance within a slice and using the sliced // format, write an index from the instance indirection // table. The indirect instance table is encoded at the end of // each slice and is always read (even if the Slice is // unknown). // const index = this._current.indirectionMap.get(v); if (index === undefined) { this._current.indirectionTable.push(v); const idx = this._current.indirectionTable.length; // Position + 1 (0 is reserved for nil) this._current.indirectionMap.set(v, idx); this._stream.writeSize(idx); } else { this._stream.writeSize(index); } } else { this.writeInstance(v); // Write the instance or a reference if already marshaled. } } writePendingValues() { return undefined; } writeException(v) { console.assert(v !== null && v !== undefined); v._write(this._stream); } startInstance(sliceType, data) { if (this._current === null) { this._current = new EncapsEncoder11.InstanceData(null); } else { this._current = this._current.next === null ? new EncapsEncoder11.InstanceData(this._current) : this._current.next; } this._current.sliceType = sliceType; this._current.firstSlice = true; if (data !== null && data !== undefined) { this.writeSlicedData(data); } } endInstance() { this._current = this._current.previous; } startSlice(typeId, compactId, last) { console.assert( (this._current.indirectionTable === null || this._current.indirectionTable.length === 0) && (this._current.indirectionMap === null || this._current.indirectionMap.size === 0), ); this._current.sliceFlagsPos = this._stream.pos; this._current.sliceFlags = 0; if (this._encaps.format === FormatType.SlicedFormat) { // Encode the slice size if using the sliced format. this._current.sliceFlags |= Protocol.FLAG_HAS_SLICE_SIZE; } if (last) { this._current.sliceFlags |= Protocol.FLAG_IS_LAST_SLICE; // This is the last slice. } this._stream.writeByte(0); // Placeholder for the slice flags // // For instance slices, encode the flag and the type ID either as a // string or index. For exception slices, always encode the type // ID a string. // if (this._current.sliceType === SliceType.ValueSlice) { // // Encode the type ID (only in the first slice for the compact // encoding). // if (this._encaps.format === FormatType.SlicedFormat || this._current.firstSlice) { if (compactId != -1) { this._current.sliceFlags |= Protocol.FLAG_HAS_TYPE_ID_COMPACT; this._stream.writeSize(compactId); } else { const index = this.registerTypeId(typeId); if (index < 0) { this._current.sliceFlags |= Protocol.FLAG_HAS_TYPE_ID_STRING; this._stream.writeString(typeId); } else { this._current.sliceFlags |= Protocol.FLAG_HAS_TYPE_ID_INDEX; this._stream.writeSize(index); } } } } else { this._stream.writeString(typeId); } if ((this._current.sliceFlags & Protocol.FLAG_HAS_SLICE_SIZE) !== 0) { this._stream.writeInt(0); // Placeholder for the slice length. } this._current.writeSlice = this._stream.pos; this._current.firstSlice = false; } endSlice() { // // Write the optional member end marker if some optional members // were encoded. Note that the optional members are encoded before // the indirection table and are included in the slice size. // if ((this._current.sliceFlags & Protocol.FLAG_HAS_OPTIONAL_MEMBERS) !== 0) { this._stream.writeByte(Protocol.OPTIONAL_END_MARKER); } // // Write the slice length if necessary. // if ((this._current.sliceFlags & Protocol.FLAG_HAS_SLICE_SIZE) !== 0) { const sz = this._stream.pos - this._current.writeSlice + 4; this._stream.rewriteInt(sz, this._current.writeSlice - 4); } // // Only write the indirection table if it contains entries. // if (this._current.indirectionTable !== null && this._current.indirectionTable.length !== 0) { console.assert(this._encaps.format === FormatType.SlicedFormat); this._current.sliceFlags |= Protocol.FLAG_HAS_INDIRECTION_TABLE; // // Write the indirection instance table. // this._stream.writeSize(this._current.indirectionTable.length); this._current.indirectionTable.forEach(o => this.writeInstance(o)); this._current.indirectionTable.length = 0; // Faster way to clean array in JavaScript this._current.indirectionMap.clear(); } // // Finally, update the slice flags. // this._stream.rewriteByte(this._current.sliceFlags, this._current.sliceFlagsPos); } writeOptional(tag, format) { if (this._current === null) { return this._stream.writeOptImpl(tag, format); } if (this._stream.writeOptImpl(tag, format)) { this._current.sliceFlags |= Protocol.FLAG_HAS_OPTIONAL_MEMBERS; return true; } return false; } writeSlicedData(slicedData) { console.assert(slicedData !== null && slicedData !== undefined); // // We only marshal preserved slices if we are using the sliced // format. Otherwise, we ignore the preserved slices, which // essentially "slices" the instance into the most-derived type // known by the sender. // if (this._encaps.format !== FormatType.SlicedFormat) { return; } slicedData.slices.forEach(info => { this.startSlice(info.typeId, info.compactId, info.isLastSlice); // // Write the bytes associated with this slice. // this._stream.writeBlob(info.bytes); if (info.hasOptionalMembers) { this._current.sliceFlags |= Protocol.FLAG_HAS_OPTIONAL_MEMBERS; } // // Make sure to also re-write the instance indirection table. // if (info.instances !== null && info.instances.length > 0) { if (this._current.indirectionTable === null) { // Lazy initialization this._current.indirectionTable = []; // Value[] this._current.indirectionMap = new Map(); // Map<Value, int> } info.instances.forEach(instance => this._current.indirectionTable.push(instance)); } this.endSlice(); }); } writeInstance(v) { console.assert(v !== null && v !== undefined); // // If the instance was already marshaled, just write it's ID. // const p = this._marshaledMap.get(v); if (p !== undefined) { this._stream.writeSize(p); return; } // // We haven't seen this instance previously, create a new ID, // insert it into the marshaled map, and write the instance. // this._marshaledMap.set(v, ++this._valueIdIndex); v.ice_preMarshal(); this._stream.writeSize(1); // Object instance marker. v._iceWrite(this._stream); } } EncapsEncoder11.InstanceData = class { constructor(previous) { console.assert(previous !== undefined); if (previous !== null) { previous.next = this; } this.previous = previous; this.next = null; // Instance attributes this.sliceType = null; this.firstSlice = false; // Slice attributes this.sliceFlags = 0; this.writeSlice = 0; // Position of the slice data members this.sliceFlagsPos = 0; // Position of the slice flags this.indirectionTable = null; // Value[] this.indirectionMap = null; // Map<Value, int> } }; export class OutputStream { constructor(arg1, arg2) { let instance = null; this._encoding = null; if (arg1 !== undefined && arg1 !== null) { if (arg1.constructor == Communicator) { instance = arg1.instance; } else if (arg1.constructor == Instance) { instance = arg1; } else if (arg1.constructor == EncodingVersion) { this._encoding = arg1; if (arg2 !== null && arg2 !== undefined) { if (arg2.constructor == FormatType) { this._format = arg2; } else { throw new InitializationException("unknown argument to OutputStream constructor"); } } } else { throw new InitializationException("unknown argument to OutputStream constructor"); } } this._buf = new Buffer(); this._closure = null; this._encapsStack = null; this._encapsCache = null; if (instance !== null) { if (this._encoding === null) { this._encoding = instance.defaultsAndOverrides().defaultEncoding; } this._format = instance.defaultsAndOverrides().defaultFormat; } else { if (this._encoding === null) { this._encoding = Protocol.currentEncoding; } this._format = FormatType.CompactFormat; } } clear() { if (this._encapsStack !== null) { console.assert(this._encapsStack.next); this._encapsStack.next = this._encapsCache; this._encapsCache = this._encapsStack; this._encapsCache.reset(); this._encapsStack = null; } } finished() { return this.prepareWrite().getArray(this.size); } swap(other) { [other._buf, this._buf] = [this._buf, other._buf]; [other._encoding, this._encoding] = [this._encoding, other._encoding]; [other._closure, this._closure] = [this._closure, other._closure]; // // Swap is never called for streams that have encapsulations being written. However, // encapsulations might still be set in case marshaling failed. We just // reset the encapsulations if there are still some set. // this.resetEncapsulation(); other.resetEncapsulation(); } resetEncapsulation() { this._encapsStack = null; } resize(sz) { this._buf.resize(sz); this._buf.position = sz; } prepareWrite() { this._buf.position = 0; return this._buf; } startValue(data) { console.assert(this._encapsStack !== null && this._encapsStack.encoder !== null); this._encapsStack.encoder.startInstance(SliceType.ValueSlice, data); } endValue() { console.assert(this._encapsStack !== null && this._encapsStack.encoder !== null); this._encapsStack.encoder.endInstance(); } startException() { console.assert(this._encapsStack !== null && this._encapsStack.encoder !== null); this._encapsStack.encoder.startInstance(SliceType.ExceptionSlice); } endException() { console.assert(this._encapsStack !== null && this._encapsStack.encoder !== null); this._encapsStack.encoder.endInstance(); } startEncapsulation(encoding, format) { // // If no encoding version is specified, use the current write // encapsulation encoding version if there's a current write // encapsulation, otherwise, use the stream encoding version. // if (encoding === undefined) { if (this._encapsStack !== null) { encoding = this._encapsStack.encoding; format = this._encapsStack.format; } else { encoding = this._encoding; format = null; } } Protocol.checkSupportedEncoding(encoding); let curr = this._encapsCache; if (curr !== null) { curr.reset(); this._encapsCache = this._encapsCache.next; } else { curr = new WriteEncaps(); } curr.next = this._encapsStack; this._encapsStack = curr; this._encapsStack.format = format || this._format; this._encapsStack.setEncoding(encoding); this._encapsStack.start = this._buf.limit; this.writeInt(0); // Placeholder for the encapsulation length. this._encapsStack.encoding._write(this); } endEncapsulation() { console.assert(this._encapsStack); // Size includes size and version. const start = this._encapsStack.start; const sz = this._buf.limit - start; this._buf.putIntAt(start, sz); const curr = this._encapsStack; this._encapsStack = curr.next; curr.next = this._encapsCache; this._encapsCache = curr; this._encapsCache.reset(); } writeEmptyEncapsulation(encoding) { Protocol.checkSupportedEncoding(encoding); this.writeInt(6); // Size encoding._write(this); } writeEncapsulation(v) { if (v.length < 6) { throw new MarshalException(`A byte sequence with ${v.length} bytes is not a valid encapsulation.`); } this.expand(v.length); this._buf.putArray(v); } getEncoding() { return this._encapsStack !== null ? this._encapsStack.encoding : this._encoding; } startSlice(typeId, compactId, last) { console.assert(this._encapsStack !== null && this._encapsStack.encoder !== null); this._encapsStack.encoder.startSlice(typeId, compactId, last); } endSlice() { console.assert(this._encapsStack !== null && this._encapsStack.encoder !== null); this._encapsStack.encoder.endSlice(); } writePendingValues() { if (this._encapsStack !== null && this._encapsStack.encoder !== null) { this._encapsStack.encoder.writePendingValues(); } else if (this._encapsStack !== null ? this._encapsStack.encoding_1_0 : this._encoding.equals(Encoding_1_0)) { // // If using the 1.0 encoding and no instances were written, we // still write an empty sequence for pending instances if // requested (i.e.: if this is called). // // This is required by the 1.0 encoding, even if no instances // are written we do marshal an empty sequence if marshaled // data types use classes. // this.writeSize(0); } } writeSize(v) { if (v > 254) { this.expand(5); this._buf.put(255); this._buf.putInt(v); } else { this.expand(1); this._buf.put(v); } } startSize() { const pos = this._buf.position; this.writeInt(0); // Placeholder for 32-bit size return pos; } endSize(pos) { console.assert(pos >= 0); this.rewriteInt(this._buf.position - pos - 4, pos); } writeBlob(v) { if (v === null || v === undefined) { return; } this.expand(v.length); this._buf.putArray(v); } // Read/write format and tag for optionals writeOptional(tag, format) { console.assert(this._encapsStack !== null); if (this._encapsStack.encoder !== null) { return this._encapsStack.encoder.writeOptional(tag, format); } return this.writeOptImpl(tag, format); } writeOptionalHelper(tag, format, write, v) { if (v !== undefined && v !== null) { if (this.writeOptional(tag, format)) { write.call(this, v); } } } writeByte(v) { this.expand(1); this._buf.put(v); } rewriteByte(v, dest) { this._buf.putAt(dest, v); } writeByteSeq(v) { if (v === null || v === undefined || v.length === 0) { this.writeSize(0); } else { this.writeSize(v.length); this.expand(v.length); this._buf.putArray(v); } } writeBool(v) { this.expand(1); this._buf.put(v ? 1 : 0); } rewriteBool(v, dest) { this._buf.putAt(dest, v ? 1 : 0); } writeShort(v) { this.expand(2); this._buf.putShort(v); } writeInt(v) { this.expand(4); this._buf.putInt(v); } rewriteInt(v, dest) { this._buf.putIntAt(dest, v); } writeLong(v) { this.expand(8); this._buf.putLong(typeof v === "number" ? BigInt(v) : v); } writeFloat(v) { this.expand(4); this._buf.putFloat(v); } writeDouble(v) { this.expand(8); this._buf.putDouble(v); } writeString(v) { if (v === null || v === undefined || v.length === 0) { this.writeSize(0); } else { this._buf.writeString(this, v); } } writeProxy(v) { if (v === null || v === undefined) { const ident = new Identity(); ident._write(this); } else { v._write(this); } } writeOptionalProxy(tag, v) { if (v !== undefined && v !== null) { if (this.writeOptional(tag, OptionalFormat.FSize)) { const pos = this.startSize(); this.writeProxy(v); this.endSize(pos); } } } writeEnum(v) { if (this.isEncoding_1_0()) { if (v.maxValue < 127) { this.writeByte(v.value); } else if (v.maxValue < 32767) { this.writeShort(v.value); } else { this.writeInt(v.value); } } else { this.writeSize(v.value); } } writeValue(v) { this.initEncaps(); this._encapsStack.encoder.writeValue(v); } writeException(e) { this.initEncaps(); // Exceptions are always encoded with the sliced format. this._encapsStack.format = FormatType.SlicedFormat; this._encapsStack.encoder.writeException(e); } writeOptImpl(tag, format) { if (this.isEncoding_1_0()) { return false; // Optional members aren't supported with the 1.0 encoding. } let v = format.value; if (tag < 30) { v |= tag << 3; this.writeByte(v); } else { v |= 0x0f0; // tag = 30 this.writeByte(v); this.writeSize(tag); } return true; } isEmpty() { return this._buf.empty(); } expand(n) { this._buf.expand(n); } isEncoding_1_0() { return this._encapsStack ? this._encapsStack.encoding_1_0 : this._encoding.equals(Encoding_1_0); } initEncaps() { if (!this._encapsStack) { // Lazy initialization this._encapsStack = this._encapsCache; if (this._encapsStack) { this._encapsCache = this._encapsCache.next; } else { this._encapsStack = new WriteEncaps(); } this._encapsStack.setEncoding(this._encoding); } if (this._encapsStack.format === null) { this._encapsStack.format = this._format; } if (!this._encapsStack.encoder) { // Lazy initialization. if (this._encapsStack.encoding_1_0) { this._encapsStack.encoder = new EncapsEncoder10(this, this._encapsStack); } else { this._encapsStack.encoder = new EncapsEncoder11(this, this._encapsStack); } } } get pos() { return this._buf.position; } set pos(value) { this._buf.position = value; } get size() { return this._buf.limit; } get buffer() { return this._buf; } } |