diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h index c3dc8a82fce940..08feeb6ffef12e 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h @@ -142,6 +142,11 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule { const char *methodName, NSInvocation *inv, NSMutableArray *retainedObjectsForInvocation); + void performVoidMethodInvocation( + jsi::Runtime &runtime, + const char *methodName, + NSInvocation *inv, + NSMutableArray *retainedObjectsForInvocation); using PromiseInvocationBlock = void (^)(RCTPromiseResolveBlock resolveWrapper, RCTPromiseRejectBlock rejectWrapper); jsi::Value createPromise(jsi::Runtime &runtime, std::string methodName, PromiseInvocationBlock invoke); diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm index 2d6aa7df8b61a0..8caecfb664b7b2 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm @@ -430,6 +430,55 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s } } +void ObjCTurboModule::performVoidMethodInvocation( + jsi::Runtime &runtime, + const char *methodName, + NSInvocation *inv, + NSMutableArray *retainedObjectsForInvocation) +{ + __weak id weakModule = instance_; + const char *moduleName = name_.c_str(); + std::string methodNameStr{methodName}; + __block int32_t asyncCallCounter = 0; + + void (^block)() = ^{ + id strongModule = weakModule; + if (!strongModule) { + return; + } + + if (shouldVoidMethodsExecuteSync_) { + TurboModulePerfLogger::syncMethodCallExecutionStart(moduleName, methodNameStr.c_str()); + } else { + TurboModulePerfLogger::asyncMethodCallExecutionStart(moduleName, methodNameStr.c_str(), asyncCallCounter); + } + + @try { + [inv invokeWithTarget:strongModule]; + } @catch (NSException *exception) { + throw convertNSExceptionToJSError(runtime, exception); + } @finally { + [retainedObjectsForInvocation removeAllObjects]; + } + + if (shouldVoidMethodsExecuteSync_) { + TurboModulePerfLogger::syncMethodCallExecutionEnd(moduleName, methodNameStr.c_str()); + } else { + TurboModulePerfLogger::asyncMethodCallExecutionEnd(moduleName, methodNameStr.c_str(), asyncCallCounter); + } + + return; + }; + + if (shouldVoidMethodsExecuteSync_) { + nativeMethodCallInvoker_->invokeSync(methodNameStr, [&]() -> void { block(); }); + } else { + asyncCallCounter = getUniqueId(); + TurboModulePerfLogger::asyncMethodCallDispatch(moduleName, methodName); + nativeMethodCallInvoker_->invokeAsync(methodNameStr, [block]() -> void { block(); }); + } +} + jsi::Value ObjCTurboModule::convertReturnIdToJSIValue( jsi::Runtime &runtime, const char *methodName, @@ -741,11 +790,11 @@ id convertJSIValueToObjCObject(jsi::Runtime &runtime, const jsi::Value &value, s break; } case VoidKind: { - id result = performMethodInvocation(runtime, isSyncInvocation, methodName, inv, retainedObjectsForInvocation); + performVoidMethodInvocation(runtime, methodName, inv, retainedObjectsForInvocation); if (isSyncInvocation) { TurboModulePerfLogger::syncMethodCallReturnConversionStart(moduleName, methodName); } - returnValue = convertReturnIdToJSIValue(runtime, methodName, returnType, result); + returnValue = jsi::Value::undefined(); if (isSyncInvocation) { TurboModulePerfLogger::syncMethodCallReturnConversionEnd(moduleName, methodName); }