mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Add isInteger to jsi::Value (#56161)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56161 Add a `isInteger` method under `jsi::Value` that returns true iff `Number.isInteger` returns true. This can be implemented without relying on the Runtime. Changelog: [Internal] Reviewed By: lavenzg Differential Revision: D96175707 fbshipit-source-id: af482ae512ffc53f18c1686dd8a2a95521d0bc59
This commit is contained in:
1 parent
a52596e1ce
commit
07c00f4332
2 files changed
+59
No files matched your search
@@ -8,6 +8,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
@@ -1899,6 +1900,28 @@ class JSI_EXPORT Value {
|
||||
return kind_ == ObjectKind;
|
||||
}
|
||||
|
||||
/// \returns true if `Number.isInteger(value)` returns true, false otherwise.
|
||||
bool isInteger() const {
|
||||
// 1. If number is an integral Number, return true.
|
||||
// 2. Return false.
|
||||
|
||||
// The spec's definition of integral number:
|
||||
// When the term integer is used in this specification, it refers to a
|
||||
// mathematical value which is in the set of integers, unless otherwise
|
||||
// stated. When the term integral Number is used in this specification, it
|
||||
// refers to a finite Number value whose mathematical value is in the set of
|
||||
// integers.
|
||||
|
||||
if (!isNumber()) {
|
||||
return false;
|
||||
}
|
||||
auto number = data_.number;
|
||||
if (!std::isfinite(number)) {
|
||||
return false;
|
||||
}
|
||||
return number == std::trunc(number);
|
||||
}
|
||||
|
||||
/// \return the boolean value, or asserts if not a boolean.
|
||||
bool getBool() const {
|
||||
assert(isBool());
|
||||
|
||||
@@ -974,6 +974,42 @@ TEST_P(JSITest, ValueTest) {
|
||||
EXPECT_EQ(eval("['zero',1,2,3]").toString(rt).utf8(rt), "zero,1,2,3");
|
||||
}
|
||||
|
||||
TEST_P(JSITest, IsIntegerTest) {
|
||||
// Non-number values should return false
|
||||
EXPECT_FALSE(Value::undefined().isInteger());
|
||||
EXPECT_FALSE(Value::null().isInteger());
|
||||
EXPECT_FALSE(Value(true).isInteger());
|
||||
EXPECT_FALSE(Value(false).isInteger());
|
||||
EXPECT_FALSE(Value(rt, String::createFromAscii(rt, "42")).isInteger());
|
||||
EXPECT_FALSE(Value(rt, Object(rt)).isInteger());
|
||||
|
||||
// NaN should return false
|
||||
EXPECT_FALSE(Value(std::numeric_limits<double>::quiet_NaN()).isInteger());
|
||||
|
||||
// Infinity should return false
|
||||
EXPECT_FALSE(Value(std::numeric_limits<double>::infinity()).isInteger());
|
||||
EXPECT_FALSE(Value(-std::numeric_limits<double>::infinity()).isInteger());
|
||||
|
||||
// Non-integer numbers should return false
|
||||
EXPECT_FALSE(Value(1.5).isInteger());
|
||||
EXPECT_FALSE(Value(-2.3).isInteger());
|
||||
EXPECT_FALSE(Value(0.1).isInteger());
|
||||
|
||||
// Integer numbers should return true
|
||||
EXPECT_TRUE(Value(0).isInteger());
|
||||
EXPECT_TRUE(Value(0.0).isInteger());
|
||||
EXPECT_TRUE(Value(-0.0).isInteger());
|
||||
EXPECT_TRUE(Value(1).isInteger());
|
||||
EXPECT_TRUE(Value(-1).isInteger());
|
||||
EXPECT_TRUE(Value(42).isInteger());
|
||||
EXPECT_TRUE(Value(1000000).isInteger());
|
||||
EXPECT_TRUE(Value(-999999).isInteger());
|
||||
|
||||
// Large integers that can be exactly represented as doubles
|
||||
EXPECT_TRUE(Value(static_cast<double>(1LL << 52)).isInteger());
|
||||
EXPECT_TRUE(Value(static_cast<double>(-(1LL << 52))).isInteger());
|
||||
}
|
||||
|
||||
TEST_P(JSITest, EqualsTest) {
|
||||
EXPECT_TRUE(Object::strictEquals(rt, rt.global(), rt.global()));
|
||||
EXPECT_TRUE(Value::strictEquals(rt, 1, 1));
|
||||
|
||||
Reference in new issue
Block a user