Skip to content

Commit

Permalink
Stop using RCTConvert to convert between primitive types
Browse files Browse the repository at this point in the history
Summary:
RCTTiming was the only NativeModule that relied on converting `double`s to other `double`s via `RCTConvert`. RCTTiming was made into a regular NativeModule in D18410788, so it's safe to strip out this logic.

Hopefully, this reduces the memory consumption enough to reduce the OOMs reported in T45151932.

Changelog:
[iOS][Removed] - Stop using RCTConvert to convert between primitive types

Reviewed By: fkgozali

Differential Revision: D18506069

fbshipit-source-id: 7316ad86bc84d47fb383735126d5b00e5491b371
  • Loading branch information
RSNara authored and facebook-github-bot committed Nov 18, 2019
1 parent ca3c8b7 commit 8797a5c
Showing 1 changed file with 0 additions and 89 deletions.
89 changes: 0 additions & 89 deletions ReactCommon/turbomodule/core/platform/ios/RCTTurboModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -482,48 +482,6 @@ static RCTResponseSenderBlock convertJSIFunctionToCallback(
if (arg->isBool()) {
bool v = arg->getBool();

/**
* Convert numbers using RCTConvert if possible.
*/
NSString *methodNameNSString = @(methodName.c_str());
NSString *argumentType = getArgumentTypeName(methodNameNSString, i);

if (argumentType != nil) {
NSString *rctConvertMethodName = [NSString stringWithFormat:@"%@:", argumentType];
SEL rctConvertSelector = NSSelectorFromString(rctConvertMethodName);

if ([RCTConvert respondsToSelector:rctConvertSelector]) {
if (objCArgType == @encode(id)) {
id (*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend;
id convertedObjCArg = convert([RCTConvert class], rctConvertSelector, [NSNumber numberWithBool:v]);

[inv setArgument:(void *)&convertedObjCArg atIndex:i + 2];
if (convertedObjCArg) {
[retainedObjectsForInvocation addObject:convertedObjCArg];
}

continue;
}

/**
* This is necessary because RCTConvert could be responsible for converting BOOLs to
* type aliases of BOOL.
*/
if (objCArgType == @encode(BOOL)) {
BOOL (*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend;
BOOL convertedObjCArg = convert([RCTConvert class], rctConvertSelector, [NSNumber numberWithBool:v]);
[inv setArgument:(void *)&convertedObjCArg atIndex:i + 2];

continue;
}

throw std::runtime_error(
"Error when invoking TurboModule method " + name_ + "." + methodName + "(). Could not convert argument " +
std::to_string(i) + " from JS boolean to ObjC type '" + std::to_string(objCArgType[0]) +
"'. Supported types are BOOL (\"" + @encode(BOOL) + "\") and object (\"" + @encode(id) + "\").");
}
}

/**
* JS type checking ensures the Objective C argument here is either a BOOL or NSNumber*.
*/
Expand All @@ -541,53 +499,6 @@ static RCTResponseSenderBlock convertJSIFunctionToCallback(
if (arg->isNumber()) {
double v = arg->getNumber();

/**
* Convert numbers using RCTConvert if possible.
*/
NSString *methodNameNSString = @(methodName.c_str());
NSString *argumentType = getArgumentTypeName(methodNameNSString, i);

if (argumentType != nil) {
NSString *rctConvertMethodName = [NSString stringWithFormat:@"%@:", argumentType];
SEL rctConvertSelector = NSSelectorFromString(rctConvertMethodName);

if ([RCTConvert respondsToSelector:rctConvertSelector]) {
if (objCArgType == @encode(id)) {
id (*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend;
id convertedObjCArg = convert([RCTConvert class], rctConvertSelector, [NSNumber numberWithDouble:v]);

[inv setArgument:(void *)&convertedObjCArg atIndex:i + 2];
if (convertedObjCArg) {
[retainedObjectsForInvocation addObject:convertedObjCArg];
}

continue;
}

/**
* This is necessary because RCTConvert could be responsible for converting doubles to
* type aliases of double. For example:
*
* Consider the following typedef:
* typedef double NSTimeInterval;
*
* RCTConvert will convert our doubles to NSTimeInterval by dividing them by 1000.
*/
if (objCArgType == @encode(double)) {
double (*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend;
double convertedObjCArg = convert([RCTConvert class], rctConvertSelector, [NSNumber numberWithDouble:v]);
[inv setArgument:(void *)&convertedObjCArg atIndex:i + 2];

continue;
}

throw std::runtime_error(
"Error when invoking TurboModule method " + name_ + "." + methodName + "(). Could not convert argument " +
std::to_string(i) + " from JS number to ObjC type '" + std::to_string(objCArgType[0]) +
"'. Supported types are double (\"" + @encode(double) + "\") and object (\"" + @encode(id) + "\").");
}
}

/**
* JS type checking ensures the Objective C argument here is either a double or NSNumber*.
*/
Expand Down

0 comments on commit 8797a5c

Please sign in to comment.