Files
react-native/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample_ValueStruct.h
T
Christoph Purrer c63ea4c3a0 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
2022-11-22 11:09:47 +00:00

51 lines
1.4 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/Bridging.h>
#include <optional>
#include <string>
#include "NativeCxxModuleExample_ObjectStruct.h"
namespace facebook::react {
struct ValueStruct {
double x;
std::string y;
ObjectStruct z;
bool operator==(const ValueStruct &other) const {
return x == other.x && y == other.y && z == other.z;
}
};
template <>
struct Bridging<ValueStruct> {
static ValueStruct fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
ValueStruct result{
bridging::fromJs<double>(rt, value.getProperty(rt, "x"), jsInvoker),
bridging::fromJs<std::string>(
rt, value.getProperty(rt, "y"), jsInvoker),
bridging::fromJs<ObjectStruct>(
rt, value.getProperty(rt, "z"), jsInvoker)};
return result;
}
static jsi::Object toJs(jsi::Runtime &rt, const ValueStruct &value) {
auto result = facebook::jsi::Object(rt);
result.setProperty(rt, "x", bridging::toJs(rt, value.x));
result.setProperty(rt, "y", bridging::toJs(rt, value.y));
result.setProperty(rt, "z", bridging::toJs(rt, value.z));
return result;
}
};
} // namespace facebook::react