Skip to content

Commit

Permalink
introduce logic to invoke sync void method (#39970)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #39970

Changelog: [Internal]

in this diff, i add the logic that makes void return values run synchronously

Reviewed By: javache

Differential Revision: D49613770

fbshipit-source-id: ef840fb3ee130430505d000a7cf74e094f9d1405
  • Loading branch information
philIip authored and facebook-github-bot committed Oct 11, 2023
1 parent 4e92f60 commit b41a2f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ TurboModuleManager::createTurboModuleProvider() {
.moduleName = name,
.instance = moduleInstance,
.jsInvoker = jsCallInvoker,
.nativeMethodCallInvoker = nativeMethodCallInvoker};
.nativeMethodCallInvoker = nativeMethodCallInvoker,
.shouldVoidMethodsExecuteSync = false};

auto turboModule = delegate->cthis()->getTurboModule(name, params);
turboModuleCache->insert({name, turboModule});
Expand Down Expand Up @@ -270,7 +271,8 @@ TurboModuleManager::createLegacyModuleProvider() {
.moduleName = name,
.instance = moduleInstance,
.jsInvoker = jsCallInvoker,
.nativeMethodCallInvoker = nativeMethodCallInvoker};
.nativeMethodCallInvoker = nativeMethodCallInvoker,
.shouldVoidMethodsExecuteSync = false};

static auto getMethodDescriptorsFromModule =
javaPart->getClass()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace TMPL = TurboModulePerfLogger;
JavaTurboModule::JavaTurboModule(const InitParams& params)
: TurboModule(params.moduleName, params.jsInvoker),
instance_(jni::make_global(params.instance)),
nativeMethodCallInvoker_(params.nativeMethodCallInvoker) {}
nativeMethodCallInvoker_(params.nativeMethodCallInvoker),
shouldVoidMethodsExecuteSync_(params.shouldVoidMethodsExecuteSync) {}

JavaTurboModule::~JavaTurboModule() {
/**
Expand Down Expand Up @@ -454,7 +455,9 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
const char* methodName = methodNameStr.c_str();
const char* moduleName = name_.c_str();

bool isMethodSync = !(valueKind == VoidKind || valueKind == PromiseKind);
bool isMethodSync =
(valueKind == VoidKind && shouldVoidMethodsExecuteSync_) ||
!(valueKind == VoidKind || valueKind == PromiseKind);

if (isMethodSync) {
TMPL::syncMethodCallStart(moduleName, methodName);
Expand Down Expand Up @@ -733,6 +736,18 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
return returnValue;
}
case VoidKind: {
if (shouldVoidMethodsExecuteSync_) {
env->CallVoidMethodA(instance, methodID, jargs.data());
TMPL::syncMethodCallExecutionEnd(moduleName, methodName);
TMPL::syncMethodCallEnd(moduleName, methodName);
try {
FACEBOOK_JNI_THROW_PENDING_EXCEPTION();
} catch (...) {
throw;
}
return jsi::Value::undefined();
}

TMPL::asyncMethodCallArgConversionEnd(moduleName, methodName);
TMPL::asyncMethodCallDispatch(moduleName, methodName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class JSI_EXPORT JavaTurboModule : public TurboModule {
jni::alias_ref<jobject> instance;
std::shared_ptr<CallInvoker> jsInvoker;
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker;
bool shouldVoidMethodsExecuteSync;
};

JavaTurboModule(const InitParams& params);
Expand All @@ -50,6 +51,7 @@ class JSI_EXPORT JavaTurboModule : public TurboModule {
// instance_ can be of type JTurboModule, or JNativeModule
jni::global_ref<jobject> instance_;
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker_;
bool shouldVoidMethodsExecuteSync_;
};

} // namespace facebook::react

0 comments on commit b41a2f9

Please sign in to comment.