Skip to content

Commit ddaba89

Browse files
authored
PlatformMessageChannel: code cleanup, bugfixes, and unit tests (flutter#8509)
1 parent 69b6bb8 commit ddaba89

File tree

3 files changed

+286
-40
lines changed

3 files changed

+286
-40
lines changed

packages/flutter/lib/src/foundation/serialization.dart

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import 'package:typed_data/typed_buffers.dart' show Uint8Buffer;
1414
/// The byte order of serialized data is [Endianness.BIG_ENDIAN].
1515
/// The byte order of deserialized data is [Endianness.HOST_ENDIAN].
1616
class WriteBuffer {
17-
Uint8Buffer _buffer;
18-
ByteData _eightBytes;
19-
Uint8List _eightBytesAsList;
20-
2117
WriteBuffer() {
2218
_buffer = new Uint8Buffer();
2319
_eightBytes = new ByteData(8);
2420
_eightBytesAsList = _eightBytes.buffer.asUint8List();
2521
}
2622

23+
Uint8Buffer _buffer;
24+
ByteData _eightBytes;
25+
Uint8List _eightBytesAsList;
26+
2727
void putUint8(int byte) {
2828
_buffer.add(byte);
2929
}
@@ -60,9 +60,8 @@ class WriteBuffer {
6060
if (Endianness.HOST_ENDIAN == Endianness.BIG_ENDIAN) {
6161
_buffer.addAll(list.buffer.asUint8List(list.offsetInBytes, 4 * list.length));
6262
} else {
63-
for (final int value in list) {
63+
for (final int value in list)
6464
putInt32(value);
65-
}
6665
}
6766
}
6867

@@ -71,9 +70,8 @@ class WriteBuffer {
7170
if (Endianness.HOST_ENDIAN == Endianness.BIG_ENDIAN) {
7271
_buffer.addAll(list.buffer.asUint8List(list.offsetInBytes, 8 * list.length));
7372
} else {
74-
for (final int value in list) {
73+
for (final int value in list)
7574
putInt64(value);
76-
}
7775
}
7876
}
7977

@@ -82,18 +80,16 @@ class WriteBuffer {
8280
if (Endianness.HOST_ENDIAN == Endianness.BIG_ENDIAN) {
8381
_buffer.addAll(list.buffer.asUint8List(list.offsetInBytes, 8 * list.length));
8482
} else {
85-
for (final double value in list) {
83+
for (final double value in list)
8684
putFloat64(value);
87-
}
8885
}
8986
}
9087

9188
void _alignTo(int alignment) {
9289
final int mod = _buffer.length % alignment;
9390
if (mod != 0) {
94-
for (int i = 0; i < alignment - mod; i++) {
91+
for (int i = 0; i < alignment - mod; i++)
9592
_buffer.add(0);
96-
}
9793
}
9894
}
9995

@@ -152,9 +148,8 @@ class ReadBuffer {
152148
list = data.buffer.asInt32List(data.offsetInBytes + position, length);
153149
} else {
154150
final ByteData invertedData = new ByteData(4 * length);
155-
for (int i = 0; i < length; i++) {
151+
for (int i = 0; i < length; i++)
156152
invertedData.setInt32(i * 4, data.getInt32(position + i * 4, Endianness.HOST_ENDIAN));
157-
}
158153
list = new Int32List.view(invertedData.buffer);
159154
}
160155
position += 4 * length;
@@ -168,9 +163,8 @@ class ReadBuffer {
168163
list = data.buffer.asInt64List(data.offsetInBytes + position, length);
169164
} else {
170165
final ByteData invertedData = new ByteData(8 * length);
171-
for (int i = 0; i < length; i++) {
166+
for (int i = 0; i < length; i++)
172167
invertedData.setInt64(i * 8, data.getInt64(position + i * 8, Endianness.HOST_ENDIAN));
173-
}
174168
list = new Int64List.view(invertedData.buffer);
175169
}
176170
position += 8 * length;
@@ -184,9 +178,8 @@ class ReadBuffer {
184178
list = data.buffer.asFloat64List(data.offsetInBytes + position, length);
185179
} else {
186180
final ByteData invertedData = new ByteData(8 * length);
187-
for (int i = 0; i < length; i++) {
181+
for (int i = 0; i < length; i++)
188182
invertedData.setFloat64(i * 8, data.getFloat64(position + i * 8, Endianness.HOST_ENDIAN));
189-
}
190183
list = new Float64List.view(invertedData.buffer);
191184
}
192185
position += 8 * length;
@@ -195,9 +188,8 @@ class ReadBuffer {
195188

196189
void _alignTo(int alignment) {
197190
final int mod = position % alignment;
198-
if (mod != 0) {
191+
if (mod != 0)
199192
position += alignment - mod;
200-
}
201193
}
202194

203195
bool get hasRemaining => position < data.lengthInBytes;

packages/flutter/lib/src/services/message_codecs.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,13 @@ class StandardMessageCodec implements MessageCodec<dynamic> {
138138
// * The serialization of the value itself follows the type byte.
139139
// * Lengths and sizes of serialized parts are encoded using an expanding
140140
// format optimized for the common case of small non-negative integers:
141-
// * values 0..<254 using one byte with that value;
142-
// * values 254..<2^16 using three bytes, the first of which is 254, the
143-
// next two the usual big-endian unsigned representation of the value;
144-
// * values 2^16..<2^32 using five bytes, the first of which is 255, the
145-
// next four the usual big-endian unsigned representation of the value.
141+
// * values 0..253 inclusive using one byte with that value;
142+
// * values 254..2^16 inclusive using three bytes, the first of which is
143+
// 254, the next two the usual big-endian unsigned representation of the
144+
// value;
145+
// * values 2^16+1..2^32 inclusive using five bytes, the first of which is
146+
// 255, the next four the usual big-endian unsigned representation of the
147+
// value.
146148
// * null, true, and false have empty serialization; they are encoded directly
147149
// in the type byte (using _kNull, _kTrue, _kFalse)
148150
// * Integers representable in 32 bits are encoded using 4 bytes big-endian,
@@ -208,19 +210,19 @@ class StandardMessageCodec implements MessageCodec<dynamic> {
208210
}
209211

210212
static void _writeSize(WriteBuffer buffer, int value) {
211-
assert(0 <= value && value < 0xffffffff);
213+
assert(0 <= value && value <= 0xffffffff);
212214
if (value < 254) {
213215
buffer.putUint8(value);
214-
} else if (value < 0xffff) {
216+
} else if (value <= 0xffff) {
215217
buffer.putUint8(254);
216218
buffer.putUint8(value >> 8);
217-
buffer.putUint8(value & 0xff);
219+
buffer.putUint8(value);
218220
} else {
219221
buffer.putUint8(255);
220222
buffer.putUint8(value >> 24);
221-
buffer.putUint8((value >> 16) & 0xff);
222-
buffer.putUint8((value >> 8) & 0xff);
223-
buffer.putUint8(value & 0xff);
223+
buffer.putUint8(value >> 16);
224+
buffer.putUint8(value >> 8);
225+
buffer.putUint8(value);
224226
}
225227
}
226228

@@ -230,15 +232,13 @@ class StandardMessageCodec implements MessageCodec<dynamic> {
230232
} else if (value is bool) {
231233
buffer.putUint8(value ? _kTrue : _kFalse);
232234
} else if (value is int) {
233-
if (-0x7fffffff <= value && value < 0x7fffffff) {
235+
if (-0x7fffffff - 1 <= value && value <= 0x7fffffff) {
234236
buffer.putUint8(_kInt32);
235237
buffer.putInt32(value);
236-
}
237-
else if (-0x7fffffffffffffff <= value && value < 0x7fffffffffffffff) {
238+
} else if (-0x7fffffffffffffff - 1 <= value && value <= 0x7fffffffffffffff) {
238239
buffer.putUint8(_kInt64);
239240
buffer.putInt64(value);
240-
}
241-
else {
241+
} else {
242242
buffer.putUint8(_kLargeInt);
243243
final List<int> hex = UTF8.encoder.convert(value.toRadixString(16));
244244
_writeSize(buffer, hex.length);
@@ -292,12 +292,12 @@ class StandardMessageCodec implements MessageCodec<dynamic> {
292292
return value;
293293
} else if (value == 254) {
294294
return (buffer.getUint8() << 8)
295-
| buffer.getUint8();
295+
| buffer.getUint8();
296296
} else {
297297
return (buffer.getUint8() << 24)
298-
| (buffer.getUint8() << 16)
299-
| (buffer.getUint8() << 8)
300-
| buffer.getUint8();
298+
| (buffer.getUint8() << 16)
299+
| (buffer.getUint8() << 8)
300+
| buffer.getUint8();
301301
}
302302
}
303303

0 commit comments

Comments
 (0)