Make enums to work as part of data structures for C++ TurboModules codegen (#36155)

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

[Changelog][Internal]

The PR  https://github.com/facebook/react-native/pull/36030 (diff D42884147 (https://github.com/facebook/react-native/commit/ceb1d0dea694739f357d86296b94f5834e5ee7f7)) added support for enum types in JS to C++ bridging in C++ TurboModules.

This only worked for enums as argument types for exposed methods, but not for the cases when enums are members of complex data structures that are also exposed through a codegen.

This diff fixes this problem, so that codegen now correctly works both with enum types as method arguments, but also as data structure members.

Some part of the change is the same as D42008724 (https://github.com/facebook/react-native/commit/963e45afd1c69771d1d26df8282774c948f762e3), but there are also some changes related to the types, that were required.

Reviewed By: christophpurrer

Differential Revision: D43292254

fbshipit-source-id: b2d6cf4a2d4d233b8cc403ecd02b5be16d5d91a7
This commit is contained in:
Ruslan Shestopalyuk
2023-02-15 06:03:12 -08:00
committed by Facebook GitHub Bot
parent 7208cd6384
commit 96fb708d3e
3 changed files with 50 additions and 50 deletions
@@ -318,18 +318,18 @@ const EnumTemplate = ({
toCases: string,
nativeEnumMemberType: NativeEnumMemberValueType,
}) => {
const fromValue =
const [fromValue, fromValueConversion, toValue] =
nativeEnumMemberType === 'std::string'
? 'const jsi::String &rawValue'
: `${nativeEnumMemberType} value`;
const fromValueConvertion =
nativeEnumMemberType === 'std::string'
? 'std::string value = rawValue.utf8(rt);'
: '';
const toValue =
nativeEnumMemberType === 'std::string' ? 'jsi::String' : 'jsi::Value';
? [
'const jsi::String &rawValue',
'std::string value = rawValue.utf8(rt);',
'jsi::String',
]
: [
'const jsi::Value &rawValue',
'double value = (double)rawValue.asNumber();',
'jsi::Value',
];
return `
#pragma mark - ${enumName}
@@ -338,12 +338,12 @@ enum ${enumName} { ${values} };
template <>
struct Bridging<${enumName}> {
static ${enumName} fromJs(jsi::Runtime &rt, ${fromValue}) {
${fromValueConvertion}
static ${enumName} fromJs(jsi::Runtime &rt, ${fromValue}, const std::shared_ptr<CallInvoker> &jsInvoker) {
${fromValueConversion}
${fromCases}
}
static ${toValue} toJs(jsi::Runtime &rt, ${enumName} value) {
static ${toValue} toJs(jsi::Runtime &rt, ${enumName} value, const std::shared_ptr<CallInvoker> &jsInvoker) {
${toCases}
}
};`;