Skip to content

Commit

Permalink
Fixes to bridging/base.h for MSVC compatibility (#47882)
Browse files Browse the repository at this point in the history
Summary:
Attempting to use the various bridging template and generated C++ Specs for native modules in a project compiling with MSVC does not build.

For the fromJs I had to add `std::move` - otherwise it could not cast.

The supportsFromJs and supportsToJs are changed to address an IntelliSense issue where the template specialization cannot have default members.

## Changelog:
[GENERAL] [FIXED] - Fix C++ bridging template compatibility with MSVC

Pull Request resolved: #47882

Test Plan: It continues to build on Android/iOS builds.  And it also builds with MSVC for react-native-windows and other out of tree platforms that use MSVC.

Reviewed By: cipolleschi

Differential Revision: D66360833

Pulled By: javache

fbshipit-source-id: bf97db1cd247a383eb86cac31c601e8ab3400fd2
  • Loading branch information
acoates-ms authored and facebook-github-bot committed Nov 22, 2024
1 parent e4fc27e commit e6848ba
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions packages/react-native/ReactCommon/react/bridging/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ using bridging_t = typename detail::bridging_wrapper<T>::type;

template <typename R, typename T, std::enable_if_t<is_jsi_v<T>, int> = 0>
auto fromJs(jsi::Runtime& rt, T&& value, const std::shared_ptr<CallInvoker>&)
-> decltype(static_cast<R>(convert(rt, std::forward<T>(value)))) {
return convert(rt, std::forward<T>(value));
-> decltype(static_cast<R>(
std::move(convert(rt, std::forward<T>(value))))) {
return static_cast<R>(std::move(convert(rt, std::forward<T>(value))));
}

template <typename R, typename T>
Expand Down Expand Up @@ -121,7 +122,7 @@ auto toJs(
template <typename, typename = jsi::Value, typename = void>
inline constexpr bool supportsFromJs = false;

template <typename T, typename Arg = jsi::Value>
template <typename T, typename Arg>
inline constexpr bool supportsFromJs<
T,
Arg,
Expand All @@ -130,10 +131,19 @@ inline constexpr bool supportsFromJs<
std::declval<Arg>(),
nullptr))>> = true;

template <typename T>
inline constexpr bool supportsFromJs<
T,
jsi::Value,
std::void_t<decltype(fromJs<T>(
std::declval<jsi::Runtime&>(),
std::declval<jsi::Value>(),
nullptr))>> = true;

template <typename, typename = jsi::Value, typename = void>
inline constexpr bool supportsToJs = false;

template <typename T, typename Ret = jsi::Value>
template <typename T, typename Ret>
inline constexpr bool supportsToJs<
T,
Ret,
Expand All @@ -148,5 +158,20 @@ inline constexpr bool supportsToJs<
nullptr)),
Ret>;

template <typename T>
inline constexpr bool supportsToJs<
T,
jsi::Value,
std::void_t<decltype(toJs(
std::declval<jsi::Runtime&>(),
std::declval<T>(),
nullptr))>> =
std::is_convertible_v<
decltype(toJs(
std::declval<jsi::Runtime&>(),
std::declval<T>(),
nullptr)),
jsi::Value>;

} // namespace bridging
} // namespace facebook::react

0 comments on commit e6848ba

Please sign in to comment.