Skip to content

Commit 603620b

Browse files
appdenfacebook-github-bot
authored andcommitted
Add asBool() method
Summary: Changelog: [General][Added] - Add asBool() method to JSI Reviewed By: mhorowitz Differential Revision: D34630901 fbshipit-source-id: 3007784618fcc0f7828eee42de5cbf2bd71258c5
1 parent 350f8c5 commit 603620b

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

ReactCommon/jsi/jsi/jsi.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,15 @@ bool Value::strictEquals(Runtime& runtime, const Value& a, const Value& b) {
283283
return false;
284284
}
285285

286+
bool Value::asBool() const {
287+
if (!isBool()) {
288+
throw JSINativeException(
289+
"Value is " + kindToString(*this) + ", expected a boolean");
290+
}
291+
292+
return getBool();
293+
}
294+
286295
double Value::asNumber() const {
287296
if (!isNumber()) {
288297
throw JSINativeException(

ReactCommon/jsi/jsi/jsi.h

+4
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,10 @@ class JSI_EXPORT Value {
10781078
return data_.boolean;
10791079
}
10801080

1081+
/// \return the boolean value, or throws JSIException if not a
1082+
/// boolean.
1083+
bool asBool() const;
1084+
10811085
/// \return the number value, or asserts if not a number.
10821086
double getNumber() const {
10831087
assert(isNumber());

ReactCommon/jsi/jsi/test/testlib.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ TEST_P(JSITest, ValueTest) {
849849
EXPECT_EQ(eval("'str'").getString(rt).utf8(rt), "str");
850850
EXPECT_TRUE(eval("[]").getObject(rt).isArray(rt));
851851

852+
EXPECT_TRUE(eval("true").asBool());
853+
EXPECT_THROW(eval("123").asBool(), JSIException);
852854
EXPECT_EQ(eval("456").asNumber(), 456);
853855
EXPECT_THROW(eval("'word'").asNumber(), JSIException);
854856
EXPECT_EQ(

0 commit comments

Comments
 (0)