2022-11-09 10:48:49 -08:00
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
|
2024-02-22 09:52:48 -08:00
|
|
|
#if __has_include(<ReactCodegen/AppSpecsJSI.h>) // CocoaPod headers on Apple
|
|
|
|
|
#include <ReactCodegen/AppSpecsJSI.h>
|
2022-11-09 10:48:49 -08:00
|
|
|
#elif __has_include("AppSpecsJSI.h") // Cmake headers on Android
|
|
|
|
|
#include "AppSpecsJSI.h"
|
|
|
|
|
#else // BUCK headers
|
|
|
|
|
#include <AppSpecs/AppSpecsJSI.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <memory>
|
2023-11-09 11:38:54 -08:00
|
|
|
#include <optional>
|
2022-11-09 10:48:49 -08:00
|
|
|
#include <set>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace facebook::react {
|
|
|
|
|
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
#pragma mark - Structs
|
2025-10-26 23:40:59 -07:00
|
|
|
using ConstantsStruct = NativeCxxModuleExampleConstantsStruct<bool, int32_t, std::string>;
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
|
|
|
|
|
template <>
|
2025-10-26 23:40:59 -07:00
|
|
|
struct Bridging<ConstantsStruct> : NativeCxxModuleExampleConstantsStructBridging<ConstantsStruct> {};
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
using ObjectStruct = NativeCxxModuleExampleObjectStruct<int32_t, std::string, std::optional<std::string>>;
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
|
|
|
|
|
template <>
|
2025-10-26 23:40:59 -07:00
|
|
|
struct Bridging<ObjectStruct> : NativeCxxModuleExampleObjectStructBridging<ObjectStruct> {};
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
using ValueStruct = NativeCxxModuleExampleValueStruct<double, std::string, ObjectStruct>;
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
|
|
|
|
|
template <>
|
2025-10-26 23:40:59 -07:00
|
|
|
struct Bridging<ValueStruct> : NativeCxxModuleExampleValueStructBridging<ValueStruct> {};
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
|
2023-02-08 20:24:28 -08:00
|
|
|
#pragma mark - enums
|
2023-12-14 06:54:49 -08:00
|
|
|
enum class CustomEnumInt : int32_t { A = 23, B = 42 };
|
2023-02-08 20:24:28 -08:00
|
|
|
|
|
|
|
|
template <>
|
Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36030
Generate enum types in c++ turbo modules.
For enums in the ts schema file such as:
```
export enum NumEnum {
ONE = 1,
TWO = 2,
}
```
This would export enums and the relevant Bridging to js and from js code to the spec H files such as:
```
#pragma mark - SampleTurboModuleCxxNumEnum
enum SampleTurboModuleCxxNumEnum { ONE, TWO };
template <>
struct Bridging<SampleTurboModuleCxxNumEnum> {
static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 1) {
return SampleTurboModuleCxxNumEnum::ONE;
} else if (value == 2) {
return SampleTurboModuleCxxNumEnum::TWO;
} else {
throw jsi::JSError(rt, "No appropriate enum member found for value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) {
if (value == SampleTurboModuleCxxNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleCxxNumEnum::TWO) {
return bridging::toJs(rt, 2);
} else {
throw jsi::JSError(rt, "No appropriate enum member found for enum value");
}
}
};
```
That code would allow us to use these enums in the cxx files like this:
```
NativeCxxModuleExampleCxxEnumInt getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg);
```
Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators
Reviewed By: christophpurrer
Differential Revision: D42884147
fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
2023-02-13 15:09:44 -08:00
|
|
|
struct Bridging<CustomEnumInt> {
|
2025-10-26 23:40:59 -07:00
|
|
|
static CustomEnumInt fromJs(jsi::Runtime &rt, jsi::Value rawValue)
|
|
|
|
|
{
|
2023-12-14 06:54:49 -08:00
|
|
|
auto value = static_cast<int32_t>(rawValue.asNumber());
|
2023-02-08 20:24:28 -08:00
|
|
|
if (value == 23) {
|
Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36030
Generate enum types in c++ turbo modules.
For enums in the ts schema file such as:
```
export enum NumEnum {
ONE = 1,
TWO = 2,
}
```
This would export enums and the relevant Bridging to js and from js code to the spec H files such as:
```
#pragma mark - SampleTurboModuleCxxNumEnum
enum SampleTurboModuleCxxNumEnum { ONE, TWO };
template <>
struct Bridging<SampleTurboModuleCxxNumEnum> {
static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 1) {
return SampleTurboModuleCxxNumEnum::ONE;
} else if (value == 2) {
return SampleTurboModuleCxxNumEnum::TWO;
} else {
throw jsi::JSError(rt, "No appropriate enum member found for value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) {
if (value == SampleTurboModuleCxxNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleCxxNumEnum::TWO) {
return bridging::toJs(rt, 2);
} else {
throw jsi::JSError(rt, "No appropriate enum member found for enum value");
}
}
};
```
That code would allow us to use these enums in the cxx files like this:
```
NativeCxxModuleExampleCxxEnumInt getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg);
```
Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators
Reviewed By: christophpurrer
Differential Revision: D42884147
fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
2023-02-13 15:09:44 -08:00
|
|
|
return CustomEnumInt::A;
|
2023-02-08 20:24:28 -08:00
|
|
|
} else if (value == 42) {
|
Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36030
Generate enum types in c++ turbo modules.
For enums in the ts schema file such as:
```
export enum NumEnum {
ONE = 1,
TWO = 2,
}
```
This would export enums and the relevant Bridging to js and from js code to the spec H files such as:
```
#pragma mark - SampleTurboModuleCxxNumEnum
enum SampleTurboModuleCxxNumEnum { ONE, TWO };
template <>
struct Bridging<SampleTurboModuleCxxNumEnum> {
static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 1) {
return SampleTurboModuleCxxNumEnum::ONE;
} else if (value == 2) {
return SampleTurboModuleCxxNumEnum::TWO;
} else {
throw jsi::JSError(rt, "No appropriate enum member found for value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) {
if (value == SampleTurboModuleCxxNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleCxxNumEnum::TWO) {
return bridging::toJs(rt, 2);
} else {
throw jsi::JSError(rt, "No appropriate enum member found for enum value");
}
}
};
```
That code would allow us to use these enums in the cxx files like this:
```
NativeCxxModuleExampleCxxEnumInt getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg);
```
Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators
Reviewed By: christophpurrer
Differential Revision: D42884147
fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
2023-02-13 15:09:44 -08:00
|
|
|
return CustomEnumInt::B;
|
2023-02-08 20:24:28 -08:00
|
|
|
} else {
|
|
|
|
|
throw jsi::JSError(rt, "Invalid enum value");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
static int32_t toJs(jsi::Runtime &rt, CustomEnumInt value)
|
|
|
|
|
{
|
2023-02-08 20:24:28 -08:00
|
|
|
return bridging::toJs(rt, static_cast<int32_t>(value));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-14 13:58:39 -07:00
|
|
|
#pragma mark - jsi::HostObjects
|
|
|
|
|
template <typename T>
|
|
|
|
|
class HostObjectWrapper : public jsi::HostObject {
|
|
|
|
|
public:
|
|
|
|
|
HostObjectWrapper(std::shared_ptr<T> value) : value_(std::move(value)) {}
|
|
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::shared_ptr<T> getValue() const
|
|
|
|
|
{
|
2023-04-14 13:58:39 -07:00
|
|
|
return value_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~HostObjectWrapper() override = default;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::shared_ptr<T> value_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CustomHostObjectRef {
|
|
|
|
|
CustomHostObjectRef(std::string a, int32_t b) : a_(a), b_(b) {}
|
|
|
|
|
std::string a_;
|
|
|
|
|
int32_t b_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using CustomHostObject = HostObjectWrapper<CustomHostObjectRef>;
|
|
|
|
|
|
2023-12-04 04:19:47 -08:00
|
|
|
#pragma mark - recursive objects
|
2023-12-04 05:54:50 -08:00
|
|
|
|
2024-05-23 03:28:52 -07:00
|
|
|
using BinaryTreeNode = NativeCxxModuleExampleBinaryTreeNode<int32_t>;
|
2023-12-04 05:54:50 -08:00
|
|
|
|
2023-12-04 08:56:42 -08:00
|
|
|
template <>
|
2025-10-26 23:40:59 -07:00
|
|
|
struct Bridging<BinaryTreeNode> : NativeCxxModuleExampleBinaryTreeNodeBridging<BinaryTreeNode> {};
|
2023-12-04 05:54:50 -08:00
|
|
|
|
2024-05-23 03:28:52 -07:00
|
|
|
using GraphNode = NativeCxxModuleExampleGraphNode<std::string>;
|
2023-12-04 04:19:47 -08:00
|
|
|
|
|
|
|
|
template <>
|
2025-10-26 23:40:59 -07:00
|
|
|
struct Bridging<GraphNode> : NativeCxxModuleExampleGraphNodeBridging<GraphNode> {};
|
2023-12-04 04:19:47 -08:00
|
|
|
|
2023-12-05 23:27:06 -08:00
|
|
|
#pragma mark - functional object properties
|
|
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
using MenuItem =
|
|
|
|
|
NativeCxxModuleExampleMenuItem<std::string, AsyncCallback<std::string, bool>, std::optional<std::string>>;
|
2023-12-05 23:27:06 -08:00
|
|
|
|
|
|
|
|
template <>
|
2024-05-23 03:28:52 -07:00
|
|
|
struct Bridging<MenuItem> : NativeCxxModuleExampleMenuItemBridging<MenuItem> {};
|
2023-12-05 23:27:06 -08:00
|
|
|
|
2024-04-30 11:16:57 -07:00
|
|
|
#pragma mark - RCTDeviceEventEmitter events
|
|
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
using CustomDeviceEvent = NativeCxxModuleExampleCustomDeviceEvent<std::string, int32_t, std::optional<float>>;
|
2024-04-30 11:16:57 -07:00
|
|
|
|
|
|
|
|
template <>
|
2025-10-26 23:40:59 -07:00
|
|
|
struct Bridging<CustomDeviceEvent> : NativeCxxModuleExampleCustomDeviceEventBridging<CustomDeviceEvent> {};
|
2024-04-30 11:16:57 -07:00
|
|
|
|
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &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;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Reviewed By: rshest
Differential Revision: D41133761
fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
|
|
|
#pragma mark - implementation
|
2025-10-26 23:40:59 -07:00
|
|
|
class NativeCxxModuleExample : public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
|
2022-11-09 10:48:49 -08:00
|
|
|
public:
|
|
|
|
|
NativeCxxModuleExample(std::shared_ptr<CallInvoker> jsInvoker);
|
|
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
void getValueWithCallback(jsi::Runtime &rt, const AsyncCallback<std::string> &callback);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::function<void()> setValueCallbackWithSubscription(jsi::Runtime &rt, AsyncCallback<std::string> callback);
|
2023-11-09 11:38:54 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::vector<std::optional<ObjectStruct>> getArray(jsi::Runtime &rt, std::vector<std::optional<ObjectStruct>> arg);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
bool getBool(jsi::Runtime &rt, bool arg);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
ConstantsStruct getConstants(jsi::Runtime &rt);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
CustomEnumInt getCustomEnum(jsi::Runtime &rt, CustomEnumInt arg);
|
Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36030
Generate enum types in c++ turbo modules.
For enums in the ts schema file such as:
```
export enum NumEnum {
ONE = 1,
TWO = 2,
}
```
This would export enums and the relevant Bridging to js and from js code to the spec H files such as:
```
#pragma mark - SampleTurboModuleCxxNumEnum
enum SampleTurboModuleCxxNumEnum { ONE, TWO };
template <>
struct Bridging<SampleTurboModuleCxxNumEnum> {
static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 1) {
return SampleTurboModuleCxxNumEnum::ONE;
} else if (value == 2) {
return SampleTurboModuleCxxNumEnum::TWO;
} else {
throw jsi::JSError(rt, "No appropriate enum member found for value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) {
if (value == SampleTurboModuleCxxNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleCxxNumEnum::TWO) {
return bridging::toJs(rt, 2);
} else {
throw jsi::JSError(rt, "No appropriate enum member found for enum value");
}
}
};
```
That code would allow us to use these enums in the cxx files like this:
```
NativeCxxModuleExampleCxxEnumInt getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg);
```
Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators
Reviewed By: christophpurrer
Differential Revision: D42884147
fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
2023-02-13 15:09:44 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::shared_ptr<CustomHostObject> getCustomHostObject(jsi::Runtime &rt);
|
2023-04-14 13:58:39 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::string consumeCustomHostObject(jsi::Runtime &rt, std::shared_ptr<CustomHostObject> arg);
|
2023-04-14 13:58:39 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
BinaryTreeNode getBinaryTreeNode(jsi::Runtime &rt, BinaryTreeNode arg);
|
2023-12-04 05:54:50 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
GraphNode getGraphNode(jsi::Runtime &rt, GraphNode arg);
|
2023-12-04 04:19:47 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
NativeCxxModuleExampleEnumInt getNumEnum(jsi::Runtime &rt, NativeCxxModuleExampleEnumInt arg);
|
Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36030
Generate enum types in c++ turbo modules.
For enums in the ts schema file such as:
```
export enum NumEnum {
ONE = 1,
TWO = 2,
}
```
This would export enums and the relevant Bridging to js and from js code to the spec H files such as:
```
#pragma mark - SampleTurboModuleCxxNumEnum
enum SampleTurboModuleCxxNumEnum { ONE, TWO };
template <>
struct Bridging<SampleTurboModuleCxxNumEnum> {
static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 1) {
return SampleTurboModuleCxxNumEnum::ONE;
} else if (value == 2) {
return SampleTurboModuleCxxNumEnum::TWO;
} else {
throw jsi::JSError(rt, "No appropriate enum member found for value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) {
if (value == SampleTurboModuleCxxNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleCxxNumEnum::TWO) {
return bridging::toJs(rt, 2);
} else {
throw jsi::JSError(rt, "No appropriate enum member found for enum value");
}
}
};
```
That code would allow us to use these enums in the cxx files like this:
```
NativeCxxModuleExampleCxxEnumInt getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg);
```
Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators
Reviewed By: christophpurrer
Differential Revision: D42884147
fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
2023-02-13 15:09:44 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
NativeCxxModuleExampleEnumStr getStrEnum(jsi::Runtime &rt, NativeCxxModuleExampleEnumNone arg);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
|
|
|
|
std::map<std::string, std::optional<int32_t>> getMap(
|
2025-10-26 23:40:59 -07:00
|
|
|
jsi::Runtime &rt,
|
2022-11-09 10:48:49 -08:00
|
|
|
std::map<std::string, std::optional<int32_t>> arg);
|
|
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
double getNumber(jsi::Runtime &rt, double arg);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
ObjectStruct getObject(jsi::Runtime &rt, ObjectStruct arg);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::set<float> getSet(jsi::Runtime &rt, std::set<float> arg);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::string getString(jsi::Runtime &rt, std::string arg);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::string getUnion(jsi::Runtime &rt, float x, const std::string &y, jsi::Object z);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
ValueStruct getValue(jsi::Runtime &rt, double x, std::string y, ObjectStruct z);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
AsyncPromise<std::string> getValueWithPromise(jsi::Runtime &rt, bool error);
|
2022-11-09 10:48:49 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
std::optional<bool> getWithWithOptionalArgs(jsi::Runtime &rt, std::optional<bool> optionalArg);
|
2023-03-22 16:57:40 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
void voidFunc(jsi::Runtime &rt);
|
2023-02-24 06:49:14 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
AsyncPromise<> voidPromise(jsi::Runtime &rt);
|
2025-07-06 19:53:51 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
void setMenu(jsi::Runtime &rt, MenuItem menuItem);
|
2023-12-05 23:27:06 -08:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
void emitCustomDeviceEvent(jsi::Runtime &rt, const std::string &eventName);
|
2023-04-03 08:34:59 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
void voidFuncThrows(jsi::Runtime &rt);
|
2023-04-03 08:34:59 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
ObjectStruct getObjectThrows(jsi::Runtime &rt, const ObjectStruct &arg);
|
2023-04-03 08:34:59 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
AsyncPromise<> promiseThrows(jsi::Runtime &rt);
|
2023-04-03 08:34:59 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
void voidFuncAssert(jsi::Runtime &rt);
|
2023-04-03 08:34:59 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
ObjectStruct getObjectAssert(jsi::Runtime &rt, const ObjectStruct &arg);
|
2023-04-03 08:34:59 -07:00
|
|
|
|
2025-10-26 23:40:59 -07:00
|
|
|
AsyncPromise<> promiseAssert(jsi::Runtime &rt);
|
2023-11-09 11:38:54 -08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::optional<AsyncCallback<std::string>> valueCallback_;
|
2022-11-09 10:48:49 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace facebook::react
|