mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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:
committed by
Facebook GitHub Bot
parent
2e3f55aced
commit
292a9904c4
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user