From 9faf256949d504d556bdd785fd45c7c71ba229a2 Mon Sep 17 00:00:00 2001 From: Neil Dhar Date: Wed, 13 Sep 2023 14:37:29 -0700 Subject: [PATCH] Add simple constructor for JSError (#39415) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39415 Add a simple constructor for `JSError` which does not accept a `jsi::Runtime` and cannot call back into JSI. This guarantees that the constructor cannot recursively invoke itself, leading to stack overflows. Changelog: [Internal] Reviewed By: avp Differential Revision: D48796703 fbshipit-source-id: 1c134e8a59ff54be64a5da901e548436d512c21d --- packages/react-native/ReactCommon/jsi/jsi/jsi.cpp | 6 ++++++ packages/react-native/ReactCommon/jsi/jsi/jsi.h | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp index 25fd946bb30..2d1003f1f83 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp +++ b/packages/react-native/ReactCommon/jsi/jsi/jsi.cpp @@ -450,6 +450,12 @@ JSError::JSError(std::string what, Runtime& rt, Value&& value) setValue(rt, std::move(value)); } +JSError::JSError(Value&& value, std::string message, std::string stack) + : JSIException(message + "\n\n" + stack), + value_(std::make_shared(std::move(value))), + message_(std::move(message)), + stack_(std::move(stack)) {} + void JSError::setValue(Runtime& rt, Value&& value) { value_ = std::make_shared(std::move(value)); diff --git a/packages/react-native/ReactCommon/jsi/jsi/jsi.h b/packages/react-native/ReactCommon/jsi/jsi/jsi.h index 917347367d3..e5112b753b6 100644 --- a/packages/react-native/ReactCommon/jsi/jsi/jsi.h +++ b/packages/react-native/ReactCommon/jsi/jsi/jsi.h @@ -1468,6 +1468,11 @@ class JSI_EXPORT JSError : public JSIException { /// but necessary to avoid ambiguity with the above. JSError(std::string what, Runtime& rt, Value&& value); + /// Creates a JSError referring to the provided value, message and stack. This + /// constructor does not take a Runtime parameter, and therefore cannot result + /// in recursively invoking the JSError constructor. + JSError(Value&& value, std::string message, std::string stack); + JSError(const JSError&) = default; virtual ~JSError();