| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.src.summary.flat_buffers; | 5 library analyzer.src.summary.flat_buffers; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:math'; | 9 import 'dart:math'; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 class ListReader<E> extends Reader<List<E>> { | 512 class ListReader<E> extends Reader<List<E>> { |
| 513 final Reader<E> _elementReader; | 513 final Reader<E> _elementReader; |
| 514 | 514 |
| 515 const ListReader(this._elementReader); | 515 const ListReader(this._elementReader); |
| 516 | 516 |
| 517 @override | 517 @override |
| 518 int get size => 4; | 518 int get size => 4; |
| 519 | 519 |
| 520 @override | 520 @override |
| 521 List<E> read(BufferPointer bp) => | 521 List<E> read(BufferPointer bp) => |
| 522 new _FbInt32List<E>(_elementReader, bp.derefObject()); | 522 new _FbGenericList<E>(_elementReader, bp.derefObject()); |
| 523 } | 523 } |
| 524 | 524 |
| 525 /** | 525 /** |
| 526 * The offset from the end of the buffer to a serialized object of the type [T]. | 526 * The offset from the end of the buffer to a serialized object of the type [T]. |
| 527 */ | 527 */ |
| 528 class Offset<T> { | 528 class Offset<T> { |
| 529 final int _tail; | 529 final int _tail; |
| 530 | 530 |
| 531 Offset(this._tail); | 531 Offset(this._tail); |
| 532 } | 532 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 T createObject(BufferPointer bp); | 599 T createObject(BufferPointer bp); |
| 600 | 600 |
| 601 @override | 601 @override |
| 602 T read(BufferPointer bp) { | 602 T read(BufferPointer bp) { |
| 603 bp = bp.derefObject(); | 603 bp = bp.derefObject(); |
| 604 return createObject(bp); | 604 return createObject(bp); |
| 605 } | 605 } |
| 606 } | 606 } |
| 607 | 607 |
| 608 /** | 608 /** |
| 609 * Reader of lists of 32-bit float values. |
| 610 * |
| 611 * The returned unmodifiable lists lazily read values on access. |
| 612 */ |
| 613 class Uint32ListReader extends Reader<List<int>> { |
| 614 const Uint32ListReader(); |
| 615 |
| 616 @override |
| 617 int get size => 4; |
| 618 |
| 619 @override |
| 620 List<int> read(BufferPointer bp) => new _FbUint32List(bp.derefObject()); |
| 621 } |
| 622 |
| 623 /** |
| 609 * The reader of unsigned 32-bit integers. | 624 * The reader of unsigned 32-bit integers. |
| 610 */ | 625 */ |
| 611 class Uint32Reader extends Reader<int> { | 626 class Uint32Reader extends Reader<int> { |
| 612 const Uint32Reader() : super(); | 627 const Uint32Reader() : super(); |
| 613 | 628 |
| 614 @override | 629 @override |
| 615 int get size => 4; | 630 int get size => 4; |
| 616 | 631 |
| 617 @override | 632 @override |
| 618 int read(BufferPointer bp) => bp._getUint32(); | 633 int read(BufferPointer bp) => bp._getUint32(); |
| 619 } | 634 } |
| 620 | 635 |
| 621 /** | 636 /** |
| 622 * The list backed by 64-bit values - Uint64 length and Float64. | 637 * The list backed by 64-bit values - Uint64 length and Float64. |
| 623 */ | 638 */ |
| 624 class _FbFloat64List extends _FbList<double> { | 639 class _FbFloat64List extends _FbList<double> { |
| 625 final BufferPointer bp; | |
| 626 | |
| 627 int _length; | |
| 628 List<double> _items; | 640 List<double> _items; |
| 629 | 641 |
| 630 _FbFloat64List(this.bp); | 642 _FbFloat64List(BufferPointer bp) : super(bp); |
| 631 | |
| 632 @override | |
| 633 int get length { | |
| 634 _length ??= bp._getUint32(); | |
| 635 return _length; | |
| 636 } | |
| 637 | 643 |
| 638 @override | 644 @override |
| 639 double operator [](int i) { | 645 double operator [](int i) { |
| 640 _items ??= new List<double>(length); | 646 _items ??= new List<double>(length); |
| 641 double item = _items[i]; | 647 double item = _items[i]; |
| 642 if (item == null) { | 648 if (item == null) { |
| 643 BufferPointer ref = bp._advance(8 + 8 * i); | 649 BufferPointer ref = bp._advance(8 + 8 * i); |
| 644 item = ref._getFloat64(); | 650 item = ref._getFloat64(); |
| 645 _items[i] = item; | 651 _items[i] = item; |
| 646 } | 652 } |
| 647 return item; | 653 return item; |
| 648 } | 654 } |
| 649 } | 655 } |
| 650 | 656 |
| 651 /** | 657 /** |
| 652 * The list backed by 32-bit values - offsets or integers. | 658 * List backed by a generic object which may have any size. |
| 653 */ | 659 */ |
| 654 class _FbInt32List<E> extends _FbList<E> { | 660 class _FbGenericList<E> extends _FbList<E> { |
| 655 final Reader<E> elementReader; | 661 final Reader<E> elementReader; |
| 656 final BufferPointer bp; | |
| 657 | 662 |
| 658 int _length; | |
| 659 List<E> _items; | 663 List<E> _items; |
| 660 | 664 |
| 661 _FbInt32List(this.elementReader, this.bp); | 665 _FbGenericList(this.elementReader, BufferPointer bp) : super(bp); |
| 662 | |
| 663 @override | |
| 664 int get length { | |
| 665 _length ??= bp._getUint32(); | |
| 666 return _length; | |
| 667 } | |
| 668 | 666 |
| 669 @override | 667 @override |
| 670 E operator [](int i) { | 668 E operator [](int i) { |
| 671 _items ??= new List<E>(length); | 669 _items ??= new List<E>(length); |
| 672 E item = _items[i]; | 670 E item = _items[i]; |
| 673 if (item == null) { | 671 if (item == null) { |
| 674 BufferPointer ref = bp._advance(4 + 4 * i); | 672 BufferPointer ref = bp._advance(4 + elementReader.size * i); |
| 675 item = elementReader.read(ref); | 673 item = elementReader.read(ref); |
| 676 _items[i] = item; | 674 _items[i] = item; |
| 677 } | 675 } |
| 678 return item; | 676 return item; |
| 679 } | 677 } |
| 680 } | 678 } |
| 681 | 679 |
| 682 /** | 680 /** |
| 683 * The base class for immutable lists read from flat buffers. | 681 * The base class for immutable lists read from flat buffers. |
| 684 */ | 682 */ |
| 685 abstract class _FbList<E> extends Object with ListMixin<E> implements List<E> { | 683 abstract class _FbList<E> extends Object with ListMixin<E> implements List<E> { |
| 684 final BufferPointer bp; |
| 685 int _length; |
| 686 |
| 687 _FbList(this.bp); |
| 688 |
| 689 @override |
| 690 int get length { |
| 691 _length ??= bp._getUint32(); |
| 692 return _length; |
| 693 } |
| 694 |
| 686 @override | 695 @override |
| 687 void set length(int i) => | 696 void set length(int i) => |
| 688 throw new StateError('Attempt to modify immutable list'); | 697 throw new StateError('Attempt to modify immutable list'); |
| 689 | 698 |
| 690 @override | 699 @override |
| 691 void operator []=(int i, E e) => | 700 void operator []=(int i, E e) => |
| 692 throw new StateError('Attempt to modify immutable list'); | 701 throw new StateError('Attempt to modify immutable list'); |
| 693 } | 702 } |
| 694 | 703 |
| 695 /** | 704 /** |
| 705 * List backed by 32-bit unsigned integers. |
| 706 */ |
| 707 class _FbUint32List extends _FbList<int> { |
| 708 List<int> _items; |
| 709 |
| 710 _FbUint32List(BufferPointer bp) : super(bp); |
| 711 |
| 712 @override |
| 713 int operator [](int i) { |
| 714 _items ??= new List<int>(length); |
| 715 int item = _items[i]; |
| 716 if (item == null) { |
| 717 item = bp._getUint32(4 + 4 * i); |
| 718 _items[i] = item; |
| 719 } |
| 720 return item; |
| 721 } |
| 722 } |
| 723 |
| 724 /** |
| 696 * Class that describes the structure of a table. | 725 * Class that describes the structure of a table. |
| 697 */ | 726 */ |
| 698 class _VTable { | 727 class _VTable { |
| 699 final List<int> fieldTails = <int>[]; | 728 final List<int> fieldTails = <int>[]; |
| 700 final List<int> fieldOffsets = <int>[]; | 729 final List<int> fieldOffsets = <int>[]; |
| 701 | 730 |
| 702 /** | 731 /** |
| 703 * The size of the table that uses this VTable. | 732 * The size of the table that uses this VTable. |
| 704 */ | 733 */ |
| 705 int tableSize; | 734 int tableSize; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 // Table size. | 788 // Table size. |
| 760 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN); | 789 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN); |
| 761 bufOffset += 2; | 790 bufOffset += 2; |
| 762 // Field offsets. | 791 // Field offsets. |
| 763 for (int fieldOffset in fieldOffsets) { | 792 for (int fieldOffset in fieldOffsets) { |
| 764 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN); | 793 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN); |
| 765 bufOffset += 2; | 794 bufOffset += 2; |
| 766 } | 795 } |
| 767 } | 796 } |
| 768 } | 797 } |
| OLD | NEW |