Skip to content

Commit

Permalink
Make enums to work as part of data structures for C++ TurboModules co…
Browse files Browse the repository at this point in the history
…degen

Summary:
[Changelog][Internal]

The PR  facebook#36030 (diff D42884147 (facebook@ceb1d0d)) 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 (facebook@963e45a), but there are also some changes related to the types, that were required.

Differential Revision: D43292254

fbshipit-source-id: 50066dc89c495ba3cad23a26024ac485d03dc9d9
  • Loading branch information
rshest authored and facebook-github-bot committed Feb 14, 2023
1 parent 35861fb commit 36b0228
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,19 @@ enum NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
static NativeEnumTurboModuleStatusRegularEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusRegularEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusRegularEnum::Active) {
return bridging::toJs(rt, \\"Active\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Paused) {
return bridging::toJs(rt, \\"Paused\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Off) {
return bridging::toJs(rt, \\"Off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusRegularEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
std::string value = rawValue.utf8(rt);
if (value == \\"Active\\") {
return NativeEnumTurboModuleStatusRegularEnum::Active;
Expand All @@ -205,18 +217,6 @@ struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusRegularEnum value) {
if (value == NativeEnumTurboModuleStatusRegularEnum::Active) {
return bridging::toJs(rt, \\"Active\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Paused) {
return bridging::toJs(rt, \\"Paused\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Off) {
return bridging::toJs(rt, \\"Off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusStrEnum
Expand All @@ -225,7 +225,19 @@ enum NativeEnumTurboModuleStatusStrEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
static NativeEnumTurboModuleStatusStrEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusStrEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusStrEnum::Active) {
return bridging::toJs(rt, \\"active\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Paused) {
return bridging::toJs(rt, \\"paused\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Off) {
return bridging::toJs(rt, \\"off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusStrEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
std::string value = rawValue.utf8(rt);
if (value == \\"active\\") {
return NativeEnumTurboModuleStatusStrEnum::Active;
Expand All @@ -237,18 +249,6 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusStrEnum value) {
if (value == NativeEnumTurboModuleStatusStrEnum::Active) {
return bridging::toJs(rt, \\"active\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Paused) {
return bridging::toJs(rt, \\"paused\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Off) {
return bridging::toJs(rt, \\"off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusNumEnum
Expand All @@ -257,20 +257,7 @@ enum NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, int32_t value) {

if (value == 2) {
return NativeEnumTurboModuleStatusNumEnum::Active;
} else if (value == 1) {
return NativeEnumTurboModuleStatusNumEnum::Paused;
} else if (value == 0) {
return NativeEnumTurboModuleStatusNumEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusNumEnum::Active) {
return bridging::toJs(rt, 2);
} else if (value == NativeEnumTurboModuleStatusNumEnum::Paused) {
Expand All @@ -281,6 +268,19 @@ struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 2) {
return NativeEnumTurboModuleStatusNumEnum::Active;
} else if (value == 1) {
return NativeEnumTurboModuleStatusNumEnum::Paused;
} else if (value == 0) {
return NativeEnumTurboModuleStatusNumEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusFractionEnum
Expand All @@ -289,20 +289,7 @@ enum NativeEnumTurboModuleStatusFractionEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
static NativeEnumTurboModuleStatusFractionEnum fromJs(jsi::Runtime &rt, float value) {

if (value == 0.2f) {
return NativeEnumTurboModuleStatusFractionEnum::Active;
} else if (value == 0.1f) {
return NativeEnumTurboModuleStatusFractionEnum::Paused;
} else if (value == 0f) {
return NativeEnumTurboModuleStatusFractionEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusFractionEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusFractionEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusFractionEnum::Active) {
return bridging::toJs(rt, 0.2f);
} else if (value == NativeEnumTurboModuleStatusFractionEnum::Paused) {
Expand All @@ -313,6 +300,19 @@ struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusFractionEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 0.2f) {
return NativeEnumTurboModuleStatusFractionEnum::Active;
} else if (value == 0.1f) {
return NativeEnumTurboModuleStatusFractionEnum::Paused;
} else if (value == 0f) {
return NativeEnumTurboModuleStatusFractionEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}
};
#pragma mark - NativeEnumTurboModuleBaseStateType

Expand Down Expand Up @@ -2136,7 +2136,19 @@ enum NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
static NativeEnumTurboModuleStatusRegularEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusRegularEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusRegularEnum::Active) {
return bridging::toJs(rt, \\"Active\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Paused) {
return bridging::toJs(rt, \\"Paused\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Off) {
return bridging::toJs(rt, \\"Off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusRegularEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
std::string value = rawValue.utf8(rt);
if (value == \\"Active\\") {
return NativeEnumTurboModuleStatusRegularEnum::Active;
Expand All @@ -2148,18 +2160,6 @@ struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusRegularEnum value) {
if (value == NativeEnumTurboModuleStatusRegularEnum::Active) {
return bridging::toJs(rt, \\"Active\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Paused) {
return bridging::toJs(rt, \\"Paused\\");
} else if (value == NativeEnumTurboModuleStatusRegularEnum::Off) {
return bridging::toJs(rt, \\"Off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusStrEnum
Expand All @@ -2168,7 +2168,19 @@ enum NativeEnumTurboModuleStatusStrEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
static NativeEnumTurboModuleStatusStrEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusStrEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusStrEnum::Active) {
return bridging::toJs(rt, \\"active\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Paused) {
return bridging::toJs(rt, \\"paused\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Off) {
return bridging::toJs(rt, \\"off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusStrEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
std::string value = rawValue.utf8(rt);
if (value == \\"active\\") {
return NativeEnumTurboModuleStatusStrEnum::Active;
Expand All @@ -2180,18 +2192,6 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusStrEnum value) {
if (value == NativeEnumTurboModuleStatusStrEnum::Active) {
return bridging::toJs(rt, \\"active\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Paused) {
return bridging::toJs(rt, \\"paused\\");
} else if (value == NativeEnumTurboModuleStatusStrEnum::Off) {
return bridging::toJs(rt, \\"off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusNumEnum
Expand All @@ -2200,20 +2200,7 @@ enum NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, int32_t value) {

if (value == 2) {
return NativeEnumTurboModuleStatusNumEnum::Active;
} else if (value == 1) {
return NativeEnumTurboModuleStatusNumEnum::Paused;
} else if (value == 0) {
return NativeEnumTurboModuleStatusNumEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusNumEnum::Active) {
return bridging::toJs(rt, 2);
} else if (value == NativeEnumTurboModuleStatusNumEnum::Paused) {
Expand All @@ -2224,6 +2211,19 @@ struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 2) {
return NativeEnumTurboModuleStatusNumEnum::Active;
} else if (value == 1) {
return NativeEnumTurboModuleStatusNumEnum::Paused;
} else if (value == 0) {
return NativeEnumTurboModuleStatusNumEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusFractionEnum
Expand All @@ -2232,20 +2232,7 @@ enum NativeEnumTurboModuleStatusFractionEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
static NativeEnumTurboModuleStatusFractionEnum fromJs(jsi::Runtime &rt, float value) {

if (value == 0.2f) {
return NativeEnumTurboModuleStatusFractionEnum::Active;
} else if (value == 0.1f) {
return NativeEnumTurboModuleStatusFractionEnum::Paused;
} else if (value == 0f) {
return NativeEnumTurboModuleStatusFractionEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusFractionEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusFractionEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == NativeEnumTurboModuleStatusFractionEnum::Active) {
return bridging::toJs(rt, 0.2f);
} else if (value == NativeEnumTurboModuleStatusFractionEnum::Paused) {
Expand All @@ -2256,6 +2243,19 @@ struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}

static NativeEnumTurboModuleStatusFractionEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 0.2f) {
return NativeEnumTurboModuleStatusFractionEnum::Active;
} else if (value == 0.1f) {
return NativeEnumTurboModuleStatusFractionEnum::Paused;
} else if (value == 0f) {
return NativeEnumTurboModuleStatusFractionEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}
};
#pragma mark - NativeEnumTurboModuleBaseStateType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -338,13 +338,13 @@ enum ${enumName} { ${values} };
template <>
struct Bridging<${enumName}> {
static ${enumName} fromJs(jsi::Runtime &rt, ${fromValue}) {
${fromValueConvertion}
${fromCases}
static ${toValue} toJs(jsi::Runtime &rt, ${enumName} value, const std::shared_ptr<CallInvoker> &jsInvoker) {
${toCases}
}
static ${toValue} toJs(jsi::Runtime &rt, ${enumName} value) {
${toCases}
static ${enumName} fromJs(jsi::Runtime &rt, ${fromValue}, const std::shared_ptr<CallInvoker> &jsInvoker) {
${fromValueConversion}
${fromCases}
}
};`;
};
Expand Down
Loading

0 comments on commit 36b0228

Please sign in to comment.