react-native code-gen > C++ TurboModules enum example (#36083)

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

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D43036612

fbshipit-source-id: fc70650bc4ba48d11f489556d1290ae9e7e58016
This commit is contained in:
Christoph Purrer
2023-02-08 20:24:28 -08:00
committed by Facebook GitHub Bot
parent 2e3f55aced
commit 292a9904c4
9 changed files with 53 additions and 10 deletions
@@ -53,6 +53,26 @@ struct Bridging<ValueStruct> : NativeCxxModuleExampleCxxBaseValueStructBridging<
std::string,
ObjectStruct> {};
#pragma mark - enums
enum EnumInt { A = 23, B = 42 };
template <>
struct Bridging<EnumInt> {
static EnumInt fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 23) {
return EnumInt::A;
} else if (value == 42) {
return EnumInt::B;
} else {
throw jsi::JSError(rt, "Invalid enum value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, EnumInt value) {
return bridging::toJs(rt, static_cast<int32_t>(value));
}
};
#pragma mark - implementation
class NativeCxxModuleExample
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
@@ -71,7 +91,7 @@ class NativeCxxModuleExample
ConstantsStruct getConstants(jsi::Runtime &rt);
int32_t getEnum(jsi::Runtime &rt, int32_t arg);
EnumInt getEnum(jsi::Runtime &rt, EnumInt arg);
std::map<std::string, std::optional<int32_t>> getMap(
jsi::Runtime &rt,