Skip to content

Commit

Permalink
Sync changes from internal repo. (flutter#86)
Browse files Browse the repository at this point in the history
* Fix type in PbList.fold() for Dart 2.
* Small performance tweaks for DDC.

Fixes #84.
Fixes #85.
  • Loading branch information
jakobr-google authored Feb 22, 2018
1 parent ee41255 commit 7f4032e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const List<String> GeneratedMessage_reservedNames = const [
'clone',
r'$_get',
r'$_getI64',
r'$_getList',
r'$_getN',
r'$_getS',
r'$_has',
Expand Down
12 changes: 10 additions & 2 deletions lib/mixins_meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,23 @@ const List<String> _reservedNamesForMap = const [
'[]',
'[]=',
'addAll',
'addEntries',
'cast',
'containsKey',
'containsValue',
'entries',
'forEach',
'putIfAbsent',
'remove',
'isEmpty',
'isNotEmpty',
'keys',
'length',
'map',
'putIfAbsent',
'remove',
'removeWhere',
'retype',
'update',
'updateAll',
'values',
];

Expand Down
12 changes: 12 additions & 0 deletions lib/src/protobuf/field_info.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,17 @@ class FieldInfo<T> {
return m.createRepeatedField<T>(tagNumber, this);
}

/// Same as above, but allow a tighter typed List to be created.
List<S> _createRepeatedFieldWithType<S extends T>(GeneratedMessage m) {
assert(isRepeated);
return m.createRepeatedField<S>(tagNumber, this);
}

/// Convenience method to thread this FieldInfo's reified type parameter to
/// _FieldSet._ensureRepeatedField.
List<T> _ensureRepeatedField(_FieldSet fs) {
return fs._ensureRepeatedField<T>(this);
}

String toString() => name;
}
29 changes: 24 additions & 5 deletions lib/src/protobuf/field_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ class _FieldSet {
return value;
}

List<T> _getDefaultList<T>(FieldInfo<T> fi) {
assert(fi.isRepeated);
if (_isReadOnly) return const [];

// TODO(skybrian) we could avoid this by generating another
// method for repeated fields:
// msg.mutableFoo().add(123);
var value = fi._createRepeatedFieldWithType<T>(_message);
_setNonExtensionFieldUnchecked(fi, value);
return value;
}

_getFieldOrNullByTag(int tagNumber) {
var fi = _getFieldInfoOrNull(tagNumber);
if (fi == null) return null;
Expand Down Expand Up @@ -256,6 +268,13 @@ class _FieldSet {
return _getDefault(_nonExtensionInfoByIndex(index)) as T;
}

/// The implementation of a generated getter for repeated fields.
List<T> _$getList<T>(int index) {
var value = _values[index];
if (value != null) return value as List<T>;
return _getDefaultList<T>(_nonExtensionInfoByIndex(index));
}

/// The implementation of a generated getter for String fields.
String _$getS(int index, String defaultValue) {
var value = _values[index];
Expand Down Expand Up @@ -520,14 +539,14 @@ class _FieldSet {
if (mustClone) {
// fieldValue must be a PbList of GeneratedMessage.
PbList<GeneratedMessage> pbList = fieldValue;
// Copy the mapped values to a List to avoid redundant cloning (since
// PbList.addAll iterates over its argument twice).
_ensureRepeatedField(fi)
.addAll(new List.from(pbList.map(_cloneMessage)));
var repeatedFields = fi._ensureRepeatedField(this);
for (int i = 0; i < pbList.length; ++i) {
repeatedFields.add(_cloneMessage(pbList[i]));
}
} else {
// fieldValue must be at least a PbList.
PbList pbList = fieldValue;
_ensureRepeatedField(fi).addAll(pbList);
fi._ensureRepeatedField(this).addAll(pbList);
}
return;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/protobuf/generated_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ abstract class GeneratedMessage {
/// For generated code only.
T $_getN<T>(int index) => _fieldSet._$getN<T>(index);

/// For generated code only.
List<T> $_getList<T>(int index) => _fieldSet._$getList<T>(index);

/// For generated code only.
String $_getS(int index, String defaultValue) =>
_fieldSet._$getS(index, defaultValue);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/protobuf/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void _mergeFromJsonMap(

void _appendJsonList(
_FieldSet fs, List jsonList, FieldInfo fi, ExtensionRegistry registry) {
List repeated = fs._ensureRepeatedField(fi);
var repeated = fi._ensureRepeatedField(fs);
// Micro optimization. Using "for in" generates the following and iterator
// alloc:
// for (t1 = J.get$iterator$ax(json), t2 = fi.tagNumber, t3 = fi.type,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/protobuf/pb_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PbList<E> extends ListBase<E> {

/// Reduces a collection to a single value by iteratively combining each
/// element of the collection with an existing value.
T fold<T>(T initialValue, T combine(dynamic previousValue, E element)) =>
T fold<T>(T initialValue, T combine(T previousValue, E element)) =>
_wrappedList.fold(initialValue, combine);

/// Checks whether every element of this iterable satisfies [test].
Expand Down
5 changes: 3 additions & 2 deletions test/reserved_names_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@TestOn("vm")
library reserved_names_test;

import 'package:test/test.dart' show test, expect, equals, fail, TestOn;
import 'package:test/test.dart';

import 'package:protobuf/meta.dart' show GeneratedMessage_reservedNames;
import 'package:protobuf/mixins_meta.dart' show findMixin;
Expand Down Expand Up @@ -52,7 +52,8 @@ void main() {
..addAll(findMemberNames("dart:collection", #MapMixin))
..removeAll(GeneratedMessage_reservedNames);

expect(actual.toList()..sort(), equals(expected.toList()..sort()));
expect(
actual.toList()..sort(), containsAllInOrder(expected.toList()..sort()));
});

test('PbEventMixin reserved names are up to date', () {
Expand Down

0 comments on commit 7f4032e

Please sign in to comment.