Skip to content

Commit 0711622

Browse files
author
Anna Gringauze
committed
Added comstruction and validation of symbol table, addressed CR comments
1 parent 6c0e889 commit 0711622

File tree

2 files changed

+432
-66
lines changed

2 files changed

+432
-66
lines changed

dwds/lib/src/debugging/metadata/module_symbols.dart

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
// Dart debug symbol information stored by DDC.
88
//
99
// The data format below stores descriptions of dart code objects and their
10-
// mapping to JS that is generated by DDC. Every field, except ids, descrbes
10+
// mapping to JS that is generated by DDC. Every field, except ids, describes
1111
// dart.
1212

1313
// Note that 'localId' and 'scopeId' combine into a unique id that is used for
1414
// object lookup and mapping between JS and dart concepts. As a result, it
15-
// needs to be either stored or easly computed for each corresponding JS object
15+
// needs to be either stored or easily computed for each corresponding JS object
1616
// created by DDC, so the debugger is able to look up dart symbol from JS ones.
1717
//
18-
// For example, to detect all dart variables in current scope and display
18+
// For example, to detect all dart variables in the current scope and display
1919
// their values, the debugger can do the following:
2020
//
2121
// - map current JS location to dart location using source maps
@@ -83,6 +83,10 @@ class SemanticVersion {
8383
}
8484
}
8585

