@@ -4,6 +4,7 @@ import 'dart:convert';
4
4
import 'dart:math' as math;
5
5
import 'dart:typed_data' ;
6
6
7
+ import 'package:collection/collection.dart' ;
7
8
import 'package:meta/meta.dart' ;
8
9
9
10
import '../agent/agent.dart' ;
@@ -1026,16 +1027,19 @@ class RecordClass extends ConstructType<Map> {
1026
1027
1027
1028
class TupleClass <T extends List > extends ConstructType <List > {
1028
1029
TupleClass (List <CType > components) : _components = components {
1029
- _fields = Map .from (_makeMap (components)).entries.toList ();
1030
- _fields.sort (
1031
- (a, b) => idlLabelToId (a.key).toInt () - idlLabelToId (b.key).toInt (),
1032
- );
1030
+ _fields = components.mapIndexed (_makeField).sorted (
1031
+ (a, b) => idlLabelToId (a.key).toInt () - idlLabelToId (b.key).toInt (),
1032
+ );
1033
1033
}
1034
1034
1035
1035
final List <CType > _components;
1036
1036
1037
- List <MapEntry > get fields => _fields;
1038
- late final List <MapEntry > _fields;
1037
+ List <MapEntry <String , CType >> get fields => _fields;
1038
+ late final List <MapEntry <String , CType >> _fields;
1039
+
1040
+ MapEntry <String , CType > _makeField (int index, CType component) {
1041
+ return MapEntry ('_${index }_' , component);
1042
+ }
1039
1043
1040
1044
@override
1041
1045
R accept <D , R >(Visitor <D , R > v, D d) {
@@ -1050,9 +1054,7 @@ class TupleClass<T extends List> extends ConstructType<List> {
1050
1054
// `>=` because tuples can be covariant when encoded.
1051
1055
return x.length >= _fields.length &&
1052
1056
_components
1053
- .asMap ()
1054
- .entries
1055
- .map ((t) => t.value.covariant (x[t.key]) ? 0 : 1 )
1057
+ .mapIndexed ((index, value) => value.covariant (x[index]) ? 0 : 1 )
1056
1058
.reduce ((value, element) => value + element) ==
1057
1059
0 ;
1058
1060
}
@@ -1099,10 +1101,7 @@ class TupleClass<T extends List> extends ConstructType<List> {
1099
1101
);
1100
1102
}
1101
1103
final res = [];
1102
- for (final entry in tuple._components.asMap ().entries) {
1103
- // [i, wireType]
1104
- final i = entry.key;
1105
- final wireType = entry.value;
1104
+ for (final (i, wireType) in tuple._components.indexed) {
1106
1105
if (i >= _components.length) {
1107
1106
// skip value
1108
1107
wireType.decodeValue (x, wireType);
@@ -1134,12 +1133,6 @@ class TupleClass<T extends List> extends ConstructType<List> {
1134
1133
final fields = _fields.map ((entry) => '${entry .key }:${entry .value .name }' );
1135
1134
return "record {${fields .join ('; ' )}}" ;
1136
1135
}
1137
-
1138
- Map <String , dynamic > _makeMap (List <CType > components) {
1139
- return {
1140
- for (final e in components) '_${components .indexOf (e )}_' : e,
1141
- };
1142
- }
1143
1136
}
1144
1137
1145
1138
class VariantClass extends ConstructType <Map <String , dynamic >> {
@@ -1408,7 +1401,7 @@ class FuncClass extends ConstructType<List> {
1408
1401
'Arity mismatch' ,
1409
1402
);
1410
1403
}
1411
- return '(${types .asMap (). entries . map (( e ) => e .value . valueToString (v [e . key ])).join (', ' )})' ;
1404
+ return '(${types .mapIndexed (( i , e ) => e .valueToString (v [i ])).join (', ' )})' ;
1412
1405
}
1413
1406
1414
1407
@override
@@ -1868,22 +1861,21 @@ List idlDecode(List<CType> retTypes, Uint8List bytes) {
1868
1861
}
1869
1862
}
1870
1863
1871
- rawTable. asMap (). forEach (( i, entry ) {
1872
- final t = buildType (entry );
1864
+ for ( final ( i, e) in rawTable.indexed ) {
1865
+ final t = buildType (e );
1873
1866
table[i].fill (t);
1874
- });
1867
+ }
1875
1868
1876
1869
final types = rawTypes.map ((t) => getType (t)).toList ();
1877
1870
1878
- final output = retTypes.asMap ().entries.map ((entry) {
1879
- final result = entry.value.decodeValue (b, types[entry.key]);
1880
- return result;
1881
- }).toList ();
1871
+ final output =
1872
+ retTypes.mapIndexed ((i, e) => e.decodeValue (b, types[i])).toList ();
1882
1873
1883
1874
// Skip unused values.
1884
1875
for (int ind = retTypes.length; ind < types.length; ind++ ) {
1885
1876
types[ind].decodeValue (b, types[ind]);
1886
1877
}
1878
+
1887
1879
if (b.buffer.isNotEmpty) {
1888
1880
throw StateError ('Unexpected left-over bytes.' );
1889
1881
}
0 commit comments