Fix cxx codegen handling of optional return types (#36581)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36581

Found that the current codegen did not properly handle a return type of `?bool` because the branch of `constexpr (is_optional_v<T>)` assumed that T was always a JSI value that needed conversion, and `supportsToJs<bool, bool>` is false.

Changelog: [General][Fixed] Issue with TurboModule C++ codegen with optional return types

Reviewed By: christophpurrer

Differential Revision: D44302352

fbshipit-source-id: 0863de06da4e5e3c18f8a1ced7179d76d8e87b99
This commit is contained in:
Pieter De Baets
2023-03-23 03:10:25 -07:00
committed by Facebook GitHub Bot
parent da027dd2fd
commit dd6d57eea1
5 changed files with 13 additions and 7 deletions
@@ -50,7 +50,7 @@ T callFromJs(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...),
jsInvoker);
} else if constexpr (is_optional_v<T>) {
} else if constexpr (is_optional_jsi_v<T>) {
static_assert(
is_optional_v<R>
? supportsToJs<typename R::value_type, typename T::value_type>
@@ -70,10 +70,8 @@ T callFromJs(
}
return convert(rt, std::move(result));
} else {
static_assert(std::is_convertible_v<R, T>, "Incompatible return type");
return (instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);
}
@@ -33,6 +33,14 @@ struct is_optional<std::optional<T>> : std::true_type {};
template <typename T>
inline constexpr bool is_optional_v = is_optional<T>::value;
template <typename T, typename = void>
inline constexpr bool is_optional_jsi_v = false;
template <typename T>
inline constexpr bool
is_optional_jsi_v<T, typename std::enable_if_t<is_optional_v<T>>> =
is_jsi_v<typename T::value_type>;
template <typename T>
struct Converter;
@@ -117,10 +117,10 @@ AsyncPromise<std::string> NativeCxxModuleExample::getValueWithPromise(
return promise;
}
bool NativeCxxModuleExample::getWithWithOptionalArgs(
std::optional<bool> NativeCxxModuleExample::getWithWithOptionalArgs(
jsi::Runtime &rt,
std::optional<bool> optionalArg) {
return optionalArg.value_or(false);
return optionalArg;
}
void NativeCxxModuleExample::voidFunc(jsi::Runtime &rt) {
@@ -120,7 +120,7 @@ class NativeCxxModuleExample
AsyncPromise<std::string> getValueWithPromise(jsi::Runtime &rt, bool error);
bool getWithWithOptionalArgs(
std::optional<bool> getWithWithOptionalArgs(
jsi::Runtime &rt,
std::optional<bool> optionalArg);
@@ -70,7 +70,7 @@ export interface Spec extends TurboModule {
+getValue: (x: number, y: string, z: ObjectStruct) => ValueStruct;
+getValueWithCallback: (callback: (value: string) => void) => void;
+getValueWithPromise: (error: boolean) => Promise<string>;
+getWithWithOptionalArgs: (optionalArg?: boolean) => boolean;
+getWithWithOptionalArgs: (optionalArg?: boolean) => ?boolean;
+voidFunc: () => void;
+emitCustomDeviceEvent: (eventName: string) => void;
}