Skip to content
Open
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 @@ -520,21 +520,23 @@ JNIArgs convertJSIArgsToJNIArgs(
return jniArgs;
}

jsi::Value convertFromJMapToValue(JNIEnv* env, jsi::Runtime& rt, jobject arg) {
struct JArguments : public jni::JavaClass<JArguments> {
constexpr static auto kJavaDescriptor =
"Lcom/facebook/react/bridge/Arguments;";

// We currently use Java Argument.makeNativeMap() method to do this conversion
// This could also be done purely in C++, but iterative over map methods
// but those may end up calling reflection methods anyway
// TODO (axe) Investigate the best way to convert Java Map to Value
jclass jArguments = env->FindClass("com/facebook/react/bridge/Arguments");
static jmethodID jMakeNativeMap = env->GetStaticMethodID(
jArguments,
"makeNativeMap",
"(Ljava/util/Map;)Lcom/facebook/react/bridge/WritableNativeMap;");
auto constants = env->CallStaticObjectMethod(jArguments, jMakeNativeMap, arg);
auto jResult = jni::adopt_local(constants);
auto result = jni::static_ref_cast<NativeMap::jhybridobject>(jResult);
return jsi::valueFromDynamic(rt, result->cthis()->consume());
}
static jni::local_ref<WritableNativeMap::jhybridobject> makeNativeMap(
jni::alias_ref<jni::JMap<jstring, jobject>> map) {
static const auto method =
javaClassStatic()
->getStaticMethod<WritableNativeMap::jhybridobject(
jni::alias_ref<jni::JMap<jstring, jobject>>)>("makeNativeMap");
return method(javaClassStatic(), map);
}
};

/**
* Creates JSError with current JS runtime stack and Throwable stack trace.
Expand Down Expand Up @@ -720,15 +722,20 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
TMPL::syncMethodCallArgConversionEnd(moduleName, methodName);
TMPL::syncMethodCallExecutionStart(moduleName, methodName);

auto constantsMap = (jobject)env->CallObjectMethod(instance, methodID);
auto constantsMap = jni::adopt_local(
static_cast<jni::JMap<jstring, jobject>::javaobject>(
env->CallObjectMethod(instance, methodID)));
checkJNIErrorForMethodCall();

TMPL::syncMethodCallExecutionEnd(moduleName, methodName);
TMPL::syncMethodCallReturnConversionStart(moduleName, methodName);

jsi::Value returnValue = constantsMap == nullptr
? jsi::Value::undefined()
: convertFromJMapToValue(env, runtime, constantsMap);
jsi::Value returnValue = jsi::Value::undefined();
if (constantsMap) {
auto nativeMap = JArguments::makeNativeMap(constantsMap);
returnValue =
jsi::valueFromDynamic(runtime, nativeMap->cthis()->consume());
Comment on lines +736 to +737

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance nativeMap would be null?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, Arguments.makeNativeMap has a non-null return type and always returns the WritableNativeMap it creates, even for a null input. If a value can't be converted it throws, and fbjni turns that into a JniException before nativeMap is assigned.

}

TMPL::syncMethodCallReturnConversionEnd(moduleName, methodName);
TMPL::syncMethodCallEnd(moduleName, methodName);
Expand Down
Loading