react-native code-gen > Add a C++ only TurboModule example (for Android/iOS/macOS/Windows) (#35138)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35138

Changelog:

[General][Added] - Add a C++ only TurboModule example (for Android/iOS/macOS/Windows)

react-native@0.69 introduced a new bridging layer to ease integration for pure C++ TurboModules using C++ std:: types directly instead of the lower level jsi:: types:
https://github.com/facebook/react-native/tree/v0.69.0/ReactCommon/react/bridging

This bridging layer can be used in JSI functions or more conveniently in C++ TurboModules.

Here is a example of an C++ only TurboModule which will work on Android and iOS and macOS/Windows (using microsoft/react-native-macos|windows) only using flow/TypeScript and standard C++ types.

C++ only TurboModules are very handy as they do not require to work with JSI APIs - instead std:: or custom C++ can by used.

Reviewed By: javache

Differential Revision: D39011736

fbshipit-source-id: 84c833d8540671fde8505f1aeb0265074b248730
This commit is contained in:
Christoph Purrer
2022-11-09 10:48:49 -08:00
committed by Facebook GitHub Bot
parent bbb3a6146c
commit d07575b1c6
17 changed files with 745 additions and 1 deletions
@@ -0,0 +1,110 @@
/*
* 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.
*/
#include "NativeCxxModuleExample.h"
namespace facebook::react {
NativeCxxModuleExample::NativeCxxModuleExample(
std::shared_ptr<CallInvoker> jsInvoker)
: NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {}
void NativeCxxModuleExample::getValueWithCallback(
jsi::Runtime &rt,
AsyncCallback<std::string> callback) {
callback({"value from callback!"});
}
std::vector<std::optional<ObjectStruct>> NativeCxxModuleExample::getArray(
jsi::Runtime &rt,
std::vector<std::optional<ObjectStruct>> arg) {
return arg;
}
bool NativeCxxModuleExample::getBool(jsi::Runtime &rt, bool arg) {
return arg;
}
ConstantsStruct NativeCxxModuleExample::getConstants(jsi::Runtime &rt) {
return ConstantsStruct{true, 69, "react-native"};
}
int32_t NativeCxxModuleExample::getEnum(jsi::Runtime &rt, int32_t arg) {
return arg;
}
std::map<std::string, std::optional<int32_t>> NativeCxxModuleExample::getMap(
jsi::Runtime &rt,
std::map<std::string, std::optional<int32_t>> arg) {
return arg;
}
double NativeCxxModuleExample::getNumber(jsi::Runtime &rt, double arg) {
return arg;
}
ObjectStruct NativeCxxModuleExample::getObject(
jsi::Runtime &rt,
ObjectStruct arg) {
return arg;
}
std::set<float> NativeCxxModuleExample::getSet(
jsi::Runtime &rt,
std::set<float> arg) {
return arg;
}
std::string NativeCxxModuleExample::getString(
jsi::Runtime &rt,
std::string arg) {
return arg;
}
std::string NativeCxxModuleExample::getUnion(
jsi::Runtime &rt,
float x,
std::string y,
jsi::Object z) {
std::string result = "x: " + std::to_string(x) + ", y: " + y + ", z: { ";
if (z.hasProperty(rt, "value")) {
result += "value: ";
result += std::to_string(z.getProperty(rt, "value").getNumber());
} else if (z.hasProperty(rt, "low")) {
result += "low: ";
result += z.getProperty(rt, "low").getString(rt).utf8(rt);
}
result += " }";
return result;
}
ValueStruct NativeCxxModuleExample::getValue(
jsi::Runtime &rt,
double x,
std::string y,
ObjectStruct z) {
ValueStruct result{x, y, z};
return result;
}
AsyncPromise<std::string> NativeCxxModuleExample::getValueWithPromise(
jsi::Runtime &rt,
bool error) {
auto promise = AsyncPromise<std::string>(rt, jsInvoker_);
if (error) {
promise.reject("intentional promise rejection");
} else {
promise.resolve("result!");
}
return promise;
}
void NativeCxxModuleExample::voidFunc(jsi::Runtime &rt) {
// Nothing to do
}
} // namespace facebook::react