Skip to content

Commit

Permalink
Add simple constructor for JSError (#39415)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
neildhar authored and facebook-github-bot committed Sep 13, 2023
1 parent dce7242 commit 9faf256
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/react-native/ReactCommon/jsi/jsi/jsi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>(std::move(value))),
message_(std::move(message)),
stack_(std::move(stack)) {}

void JSError::setValue(Runtime& rt, Value&& value) {
value_ = std::make_shared<Value>(std::move(value));

Expand Down
5 changes: 5 additions & 0 deletions packages/react-native/ReactCommon/jsi/jsi/jsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9faf256

Please sign in to comment.