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 Lorenzo Sciandra
parent d587e0c2a5
commit c63ea4c3a0
17 changed files with 745 additions and 1 deletions
@@ -0,0 +1,52 @@
/*
* 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/Bridging.h>
#include <optional>
#include <string>
namespace facebook::react {
struct ObjectStruct {
int32_t a;
std::string b;
std::optional<std::string> c;
bool operator==(const ObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <>
struct Bridging<ObjectStruct> {
static ObjectStruct fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
ObjectStruct result{
bridging::fromJs<int32_t>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<std::string>(
rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<std::optional<std::string>>(
rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(jsi::Runtime &rt, const ObjectStruct &value) {
auto result = facebook::jsi::Object(rt);
result.setProperty(rt, "a", bridging::toJs(rt, value.a));
result.setProperty(rt, "b", bridging::toJs(rt, value.b));
if (value.c) {
result.setProperty(rt, "c", bridging::toJs(rt, value.c.value()));
}
return result;
}
};
} // namespace facebook::react