|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <react/bridging/Convert.h> |
| 11 | + |
| 12 | +#include <ReactCommon/CallInvoker.h> |
| 13 | +#include <folly/Function.h> |
| 14 | +#include <jsi/jsi.h> |
| 15 | + |
| 16 | +#include <cstdint> |
| 17 | +#include <memory> |
| 18 | +#include <type_traits> |
| 19 | + |
| 20 | +namespace facebook::react { |
| 21 | + |
| 22 | +template <typename T, typename = void> |
| 23 | +struct Bridging; |
| 24 | + |
| 25 | +template <> |
| 26 | +struct Bridging<void> { |
| 27 | + // Highly generic code may result in "casting" to void. |
| 28 | + static void fromJs(jsi::Runtime &, const jsi::Value &) {} |
| 29 | +}; |
| 30 | + |
| 31 | +namespace bridging { |
| 32 | +namespace detail { |
| 33 | + |
| 34 | +template <typename F> |
| 35 | +struct function_wrapper; |
| 36 | + |
| 37 | +template <typename C, typename R, typename... Args> |
| 38 | +struct function_wrapper<R (C::*)(Args...)> { |
| 39 | + using type = folly::Function<R(Args...)>; |
| 40 | +}; |
| 41 | + |
| 42 | +template <typename C, typename R, typename... Args> |
| 43 | +struct function_wrapper<R (C::*)(Args...) const> { |
| 44 | + using type = folly::Function<R(Args...)>; |
| 45 | +}; |
| 46 | + |
| 47 | +template <typename T, typename = void> |
| 48 | +struct bridging_wrapper { |
| 49 | + using type = remove_cvref_t<T>; |
| 50 | +}; |
| 51 | + |
| 52 | +// Convert lambda types to move-only function types since we can't specialize |
| 53 | +// Bridging templates for arbitrary lambdas. |
| 54 | +template <typename T> |
| 55 | +struct bridging_wrapper< |
| 56 | + T, |
| 57 | + std::void_t<decltype(&remove_cvref_t<T>::operator())>> |
| 58 | + : function_wrapper<decltype(&remove_cvref_t<T>::operator())> {}; |
| 59 | + |
| 60 | +} // namespace detail |
| 61 | + |
| 62 | +template <typename T> |
| 63 | +using bridging_t = typename detail::bridging_wrapper<T>::type; |
| 64 | + |
| 65 | +template <typename R, typename T, std::enable_if_t<is_jsi_v<T>, int> = 0> |
| 66 | +auto fromJs(jsi::Runtime &rt, T &&value, const std::shared_ptr<CallInvoker> &) |
| 67 | + -> decltype(static_cast<R>(convert(rt, std::forward<T>(value)))) { |
| 68 | + return convert(rt, std::forward<T>(value)); |
| 69 | +} |
| 70 | + |
| 71 | +template <typename R, typename T> |
| 72 | +auto fromJs(jsi::Runtime &rt, T &&value, const std::shared_ptr<CallInvoker> &) |
| 73 | + -> decltype(Bridging<remove_cvref_t<R>>::fromJs( |
| 74 | + rt, |
| 75 | + convert(rt, std::forward<T>(value)))) { |
| 76 | + return Bridging<remove_cvref_t<R>>::fromJs( |
| 77 | + rt, convert(rt, std::forward<T>(value))); |
| 78 | +} |
| 79 | + |
| 80 | +template <typename R, typename T> |
| 81 | +auto fromJs( |
| 82 | + jsi::Runtime &rt, |
| 83 | + T &&value, |
| 84 | + const std::shared_ptr<CallInvoker> &jsInvoker) |
| 85 | + -> decltype(Bridging<remove_cvref_t<R>>::fromJs( |
| 86 | + rt, |
| 87 | + convert(rt, std::forward<T>(value)), |
| 88 | + jsInvoker)) { |
| 89 | + return Bridging<remove_cvref_t<R>>::fromJs( |
| 90 | + rt, convert(rt, std::forward<T>(value)), jsInvoker); |
| 91 | +} |
| 92 | + |
| 93 | +template <typename T, std::enable_if_t<is_jsi_v<T>, int> = 0> |
| 94 | +auto toJs( |
| 95 | + jsi::Runtime &rt, |
| 96 | + T &&value, |
| 97 | + const std::shared_ptr<CallInvoker> & = nullptr) |
| 98 | + -> decltype(convert(rt, std::forward<T>(value))) { |
| 99 | + return convert(rt, std::forward<T>(value)); |
| 100 | +} |
| 101 | + |
| 102 | +template <typename T> |
| 103 | +auto toJs( |
| 104 | + jsi::Runtime &rt, |
| 105 | + T &&value, |
| 106 | + const std::shared_ptr<CallInvoker> & = nullptr) |
| 107 | + -> decltype(Bridging<bridging_t<T>>::toJs(rt, std::forward<T>(value))) { |
| 108 | + return Bridging<bridging_t<T>>::toJs(rt, std::forward<T>(value)); |
| 109 | +} |
| 110 | + |
| 111 | +template <typename T> |
| 112 | +auto toJs( |
| 113 | + jsi::Runtime &rt, |
| 114 | + T &&value, |
| 115 | + const std::shared_ptr<CallInvoker> &jsInvoker) |
| 116 | + -> decltype(Bridging<bridging_t<T>>::toJs( |
| 117 | + rt, |
| 118 | + std::forward<T>(value), |
| 119 | + jsInvoker)) { |
| 120 | + return Bridging<bridging_t<T>>::toJs(rt, std::forward<T>(value), jsInvoker); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace bridging |
| 124 | +} // namespace facebook::react |
0 commit comments