Files
react-native/ReactCommon/react/bridging/Base.h
T
Scott Kyle 30cb78e709 New bridging API for JSI <-> C++
Summary:
This adds  `bridging::toJs` and `bridging::fromJs` functions that will safely cast to and from JSI values and C++ types. This is extensible by specializing `Bridging<T>` with `toJs` and/or `fromJs` static methods. There are specializations for most common C++ and JSI types along with tests for those.

C++ functions and lambdas will effortlessly bridge into JS, and bridging JS functions back into C++ require you to choose `SyncCallback<R(Args...)>` or `AsyncCallback<Args...>` types. The sync version allows for having a return value and is strictly not movable to prevent accidentally moving onto another thread. The async version will move its args onto the JS thread and safely call the callback there, but hence always has a `void` return value.

For promises, you can construct a `AsyncPromise<T>` that has `resolve` and `reject` methods that can be called from any thread, and will bridge into JS as a regular `Promise`.

Changelog:
[General][Added] - New bridging API for JSI <-> C++

Reviewed By: christophpurrer

Differential Revision: D34607143

fbshipit-source-id: d832ac24cf84b4c1672a7b544d82e324d5fca3ef
2022-03-11 12:47:51 -08:00

125 lines
3.3 KiB
C++

/*
* 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 <react/bridging/Convert.h>
#include <ReactCommon/CallInvoker.h>
#include <folly/Function.h>
#include <jsi/jsi.h>
#include <cstdint>
#include <memory>
#include <type_traits>
namespace facebook::react {
template <typename T, typename = void>
struct Bridging;
template <>
struct Bridging<void> {
// Highly generic code may result in "casting" to void.
static void fromJs(jsi::Runtime &, const jsi::Value &) {}
};
namespace bridging {
namespace detail {
template <typename F>
struct function_wrapper;
template <typename C, typename R, typename... Args>
struct function_wrapper<R (C::*)(Args...)> {
using type = folly::Function<R(Args...)>;
};
template <typename C, typename R, typename... Args>
struct function_wrapper<R (C::*)(Args...) const> {
using type = folly::Function<R(Args...)>;
};
template <typename T, typename = void>
struct bridging_wrapper {
using type = remove_cvref_t<T>;
};
// Convert lambda types to move-only function types since we can't specialize
// Bridging templates for arbitrary lambdas.
template <typename T>
struct bridging_wrapper<
T,
std::void_t<decltype(&remove_cvref_t<T>::operator())>>
: function_wrapper<decltype(&remove_cvref_t<T>::operator())> {};
} // namespace detail
template <typename T>
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));
}
template <typename R, typename T>
auto fromJs(jsi::Runtime &rt, T &&value, const std::shared_ptr<CallInvoker> &)
-> decltype(Bridging<remove_cvref_t<R>>::fromJs(
rt,
convert(rt, std::forward<T>(value)))) {
return Bridging<remove_cvref_t<R>>::fromJs(
rt, convert(rt, std::forward<T>(value)));
}
template <typename R, typename T>
auto fromJs(
jsi::Runtime &rt,
T &&value,
const std::shared_ptr<CallInvoker> &jsInvoker)
-> decltype(Bridging<remove_cvref_t<R>>::fromJs(
rt,
convert(rt, std::forward<T>(value)),
jsInvoker)) {
return Bridging<remove_cvref_t<R>>::fromJs(
rt, convert(rt, std::forward<T>(value)), jsInvoker);
}
template <typename T, std::enable_if_t<is_jsi_v<T>, int> = 0>
auto toJs(
jsi::Runtime &rt,
T &&value,
const std::shared_ptr<CallInvoker> & = nullptr)
-> decltype(convert(rt, std::forward<T>(value))) {
return convert(rt, std::forward<T>(value));
}
template <typename T>
auto toJs(
jsi::Runtime &rt,
T &&value,
const std::shared_ptr<CallInvoker> & = nullptr)
-> decltype(Bridging<bridging_t<T>>::toJs(rt, std::forward<T>(value))) {
return Bridging<bridging_t<T>>::toJs(rt, std::forward<T>(value));
}
template <typename T>
auto toJs(
jsi::Runtime &rt,
T &&value,
const std::shared_ptr<CallInvoker> &jsInvoker)
-> decltype(Bridging<bridging_t<T>>::toJs(
rt,
std::forward<T>(value),
jsInvoker)) {
return Bridging<bridging_t<T>>::toJs(rt, std::forward<T>(value), jsInvoker);
}
} // namespace bridging
} // namespace facebook::react