86+
/// Base class for a symbol table element.
87+
///
88+
/// Symbol tables and its elements are stored by the compiler using
89+
/// [toJson] method, and are read from json using `fromJson` constructors.
8690
abstract class SymbolTableElement {
8791
Map<String, dynamic> toJson();
8892
}
@@ -168,7 +172,6 @@ class ModuleSymbols implements SymbolTableElement {
168172

169173
_setValueIfNotNull(json, 'version', version);
170174
_setValueIfNotNull(json, 'moduleName', moduleName);
171-
172175
_setObjectListIfNotNull(json, 'libraries', libraries);
173176
_setObjectListIfNotNull(json, 'scripts', scripts);
174177
_setObjectListIfNotNull(json, 'classes', classes);
@@ -195,7 +198,7 @@ class Symbol implements SymbolTableElement {
195198
/// '<scope id>|<js name>'
196199
///
197200
/// Where scope refers to a Library, Class, Function, or Scope.
198-
String get id => '$scopeId|$localId';
201+
String get id => scopeId == null ? localId : '$scopeId|$localId';
199202

200203
/// Source location of the symbol.
201204
SourceLocation location;
@@ -316,8 +319,8 @@ class ClassSymbol extends ScopeSymbol implements TypeSymbol {
316319
/// A list of interface types for this class.
317320
List<String> interfaceIds;
318321

319-
/// Type parameters for this class.
320-
List<String> typeParameters;
322+
/// Mapping of type parameter dart names to JS names.
323+
Map<String, String> typeParameters;
321324

322325
/// Library that contains this class.
323326
String get libraryId => scopeId;
@@ -359,9 +362,8 @@ class ClassSymbol extends ScopeSymbol implements TypeSymbol {
359362
isAbstract = _createValue(json['isAbstract']);
360363
isConst = _createValue(json['isConst']);
361364
superClassId = _createValue(json['superClassId']);
362-
363365
interfaceIds = _createValueList(json['interfaceIds']);
364-
typeParameters = _createValueList(json['typeParameters']);
366+
typeParameters = _createValueMap(json['typeParameters']);
365367
}
366368

367369
@override
@@ -372,7 +374,6 @@ class ClassSymbol extends ScopeSymbol implements TypeSymbol {
372374
_setValueIfNotNull(json, 'isAbstract', isAbstract);
373375
_setValueIfNotNull(json, 'isConst', isConst);
374376
_setValueIfNotNull(json, 'superClassId', superClassId);
375-
376377
_setValueIfNotNull(json, 'interfaceIds', interfaceIds);
377378
_setValueIfNotNull(json, 'typeParameters', typeParameters);
378379

@@ -381,8 +382,8 @@ class ClassSymbol extends ScopeSymbol implements TypeSymbol {
381382
}
382383

383384
class FunctionTypeSymbol extends Symbol implements TypeSymbol {
384-
/// Type parameters for this function.
385-
List<String> typeParameters;
385+
/// Mapping of dart type parameter names to JS names.
386+
Map<String, String> typeParameters;
386387

387388
/// Types for positional parameters for this function.
388389
List<String> parameterTypeIds;
@@ -409,26 +410,23 @@ class FunctionTypeSymbol extends Symbol implements TypeSymbol {
409410

410411
FunctionTypeSymbol.fromJson(Map<String, dynamic> json)
411412
: super.fromJson(json) {
412-
typeParameters = _createValueList(json['typeParameters']);
413413
parameterTypeIds = _createValueList(json['parameterTypeIds']);
414414
optionalParameterTypeIds =
415415
_createValueList(json['optionalParameterTypeIds']);
416-
417-
namedParameterTypeIds = _createObject(json['namedParameterTypeIds'],
418-
(json) => Map<String, String>.from(json));
416+
namedParameterTypeIds = _createValueMap(json['namedParameterTypeIds']);
417+
typeParameters = _createValueMap(json['typeParameters']);
419418
returnTypeId = _createValue(json['returnTypeId']);
420419
}
421420

422421
@override
423422
Map<String, dynamic> toJson() {
424423
final json = super.toJson();
425424

426-
_setValueIfNotNull(json, 'typeParameters', typeParameters);
427425
_setValueIfNotNull(json, 'parameterTypeIds', parameterTypeIds);
428426
_setValueIfNotNull(
429427
json, 'optionalParameterTypeIds', optionalParameterTypeIds);
430-
431428
_setValueIfNotNull(json, 'namedParameterTypeIds', namedParameterTypeIds);
429+
_setValueIfNotNull(json, 'typeParameters', typeParameters);
432430
_setValueIfNotNull(json, 'returnTypeId', returnTypeId);
433431

434432
return json;
@@ -524,17 +522,14 @@ class LibrarySymbol extends ScopeSymbol {
524522
List<String> scopeIds,
525523
}) : super(
526524
localId: uri,
527-
scopeId: null,
528525
variableIds: variableIds,
529526
scopeIds: scopeIds,
530-
location: null,
531527
);
532528

533529
LibrarySymbol.fromJson(Map<String, dynamic> json) : super.fromJson(json) {
534-
name = _createValue(json['name'], ifNull: '');
530+
name = _createValue(json['name']);
535531
uri = _createValue(json['uri']);
536532
scriptIds = _createValueList(json['scriptIds']);
537-
538533
dependencies = _createObjectList(
539534
json['dependencies'], (json) => LibraryDependency.fromJson(json));
540535
}
@@ -544,10 +539,8 @@ class LibrarySymbol extends ScopeSymbol {
544539
final json = super.toJson();
545540

546541
_setValueIfNotNull(json, 'name', name);
547-
_setValueIfNotNull(json, 'id', id);
548542
_setValueIfNotNull(json, 'uri', uri);
549543
_setValueIfNotNull(json, 'scriptIds', scriptIds);
550-
551544
_setObjectListIfNotNull(json, 'dependencies', dependencies);
552545

553546
return json;
@@ -603,24 +596,31 @@ class Script implements SymbolTableElement {
603596
/// This can be just an integer. The mapping from JS to dart script
604597
/// happens using the source map. The id is only used for references
605598
/// in other elements.
606-
String id;
599+
String localId;
600+
601+
String libraryId;
602+
603+
String get id => '$libraryId|$localId';
607604

608605
Script({
609606
this.uri,
610-
this.id,
607+
this.localId,
608+
this.libraryId,
611609
});
612610

613611
Script.fromJson(Map<String, dynamic> json) {
614612
uri = _createValue(json['uri']);
615-
id = _createValue(json['id']);
613+
localId = _createValue(json['localId']);
614+
libraryId = _createValue(json['libraryId']);
616615
}
617616

618617
@override
619618
Map<String, dynamic> toJson() {
620619
final json = <String, dynamic>{};
621620

622621
_setValueIfNotNull(json, 'uri', uri);
623-
_setValueIfNotNull(json, 'id', id);
622+
_setValueIfNotNull(json, 'localId', localId);
623+
_setValueIfNotNull(json, 'libraryId', libraryId);
624624

625625
return json;
626626
}
@@ -697,10 +697,9 @@ class SourceLocation implements SymbolTableElement {
697697
}
698698
}
699699

700-
List<T> _createObjectList<T>(
700+
List<T> _createObjectList<T extends SymbolTableElement>(
701701
dynamic json, T Function(Map<String, dynamic>) creator) {
702-
if (json == null) return null;
703-
702+
if (json == null) return [];
704703
if (json is List) {
705704
return json.map((e) => _createObject(e, creator)).toList();
706705
}
@@ -709,7 +708,6 @@ List<T> _createObjectList<T>(
709708

710709
T _createObject<T>(dynamic json, T Function(Map<String, dynamic>) creator) {
711710
if (json == null) return null;
712-
713711
if (json is Map<String, dynamic>) {
714712
return creator(json);
715713
}
@@ -718,8 +716,7 @@ T _createObject<T>(dynamic json, T Function(Map<String, dynamic>) creator) {
718716

719717
List<T> _createValueList<T>(dynamic json,
720718
{T ifNull, T Function(String) parse}) {
721-
if (json == null) return null;
722-
719+
if (json == null) return [];
723720
if (json is List) {
724721
return json
725722
.map((e) => _createValue<T>(e, ifNull: ifNull, parse: parse))
@@ -728,12 +725,14 @@ List<T> _createValueList<T>(dynamic json,
728725
throw ArgumentError('Not a list: $json');
729726
}
730727

728+
Map<String, T> _createValueMap<T>(dynamic json) {
729+
if (json == null) return {};
730+
return Map<String, T>.from(json as Map<String, dynamic>);
731+
}
732+
731733
T _createValue<T>(dynamic json, {T ifNull, T Function(String) parse}) {
732734
if (json == null) return ifNull;
733-
734-
if (json is T) {
735-
return json;
736-
}
735+
if (json is T) return json;
737736
if (json is String && parse != null) {
738737
return parse(json);
739738
}

0 commit comments

Comments
 (0)