Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce logic to invoke sync void method #39970

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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