Skip to content

Commit 8190e93

Browse files
Simplify conversion of numeric types in the message codec on iOS (flutter#7097)
Consistently handle Int32/Int64 types on both 32-bit and 64-bit versions of iOS. Drop usage of the obsolete hex string encoding for BigInt types. Fixes flutter#21313
1 parent 08465e8 commit 8190e93

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -265,36 +265,37 @@ - (void)writeValue:(id)value {
265265
if (value == nil || value == [NSNull null]) {
266266
[self writeByte:FlutterStandardFieldNil];
267267
} else if ([value isKindOfClass:[NSNumber class]]) {
268-
NSNumber* number = value;
269-
const char* type = [number objCType];
270-
if ([self isBool:number type:type]) {
271-
BOOL b = number.boolValue;
268+
CFNumberRef number = (CFNumberRef)value;
269+
BOOL success = NO;
270+
if (CFGetTypeID(number) == CFBooleanGetTypeID()) {
271+
BOOL b = CFBooleanGetValue((CFBooleanRef)number);
272272
[self writeByte:(b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)];
273-
} else if (strcmp(type, @encode(signed int)) == 0 || strcmp(type, @encode(signed short)) == 0 ||
274-
strcmp(type, @encode(unsigned short)) == 0 ||
275-
strcmp(type, @encode(signed char)) == 0 ||
276-
strcmp(type, @encode(unsigned char)) == 0) {
277-
SInt32 n = number.intValue;
278-
[self writeByte:FlutterStandardFieldInt32];
279-
[self writeBytes:(UInt8*)&n length:4];
280-
} else if (strcmp(type, @encode(signed long)) == 0 ||
281-
strcmp(type, @encode(unsigned int)) == 0) {
282-
SInt64 n = number.longValue;
283-
[self writeByte:FlutterStandardFieldInt64];
284-
[self writeBytes:(UInt8*)&n length:8];
285-
} else if (strcmp(type, @encode(double)) == 0 || strcmp(type, @encode(float)) == 0) {
286-
Float64 f = number.doubleValue;
287-
[self writeByte:FlutterStandardFieldFloat64];
288-
[self writeAlignment:8];
289-
[self writeBytes:(UInt8*)&f length:8];
290-
} else if (strcmp(type, @encode(unsigned long)) == 0 ||
291-
strcmp(type, @encode(signed long long)) == 0 ||
292-
strcmp(type, @encode(unsigned long long)) == 0) {
293-
NSString* hex = [NSString stringWithFormat:@"%llx", number.unsignedLongLongValue];
294-
[self writeByte:FlutterStandardFieldIntHex];
295-
[self writeUTF8:hex];
296-
} else {
297-
NSLog(@"Unsupported value: %@ of type %s", value, type);
273+
success = YES;
274+
} else if (CFNumberIsFloatType(number)) {
275+
Float64 f;
276+
success = CFNumberGetValue(number, kCFNumberFloat64Type, &f);
277+
if (success) {
278+
[self writeByte:FlutterStandardFieldFloat64];
279+
[self writeAlignment:8];
280+
[self writeBytes:(UInt8*)&f length:8];
281+
}
282+
} else if (CFNumberGetByteSize(number) <= 4) {
283+
SInt32 n;
284+
success = CFNumberGetValue(number, kCFNumberSInt32Type, &n);
285+
if (success) {
286+
[self writeByte:FlutterStandardFieldInt32];
287+
[self writeBytes:(UInt8*)&n length:4];
288+
}
289+
} else if (CFNumberGetByteSize(number) <= 8) {
290+
SInt64 n;
291+
success = CFNumberGetValue(number, kCFNumberSInt64Type, &n);
292+
if (success) {
293+
[self writeByte:FlutterStandardFieldInt64];
294+
[self writeBytes:(UInt8*)&n length:8];
295+
}
296+
}
297+
if (!success) {
298+
NSLog(@"Unsupported value: %@ of number type %ld", value, CFNumberGetType(number));
298299
NSAssert(NO, @"Unsupported value for standard codec");
299300
}
300301
} else if ([value isKindOfClass:[NSString class]]) {
@@ -329,11 +330,6 @@ - (void)writeValue:(id)value {
329330
NSAssert(NO, @"Unsupported value for standard codec");
330331
}
331332
}
332-
333-
- (BOOL)isBool:(NSNumber*)number type:(const char*)type {
334-
return strcmp(type, @encode(signed char)) == 0 &&
335-
[NSStringFromClass([number class]) isEqual:@"__NSCFBoolean"];
336-
}
337333
@end
338334

339335
@implementation FlutterStandardReader {
@@ -428,12 +424,12 @@ - (id)readValueOfType:(UInt8)type {
428424
case FlutterStandardFieldInt32: {
429425
SInt32 value;
430426
[self readBytes:&value length:4];
431-
return [NSNumber numberWithInt:value];
427+
return @(value);
432428
}
433429
case FlutterStandardFieldInt64: {
434430
SInt64 value;
435431
[self readBytes:&value length:8];
436-
return [NSNumber numberWithLong:value];
432+
return @(value);
437433
}
438434
case FlutterStandardFieldFloat64: {
439435
Float64 value;

0 commit comments

Comments
 (0)