Skip to content

Commit

Permalink
Add simple constructor for JSError
Browse files Browse the repository at this point in the history
Summary:
Original Author: [email protected]
Original Git: ce1957b
Original Reviewed By: avp
Original Revision: D48796703

X-link: facebook/react-native#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: neildhar

Differential Revision: D50992774

fbshipit-source-id: fe3b63040e345f61f8c396d4ec72ded8f6691bf0
  • Loading branch information
avp authored and facebook-github-bot committed Nov 8, 2023
1 parent 2037dd9 commit e1f7d07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions API/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 API/jsi/jsi/jsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,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 e1f7d07

Please sign in to comment.