diff --git a/packages/react-native-codegen/e2e/__tests__/modules/__snapshots__/GenerateModuleH-test.js.snap b/packages/react-native-codegen/e2e/__tests__/modules/__snapshots__/GenerateModuleH-test.js.snap index 20b3a737fbee08..a5966eb823bdce 100644 --- a/packages/react-native-codegen/e2e/__tests__/modules/__snapshots__/GenerateModuleH-test.js.snap +++ b/packages/react-native-codegen/e2e/__tests__/modules/__snapshots__/GenerateModuleH-test.js.snap @@ -18,6 +18,8 @@ exports[`GenerateModuleH can generate a header file NativeModule specs 1`] = ` namespace facebook { namespace react { + + class JSI_EXPORT NativeArrayTurboModuleCxxSpecJSI : public TurboModule { protected: NativeArrayTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -79,6 +81,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeBooleanTurboModuleCxxSpecJSI : public TurboModule { protected: NativeBooleanTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -131,6 +135,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeCallbackTurboModuleCxxSpecJSI : public TurboModule { protected: NativeCallbackTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -183,6 +189,95 @@ private: Delegate delegate_; }; +#pragma mark - NativeEnumTurboModuleStatusRegularEnum +enum class NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusRegularEnum &result) { + auto string = (std::string)value; + if (string == \\"Active\\") { result = NativeEnumTurboModuleStatusRegularEnum::Active; return; } + if (string == \\"Paused\\") { result = NativeEnumTurboModuleStatusRegularEnum::Paused; return; } + if (string == \\"Off\\") { result = NativeEnumTurboModuleStatusRegularEnum::Off; return; } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusRegularEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusRegularEnum::Active: return \\"Active\\"; + case NativeEnumTurboModuleStatusRegularEnum::Paused: return \\"Paused\\"; + case NativeEnumTurboModuleStatusRegularEnum::Off: return \\"Off\\"; + } +} +#pragma mark - NativeEnumTurboModuleStatusStrEnum +enum class NativeEnumTurboModuleStatusStrEnum { Active, Paused, Off }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusStrEnum &result) { + auto string = (std::string)value; + if (string == \\"active\\") { result = NativeEnumTurboModuleStatusStrEnum::Active; return; } + if (string == \\"paused\\") { result = NativeEnumTurboModuleStatusStrEnum::Paused; return; } + if (string == \\"off\\") { result = NativeEnumTurboModuleStatusStrEnum::Off; return; } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusStrEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusStrEnum::Active: return \\"active\\"; + case NativeEnumTurboModuleStatusStrEnum::Paused: return \\"paused\\"; + case NativeEnumTurboModuleStatusStrEnum::Off: return \\"off\\"; + } +} +enum class NativeEnumTurboModuleStatusNumEnum { Active = 2, Paused = 1, Off = 0 }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusNumEnum &result) { + assert(value.hasType()); + auto integerValue = (int)value; + switch (integerValue) { + case 2: + result = NativeEnumTurboModuleStatusNumEnum::Active; + return; + case 1: + result = NativeEnumTurboModuleStatusNumEnum::Paused; + return; + case 0: + result = NativeEnumTurboModuleStatusNumEnum::Off; + return; + } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusNumEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusNumEnum::Active: return \\"2\\"; + case NativeEnumTurboModuleStatusNumEnum::Paused: return \\"1\\"; + case NativeEnumTurboModuleStatusNumEnum::Off: return \\"0\\"; + } +} +enum class NativeEnumTurboModuleStatusFractionEnum { Active = 0.2, Paused = 0.1, Off = 0 }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusFractionEnum &result) { + assert(value.hasType()); + auto integerValue = (int)value; + switch (integerValue) { + case 0.2: + result = NativeEnumTurboModuleStatusFractionEnum::Active; + return; + case 0.1: + result = NativeEnumTurboModuleStatusFractionEnum::Paused; + return; + case 0: + result = NativeEnumTurboModuleStatusFractionEnum::Off; + return; + } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusFractionEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusFractionEnum::Active: return \\"0.2\\"; + case NativeEnumTurboModuleStatusFractionEnum::Paused: return \\"0.1\\"; + case NativeEnumTurboModuleStatusFractionEnum::Off: return \\"0\\"; + } +} + #pragma mark - NativeEnumTurboModuleBaseStateType template @@ -254,16 +349,16 @@ struct NativeEnumTurboModuleBaseStateTypeWithEnumsBridging { static jsi::String stateToJs(jsi::Runtime &rt, P0 value) { return bridging::toJs(rt, value); } - static jsi::String regularToJs(jsi::Runtime &rt, P1 value) { + static StatusRegularEnum regularToJs(jsi::Runtime &rt, P1 value) { return bridging::toJs(rt, value); } - static jsi::String strToJs(jsi::Runtime &rt, P2 value) { + static StatusStrEnum strToJs(jsi::Runtime &rt, P2 value) { return bridging::toJs(rt, value); } - static double numToJs(jsi::Runtime &rt, P3 value) { + static StatusNumEnum numToJs(jsi::Runtime &rt, P3 value) { return bridging::toJs(rt, value); } - static double fractionToJs(jsi::Runtime &rt, P4 value) { + static StatusFractionEnum fractionToJs(jsi::Runtime &rt, P4 value) { return bridging::toJs(rt, value); } #endif @@ -287,11 +382,11 @@ protected: NativeEnumTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); public: - virtual jsi::String getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual jsi::String getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual double getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual double getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, double c, double d) = 0; + virtual StatusRegularEnum getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual StatusStrEnum getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual StatusNumEnum getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual StatusFractionEnum getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual jsi::Object getStateType(jsi::Runtime &rt, StatusRegularEnum a, StatusStrEnum b, StatusNumEnum c, StatusFractionEnum d) = 0; virtual jsi::Object getStateTypeWithEnums(jsi::Runtime &rt, jsi::Object paramOfTypeWithEnums) = 0; }; @@ -314,39 +409,39 @@ private: Delegate(T *instance, std::shared_ptr jsInvoker) : NativeEnumTurboModuleCxxSpecJSI(std::move(jsInvoker)), instance_(instance) {} - jsi::String getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusRegularEnum getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusRegular) == 2, \\"Expected getStatusRegular(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusRegular, jsInvoker_, instance_, std::move(statusProp)); } - jsi::String getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusStrEnum getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusStr) == 2, \\"Expected getStatusStr(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusStr, jsInvoker_, instance_, std::move(statusProp)); } - double getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusNumEnum getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusNum) == 2, \\"Expected getStatusNum(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusNum, jsInvoker_, instance_, std::move(statusProp)); } - double getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusFractionEnum getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusFraction) == 2, \\"Expected getStatusFraction(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusFraction, jsInvoker_, instance_, std::move(statusProp)); } - jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, double c, double d) override { + jsi::Object getStateType(jsi::Runtime &rt, StatusRegularEnum a, StatusStrEnum b, StatusNumEnum c, StatusFractionEnum d) override { static_assert( bridging::getParameterCount(&T::getStateType) == 5, \\"Expected getStateType(...) to have 5 parameters\\"); @@ -370,6 +465,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeNullableTurboModuleCxxSpecJSI : public TurboModule { protected: NativeNullableTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -458,6 +555,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeNumberTurboModuleCxxSpecJSI : public TurboModule { protected: NativeNumberTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -510,6 +609,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeObjectTurboModuleCxxSpecJSI : public TurboModule { protected: NativeObjectTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -589,6 +690,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeOptionalObjectTurboModuleCxxSpecJSI : public TurboModule { protected: NativeOptionalObjectTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -632,6 +735,8 @@ private: Delegate delegate_; }; + + #pragma mark - NativePartialAnnotationTurboModuleBaseSomeObj template @@ -747,6 +852,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativePromiseTurboModuleCxxSpecJSI : public TurboModule { protected: NativePromiseTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -799,6 +906,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleBaseAnimal template @@ -987,6 +1096,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleArraysBaseAnimal template @@ -1175,6 +1286,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleNullableBaseAnimal template @@ -1363,6 +1476,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleNullableAndOptionalBaseAnimal template @@ -1553,6 +1668,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleOptionalBaseAnimal template @@ -1743,6 +1860,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeStringTurboModuleCxxSpecJSI : public TurboModule { protected: NativeStringTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -1818,6 +1937,8 @@ exports[`GenerateModuleH can generate a header file NativeModule specs with assu namespace facebook { namespace react { + + class JSI_EXPORT NativeArrayTurboModuleCxxSpecJSI : public TurboModule { protected: NativeArrayTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -1879,6 +2000,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeBooleanTurboModuleCxxSpecJSI : public TurboModule { protected: NativeBooleanTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -1931,6 +2054,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeCallbackTurboModuleCxxSpecJSI : public TurboModule { protected: NativeCallbackTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -1983,6 +2108,95 @@ private: Delegate delegate_; }; +#pragma mark - NativeEnumTurboModuleStatusRegularEnum +enum class NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusRegularEnum &result) { + auto string = (std::string)value; + if (string == \\"Active\\") { result = NativeEnumTurboModuleStatusRegularEnum::Active; return; } + if (string == \\"Paused\\") { result = NativeEnumTurboModuleStatusRegularEnum::Paused; return; } + if (string == \\"Off\\") { result = NativeEnumTurboModuleStatusRegularEnum::Off; return; } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusRegularEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusRegularEnum::Active: return \\"Active\\"; + case NativeEnumTurboModuleStatusRegularEnum::Paused: return \\"Paused\\"; + case NativeEnumTurboModuleStatusRegularEnum::Off: return \\"Off\\"; + } +} +#pragma mark - NativeEnumTurboModuleStatusStrEnum +enum class NativeEnumTurboModuleStatusStrEnum { Active, Paused, Off }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusStrEnum &result) { + auto string = (std::string)value; + if (string == \\"active\\") { result = NativeEnumTurboModuleStatusStrEnum::Active; return; } + if (string == \\"paused\\") { result = NativeEnumTurboModuleStatusStrEnum::Paused; return; } + if (string == \\"off\\") { result = NativeEnumTurboModuleStatusStrEnum::Off; return; } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusStrEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusStrEnum::Active: return \\"active\\"; + case NativeEnumTurboModuleStatusStrEnum::Paused: return \\"paused\\"; + case NativeEnumTurboModuleStatusStrEnum::Off: return \\"off\\"; + } +} +enum class NativeEnumTurboModuleStatusNumEnum { Active = 2, Paused = 1, Off = 0 }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusNumEnum &result) { + assert(value.hasType()); + auto integerValue = (int)value; + switch (integerValue) { + case 2: + result = NativeEnumTurboModuleStatusNumEnum::Active; + return; + case 1: + result = NativeEnumTurboModuleStatusNumEnum::Paused; + return; + case 0: + result = NativeEnumTurboModuleStatusNumEnum::Off; + return; + } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusNumEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusNumEnum::Active: return \\"2\\"; + case NativeEnumTurboModuleStatusNumEnum::Paused: return \\"1\\"; + case NativeEnumTurboModuleStatusNumEnum::Off: return \\"0\\"; + } +} +enum class NativeEnumTurboModuleStatusFractionEnum { Active = 0.2, Paused = 0.1, Off = 0 }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, NativeEnumTurboModuleStatusFractionEnum &result) { + assert(value.hasType()); + auto integerValue = (int)value; + switch (integerValue) { + case 0.2: + result = NativeEnumTurboModuleStatusFractionEnum::Active; + return; + case 0.1: + result = NativeEnumTurboModuleStatusFractionEnum::Paused; + return; + case 0: + result = NativeEnumTurboModuleStatusFractionEnum::Off; + return; + } + abort(); +} + +static inline std::string toString(const NativeEnumTurboModuleStatusFractionEnum &value) { + switch (value) { + case NativeEnumTurboModuleStatusFractionEnum::Active: return \\"0.2\\"; + case NativeEnumTurboModuleStatusFractionEnum::Paused: return \\"0.1\\"; + case NativeEnumTurboModuleStatusFractionEnum::Off: return \\"0\\"; + } +} + #pragma mark - NativeEnumTurboModuleBaseStateType template @@ -2054,16 +2268,16 @@ struct NativeEnumTurboModuleBaseStateTypeWithEnumsBridging { static jsi::String stateToJs(jsi::Runtime &rt, P0 value) { return bridging::toJs(rt, value); } - static jsi::String regularToJs(jsi::Runtime &rt, P1 value) { + static StatusRegularEnum regularToJs(jsi::Runtime &rt, P1 value) { return bridging::toJs(rt, value); } - static jsi::String strToJs(jsi::Runtime &rt, P2 value) { + static StatusStrEnum strToJs(jsi::Runtime &rt, P2 value) { return bridging::toJs(rt, value); } - static double numToJs(jsi::Runtime &rt, P3 value) { + static StatusNumEnum numToJs(jsi::Runtime &rt, P3 value) { return bridging::toJs(rt, value); } - static double fractionToJs(jsi::Runtime &rt, P4 value) { + static StatusFractionEnum fractionToJs(jsi::Runtime &rt, P4 value) { return bridging::toJs(rt, value); } #endif @@ -2087,11 +2301,11 @@ protected: NativeEnumTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); public: - virtual jsi::String getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual jsi::String getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual double getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual double getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) = 0; - virtual jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, double c, double d) = 0; + virtual StatusRegularEnum getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual StatusStrEnum getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual StatusNumEnum getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual StatusFractionEnum getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) = 0; + virtual jsi::Object getStateType(jsi::Runtime &rt, StatusRegularEnum a, StatusStrEnum b, StatusNumEnum c, StatusFractionEnum d) = 0; virtual jsi::Object getStateTypeWithEnums(jsi::Runtime &rt, jsi::Object paramOfTypeWithEnums) = 0; }; @@ -2114,39 +2328,39 @@ private: Delegate(T *instance, std::shared_ptr jsInvoker) : NativeEnumTurboModuleCxxSpecJSI(std::move(jsInvoker)), instance_(instance) {} - jsi::String getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusRegularEnum getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusRegular) == 2, \\"Expected getStatusRegular(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusRegular, jsInvoker_, instance_, std::move(statusProp)); } - jsi::String getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusStrEnum getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusStr) == 2, \\"Expected getStatusStr(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusStr, jsInvoker_, instance_, std::move(statusProp)); } - double getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusNumEnum getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusNum) == 2, \\"Expected getStatusNum(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusNum, jsInvoker_, instance_, std::move(statusProp)); } - double getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) override { + StatusFractionEnum getStatusFraction(jsi::Runtime &rt, jsi::Object statusProp) override { static_assert( bridging::getParameterCount(&T::getStatusFraction) == 2, \\"Expected getStatusFraction(...) to have 2 parameters\\"); - return bridging::callFromJs( + return bridging::callFromJs( rt, &T::getStatusFraction, jsInvoker_, instance_, std::move(statusProp)); } - jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, double c, double d) override { + jsi::Object getStateType(jsi::Runtime &rt, StatusRegularEnum a, StatusStrEnum b, StatusNumEnum c, StatusFractionEnum d) override { static_assert( bridging::getParameterCount(&T::getStateType) == 5, \\"Expected getStateType(...) to have 5 parameters\\"); @@ -2170,6 +2384,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeNullableTurboModuleCxxSpecJSI : public TurboModule { protected: NativeNullableTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -2258,6 +2474,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeNumberTurboModuleCxxSpecJSI : public TurboModule { protected: NativeNumberTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -2310,6 +2528,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeObjectTurboModuleCxxSpecJSI : public TurboModule { protected: NativeObjectTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -2389,6 +2609,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeOptionalObjectTurboModuleCxxSpecJSI : public TurboModule { protected: NativeOptionalObjectTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -2432,6 +2654,8 @@ private: Delegate delegate_; }; + + #pragma mark - NativePartialAnnotationTurboModuleBaseSomeObj template @@ -2547,6 +2771,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativePromiseTurboModuleCxxSpecJSI : public TurboModule { protected: NativePromiseTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -2599,6 +2825,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleBaseAnimal template @@ -2787,6 +3015,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleArraysBaseAnimal template @@ -2975,6 +3205,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleNullableBaseAnimal template @@ -3163,6 +3395,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleNullableAndOptionalBaseAnimal template @@ -3353,6 +3587,8 @@ private: Delegate delegate_; }; + + #pragma mark - SampleTurboModuleOptionalBaseAnimal template @@ -3543,6 +3779,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeStringTurboModuleCxxSpecJSI : public TurboModule { protected: NativeStringTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js index a3156c4d7e49c9..da519a672fadef 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js @@ -17,6 +17,8 @@ import type { NativeModuleFunctionTypeAnnotation, NativeModulePropertyShape, NativeModuleAliasMap, + NativeModuleEnumMap, + NativeModuleEnumMembers, } from '../../CodegenSchema'; import type {AliasResolver} from './Utils'; @@ -29,13 +31,17 @@ type FilesOutput = Map; const ModuleClassDeclarationTemplate = ({ hasteModuleName, moduleProperties, - structs, + structsStr, + enumsStr, }: $ReadOnly<{ hasteModuleName: string, moduleProperties: string[], - structs: string, + structsStr: string, + enumsStr: string, }>) => { - return `${structs}class JSI_EXPORT ${hasteModuleName}CxxSpecJSI : public TurboModule { + return `${enumsStr} + +${structsStr}class JSI_EXPORT ${hasteModuleName}CxxSpecJSI : public TurboModule { protected: ${hasteModuleName}CxxSpecJSI(std::shared_ptr jsInvoker); @@ -155,14 +161,7 @@ function translatePrimitiveJSTypeToCpp( case 'BooleanTypeAnnotation': return wrap('bool'); case 'EnumDeclaration': - switch (realTypeAnnotation.memberType) { - case 'NumberTypeAnnotation': - return wrap('double'); - case 'StringTypeAnnotation': - return wrap('jsi::String'); - default: - throw new Error(createErrorMessage(realTypeAnnotation.type)); - } + return wrap(realTypeAnnotation.name); case 'GenericObjectTypeAnnotation': return wrap('jsi::Object'); case 'UnionTypeAnnotation': @@ -192,13 +191,14 @@ function translatePrimitiveJSTypeToCpp( } } -function createStructs( +function createStructsString( moduleName: string, aliasMap: NativeModuleAliasMap, resolveAlias: AliasResolver, ): string { return Object.keys(aliasMap) .map(alias => { + // TODO: shouldn't this use "resolveAlias" instead? const value = aliasMap[alias]; if (value.properties.length === 0) { return ''; @@ -281,6 +281,183 @@ ${paramemterConversion} .join('\n'); } +// TODO: Do I need fromRawValue and toString? +const EnumTemplate = ({ + enumName, + values, + fromCases, + toCases, +}: { + enumName: string, + values: string, + fromCases: string, + toCases: string, +}) => + `#pragma mark - ${enumName} +enum class ${enumName} { ${values} }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ${enumName} &result) { + auto string = (std::string)value; + ${fromCases} + abort(); +} + +static inline std::string toString(const ${enumName} &value) { + switch (value) { + ${toCases} + } +} +`.trim(); + +const IntEnumTemplate = ({ + enumName, + values, + fromCases, + toCases, +}: { + enumName: string, + values: string, + fromCases: string, + toCases: string, +}) => + ` +enum class ${enumName} { ${values} }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ${enumName} &result) { + assert(value.hasType()); + auto integerValue = (int)value; + switch (integerValue) {${fromCases} + } + abort(); +} + +static inline std::string toString(const ${enumName} &value) { + switch (value) { + ${toCases} + } +} +`.trim(); + +function upperCaseFirst(inString: string): string { + if (inString.length === 0) { + return inString; + } + + return inString[0].toUpperCase() + inString.slice(1); +} + +function toSafeCppString(input: string): string { + return input.split('-').map(upperCaseFirst).join(''); +} + +function convertValueToEnumOption(value: string): string { + return toSafeCppString(value); +} + +function getEnumName(moduleName: string, origEnumName: string): string { + const uppercasedPropName = toSafeCppString(origEnumName); + return `${moduleName}${uppercasedPropName}`; +} + +function generateStringEnum( + moduleName: string, + origEnumName: string, + members: NativeModuleEnumMembers, +): string { + const enumName = getEnumName(moduleName, origEnumName); + + const fromCases = members + .map( + member => + `if (string == "${ + member.value + }") { result = ${enumName}::${convertValueToEnumOption( + member.name, + )}; return; }`, + ) + .join('\n' + ' '); + + const toCases = members + .map( + member => + `case ${enumName}::${convertValueToEnumOption(member.name)}: return "${ + member.value + }";`, + ) + .join('\n' + ' '); + + return EnumTemplate({ + enumName, + values: members + .map(member => member.name) + .map(toSafeCppString) + .join(', '), + fromCases: fromCases, + toCases: toCases, + }); +} + +function generateIntEnum( + moduleName: string, + origEnumName: string, + members: NativeModuleEnumMembers, +): string { + const enumName = getEnumName(moduleName, origEnumName); + + const fromCases = members + .map( + member => + ` + case ${member.value}: + result = ${enumName}::${convertValueToEnumOption(member.name)}; + return;`, + ) + .join(''); + + const toCases = members + .map( + member => + `case ${enumName}::${convertValueToEnumOption(member.name)}: return "${ + member.value + }";`, + ) + .join('\n' + ' '); + + const valueVariables = members + .map(member => `${convertValueToEnumOption(member.name)} = ${member.value}`) + .join(', '); + + return IntEnumTemplate({ + enumName, + values: valueVariables, + fromCases, + toCases, + }); +} + +function createEnumsString( + moduleName: string, + enumMap: NativeModuleEnumMap, + resolveAlias: AliasResolver, +): string { + return Object.entries(enumMap) + .map(([enumName, enumNode]) => { + if (enumNode.memberType === 'StringTypeAnnotation') { + return generateStringEnum(moduleName, enumName, enumNode.members); + } + + if (enumNode.memberType === 'NumberTypeAnnotation') { + return generateIntEnum(moduleName, enumName, enumNode.members); + } + + throw new TypeError( + `Unrecognized enum member type ${enumNode.memberType}`, + ); + }) + .filter(Boolean) + .join('\n'); +} + function translatePropertyToCpp( prop: NativeModulePropertyShape, resolveAlias: AliasResolver, @@ -342,11 +519,17 @@ module.exports = { const modules = Object.keys(nativeModules).flatMap(hasteModuleName => { const { aliasMap, + enumMap, spec: {properties}, moduleName, } = nativeModules[hasteModuleName]; const resolveAlias = createAliasResolver(aliasMap); - const structs = createStructs(moduleName, aliasMap, resolveAlias); + const structsStr = createStructsString( + moduleName, + aliasMap, + resolveAlias, + ); + const enumsStr = createEnumsString(moduleName, enumMap, resolveAlias); return [ ModuleClassDeclarationTemplate({ @@ -354,7 +537,8 @@ module.exports = { moduleProperties: properties.map(prop => translatePropertyToCpp(prop, resolveAlias, true), ), - structs, + structsStr, + enumsStr, }), ModuleSpecClassDeclarationTemplate({ hasteModuleName, diff --git a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js index 5764facb3bc32e..3f850e65b2537c 100644 --- a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js @@ -31,7 +31,53 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { NativeSampleTurboModule: { type: 'NativeModule', aliasMap: {}, - enumMap: {}, + enumMap: { + NumEnum: { + type: 'EnumDeclarationWithMembers', + name: 'NumEnum', + memberType: 'NumberTypeAnnotation', + members: [ + { + name: 'ONE', + value: '1', + }, + { + name: 'TWO', + value: '2', + }, + ], + }, + FloatEnum: { + type: 'EnumDeclarationWithMembers', + name: 'FloatEnum', + memberType: 'StringTypeAnnotation', + members: [ + { + name: 'POINT_ONE', + value: '0.1', + }, + { + name: 'POINT_TWO', + value: '0.2', + }, + ], + }, + StringEnum: { + type: 'EnumDeclarationWithMembers', + name: 'StringEnum', + memberType: 'StringTypeAnnotation', + members: [ + { + name: 'HELLO', + value: 'hello', + }, + { + name: 'GoodBye', + value: 'goodbye', + }, + ], + }, + }, spec: { properties: [ { @@ -309,6 +355,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { name: 'enumInt', optional: false, typeAnnotation: { + name: 'NumEnum', type: 'EnumDeclaration', memberType: 'NumberTypeAnnotation', }, @@ -317,6 +364,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { name: 'enumFloat', optional: false, typeAnnotation: { + name: 'FloatEnum', type: 'EnumDeclaration', memberType: 'NumberTypeAnnotation', }, @@ -325,6 +373,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { name: 'enumString', optional: false, typeAnnotation: { + name: 'StringEnum', type: 'EnumDeclaration', memberType: 'StringTypeAnnotation', }, @@ -1516,7 +1565,53 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = { ], }, }, - enumMap: {}, + enumMap: { + NumEnum: { + type: 'EnumDeclarationWithMembers', + name: 'NumEnum', + memberType: 'NumberTypeAnnotation', + members: [ + { + name: 'ONE', + value: '1', + }, + { + name: 'TWO', + value: '2', + }, + ], + }, + FloatEnum: { + type: 'EnumDeclarationWithMembers', + name: 'FloatEnum', + memberType: 'StringTypeAnnotation', + members: [ + { + name: 'POINT_ONE', + value: '0.1', + }, + { + name: 'POINT_TWO', + value: '0.2', + }, + ], + }, + StringEnum: { + type: 'EnumDeclarationWithMembers', + name: 'StringEnum', + memberType: 'StringTypeAnnotation', + members: [ + { + name: 'HELLO', + value: 'hello', + }, + { + name: 'GoodBye', + value: 'goodbye', + }, + ], + }, + }, spec: { properties: [ { @@ -1577,6 +1672,7 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = { name: 'enumInt', optional: false, typeAnnotation: { + name: 'NumEnum', type: 'EnumDeclaration', memberType: 'NumberTypeAnnotation', }, @@ -1585,6 +1681,7 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = { name: 'enumFloat', optional: false, typeAnnotation: { + name: 'FloatEnum', type: 'EnumDeclaration', memberType: 'NumberTypeAnnotation', }, @@ -1593,6 +1690,7 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = { name: 'enumString', optional: false, typeAnnotation: { + name: 'StringEnum', type: 'EnumDeclaration', memberType: 'StringTypeAnnotation', }, diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap index 82800da2235299..1a17c35e621a43 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap @@ -19,6 +19,8 @@ Map { namespace facebook { namespace react { + + class JSI_EXPORT NativeSampleTurboModuleCxxSpecJSI : public TurboModule { protected: NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -80,6 +82,8 @@ Map { namespace facebook { namespace react { + + class JSI_EXPORT NativeSampleTurboModuleCxxSpecJSI : public TurboModule { protected: NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -202,6 +206,61 @@ Map { namespace facebook { namespace react { +enum class SampleTurboModuleCxxNumEnum { ONE = 1, TWO = 2 }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, SampleTurboModuleCxxNumEnum &result) { + assert(value.hasType()); + auto integerValue = (int)value; + switch (integerValue) { + case 1: + result = SampleTurboModuleCxxNumEnum::ONE; + return; + case 2: + result = SampleTurboModuleCxxNumEnum::TWO; + return; + } + abort(); +} + +static inline std::string toString(const SampleTurboModuleCxxNumEnum &value) { + switch (value) { + case SampleTurboModuleCxxNumEnum::ONE: return \\"1\\"; + case SampleTurboModuleCxxNumEnum::TWO: return \\"2\\"; + } +} +#pragma mark - SampleTurboModuleCxxFloatEnum +enum class SampleTurboModuleCxxFloatEnum { POINT_ONE, POINT_TWO }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, SampleTurboModuleCxxFloatEnum &result) { + auto string = (std::string)value; + if (string == \\"0.1\\") { result = SampleTurboModuleCxxFloatEnum::POINT_ONE; return; } + if (string == \\"0.2\\") { result = SampleTurboModuleCxxFloatEnum::POINT_TWO; return; } + abort(); +} + +static inline std::string toString(const SampleTurboModuleCxxFloatEnum &value) { + switch (value) { + case SampleTurboModuleCxxFloatEnum::POINT_ONE: return \\"0.1\\"; + case SampleTurboModuleCxxFloatEnum::POINT_TWO: return \\"0.2\\"; + } +} +#pragma mark - SampleTurboModuleCxxStringEnum +enum class SampleTurboModuleCxxStringEnum { HELLO, GoodBye }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, SampleTurboModuleCxxStringEnum &result) { + auto string = (std::string)value; + if (string == \\"hello\\") { result = SampleTurboModuleCxxStringEnum::HELLO; return; } + if (string == \\"goodbye\\") { result = SampleTurboModuleCxxStringEnum::GoodBye; return; } + abort(); +} + +static inline std::string toString(const SampleTurboModuleCxxStringEnum &value) { + switch (value) { + case SampleTurboModuleCxxStringEnum::HELLO: return \\"hello\\"; + case SampleTurboModuleCxxStringEnum::GoodBye: return \\"goodbye\\"; + } +} + #pragma mark - SampleTurboModuleCxxBaseObjectAlias template @@ -246,7 +305,7 @@ protected: public: virtual jsi::Value getMixed(jsi::Runtime &rt, jsi::Value arg) = 0; virtual std::optional getNullableNumberFromNullableAlias(jsi::Runtime &rt, std::optional a) = 0; - virtual jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) = 0; + virtual jsi::String getEnums(jsi::Runtime &rt, NumEnum enumInt, FloatEnum enumFloat, StringEnum enumString) = 0; virtual jsi::Object getUnion(jsi::Runtime &rt, double chooseInt, double chooseFloat, jsi::Object chooseObject, jsi::String chooseString) = 0; }; @@ -285,7 +344,7 @@ private: return bridging::callFromJs>( rt, &T::getNullableNumberFromNullableAlias, jsInvoker_, instance_, std::move(a)); } - jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) override { + jsi::String getEnums(jsi::Runtime &rt, NumEnum enumInt, FloatEnum enumFloat, StringEnum enumString) override { static_assert( bridging::getParameterCount(&T::getEnums) == 4, \\"Expected getEnums(...) to have 4 parameters\\"); @@ -334,6 +393,8 @@ Map { namespace facebook { namespace react { + + class JSI_EXPORT NativeSampleTurboModuleCxxSpecJSI : public TurboModule { protected: NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -395,6 +456,8 @@ Map { namespace facebook { namespace react { + + #pragma mark - AliasTurboModuleBaseOptions template @@ -539,6 +602,8 @@ Map { namespace facebook { namespace react { + + #pragma mark - CameraRollManagerBasePhotoIdentifierImage template @@ -846,6 +911,8 @@ private: Delegate delegate_; }; + + #pragma mark - ExceptionsManagerBaseStackFrame template @@ -1099,6 +1166,61 @@ Map { namespace facebook { namespace react { +enum class SampleTurboModuleNumEnum { ONE = 1, TWO = 2 }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, SampleTurboModuleNumEnum &result) { + assert(value.hasType()); + auto integerValue = (int)value; + switch (integerValue) { + case 1: + result = SampleTurboModuleNumEnum::ONE; + return; + case 2: + result = SampleTurboModuleNumEnum::TWO; + return; + } + abort(); +} + +static inline std::string toString(const SampleTurboModuleNumEnum &value) { + switch (value) { + case SampleTurboModuleNumEnum::ONE: return \\"1\\"; + case SampleTurboModuleNumEnum::TWO: return \\"2\\"; + } +} +#pragma mark - SampleTurboModuleFloatEnum +enum class SampleTurboModuleFloatEnum { POINT_ONE, POINT_TWO }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, SampleTurboModuleFloatEnum &result) { + auto string = (std::string)value; + if (string == \\"0.1\\") { result = SampleTurboModuleFloatEnum::POINT_ONE; return; } + if (string == \\"0.2\\") { result = SampleTurboModuleFloatEnum::POINT_TWO; return; } + abort(); +} + +static inline std::string toString(const SampleTurboModuleFloatEnum &value) { + switch (value) { + case SampleTurboModuleFloatEnum::POINT_ONE: return \\"0.1\\"; + case SampleTurboModuleFloatEnum::POINT_TWO: return \\"0.2\\"; + } +} +#pragma mark - SampleTurboModuleStringEnum +enum class SampleTurboModuleStringEnum { HELLO, GoodBye }; + +static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, SampleTurboModuleStringEnum &result) { + auto string = (std::string)value; + if (string == \\"hello\\") { result = SampleTurboModuleStringEnum::HELLO; return; } + if (string == \\"goodbye\\") { result = SampleTurboModuleStringEnum::GoodBye; return; } + abort(); +} + +static inline std::string toString(const SampleTurboModuleStringEnum &value) { + switch (value) { + case SampleTurboModuleStringEnum::HELLO: return \\"hello\\"; + case SampleTurboModuleStringEnum::GoodBye: return \\"goodbye\\"; + } +} + class JSI_EXPORT NativeSampleTurboModuleCxxSpecJSI : public TurboModule { protected: NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -1116,7 +1238,7 @@ public: virtual void getValueWithCallback(jsi::Runtime &rt, jsi::Function callback) = 0; virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0; virtual jsi::Value getValueWithOptionalArg(jsi::Runtime &rt, std::optional parameter) = 0; - virtual jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) = 0; + virtual jsi::String getEnums(jsi::Runtime &rt, NumEnum enumInt, FloatEnum enumFloat, StringEnum enumString) = 0; }; @@ -1234,7 +1356,7 @@ private: return bridging::callFromJs( rt, &T::getValueWithOptionalArg, jsInvoker_, instance_, std::move(parameter)); } - jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) override { + jsi::String getEnums(jsi::Runtime &rt, NumEnum enumInt, FloatEnum enumFloat, StringEnum enumString) override { static_assert( bridging::getParameterCount(&T::getEnums) == 4, \\"Expected getEnums(...) to have 4 parameters\\"); @@ -1275,6 +1397,8 @@ Map { namespace facebook { namespace react { + + class JSI_EXPORT NativeSampleTurboModuleCxxSpecJSI : public TurboModule { protected: NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker); @@ -1318,6 +1442,8 @@ private: Delegate delegate_; }; + + class JSI_EXPORT NativeSampleTurboModule2CxxSpecJSI : public TurboModule { protected: NativeSampleTurboModule2CxxSpecJSI(std::shared_ptr jsInvoker);