Skip to content

Commit a8414a4

Browse files
srawlinsCommit Queue
authored andcommitted
vm_service: Use less dynamic, both explicit and implicit
Change-Id: Idde221b094fd23ded8e6d77d47f8ce73ab2549c3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437022 Reviewed-by: Ben Konyi <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent c537260 commit a8414a4

File tree

3 files changed

+49
-53
lines changed

3 files changed

+49
-53
lines changed

pkg/vm_service/lib/src/vm_service.dart

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ String decodeBase64(String str) => utf8.decode(base64.decode(str));
4040
bool _isNullInstance(Map json) =>
4141
((json['type'] == '@Instance') && (json['kind'] == 'Null'));
4242

43-
Object? createServiceObject(dynamic json, List<String> expectedTypes) {
43+
Object? createServiceObject(Object? json, List<String> expectedTypes) {
4444
if (json == null) return null;
4545

4646
if (json is List) {
@@ -75,7 +75,7 @@ Object? createServiceObject(dynamic json, List<String> expectedTypes) {
7575
}
7676

7777
dynamic _createSpecificObject(
78-
dynamic json, dynamic Function(Map<String, dynamic> map) creator) {
78+
Object? json, dynamic Function(Map<String, dynamic> map) creator) {
7979
if (json == null) return null;
8080

8181
if (json is List) {
@@ -1981,8 +1981,8 @@ class VmService {
19811981
_services[service] = cb;
19821982
}
19831983

1984-
void _processMessage(dynamic message) {
1985-
// Expect a String, an int[], or a ByteData.
1984+
void _processMessage(Object? message) {
1985+
// Expect a String, a List<int>, or a ByteData.
19861986
if (message is String) {
19871987
_processMessageStr(message);
19881988
} else if (message is Uint8List) {
@@ -2005,7 +2005,7 @@ class VmService {
20052005
final dataLength = bytes.lengthInBytes - dataOffset;
20062006
final decoder = (const Utf8Decoder()).fuse(const JsonDecoder());
20072007
final map = decoder.convert(Uint8List.view(
2008-
bytes.buffer, bytes.offsetInBytes + metaOffset, metaLength)) as dynamic;
2008+
bytes.buffer, bytes.offsetInBytes + metaOffset, metaLength)) as Map;
20092009
final data = ByteData.view(
20102010
bytes.buffer, bytes.offsetInBytes + dataOffset, dataLength);
20112011
if (map['method'] == 'streamNotify') {
@@ -2043,8 +2043,8 @@ class VmService {
20432043
final request = _outstandingRequests.remove(json['id']);
20442044
if (request == null) {
20452045
_log.severe('unmatched request response: ${jsonEncode(json)}');
2046-
} else if (json['error'] != null) {
2047-
request.completeError(RPCError.parse(request.method, json['error']));
2046+
} else if (json['error'] case Map error) {
2047+
request.completeError(RPCError.parse(request.method, error));
20482048
} else {
20492049
final result = json['result'] as Map<String, dynamic>;
20502050
final type = result['type'];
@@ -2059,7 +2059,7 @@ class VmService {
20592059
}
20602060
}
20612061

2062-
Future _processRequest(Map<String, dynamic> json) async {
2062+
Future<void> _processRequest(Map<String, dynamic> json) async {
20632063
final result = await _routeRequest(
20642064
json['method'], json['params'] ?? <String, dynamic>{});
20652065
if (_disposed) {
@@ -2068,12 +2068,12 @@ class VmService {
20682068
}
20692069
result['id'] = json['id'];
20702070
result['jsonrpc'] = '2.0';
2071-
String message = jsonEncode(result);
2071+
final message = jsonEncode(result);
20722072
_onSend.add(message);
20732073
_writeMessage(message);
20742074
}
20752075

2076-
Future _processNotification(Map<String, dynamic> json) async {
2076+
Future<void> _processNotification(Map<String, dynamic> json) async {
20772077
final method = json['method'];
20782078
final params = json['params'] ?? <String, dynamic>{};
20792079
if (method == 'streamNotify') {
@@ -2096,7 +2096,7 @@ class VmService {
20962096
try {
20972097
return await service(params);
20982098
} catch (e, st) {
2099-
RPCError error = RPCError.withDetails(
2099+
final error = RPCError.withDetails(
21002100
method,
21012101
RPCErrorKind.kServerError.code,
21022102
'$e',
@@ -2107,7 +2107,7 @@ class VmService {
21072107
}
21082108
}
21092109

2110-
typedef DisposeHandler = Future Function();
2110+
typedef DisposeHandler = Future<void> Function();
21112111

21122112
// These error codes must be kept in sync with those in vm/json_stream.h and
21132113
// vmservice.dart.
@@ -2214,7 +2214,7 @@ enum RPCErrorKind {
22142214
}
22152215

22162216
class RPCError implements Exception {
2217-
static RPCError parse(String callingMethod, dynamic json) {
2217+
static RPCError parse(String callingMethod, Map json) {
22182218
return RPCError(callingMethod, json['code'], json['message'], json['data']);
22192219
}
22202220

@@ -2734,10 +2734,9 @@ class BoundField {
27342734
BoundField._fromJson(Map<String, dynamic> json)
27352735
: decl =
27362736
createServiceObject(json['decl'], const ['FieldRef']) as FieldRef?,
2737-
name = createServiceObject(json['name'], const ['String', 'int'])
2738-
as dynamic,
2737+
name = createServiceObject(json['name'], const ['String', 'int']),
27392738
value = createServiceObject(
2740-
json['value'], const ['InstanceRef', 'Sentinel']) as dynamic;
2739+
json['value'], const ['InstanceRef', 'Sentinel']);
27412740

27422741
Map<String, dynamic> toJson() => <String, Object?>{
27432742
'decl': decl?.toJson(),
@@ -2786,7 +2785,7 @@ class BoundVariable extends Response {
27862785
BoundVariable._fromJson(super.json)
27872786
: name = json['name'] ?? '',
27882787
value = createServiceObject(json['value'],
2789-
const ['InstanceRef', 'TypeArgumentsRef', 'Sentinel']) as dynamic,
2788+
const ['InstanceRef', 'TypeArgumentsRef', 'Sentinel']),
27902789
declarationTokenPos = json['declarationTokenPos'] ?? -1,
27912790
scopeStartTokenPos = json['scopeStartTokenPos'] ?? -1,
27922791
scopeEndTokenPos = json['scopeEndTokenPos'] ?? -1,
@@ -2857,7 +2856,7 @@ class Breakpoint extends Obj {
28572856
resolved = json['resolved'] ?? false,
28582857
isSyntheticAsyncContinuation = json['isSyntheticAsyncContinuation'],
28592858
location = createServiceObject(json['location'],
2860-
const ['SourceLocation', 'UnresolvedSourceLocation']) as dynamic,
2859+
const ['SourceLocation', 'UnresolvedSourceLocation']),
28612860
super._fromJson();
28622861

28632862
@override
@@ -3260,7 +3259,7 @@ class CodeRef extends ObjRef {
32603259
: name = json['name'] ?? '',
32613260
kind = json['kind'] ?? '',
32623261
function = createServiceObject(
3263-
json['function'], const ['FuncRef', 'NativeFunction']) as dynamic,
3262+
json['function'], const ['FuncRef', 'NativeFunction']),
32643263
super._fromJson();
32653264

32663265
@override
@@ -3319,7 +3318,7 @@ class Code extends Obj implements CodeRef {
33193318
: name = json['name'] ?? '',
33203319
kind = json['kind'] ?? '',
33213320
function = createServiceObject(
3322-
json['function'], const ['FuncRef', 'NativeFunction']) as dynamic,
3321+
json['function'], const ['FuncRef', 'NativeFunction']),
33233322
super._fromJson();
33243323

33253324
@override
@@ -3453,7 +3452,7 @@ class ContextElement {
34533452

34543453
ContextElement._fromJson(Map<String, dynamic> json)
34553454
: value = createServiceObject(
3456-
json['value'], const ['InstanceRef', 'Sentinel']) as dynamic;
3455+
json['value'], const ['InstanceRef', 'Sentinel']);
34573456

34583457
Map<String, dynamic> toJson() => <String, Object?>{
34593458
'value': value?.toJson(),
@@ -4371,7 +4370,7 @@ class Field extends Obj implements FieldRef {
43714370
createServiceObject(json['location'], const ['SourceLocation'])
43724371
as SourceLocation?,
43734372
staticValue = createServiceObject(
4374-
json['staticValue'], const ['InstanceRef', 'Sentinel']) as dynamic,
4373+
json['staticValue'], const ['InstanceRef', 'Sentinel']),
43754374
super._fromJson();
43764375

43774376
@override
@@ -4606,8 +4605,7 @@ class FuncRef extends ObjRef {
46064605
FuncRef._fromJson(super.json)
46074606
: name = json['name'] ?? '',
46084607
owner = createServiceObject(
4609-
json['owner'], const ['LibraryRef', 'ClassRef', 'FuncRef'])
4610-
as dynamic,
4608+
json['owner'], const ['LibraryRef', 'ClassRef', 'FuncRef']),
46114609
isStatic = json['static'] ?? false,
46124610
isConst = json['const'] ?? false,
46134611
implicit = json['implicit'] ?? false,
@@ -4727,8 +4725,7 @@ class Func extends Obj implements FuncRef {
47274725
Func._fromJson(super.json)
47284726
: name = json['name'] ?? '',
47294727
owner = createServiceObject(
4730-
json['owner'], const ['LibraryRef', 'ClassRef', 'FuncRef'])
4731-
as dynamic,
4728+
json['owner'], const ['LibraryRef', 'ClassRef', 'FuncRef']),
47324729
isStatic = json['static'] ?? false,
47334730
isConst = json['const'] ?? false,
47344731
implicit = json['implicit'] ?? false,
@@ -6171,8 +6168,7 @@ class InboundReference {
61716168
createServiceObject(json['source'], const ['ObjRef']) as ObjRef?,
61726169
parentListIndex = json['parentListIndex'],
61736170
parentField = createServiceObject(
6174-
json['parentField'], const ['FieldRef', 'String', 'int'])
6175-
as dynamic;
6171+
json['parentField'], const ['FieldRef', 'String', 'int']);
61766172

61776173
Map<String, dynamic> toJson() => <String, Object?>{
61786174
'source': source?.toJson(),
@@ -6521,10 +6517,9 @@ class MapAssociation {
65216517

65226518
MapAssociation._fromJson(Map<String, dynamic> json)
65236519
: key =
6524-
createServiceObject(json['key'], const ['InstanceRef', 'Sentinel'])
6525-
as dynamic,
6520+
createServiceObject(json['key'], const ['InstanceRef', 'Sentinel']),
65266521
value = createServiceObject(
6527-
json['value'], const ['InstanceRef', 'Sentinel']) as dynamic;
6522+
json['value'], const ['InstanceRef', 'Sentinel']);
65286523

65296524
Map<String, dynamic> toJson() => <String, Object?>{
65306525
'key': key?.toJson(),
@@ -7179,8 +7174,7 @@ class ProfileFunction {
71797174
inclusiveTicks = json['inclusiveTicks'] ?? -1,
71807175
exclusiveTicks = json['exclusiveTicks'] ?? -1,
71817176
resolvedUrl = json['resolvedUrl'] ?? '',
7182-
function =
7183-
createServiceObject(json['function'], const ['dynamic']) as dynamic;
7177+
function = createServiceObject(json['function'], const ['dynamic']);
71847178

71857179
Map<String, dynamic> toJson() => <String, Object?>{
71867180
'kind': kind ?? '',
@@ -7449,8 +7443,7 @@ class RetainingObject {
74497443
createServiceObject(json['parentMapKey'], const ['ObjRef'])
74507444
as ObjRef?,
74517445
parentField =
7452-
createServiceObject(json['parentField'], const ['String', 'int'])
7453-
as dynamic;
7446+
createServiceObject(json['parentField'], const ['String', 'int']);
74547447

74557448
Map<String, dynamic> toJson() => <String, Object?>{
74567449
'value': value?.toJson(),

pkg/vm_service/tool/dart/generate_dart_client.dart

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
134134
_services[service] = cb;
135135
}
136136
137-
void _processMessage(dynamic message) {
138-
// Expect a String, an int[], or a ByteData.
137+
void _processMessage(Object? message) {
138+
// Expect a String, a List<int>, or a ByteData.
139139
if (message is String) {
140140
_processMessageStr(message);
141141
} else if (message is Uint8List) {
@@ -158,7 +158,7 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
158158
final dataLength = bytes.lengthInBytes - dataOffset;
159159
final decoder = (const Utf8Decoder()).fuse(const JsonDecoder());
160160
final map = decoder.convert(Uint8List.view(
161-
bytes.buffer, bytes.offsetInBytes + metaOffset, metaLength)) as dynamic;
161+
bytes.buffer, bytes.offsetInBytes + metaOffset, metaLength)) as Map;
162162
final data = ByteData.view(
163163
bytes.buffer, bytes.offsetInBytes + dataOffset, dataLength);
164164
if (map['method'] == 'streamNotify') {
@@ -197,8 +197,8 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
197197
final request = _outstandingRequests.remove(json['id']);
198198
if (request == null) {
199199
_log.severe('unmatched request response: ${jsonEncode(json)}');
200-
} else if (json['error'] != null) {
201-
request.completeError(RPCError.parse(request.method, json['error']));
200+
} else if (json['error'] case Map error) {
201+
request.completeError(RPCError.parse(request.method, error));
202202
} else {
203203
final result = json['result'] as Map<String, dynamic>;
204204
final type = result['type'];
@@ -213,25 +213,27 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
213213
}
214214
}
215215
216-
Future _processRequest(Map<String, dynamic> json) async {
217-
final result = await _routeRequest(json['method'], json['params'] ?? <String, dynamic>{});
216+
Future<void> _processRequest(Map<String, dynamic> json) async {
217+
final result = await _routeRequest(
218+
json['method'], json['params'] ?? <String, dynamic>{});
218219
if (_disposed) {
219220
// The service has disappeared. Don't try to send the response.
220221
return;
221222
}
222223
result['id'] = json['id'];
223224
result['jsonrpc'] = '2.0';
224-
String message = jsonEncode(result);
225+
final message = jsonEncode(result);
225226
_onSend.add(message);
226227
_writeMessage(message);
227228
}
228229
229-
Future _processNotification(Map<String, dynamic> json) async {
230+
Future<void> _processNotification(Map<String, dynamic> json) async {
230231
final method = json['method'];
231232
final params = json['params'] ?? <String, dynamic>{};
232233
if (method == 'streamNotify') {
233234
final streamId = params['streamId'];
234-
_getEventController(streamId).add(createServiceObject(params['event'], const ['Event'])! as Event);
235+
_getEventController(streamId)
236+
.add(createServiceObject(params['event'], const ['Event'])! as Event);
235237
} else {
236238
await _routeRequest(method, params);
237239
}
@@ -248,7 +250,7 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
248250
try {
249251
return await service(params);
250252
} catch (e, st) {
251-
RPCError error = RPCError.withDetails(
253+
final error = RPCError.withDetails(
252254
method,
253255
RPCErrorKind.kServerError.code,
254256
'$e',
@@ -261,7 +263,7 @@ export 'snapshot_graph.dart' show HeapSnapshotClass,
261263

262264
static const _rpcError = r'''
263265
264-
typedef DisposeHandler = Future Function();
266+
typedef DisposeHandler = Future<void> Function();
265267
266268
// These error codes must be kept in sync with those in vm/json_stream.h and
267269
// vmservice.dart.
@@ -362,7 +364,7 @@ enum RPCErrorKind {
362364
}
363365
364366
class RPCError implements Exception {
365-
static RPCError parse(String callingMethod, dynamic json) {
367+
static RPCError parse(String callingMethod, Map json) {
366368
return RPCError(callingMethod, json['code'], json['message'], json['data']);
367369
}
368370
@@ -465,7 +467,7 @@ String decodeBase64(String str) => utf8.decode(base64.decode(str));
465467
bool _isNullInstance(Map json) => ((json['type'] == '@Instance') &&
466468
(json['kind'] == 'Null'));
467469
468-
Object? createServiceObject(dynamic json, List<String> expectedTypes) {
470+
Object? createServiceObject(Object? json, List<String> expectedTypes) {
469471
if (json == null) return null;
470472
471473
if (json is List) {
@@ -499,7 +501,7 @@ Object? createServiceObject(dynamic json, List<String> expectedTypes) {
499501
}
500502
501503
dynamic _createSpecificObject(
502-
dynamic json, dynamic Function(Map<String, dynamic> map) creator) {
504+
Object? json, dynamic Function(Map<String, dynamic> map) creator) {
503505
if (json == null) return null;
504506
505507
if (json is List) {

pkg/vm_service/tool/dart/generate_dart_common.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,13 @@ class Type extends Member {
759759
}
760760
}
761761
} else {
762-
String typesList = typeRefListToString(field.type.types);
763-
String nullable = field.type.name != 'dynamic' ? '?' : '';
762+
final typeName = field.type.name;
763+
final typesList = typeRefListToString(field.type.types);
764+
final cast = typeName == 'dynamic' ? '' : 'as $typeName?';
764765
gen.writeln(
765766
'${field.generatableName} = '
766767
"createServiceObject(json['${field.name}'], "
767-
'$typesList) as ${field.type.name}$nullable',
768+
'$typesList) $cast',
768769
);
769770
}
770771

0 commit comments

Comments
 (0)