Skip to content

Commit 843890c

Browse files
author
Anna Gringauze
authored
Refactor record shape processing out of calculating record bound fields (#2074)
1 parent 7546291 commit 843890c

File tree

2 files changed

+61
-34
lines changed

2 files changed

+61
-34
lines changed

dwds/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 19.0.1-dev
22

33
- Do not show async frame errors on evaluation. - [#2073](https://github.com/dart-lang/webdev/pull/2073)
4+
- Refactor code for presenting record instances. - [#2074](https://github.com/dart-lang/webdev/pull/2074)
45

56
## 19.0.0
67

dwds/lib/src/debugging/instance.dart

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -446,52 +446,32 @@ class InstanceHelper extends Domain {
446446
.where((p) => p.name != null && int.tryParse(p.name!) != null)
447447
.toList();
448448

449-
/// The fields for a Dart Record.
449+
/// The field names for a Dart record shape.
450450
///
451451
/// Returns a range of [count] fields, if available, starting from
452452
/// the [offset].
453453
///
454454
/// If [offset] is `null`, assumes 0 offset.
455455
/// If [count] is `null`, return all fields starting from the offset.
456-
Future<List<BoundField>> _recordFields(
457-
RemoteObject record, {
456+
/// The [shape] object describes the shape using `positionalCount`
457+
/// and `named` fields.
458+
///
459+
/// Returns list of field names for the record shape.
460+
Future<List<dynamic>> _recordShapeFields(
461+
RemoteObject shape, {
458462
int? offset,
459463
int? count,
460464
}) async {
461-
// We do this in in awkward way because we want the keys and values, but we
462-
// can't return things by value or some Dart objects will come back as
463-
// values that we need to be RemoteObject, e.g. a List of int.
464-
final expression = '''
465-
function() {
466-
var sdkUtils = ${globalLoadStrategy.loadModuleSnippet}('dart_sdk').dart;
467-
var shape = sdkUtils.dloadRepl(this, "shape");
468-
var positionalCount = sdkUtils.dloadRepl(shape, "positionals");
469-
var named = sdkUtils.dloadRepl(shape, "named");
470-
named = named == null? null: sdkUtils.dsendRepl(named, "toList", []);
471-
var values = sdkUtils.dloadRepl(this, "values");
472-
values = sdkUtils.dsendRepl(values, "toList", []);
473-
474-
return {
475-
positionalCount: positionalCount,
476-
named: named,
477-
values: values
478-
};
479-
}
480-
''';
481-
final result = await inspector.jsCallFunctionOn(record, expression, []);
482465
final positionalCountObject =
483-
await inspector.loadField(result, 'positionalCount');
466+
await inspector.loadField(shape, 'positionalCount');
484467
if (positionalCountObject == null || positionalCountObject.value is! int) {
485468
_logger.warning(
486469
'Unexpected positional count from record: $positionalCountObject',
487470
);
488471
return [];
489472
}
490473

491-
final namedObject = await inspector.loadField(result, 'named');
492-
final valuesObject = await inspector.loadField(result, 'values');
493-
494-
// Collect positional fields in the requested range.
474+
final namedObject = await inspector.loadField(shape, 'named');
495475
final positionalCount = positionalCountObject.value as int;
496476
final positionalOffset = offset ?? 0;
497477
final positionalAvailable =
@@ -519,11 +499,49 @@ class InstanceHelper extends Domain {
519499
final namedElements =
520500
namedInstance?.elements?.map((e) => e.valueAsString) ?? [];
521501

522-
final fieldNameElements = [
502+
return [
523503
...positionalElements,
524504
...namedElements,
525505
];
506+
}
507+
508+
/// The fields for a Dart Record.
509+
///
510+
/// Returns a range of [count] fields, if available, starting from
511+
/// the [offset].
512+
///
513+
/// If [offset] is `null`, assumes 0 offset.
514+
/// If [count] is `null`, return all fields starting from the offset.
515+
Future<List<BoundField>> _recordFields(
516+
RemoteObject record, {
517+
int? offset,
518+
int? count,
519+
}) async {
520+
// We do this in in awkward way because we want the keys and values, but we
521+
// can't return things by value or some Dart objects will come back as
522+
// values that we need to be RemoteObject, e.g. a List of int.
523+
final expression = '''
524+
function() {
525+
var sdkUtils = ${globalLoadStrategy.loadModuleSnippet}('dart_sdk').dart;
526+
var shape = sdkUtils.dloadRepl(this, "shape");
527+
var positionalCount = sdkUtils.dloadRepl(shape, "positionals");
528+
var named = sdkUtils.dloadRepl(shape, "named");
529+
named = named == null? null: sdkUtils.dsendRepl(named, "toList", []);
530+
var values = sdkUtils.dloadRepl(this, "values");
531+
values = sdkUtils.dsendRepl(values, "toList", []);
532+
533+
return {
534+
positionalCount: positionalCount,
535+
named: named,
536+
values: values
537+
};
538+
}
539+
''';
540+
final result = await inspector.jsCallFunctionOn(record, expression, []);
541+
final fieldNameElements =
542+
await _recordShapeFields(result, offset: offset, count: count);
526543

544+
final valuesObject = await inspector.loadField(result, 'values');
527545
final valuesInstance =
528546
await instanceFor(valuesObject, offset: offset, count: count);
529547
final valueElements = valuesInstance?.elements ?? [];
@@ -533,11 +551,19 @@ class InstanceHelper extends Domain {
533551
return [];
534552
}
535553

536-
final fields = <BoundField>[];
537-
Map.fromIterables(fieldNameElements, valueElements).forEach((key, value) {
538-
fields.add(BoundField(name: key, value: value));
554+
return _elementsToBoundFields(fieldNameElements, valueElements);
555+
}
556+
557+
/// Create a list of `BoundField`s from field [names] and [values].
558+
static List<BoundField> _elementsToBoundFields(
559+
List<dynamic> names,
560+
List<dynamic> values,
561+
) {
562+
final boundFields = <BoundField>[];
563+
Map.fromIterables(names, values).forEach((name, value) {
564+
boundFields.add(BoundField(name: name, value: value));
539565
});
540-
return fields;
566+
return boundFields;
541567
}
542568

543569
static int _remainingCount(int collected, int requested) {

0 commit comments

Comments
 (0)