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 (facebook#36155)

Summary:
Pull Request resolved: facebook#36155

[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.

Reviewed By: christophpurrer

Differential Revision: D43292254

fbshipit-source-id: b2d6cf4a2d4d233b8cc403ecd02b5be16d5d91a7
  • Loading branch information
rshest authored and OlimpiaZurek committed May 22, 2023
1 parent b7e9736 commit 416ace9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ enum NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
static NativeEnumTurboModuleStatusRegularEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
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 @@ -206,7 +206,7 @@ struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusRegularEnum value) {
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) {
Expand All @@ -225,7 +225,7 @@ enum NativeEnumTurboModuleStatusStrEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
static NativeEnumTurboModuleStatusStrEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
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 @@ -238,7 +238,7 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusStrEnum value) {
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) {
Expand All @@ -257,8 +257,8 @@ enum NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, int32_t 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) {
Expand All @@ -270,7 +270,7 @@ struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
}
}

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 @@ -289,8 +289,8 @@ enum NativeEnumTurboModuleStatusFractionEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
static NativeEnumTurboModuleStatusFractionEnum fromJs(jsi::Runtime &rt, float 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) {
Expand All @@ -302,7 +302,7 @@ struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
}
}

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 Down Expand Up @@ -2136,7 +2136,7 @@ enum NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
static NativeEnumTurboModuleStatusRegularEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
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 @@ -2149,7 +2149,7 @@ struct Bridging<NativeEnumTurboModuleStatusRegularEnum> {
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusRegularEnum value) {
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) {
Expand All @@ -2168,7 +2168,7 @@ enum NativeEnumTurboModuleStatusStrEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
static NativeEnumTurboModuleStatusStrEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
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 @@ -2181,7 +2181,7 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusStrEnum value) {
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) {
Expand All @@ -2200,8 +2200,8 @@ enum NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, int32_t 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) {
Expand All @@ -2213,7 +2213,7 @@ struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
}
}

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 @@ -2232,8 +2232,8 @@ enum NativeEnumTurboModuleStatusFractionEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
static NativeEnumTurboModuleStatusFractionEnum fromJs(jsi::Runtime &rt, float 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) {
Expand All @@ -2245,7 +2245,7 @@ struct Bridging<NativeEnumTurboModuleStatusFractionEnum> {
}
}

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 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,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}
}
};`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ enum SampleTurboModuleCxxNumEnum { ONE, TWO };

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

static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 1) {
return SampleTurboModuleCxxNumEnum::ONE;
} else if (value == 2) {
Expand All @@ -222,7 +222,7 @@ struct Bridging<SampleTurboModuleCxxNumEnum> {
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == SampleTurboModuleCxxNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleCxxNumEnum::TWO) {
Expand All @@ -239,8 +239,8 @@ enum SampleTurboModuleCxxFloatEnum { POINT_ZERO, POINT_ONE, POINT_TWO };

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

static SampleTurboModuleCxxFloatEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 0.0f) {
return SampleTurboModuleCxxFloatEnum::POINT_ZERO;
} else if (value == 0.1f) {
Expand All @@ -252,7 +252,7 @@ struct Bridging<SampleTurboModuleCxxFloatEnum> {
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxFloatEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxFloatEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == SampleTurboModuleCxxFloatEnum::POINT_ZERO) {
return bridging::toJs(rt, 0.0f);
} else if (value == SampleTurboModuleCxxFloatEnum::POINT_ONE) {
Expand All @@ -271,7 +271,7 @@ enum SampleTurboModuleCxxStringEnum { HELLO, GoodBye };

template <>
struct Bridging<SampleTurboModuleCxxStringEnum> {
static SampleTurboModuleCxxStringEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
static SampleTurboModuleCxxStringEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
std::string value = rawValue.utf8(rt);
if (value == \\"hello\\") {
return SampleTurboModuleCxxStringEnum::HELLO;
Expand All @@ -282,7 +282,7 @@ struct Bridging<SampleTurboModuleCxxStringEnum> {
}
}

static jsi::String toJs(jsi::Runtime &rt, SampleTurboModuleCxxStringEnum value) {
static jsi::String toJs(jsi::Runtime &rt, SampleTurboModuleCxxStringEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == SampleTurboModuleCxxStringEnum::HELLO) {
return bridging::toJs(rt, \\"hello\\");
} else if (value == SampleTurboModuleCxxStringEnum::GoodBye) {
Expand Down Expand Up @@ -1209,8 +1209,8 @@ enum SampleTurboModuleNumEnum { ONE, TWO };

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

static SampleTurboModuleNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 1) {
return SampleTurboModuleNumEnum::ONE;
} else if (value == 2) {
Expand All @@ -1220,7 +1220,7 @@ struct Bridging<SampleTurboModuleNumEnum> {
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleNumEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleNumEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == SampleTurboModuleNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleNumEnum::TWO) {
Expand All @@ -1237,8 +1237,8 @@ enum SampleTurboModuleFloatEnum { POINT_ZERO, POINT_ONE, POINT_TWO };

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

static SampleTurboModuleFloatEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
double value = (double)rawValue.asNumber();
if (value == 0.0f) {
return SampleTurboModuleFloatEnum::POINT_ZERO;
} else if (value == 0.1f) {
Expand All @@ -1250,7 +1250,7 @@ struct Bridging<SampleTurboModuleFloatEnum> {
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleFloatEnum value) {
static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleFloatEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == SampleTurboModuleFloatEnum::POINT_ZERO) {
return bridging::toJs(rt, 0.0f);
} else if (value == SampleTurboModuleFloatEnum::POINT_ONE) {
Expand All @@ -1269,7 +1269,7 @@ enum SampleTurboModuleStringEnum { HELLO, GoodBye };

template <>
struct Bridging<SampleTurboModuleStringEnum> {
static SampleTurboModuleStringEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
static SampleTurboModuleStringEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue, const std::shared_ptr<CallInvoker> &jsInvoker) {
std::string value = rawValue.utf8(rt);
if (value == \\"hello\\") {
return SampleTurboModuleStringEnum::HELLO;
Expand All @@ -1280,7 +1280,7 @@ struct Bridging<SampleTurboModuleStringEnum> {
}
}

static jsi::String toJs(jsi::Runtime &rt, SampleTurboModuleStringEnum value) {
static jsi::String toJs(jsi::Runtime &rt, SampleTurboModuleStringEnum value, const std::shared_ptr<CallInvoker> &jsInvoker) {
if (value == SampleTurboModuleStringEnum::HELLO) {
return bridging::toJs(rt, \\"hello\\");
} else if (value == SampleTurboModuleStringEnum::GoodBye) {
Expand Down

0 comments on commit 416ace9

Please sign in to comment.