SIGN IN SIGN UP
facebook / react-native UNCLAIMED

A framework for building native applications using React

0 0 37 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(<ReactCodegen/AppSpecsJSI.h>) // CocoaPod headers on Apple
#include <ReactCodegen/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 <optional>
#include <set>
#include <string>
#include <vector>
namespace facebook::react {
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
#pragma mark - Structs
using ConstantsStruct = NativeCxxModuleExampleConstantsStruct<bool, int32_t, std::string>;
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
template <>
struct Bridging<ConstantsStruct> : NativeCxxModuleExampleConstantsStructBridging<ConstantsStruct> {};
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
using ObjectStruct = NativeCxxModuleExampleObjectStruct<int32_t, std::string, std::optional<std::string>>;
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
template <>
struct Bridging<ObjectStruct> : NativeCxxModuleExampleObjectStructBridging<ObjectStruct> {};
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
using ValueStruct = NativeCxxModuleExampleValueStruct<double, std::string, ObjectStruct>;
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
template <>
struct Bridging<ValueStruct> : NativeCxxModuleExampleValueStructBridging<ValueStruct> {};
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
#pragma mark - enums
enum class CustomEnumInt : int32_t { A = 23, B = 42 };
template <>
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
struct Bridging<CustomEnumInt> {
static CustomEnumInt fromJs(jsi::Runtime &rt, jsi::Value rawValue)
{
auto value = static_cast<int32_t>(rawValue.asNumber());
if (value == 23) {
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
return CustomEnumInt::A;
} else if (value == 42) {
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
return CustomEnumInt::B;
} else {
throw jsi::JSError(rt, "Invalid enum value");
}
}
static int32_t toJs(jsi::Runtime &rt, CustomEnumInt value)
{
return bridging::toJs(rt, static_cast<int32_t>(value));
}
};
#pragma mark - jsi::HostObjects
template <typename T>
class HostObjectWrapper : public jsi::HostObject {
public:
HostObjectWrapper(std::shared_ptr<T> value) : value_(std::move(value)) {}
std::shared_ptr<T> getValue() const
{
return value_;
}
~HostObjectWrapper() override = default;
private:
std::shared_ptr<T> value_;
};
struct CustomHostObjectRef {
CustomHostObjectRef(std::string a, int32_t b) : a_(a), b_(b) {}
std::string a_;
int32_t b_;
};
using CustomHostObject = HostObjectWrapper<CustomHostObjectRef>;
#pragma mark - recursive objects
Use hasteModuleName for C++ Turbo Module structs (#44630) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44630 Changelog: [General][Breaking] Use hasteModuleName for C++ Turbo Module structs This changes the names of C++ Turbo Modules structs to use the `hasteModuleName`. Example: `NativeMyAbcModule.js` with this spec: ``` export type ValueStruct = { x: number, y: string, z: ObjectStruct, }; export interface Spec extends TurboModule { +getValueStruct: () => ValueStruct } export default (TurboModuleRegistry.get<Spec>('MyAbcModuleCxx'): ?Spec); ``` Before now we generated a base C++ struct with the name: ``` MyAbcModuleCxxValueStruct ^^^ ``` Now the generate name is: ``` NativeMyAbcModuleValueStruct ^^^^^^ ``` ## Changes: - No `Cxx` injected anymore - Ensure base struct is `Native` prefixed (all RN JS TM specs start with it) ## Why? - The `Cxx` extension is a temporary hint to react-native-codegen to enable extra capabilities and might disappear eventually - The C++ base struct name should be 'stable' - The name of the exported TM JS spec `TurboModuleRegistry.get<Spec>(...)` is abritrary, the hasteName is not - The name of the RN JS TM spec must start with `Native` which better guarantees a consistent naming scheme for these generated base class - The C++ Turbo Module base class has now the same prefix as the generated structs - `NativeMyAbcModule` for the example above Reviewed By: cipolleschi Differential Revision: D57599257 fbshipit-source-id: 4fafe6c7e920737fa766bd7e8e68e521f608e775
2024-05-23 03:28:52 -07:00
using BinaryTreeNode = NativeCxxModuleExampleBinaryTreeNode<int32_t>;
template <>
struct Bridging<BinaryTreeNode> : NativeCxxModuleExampleBinaryTreeNodeBridging<BinaryTreeNode> {};
Use hasteModuleName for C++ Turbo Module structs (#44630) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44630 Changelog: [General][Breaking] Use hasteModuleName for C++ Turbo Module structs This changes the names of C++ Turbo Modules structs to use the `hasteModuleName`. Example: `NativeMyAbcModule.js` with this spec: ``` export type ValueStruct = { x: number, y: string, z: ObjectStruct, }; export interface Spec extends TurboModule { +getValueStruct: () => ValueStruct } export default (TurboModuleRegistry.get<Spec>('MyAbcModuleCxx'): ?Spec); ``` Before now we generated a base C++ struct with the name: ``` MyAbcModuleCxxValueStruct ^^^ ``` Now the generate name is: ``` NativeMyAbcModuleValueStruct ^^^^^^ ``` ## Changes: - No `Cxx` injected anymore - Ensure base struct is `Native` prefixed (all RN JS TM specs start with it) ## Why? - The `Cxx` extension is a temporary hint to react-native-codegen to enable extra capabilities and might disappear eventually - The C++ base struct name should be 'stable' - The name of the exported TM JS spec `TurboModuleRegistry.get<Spec>(...)` is abritrary, the hasteName is not - The name of the RN JS TM spec must start with `Native` which better guarantees a consistent naming scheme for these generated base class - The C++ Turbo Module base class has now the same prefix as the generated structs - `NativeMyAbcModule` for the example above Reviewed By: cipolleschi Differential Revision: D57599257 fbshipit-source-id: 4fafe6c7e920737fa766bd7e8e68e521f608e775
2024-05-23 03:28:52 -07:00
using GraphNode = NativeCxxModuleExampleGraphNode<std::string>;
template <>
struct Bridging<GraphNode> : NativeCxxModuleExampleGraphNodeBridging<GraphNode> {};
#pragma mark - functional object properties
using MenuItem =
NativeCxxModuleExampleMenuItem<std::string, AsyncCallback<std::string, bool>, std::optional<std::string>>;
template <>
Use hasteModuleName for C++ Turbo Module structs (#44630) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44630 Changelog: [General][Breaking] Use hasteModuleName for C++ Turbo Module structs This changes the names of C++ Turbo Modules structs to use the `hasteModuleName`. Example: `NativeMyAbcModule.js` with this spec: ``` export type ValueStruct = { x: number, y: string, z: ObjectStruct, }; export interface Spec extends TurboModule { +getValueStruct: () => ValueStruct } export default (TurboModuleRegistry.get<Spec>('MyAbcModuleCxx'): ?Spec); ``` Before now we generated a base C++ struct with the name: ``` MyAbcModuleCxxValueStruct ^^^ ``` Now the generate name is: ``` NativeMyAbcModuleValueStruct ^^^^^^ ``` ## Changes: - No `Cxx` injected anymore - Ensure base struct is `Native` prefixed (all RN JS TM specs start with it) ## Why? - The `Cxx` extension is a temporary hint to react-native-codegen to enable extra capabilities and might disappear eventually - The C++ base struct name should be 'stable' - The name of the exported TM JS spec `TurboModuleRegistry.get<Spec>(...)` is abritrary, the hasteName is not - The name of the RN JS TM spec must start with `Native` which better guarantees a consistent naming scheme for these generated base class - The C++ Turbo Module base class has now the same prefix as the generated structs - `NativeMyAbcModule` for the example above Reviewed By: cipolleschi Differential Revision: D57599257 fbshipit-source-id: 4fafe6c7e920737fa766bd7e8e68e521f608e775
2024-05-23 03:28:52 -07:00
struct Bridging<MenuItem> : NativeCxxModuleExampleMenuItemBridging<MenuItem> {};
#pragma mark - RCTDeviceEventEmitter events
using CustomDeviceEvent = NativeCxxModuleExampleCustomDeviceEvent<std::string, int32_t, std::optional<float>>;
template <>
struct Bridging<CustomDeviceEvent> : NativeCxxModuleExampleCustomDeviceEventBridging<CustomDeviceEvent> {};
react-native code-gen > C++ TurboModules struct support (#35265) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
2022-11-09 13:23:05 -08:00
#pragma mark - implementation
class NativeCxxModuleExample : public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
public:
NativeCxxModuleExample(std::shared_ptr<CallInvoker> jsInvoker);
void getValueWithCallback(jsi::Runtime &rt, const AsyncCallback<std::string> &callback);
std::function<void()> setValueCallbackWithSubscription(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);
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
std::shared_ptr<CustomHostObject> getCustomHostObject(jsi::Runtime &rt);
std::string consumeCustomHostObject(jsi::Runtime &rt, std::shared_ptr<CustomHostObject> arg);
BinaryTreeNode getBinaryTreeNode(jsi::Runtime &rt, BinaryTreeNode arg);
GraphNode getGraphNode(jsi::Runtime &rt, GraphNode arg);
NativeCxxModuleExampleEnumInt getNumEnum(jsi::Runtime &rt, NativeCxxModuleExampleEnumInt arg);
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
NativeCxxModuleExampleEnumStr getStrEnum(jsi::Runtime &rt, NativeCxxModuleExampleEnumNone 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, const 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);
std::optional<bool> getWithWithOptionalArgs(jsi::Runtime &rt, std::optional<bool> optionalArg);
void voidFunc(jsi::Runtime &rt);
AsyncPromise<> voidPromise(jsi::Runtime &rt);
void setMenu(jsi::Runtime &rt, MenuItem menuItem);
void emitCustomDeviceEvent(jsi::Runtime &rt, const std::string &eventName);
Add error reporting examples to rn-tester turbo modules (#36729) Summary: This PR is adding examples of Turbo Modules functions throwing runtime exceptions and asserts. This should make it easier to collaborate and develop the error reporting for a new architecture that is being discussed in the React Native New Architecture Working Group -> https://github.com/reactwg/react-native-new-architecture/discussions/122. I'm not sure what return type should be used for the JS function returning `Promise<void>` in Cxx, I used [`AsyncPromise<jsi::Value>`](https://github.com/facebook/react-native/pull/36729/files#diff-9cebc75f48fd35fd6fef71138f98dfd0ba28a754b2aab0d6fe44fd685f74ce16R135), what would you use, I've not found `void` type to use? ### Added functions The table shows the current behavior. <table> <tr> <td> Function <td> Description <td> Turbo Module <td> Cxx Module <tr> <td> voidFuncThrows <td> function with return type void throws a runtime exception <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> getObjectThrows <td> function with return type object throws a runtime exception <td> JS error no platform stack trace <td> JS error no native stack trace <tr> <td> promiseThrows <td> function with return type promise throws a runtime exception before settling the promise <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> voidFuncAssert <td> function with return type void asserts <td> platform error no JS stack trace <td> native error no JS stack trace <tr> <td> getObjectAssert <td> function with return type object asserts <td> JS error no platform stack trace <td> native error no JS stack trace <tr> <td> promiseAssert <td> function with return type promise asserts before settling the promise <td> platform error no JS stack trace <td> native error no JS stack trace </table> ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [ADDED] - Error reporting examples in rn-tester turbo modules Pull Request resolved: https://github.com/facebook/react-native/pull/36729 Test Plan: This PR doesn't change any RN behavior. Only shows the current state by adding an example to rn-tester. I'm happy to add these examples to the unit/integration test, just point me to where would be a good place. Reviewed By: rshest Differential Revision: D44623027 Pulled By: javache fbshipit-source-id: d9cc04852b05d810ed11d7a94f1b2d455ef554a5
2023-04-03 08:34:59 -07:00
void voidFuncThrows(jsi::Runtime &rt);
Add error reporting examples to rn-tester turbo modules (#36729) Summary: This PR is adding examples of Turbo Modules functions throwing runtime exceptions and asserts. This should make it easier to collaborate and develop the error reporting for a new architecture that is being discussed in the React Native New Architecture Working Group -> https://github.com/reactwg/react-native-new-architecture/discussions/122. I'm not sure what return type should be used for the JS function returning `Promise<void>` in Cxx, I used [`AsyncPromise<jsi::Value>`](https://github.com/facebook/react-native/pull/36729/files#diff-9cebc75f48fd35fd6fef71138f98dfd0ba28a754b2aab0d6fe44fd685f74ce16R135), what would you use, I've not found `void` type to use? ### Added functions The table shows the current behavior. <table> <tr> <td> Function <td> Description <td> Turbo Module <td> Cxx Module <tr> <td> voidFuncThrows <td> function with return type void throws a runtime exception <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> getObjectThrows <td> function with return type object throws a runtime exception <td> JS error no platform stack trace <td> JS error no native stack trace <tr> <td> promiseThrows <td> function with return type promise throws a runtime exception before settling the promise <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> voidFuncAssert <td> function with return type void asserts <td> platform error no JS stack trace <td> native error no JS stack trace <tr> <td> getObjectAssert <td> function with return type object asserts <td> JS error no platform stack trace <td> native error no JS stack trace <tr> <td> promiseAssert <td> function with return type promise asserts before settling the promise <td> platform error no JS stack trace <td> native error no JS stack trace </table> ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [ADDED] - Error reporting examples in rn-tester turbo modules Pull Request resolved: https://github.com/facebook/react-native/pull/36729 Test Plan: This PR doesn't change any RN behavior. Only shows the current state by adding an example to rn-tester. I'm happy to add these examples to the unit/integration test, just point me to where would be a good place. Reviewed By: rshest Differential Revision: D44623027 Pulled By: javache fbshipit-source-id: d9cc04852b05d810ed11d7a94f1b2d455ef554a5
2023-04-03 08:34:59 -07:00
ObjectStruct getObjectThrows(jsi::Runtime &rt, const ObjectStruct &arg);
Add error reporting examples to rn-tester turbo modules (#36729) Summary: This PR is adding examples of Turbo Modules functions throwing runtime exceptions and asserts. This should make it easier to collaborate and develop the error reporting for a new architecture that is being discussed in the React Native New Architecture Working Group -> https://github.com/reactwg/react-native-new-architecture/discussions/122. I'm not sure what return type should be used for the JS function returning `Promise<void>` in Cxx, I used [`AsyncPromise<jsi::Value>`](https://github.com/facebook/react-native/pull/36729/files#diff-9cebc75f48fd35fd6fef71138f98dfd0ba28a754b2aab0d6fe44fd685f74ce16R135), what would you use, I've not found `void` type to use? ### Added functions The table shows the current behavior. <table> <tr> <td> Function <td> Description <td> Turbo Module <td> Cxx Module <tr> <td> voidFuncThrows <td> function with return type void throws a runtime exception <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> getObjectThrows <td> function with return type object throws a runtime exception <td> JS error no platform stack trace <td> JS error no native stack trace <tr> <td> promiseThrows <td> function with return type promise throws a runtime exception before settling the promise <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> voidFuncAssert <td> function with return type void asserts <td> platform error no JS stack trace <td> native error no JS stack trace <tr> <td> getObjectAssert <td> function with return type object asserts <td> JS error no platform stack trace <td> native error no JS stack trace <tr> <td> promiseAssert <td> function with return type promise asserts before settling the promise <td> platform error no JS stack trace <td> native error no JS stack trace </table> ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [ADDED] - Error reporting examples in rn-tester turbo modules Pull Request resolved: https://github.com/facebook/react-native/pull/36729 Test Plan: This PR doesn't change any RN behavior. Only shows the current state by adding an example to rn-tester. I'm happy to add these examples to the unit/integration test, just point me to where would be a good place. Reviewed By: rshest Differential Revision: D44623027 Pulled By: javache fbshipit-source-id: d9cc04852b05d810ed11d7a94f1b2d455ef554a5
2023-04-03 08:34:59 -07:00
AsyncPromise<> promiseThrows(jsi::Runtime &rt);
Add error reporting examples to rn-tester turbo modules (#36729) Summary: This PR is adding examples of Turbo Modules functions throwing runtime exceptions and asserts. This should make it easier to collaborate and develop the error reporting for a new architecture that is being discussed in the React Native New Architecture Working Group -> https://github.com/reactwg/react-native-new-architecture/discussions/122. I'm not sure what return type should be used for the JS function returning `Promise<void>` in Cxx, I used [`AsyncPromise<jsi::Value>`](https://github.com/facebook/react-native/pull/36729/files#diff-9cebc75f48fd35fd6fef71138f98dfd0ba28a754b2aab0d6fe44fd685f74ce16R135), what would you use, I've not found `void` type to use? ### Added functions The table shows the current behavior. <table> <tr> <td> Function <td> Description <td> Turbo Module <td> Cxx Module <tr> <td> voidFuncThrows <td> function with return type void throws a runtime exception <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> getObjectThrows <td> function with return type object throws a runtime exception <td> JS error no platform stack trace <td> JS error no native stack trace <tr> <td> promiseThrows <td> function with return type promise throws a runtime exception before settling the promise <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> voidFuncAssert <td> function with return type void asserts <td> platform error no JS stack trace <td> native error no JS stack trace <tr> <td> getObjectAssert <td> function with return type object asserts <td> JS error no platform stack trace <td> native error no JS stack trace <tr> <td> promiseAssert <td> function with return type promise asserts before settling the promise <td> platform error no JS stack trace <td> native error no JS stack trace </table> ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [ADDED] - Error reporting examples in rn-tester turbo modules Pull Request resolved: https://github.com/facebook/react-native/pull/36729 Test Plan: This PR doesn't change any RN behavior. Only shows the current state by adding an example to rn-tester. I'm happy to add these examples to the unit/integration test, just point me to where would be a good place. Reviewed By: rshest Differential Revision: D44623027 Pulled By: javache fbshipit-source-id: d9cc04852b05d810ed11d7a94f1b2d455ef554a5
2023-04-03 08:34:59 -07:00
void voidFuncAssert(jsi::Runtime &rt);
Add error reporting examples to rn-tester turbo modules (#36729) Summary: This PR is adding examples of Turbo Modules functions throwing runtime exceptions and asserts. This should make it easier to collaborate and develop the error reporting for a new architecture that is being discussed in the React Native New Architecture Working Group -> https://github.com/reactwg/react-native-new-architecture/discussions/122. I'm not sure what return type should be used for the JS function returning `Promise<void>` in Cxx, I used [`AsyncPromise<jsi::Value>`](https://github.com/facebook/react-native/pull/36729/files#diff-9cebc75f48fd35fd6fef71138f98dfd0ba28a754b2aab0d6fe44fd685f74ce16R135), what would you use, I've not found `void` type to use? ### Added functions The table shows the current behavior. <table> <tr> <td> Function <td> Description <td> Turbo Module <td> Cxx Module <tr> <td> voidFuncThrows <td> function with return type void throws a runtime exception <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> getObjectThrows <td> function with return type object throws a runtime exception <td> JS error no platform stack trace <td> JS error no native stack trace <tr> <td> promiseThrows <td> function with return type promise throws a runtime exception before settling the promise <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> voidFuncAssert <td> function with return type void asserts <td> platform error no JS stack trace <td> native error no JS stack trace <tr> <td> getObjectAssert <td> function with return type object asserts <td> JS error no platform stack trace <td> native error no JS stack trace <tr> <td> promiseAssert <td> function with return type promise asserts before settling the promise <td> platform error no JS stack trace <td> native error no JS stack trace </table> ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [ADDED] - Error reporting examples in rn-tester turbo modules Pull Request resolved: https://github.com/facebook/react-native/pull/36729 Test Plan: This PR doesn't change any RN behavior. Only shows the current state by adding an example to rn-tester. I'm happy to add these examples to the unit/integration test, just point me to where would be a good place. Reviewed By: rshest Differential Revision: D44623027 Pulled By: javache fbshipit-source-id: d9cc04852b05d810ed11d7a94f1b2d455ef554a5
2023-04-03 08:34:59 -07:00
ObjectStruct getObjectAssert(jsi::Runtime &rt, const ObjectStruct &arg);
Add error reporting examples to rn-tester turbo modules (#36729) Summary: This PR is adding examples of Turbo Modules functions throwing runtime exceptions and asserts. This should make it easier to collaborate and develop the error reporting for a new architecture that is being discussed in the React Native New Architecture Working Group -> https://github.com/reactwg/react-native-new-architecture/discussions/122. I'm not sure what return type should be used for the JS function returning `Promise<void>` in Cxx, I used [`AsyncPromise<jsi::Value>`](https://github.com/facebook/react-native/pull/36729/files#diff-9cebc75f48fd35fd6fef71138f98dfd0ba28a754b2aab0d6fe44fd685f74ce16R135), what would you use, I've not found `void` type to use? ### Added functions The table shows the current behavior. <table> <tr> <td> Function <td> Description <td> Turbo Module <td> Cxx Module <tr> <td> voidFuncThrows <td> function with return type void throws a runtime exception <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> getObjectThrows <td> function with return type object throws a runtime exception <td> JS error no platform stack trace <td> JS error no native stack trace <tr> <td> promiseThrows <td> function with return type promise throws a runtime exception before settling the promise <td> platform error no JS stack trace <td> JS error no native stack trace <tr> <td> voidFuncAssert <td> function with return type void asserts <td> platform error no JS stack trace <td> native error no JS stack trace <tr> <td> getObjectAssert <td> function with return type object asserts <td> JS error no platform stack trace <td> native error no JS stack trace <tr> <td> promiseAssert <td> function with return type promise asserts before settling the promise <td> platform error no JS stack trace <td> native error no JS stack trace </table> ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [ADDED] - Error reporting examples in rn-tester turbo modules Pull Request resolved: https://github.com/facebook/react-native/pull/36729 Test Plan: This PR doesn't change any RN behavior. Only shows the current state by adding an example to rn-tester. I'm happy to add these examples to the unit/integration test, just point me to where would be a good place. Reviewed By: rshest Differential Revision: D44623027 Pulled By: javache fbshipit-source-id: d9cc04852b05d810ed11d7a94f1b2d455ef554a5
2023-04-03 08:34:59 -07:00
AsyncPromise<> promiseAssert(jsi::Runtime &rt);
private:
std::optional<AsyncCallback<std::string>> valueCallback_;
};
} // namespace facebook::react