From 5463140ef416809b2ee0eaa3216c40253e9caca6 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Mon, 17 Dec 2018 15:54:57 -0800 Subject: [PATCH] Simplify conversion of numeric types in the message codec on iOS (flutter/engine#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 https://github.com/flutter/flutter/issues/21313 --- .../framework/Source/FlutterStandardCodec.mm | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm index c96887feb7..d7c151bfa7 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterStandardCodec.mm @@ -265,36 +265,37 @@ using namespace shell; if (value == nil || value == [NSNull null]) { [self writeByte:FlutterStandardFieldNil]; } else if ([value isKindOfClass:[NSNumber class]]) { - NSNumber* number = value; - const char* type = [number objCType]; - if ([self isBool:number type:type]) { - BOOL b = number.boolValue; + CFNumberRef number = (CFNumberRef)value; + BOOL success = NO; + if (CFGetTypeID(number) == CFBooleanGetTypeID()) { + BOOL b = CFBooleanGetValue((CFBooleanRef)number); [self writeByte:(b ? FlutterStandardFieldTrue : FlutterStandardFieldFalse)]; - } else if (strcmp(type, @encode(signed int)) == 0 || strcmp(type, @encode(signed short)) == 0 || - strcmp(type, @encode(unsigned short)) == 0 || - strcmp(type, @encode(signed char)) == 0 || - strcmp(type, @encode(unsigned char)) == 0) { - SInt32 n = number.intValue; - [self writeByte:FlutterStandardFieldInt32]; - [self writeBytes:(UInt8*)&n length:4]; - } else if (strcmp(type, @encode(signed long)) == 0 || - strcmp(type, @encode(unsigned int)) == 0) { - SInt64 n = number.longValue; - [self writeByte:FlutterStandardFieldInt64]; - [self writeBytes:(UInt8*)&n length:8]; - } else if (strcmp(type, @encode(double)) == 0 || strcmp(type, @encode(float)) == 0) { - Float64 f = number.doubleValue; - [self writeByte:FlutterStandardFieldFloat64]; - [self writeAlignment:8]; - [self writeBytes:(UInt8*)&f length:8]; - } else if (strcmp(type, @encode(unsigned long)) == 0 || - strcmp(type, @encode(signed long long)) == 0 || - strcmp(type, @encode(unsigned long long)) == 0) { - NSString* hex = [NSString stringWithFormat:@"%llx", number.unsignedLongLongValue]; - [self writeByte:FlutterStandardFieldIntHex]; - [self writeUTF8:hex]; - } else { - NSLog(@"Unsupported value: %@ of type %s", value, type); + success = YES; + } else if (CFNumberIsFloatType(number)) { + Float64 f; + success = CFNumberGetValue(number, kCFNumberFloat64Type, &f); + if (success) { + [self writeByte:FlutterStandardFieldFloat64]; + [self writeAlignment:8]; + [self writeBytes:(UInt8*)&f length:8]; + } + } else if (CFNumberGetByteSize(number) <= 4) { + SInt32 n; + success = CFNumberGetValue(number, kCFNumberSInt32Type, &n); + if (success) { + [self writeByte:FlutterStandardFieldInt32]; + [self writeBytes:(UInt8*)&n length:4]; + } + } else if (CFNumberGetByteSize(number) <= 8) { + SInt64 n; + success = CFNumberGetValue(number, kCFNumberSInt64Type, &n); + if (success) { + [self writeByte:FlutterStandardFieldInt64]; + [self writeBytes:(UInt8*)&n length:8]; + } + } + if (!success) { + NSLog(@"Unsupported value: %@ of number type %ld", value, CFNumberGetType(number)); NSAssert(NO, @"Unsupported value for standard codec"); } } else if ([value isKindOfClass:[NSString class]]) { @@ -329,11 +330,6 @@ using namespace shell; NSAssert(NO, @"Unsupported value for standard codec"); } } - -- (BOOL)isBool:(NSNumber*)number type:(const char*)type { - return strcmp(type, @encode(signed char)) == 0 && - [NSStringFromClass([number class]) isEqual:@"__NSCFBoolean"]; -} @end @implementation FlutterStandardReader { @@ -428,12 +424,12 @@ using namespace shell; case FlutterStandardFieldInt32: { SInt32 value; [self readBytes:&value length:4]; - return [NSNumber numberWithInt:value]; + return @(value); } case FlutterStandardFieldInt64: { SInt64 value; [self readBytes:&value length:8]; - return [NSNumber numberWithLong:value]; + return @(value); } case FlutterStandardFieldFloat64: { Float64 value;