Files
react-native/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h
T
Vitali Zaidman ceb1d0dea6 Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
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
2023-02-13 15:09:44 -08:00

127 lines
3.5 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.
*/
#pragma once
#if __has_include(<React-Codegen/AppSpecsJSI.h>) // CocoaPod headers on Apple
#include <React-Codegen/AppSpecsJSI.h>
#elif __has_include("AppSpecsJSI.h") // Cmake headers on Android
#include "AppSpecsJSI.h"
#else // BUCK headers
#include <AppSpecs/AppSpecsJSI.h>
#endif
#include <memory>
#include <set>
#include <string>
#include <vector>
namespace facebook::react {
#pragma mark - Structs
using ConstantsStruct =
NativeCxxModuleExampleCxxBaseConstantsStruct<bool, int32_t, std::string>;
template <>
struct Bridging<ConstantsStruct>
: NativeCxxModuleExampleCxxBaseConstantsStructBridging<
bool,
int32_t,
std::string> {};
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
using ValueStruct =
NativeCxxModuleExampleCxxBaseValueStruct<double, std::string, ObjectStruct>;
template <>
struct Bridging<ValueStruct> : NativeCxxModuleExampleCxxBaseValueStructBridging<
double,
std::string,
ObjectStruct> {};
#pragma mark - enums
enum CustomEnumInt { A = 23, B = 42 };
template <>
struct Bridging<CustomEnumInt> {
static CustomEnumInt fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 23) {
return CustomEnumInt::A;
} else if (value == 42) {
return CustomEnumInt::B;
} else {
throw jsi::JSError(rt, "Invalid enum value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, CustomEnumInt value) {
return bridging::toJs(rt, static_cast<int32_t>(value));
}
};
#pragma mark - implementation
class NativeCxxModuleExample
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
public:
NativeCxxModuleExample(std::shared_ptr<CallInvoker> jsInvoker);
void getValueWithCallback(
jsi::Runtime &rt,
AsyncCallback<std::string> callback);
std::vector<std::optional<ObjectStruct>> getArray(
jsi::Runtime &rt,
std::vector<std::optional<ObjectStruct>> arg);
bool getBool(jsi::Runtime &rt, bool arg);
ConstantsStruct getConstants(jsi::Runtime &rt);
CustomEnumInt getCustomEnum(jsi::Runtime &rt, CustomEnumInt arg);
NativeCxxModuleExampleCxxEnumFloat getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg);
NativeCxxModuleExampleCxxEnumStr getStrEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumNone arg);
std::map<std::string, std::optional<int32_t>> getMap(
jsi::Runtime &rt,
std::map<std::string, std::optional<int32_t>> arg);
double getNumber(jsi::Runtime &rt, double arg);
ObjectStruct getObject(jsi::Runtime &rt, ObjectStruct arg);
std::set<float> getSet(jsi::Runtime &rt, std::set<float> arg);
std::string getString(jsi::Runtime &rt, std::string arg);
std::string getUnion(jsi::Runtime &rt, float x, std::string y, jsi::Object z);
ValueStruct
getValue(jsi::Runtime &rt, double x, std::string y, ObjectStruct z);
AsyncPromise<std::string> getValueWithPromise(jsi::Runtime &rt, bool error);
void voidFunc(jsi::Runtime &rt);
};
} // namespace facebook::react