mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
ceb1d0dea6
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36030 Generate enum types in c++ turbo modules. For enums in the ts schema file such as: ``` export enum NumEnum { ONE = 1, TWO = 2, } ``` This would export enums and the relevant Bridging to js and from js code to the spec H files such as: ``` #pragma mark - SampleTurboModuleCxxNumEnum enum SampleTurboModuleCxxNumEnum { ONE, TWO }; template <> struct Bridging<SampleTurboModuleCxxNumEnum> { static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) { if (value == 1) { return SampleTurboModuleCxxNumEnum::ONE; } else if (value == 2) { return SampleTurboModuleCxxNumEnum::TWO; } else { throw jsi::JSError(rt, "No appropriate enum member found for value"); } } static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) { if (value == SampleTurboModuleCxxNumEnum::ONE) { return bridging::toJs(rt, 1); } else if (value == SampleTurboModuleCxxNumEnum::TWO) { return bridging::toJs(rt, 2); } else { throw jsi::JSError(rt, "No appropriate enum member found for enum value"); } } }; ``` That code would allow us to use these enums in the cxx files like this: ``` NativeCxxModuleExampleCxxEnumInt getNumEnum( jsi::Runtime &rt, NativeCxxModuleExampleCxxEnumInt arg); ``` Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators Reviewed By: christophpurrer Differential Revision: D42884147 fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
125 lines
3.1 KiB
C++
125 lines
3.1 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "NativeCxxModuleExample.h"
|
|
|
|
namespace facebook::react {
|
|
|
|
NativeCxxModuleExample::NativeCxxModuleExample(
|
|
std::shared_ptr<CallInvoker> jsInvoker)
|
|
: NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {}
|
|
|
|
void NativeCxxModuleExample::getValueWithCallback(
|
|
jsi::Runtime &rt,
|
|
AsyncCallback<std::string> callback) {
|
|
callback({"value from callback!"});
|
|
}
|
|
|
|
std::vector<std::optional<ObjectStruct>> NativeCxxModuleExample::getArray(
|
|
jsi::Runtime &rt,
|
|
std::vector<std::optional<ObjectStruct>> arg) {
|
|
return arg;
|
|
}
|
|
|
|
bool NativeCxxModuleExample::getBool(jsi::Runtime &rt, bool arg) {
|
|
return arg;
|
|
}
|
|
|
|
ConstantsStruct NativeCxxModuleExample::getConstants(jsi::Runtime &rt) {
|
|
return ConstantsStruct{true, 69, "react-native"};
|
|
}
|
|
|
|
CustomEnumInt NativeCxxModuleExample::getCustomEnum(
|
|
jsi::Runtime &rt,
|
|
CustomEnumInt arg) {
|
|
return arg;
|
|
}
|
|
|
|
NativeCxxModuleExampleCxxEnumFloat NativeCxxModuleExample::getNumEnum(
|
|
jsi::Runtime &rt,
|
|
NativeCxxModuleExampleCxxEnumInt arg) {
|
|
return NativeCxxModuleExampleCxxEnumFloat::FB;
|
|
}
|
|
|
|
NativeCxxModuleExampleCxxEnumStr NativeCxxModuleExample::getStrEnum(
|
|
jsi::Runtime &rt,
|
|
NativeCxxModuleExampleCxxEnumNone arg) {
|
|
return NativeCxxModuleExampleCxxEnumStr::SB;
|
|
}
|
|
|
|
std::map<std::string, std::optional<int32_t>> NativeCxxModuleExample::getMap(
|
|
jsi::Runtime &rt,
|
|
std::map<std::string, std::optional<int32_t>> arg) {
|
|
return arg;
|
|
}
|
|
|
|
double NativeCxxModuleExample::getNumber(jsi::Runtime &rt, double arg) {
|
|
return arg;
|
|
}
|
|
|
|
ObjectStruct NativeCxxModuleExample::getObject(
|
|
jsi::Runtime &rt,
|
|
ObjectStruct arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::set<float> NativeCxxModuleExample::getSet(
|
|
jsi::Runtime &rt,
|
|
std::set<float> arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::string NativeCxxModuleExample::getString(
|
|
jsi::Runtime &rt,
|
|
std::string arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::string NativeCxxModuleExample::getUnion(
|
|
jsi::Runtime &rt,
|
|
float x,
|
|
std::string y,
|
|
jsi::Object z) {
|
|
std::string result = "x: " + std::to_string(x) + ", y: " + y + ", z: { ";
|
|
if (z.hasProperty(rt, "value")) {
|
|
result += "value: ";
|
|
result += std::to_string(z.getProperty(rt, "value").getNumber());
|
|
} else if (z.hasProperty(rt, "low")) {
|
|
result += "low: ";
|
|
result += z.getProperty(rt, "low").getString(rt).utf8(rt);
|
|
}
|
|
result += " }";
|
|
return result;
|
|
}
|
|
|
|
ValueStruct NativeCxxModuleExample::getValue(
|
|
jsi::Runtime &rt,
|
|
double x,
|
|
std::string y,
|
|
ObjectStruct z) {
|
|
ValueStruct result{x, y, z};
|
|
return result;
|
|
}
|
|
|
|
AsyncPromise<std::string> NativeCxxModuleExample::getValueWithPromise(
|
|
jsi::Runtime &rt,
|
|
bool error) {
|
|
auto promise = AsyncPromise<std::string>(rt, jsInvoker_);
|
|
if (error) {
|
|
promise.reject("intentional promise rejection");
|
|
} else {
|
|
promise.resolve("result!");
|
|
}
|
|
return promise;
|
|
}
|
|
|
|
void NativeCxxModuleExample::voidFunc(jsi::Runtime &rt) {
|
|
// Nothing to do
|
|
}
|
|
|
|
} // namespace facebook::react
|