/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include #include #include #include #include namespace facebook::react { template struct Bridging; template <> struct Bridging { // Highly generic code may result in "casting" to void. static void fromJs(jsi::Runtime &, const jsi::Value &) {} }; namespace bridging { namespace detail { template struct function_wrapper; template struct function_wrapper { using type = butter::function; }; template struct function_wrapper { using type = butter::function; }; template struct bridging_wrapper { using type = remove_cvref_t; }; // Convert lambda types to move-only function types since we can't specialize // Bridging templates for arbitrary lambdas. template struct bridging_wrapper< T, std::void_t::operator())>> : function_wrapper::operator())> {}; } // namespace detail template using bridging_t = typename detail::bridging_wrapper::type; template , int> = 0> auto fromJs(jsi::Runtime &rt, T &&value, const std::shared_ptr &) -> decltype(static_cast(convert(rt, std::forward(value)))) { return convert(rt, std::forward(value)); } template auto fromJs(jsi::Runtime &rt, T &&value, const std::shared_ptr &) -> decltype(Bridging>::fromJs( rt, convert(rt, std::forward(value)))) { return Bridging>::fromJs( rt, convert(rt, std::forward(value))); } template auto fromJs( jsi::Runtime &rt, T &&value, const std::shared_ptr &jsInvoker) -> decltype(Bridging>::fromJs( rt, convert(rt, std::forward(value)), jsInvoker)) { return Bridging>::fromJs( rt, convert(rt, std::forward(value)), jsInvoker); } template , int> = 0> auto toJs( jsi::Runtime &rt, T &&value, const std::shared_ptr & = nullptr) -> remove_cvref_t { return convert(rt, std::forward(value)); } template auto toJs( jsi::Runtime &rt, T &&value, const std::shared_ptr & = nullptr) -> decltype(Bridging>::toJs(rt, std::forward(value))) { return Bridging>::toJs(rt, std::forward(value)); } template auto toJs( jsi::Runtime &rt, T &&value, const std::shared_ptr &jsInvoker) -> decltype(Bridging>::toJs( rt, std::forward(value), jsInvoker)) { return Bridging>::toJs(rt, std::forward(value), jsInvoker); } template inline constexpr bool supportsFromJs = false; template inline constexpr bool supportsFromJs< T, Arg, std::void_t( std::declval(), std::declval(), nullptr))>> = true; template inline constexpr bool supportsToJs = false; template inline constexpr bool supportsToJs< T, Ret, std::void_t(), std::declval(), nullptr))>> = std::is_convertible_v< decltype(toJs( std::declval(), std::declval(), nullptr)), Ret>; } // namespace bridging } // namespace facebook::react