SIGN IN SIGN UP
facebook / react-native UNCLAIMED

A framework for building native applications using React

/*
* 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"
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
#include <react/debug/react_native_assert.h>
#include <iomanip>
#include <ostream>
#include <sstream>
#include <utility>
namespace facebook::react {
namespace {
std::string to_string_with_precision(double value, int precision = 2) {
std::ostringstream oss;
oss << std::setprecision(precision) << std::fixed << value;
return oss.str();
}
} // namespace
NativeCxxModuleExample::NativeCxxModuleExample(
std::shared_ptr<CallInvoker> jsInvoker)
: NativeCxxModuleExampleCxxSpec(std::move(jsInvoker)) {}
void NativeCxxModuleExample::getValueWithCallback(
jsi::Runtime& /*rt*/,
const AsyncCallback<std::string>& callback) {
callback({"value from callback!"});
}
std::function<void()> NativeCxxModuleExample::setValueCallbackWithSubscription(
jsi::Runtime& /*rt*/,
AsyncCallback<std::string> callback) {
valueCallback_ = std::make_optional(callback);
return [&]() {
if (valueCallback_.has_value()) {
valueCallback_.value()({"value from callback on clean up!"});
valueCallback_ = std::nullopt;
}
};
}
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"};
}
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
CustomEnumInt NativeCxxModuleExample::getCustomEnum(
jsi::Runtime& /*rt*/,
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
CustomEnumInt arg) {
return arg;
}
std::shared_ptr<CustomHostObject> NativeCxxModuleExample::getCustomHostObject(
jsi::Runtime& /*rt*/) {
return std::make_shared<CustomHostObject>(
std::make_shared<CustomHostObjectRef>("answer", 42));
}
std::string NativeCxxModuleExample::consumeCustomHostObject(
jsi::Runtime& /*rt*/,
std::shared_ptr<CustomHostObject> arg) {
auto value = arg->getValue();
return value->a_ + std::to_string(value->b_);
}
BinaryTreeNode NativeCxxModuleExample::getBinaryTreeNode(
jsi::Runtime& /*rt*/,
BinaryTreeNode arg) {
return arg;
}
GraphNode NativeCxxModuleExample::getGraphNode(
jsi::Runtime& /*rt*/,
GraphNode arg) {
if (arg.neighbors) {
arg.neighbors->emplace_back(GraphNode{.label = "top"});
arg.neighbors->emplace_back(GraphNode{.label = "down"});
}
return arg;
}
NativeCxxModuleExampleEnumInt NativeCxxModuleExample::getNumEnum(
jsi::Runtime& /*rt*/,
NativeCxxModuleExampleEnumInt arg) {
return 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 NativeCxxModuleExample::getStrEnum(
jsi::Runtime& /*rt*/,
NativeCxxModuleExampleEnumNone /*arg*/) {
return NativeCxxModuleExampleEnumStr::SB;
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::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,
const std::string& y,
jsi::Object z) {
std::string result =
"x: " + to_string_with_precision(x) + ", y: " + y + ", z: { ";
if (z.hasProperty(rt, "value")) {
result += "value: ";
result +=
to_string_with_precision(z.getProperty(rt, "value").getNumber(), 0);
} 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, std::move(y), std::move(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;
}
std::optional<bool> NativeCxxModuleExample::getWithWithOptionalArgs(
jsi::Runtime& /*rt*/,
std::optional<bool> optionalArg) {
return optionalArg;
}
void NativeCxxModuleExample::voidFunc(jsi::Runtime& /*rt*/) {
// Emit some events
emitOnPress();
emitOnClick<std::string>("value from callback on click!");
emitOnChange(ObjectStruct{1, "two", std::nullopt});
emitOnSubmit(
std::vector{
ObjectStruct{1, "two", std::nullopt},
ObjectStruct{3, "four", std::nullopt},
ObjectStruct{5, "six", std::nullopt}});
emitOnEvent(NativeCxxModuleExampleEnumNone::NA);
}
AsyncPromise<> NativeCxxModuleExample::voidPromise(jsi::Runtime& rt) {
AsyncPromise<> promise(rt, jsInvoker_);
promise.resolve();
return promise;
}
void NativeCxxModuleExample::setMenu(jsi::Runtime& /*rt*/, MenuItem menuItem) {
menuItem.onPress("value", true);
if (menuItem.items) {
for (const auto& subMenuItem : *menuItem.items) {
subMenuItem.onPress("another value", false);
}
}
}
void NativeCxxModuleExample::emitCustomDeviceEvent(
jsi::Runtime& /*rt*/,
const std::string& eventName) {
// Test emitting device events (RCTDeviceEventEmitter.emit) from C++
// TurboModule with arbitrary arguments. This can be called from any thread
emitDeviceEvent(
eventName,
[jsInvoker = jsInvoker_](
jsi::Runtime& rt, std::vector<jsi::Value>& args) {
args.emplace_back(
jsi::Array::createWithElements(
rt,
jsi::Value(true),
jsi::Value(42),
jsi::String::createFromAscii(rt, "stringArg"),
bridging::toJs(
rt, CustomDeviceEvent{"one", 2, std::nullopt}, jsInvoker)));
});
}
void NativeCxxModuleExample::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
throw std::runtime_error("Intentional exception from Cxx voidFuncThrows");
};
ObjectStruct NativeCxxModuleExample::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
throw std::runtime_error("Intentional exception from Cxx getObjectThrows");
};
AsyncPromise<> NativeCxxModuleExample::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
throw std::runtime_error("Intentional exception from Cxx promiseThrows");
};
void NativeCxxModuleExample::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
react_native_assert(false && "Intentional assert from Cxx voidFuncAssert");
};
ObjectStruct NativeCxxModuleExample::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
react_native_assert(false && "Intentional assert from Cxx getObjectAssert");
// Asserts disabled
return {};
};
AsyncPromise<> NativeCxxModuleExample::promiseAssert(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
react_native_assert(false && "Intentional assert from Cxx promiseAssert");
// Asserts disabled
auto promise = AsyncPromise<>(rt, jsInvoker_);
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
promise.reject("Asserts disabled");
return promise;
};
} // namespace facebook::react