Files
react-native/ReactCommon/react/bridging/Class.h
T
Scott Kyle 31f0796237 Add codegen for C++ TurboModule automatic type conversions
Summary:
This adds the *option* for C++ TurboModules to use a `*CxxSpec<T>` base class that extends the existing (and unchanged) corresponding `*CxxSpecJSI` base class with code-generated methods that use `bridging::calFromJs` to safely convert types between JSI and C++. If a type conversion cannot be made, then it will fail to compile.

Changelog:
[General][Added] - Automatic type conversions for C++ TurboModules

Reviewed By: christophpurrer

Differential Revision: D34780512

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

66 lines
1.6 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/Base.h>
namespace facebook::react::bridging {
template <
typename T,
typename C,
typename R,
typename... Args,
typename... JSArgs>
T callFromJs(
jsi::Runtime &rt,
R (C::*method)(jsi::Runtime &, Args...),
const std::shared_ptr<CallInvoker> &jsInvoker,
C *instance,
JSArgs &&...args) {
static_assert(
sizeof...(Args) == sizeof...(JSArgs), "Incorrect arguments length");
if constexpr (std::is_void_v<T>) {
(instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);
} else if constexpr (std::is_void_v<R>) {
static_assert(
std::is_same_v<T, jsi::Value>,
"Void functions may only return undefined");
(instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);
return jsi::Value();
} else if constexpr (is_jsi_v<T>) {
return toJs(
rt,
(instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...),
jsInvoker);
} else {
return (instance->*method)(
rt, fromJs<Args>(rt, std::forward<JSArgs>(args), jsInvoker)...);
}
}
template <typename R, typename... Args>
constexpr size_t getParameterCount(R (*)(Args...)) {
return sizeof...(Args);
}
template <typename C, typename R, typename... Args>
constexpr size_t getParameterCount(R (C::*)(Args...)) {
return sizeof...(Args);
}
} // namespace facebook::react::bridging