Skip to content

Commit

Permalink
switch on return types for invokeObjCMethod instead of cascading if e…
Browse files Browse the repository at this point in the history
…lse (facebook#39591)

Summary:
Pull Request resolved: facebook#39591

Changelog: [Internal]

I'm refactoring this for a couple of reasons:
1) it isolates the void execution path, which we are changing now
2) switch case is safer than if else, in the future if we introduce new return types

Differential Revision: D49521866

fbshipit-source-id: 6934ba2d94211d7d67ace0e4eefeb6268fcdee49
  • Loading branch information
philIip authored and facebook-github-bot committed Sep 21, 2023
1 parent 9892c5a commit 81f662a
Showing 1 changed file with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -721,32 +721,43 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s

jsi::Value returnValue = jsi::Value::undefined();

if (returnType == PromiseKind) {
returnValue = createPromise(
runtime, methodNameStr, ^(RCTPromiseResolveBlock resolveBlock, RCTPromiseRejectBlock rejectBlock) {
RCTPromiseResolveBlock resolveCopy = [resolveBlock copy];
RCTPromiseRejectBlock rejectCopy = [rejectBlock copy];

[inv setArgument:(void *)&resolveCopy atIndex:count + 2];
[inv setArgument:(void *)&rejectCopy atIndex:count + 3];
[retainedObjectsForInvocation addObject:resolveCopy];
[retainedObjectsForInvocation addObject:rejectCopy];
// The return type becomes void in the ObjC side.
performMethodInvocation(runtime, isSyncInvocation, methodName, inv, retainedObjectsForInvocation);
});
} else {
id result =
performMethodInvocation(runtime, isSyncInvocation, methodName, inv, retainedObjectsForInvocation);

if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallReturnConversionStart(moduleName, methodName);
switch (returnType) {
case PromiseKind: {
returnValue = createPromise(
runtime, methodNameStr, ^(RCTPromiseResolveBlock resolveBlock, RCTPromiseRejectBlock rejectBlock) {
RCTPromiseResolveBlock resolveCopy = [resolveBlock copy];
RCTPromiseRejectBlock rejectCopy = [rejectBlock copy];
[inv setArgument:(void *)&resolveCopy atIndex:count + 2];
[inv setArgument:(void *)&rejectCopy atIndex:count + 3];
[retainedObjectsForInvocation addObject:resolveCopy];
[retainedObjectsForInvocation addObject:rejectCopy];
// The return type becomes void in the ObjC side.
performMethodInvocation(runtime, isSyncInvocation, methodName, inv, retainedObjectsForInvocation);
});
break;
}

returnValue = convertReturnIdToJSIValue(runtime, methodName, returnType, result);

if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallReturnConversionEnd(moduleName, methodName);
case VoidKind: {
id result = performMethodInvocation(runtime, isSyncInvocation, methodName, inv, retainedObjectsForInvocation);
if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallReturnConversionStart(moduleName, methodName);
}
returnValue = convertReturnIdToJSIValue(runtime, methodName, returnType, result);
if (isSyncInvocation) {
TurboModulePerfLogger::syncMethodCallReturnConversionEnd(moduleName, methodName);
}
break;
}
case BooleanKind:
case NumberKind:
case StringKind:
case ObjectKind:
case ArrayKind:
case FunctionKind: {
id result = performMethodInvocation(runtime, true, methodName, inv, retainedObjectsForInvocation);
TurboModulePerfLogger::syncMethodCallReturnConversionStart(moduleName, methodName);
returnValue = convertReturnIdToJSIValue(runtime, methodName, returnType, result);
TurboModulePerfLogger::syncMethodCallReturnConversionEnd(moduleName, methodName);
} break;
}

if (isSyncInvocation) {
Expand Down

0 comments on commit 81f662a

Please sign in to comment.