Fix how CefV8ValueImpl differentiates INT and UINT types (fixes #3657)

This commit is contained in:
Nik Pavlov 2024-04-12 15:37:33 +00:00 committed by Marshall Greenblatt
parent f1effba336
commit 96dc172980
2 changed files with 71 additions and 4 deletions

View File

@ -6,6 +6,7 @@
// Otherwise there will be compile errors in wtf/MathExtras.h. // Otherwise there will be compile errors in wtf/MathExtras.h.
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <limits>
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
@ -49,6 +50,8 @@
namespace { namespace {
static const char kCefTrackObject[] = "Cef::TrackObject"; static const char kCefTrackObject[] = "Cef::TrackObject";
constexpr int32_t kMaxInt32 = std::numeric_limits<int32_t>::max();
constexpr uint32_t kMaxInt32AsUint32 = static_cast<uint32_t>(kMaxInt32);
void MessageListenerCallbackImpl(v8::Handle<v8::Message> message, void MessageListenerCallbackImpl(v8::Handle<v8::Message> message,
v8::Handle<v8::Value> data); v8::Handle<v8::Value> data);
@ -1693,12 +1696,13 @@ bool CefV8ValueImpl::IsBool() {
bool CefV8ValueImpl::IsInt() { bool CefV8ValueImpl::IsInt() {
CEF_V8_REQUIRE_ISOLATE_RETURN(false); CEF_V8_REQUIRE_ISOLATE_RETURN(false);
return (type_ == TYPE_INT || type_ == TYPE_UINT); return type_ == TYPE_INT ||
(type_ == TYPE_UINT && uint_value_ <= kMaxInt32AsUint32);
} }
bool CefV8ValueImpl::IsUInt() { bool CefV8ValueImpl::IsUInt() {
CEF_V8_REQUIRE_ISOLATE_RETURN(false); CEF_V8_REQUIRE_ISOLATE_RETURN(false);
return (type_ == TYPE_INT || type_ == TYPE_UINT); return type_ == TYPE_UINT || (type_ == TYPE_INT && int_value_ >= 0);
} }
bool CefV8ValueImpl::IsDouble() { bool CefV8ValueImpl::IsDouble() {
@ -1807,17 +1811,23 @@ bool CefV8ValueImpl::GetBoolValue() {
int32_t CefV8ValueImpl::GetIntValue() { int32_t CefV8ValueImpl::GetIntValue() {
CEF_V8_REQUIRE_ISOLATE_RETURN(0); CEF_V8_REQUIRE_ISOLATE_RETURN(0);
if (type_ == TYPE_INT || type_ == TYPE_UINT) { if (type_ == TYPE_INT) {
return int_value_; return int_value_;
} }
if (type_ == TYPE_UINT && uint_value_ <= kMaxInt32AsUint32) {
return static_cast<int32_t>(uint_value_);
}
return 0; return 0;
} }
uint32_t CefV8ValueImpl::GetUIntValue() { uint32_t CefV8ValueImpl::GetUIntValue() {
CEF_V8_REQUIRE_ISOLATE_RETURN(0); CEF_V8_REQUIRE_ISOLATE_RETURN(0);
if (type_ == TYPE_INT || type_ == TYPE_UINT) { if (type_ == TYPE_UINT) {
return uint_value_; return uint_value_;
} }
if (type_ == TYPE_INT && int_value_ >= 0) {
return static_cast<uint32_t>(int_value_);
}
return 0; return 0;
} }

View File

@ -49,7 +49,9 @@ enum V8TestMode {
V8TEST_NULL_CREATE, V8TEST_NULL_CREATE,
V8TEST_BOOL_CREATE, V8TEST_BOOL_CREATE,
V8TEST_INT_CREATE, V8TEST_INT_CREATE,
V8TEST_NEGATIVE_INT_CREATE,
V8TEST_UINT_CREATE, V8TEST_UINT_CREATE,
V8TEST_BIG_UINT_CREATE,
V8TEST_DOUBLE_CREATE, V8TEST_DOUBLE_CREATE,
V8TEST_DATE_CREATE, V8TEST_DATE_CREATE,
V8TEST_STRING_CREATE, V8TEST_STRING_CREATE,
@ -120,9 +122,15 @@ class V8RendererTest : public ClientAppRenderer::Delegate,
case V8TEST_INT_CREATE: case V8TEST_INT_CREATE:
RunIntCreateTest(); RunIntCreateTest();
break; break;
case V8TEST_NEGATIVE_INT_CREATE:
RunNegativeIntCreateTest();
break;
case V8TEST_UINT_CREATE: case V8TEST_UINT_CREATE:
RunUIntCreateTest(); RunUIntCreateTest();
break; break;
case V8TEST_BIG_UINT_CREATE:
RunBigUIntCreateTest();
break;
case V8TEST_DOUBLE_CREATE: case V8TEST_DOUBLE_CREATE:
RunDoubleCreateTest(); RunDoubleCreateTest();
break; break;
@ -349,6 +357,29 @@ class V8RendererTest : public ClientAppRenderer::Delegate,
DestroyTest(); DestroyTest();
} }
void RunNegativeIntCreateTest() {
CefRefPtr<CefV8Value> value = CefV8Value::CreateInt(-12);
EXPECT_TRUE(value.get());
EXPECT_TRUE(value->IsInt());
EXPECT_TRUE(value->IsDouble());
EXPECT_EQ(-12, value->GetIntValue());
EXPECT_EQ(-12, value->GetDoubleValue());
EXPECT_EQ(0u, value->GetUIntValue());
EXPECT_FALSE(value->IsUInt());
EXPECT_FALSE(value->IsUndefined());
EXPECT_FALSE(value->IsArray());
EXPECT_FALSE(value->IsBool());
EXPECT_FALSE(value->IsDate());
EXPECT_FALSE(value->IsFunction());
EXPECT_FALSE(value->IsNull());
EXPECT_FALSE(value->IsObject());
EXPECT_FALSE(value->IsPromise());
EXPECT_FALSE(value->IsString());
DestroyTest();
}
void RunUIntCreateTest() { void RunUIntCreateTest() {
CefRefPtr<CefV8Value> value = CefV8Value::CreateUInt(12); CefRefPtr<CefV8Value> value = CefV8Value::CreateUInt(12);
EXPECT_TRUE(value.get()); EXPECT_TRUE(value.get());
@ -372,6 +403,30 @@ class V8RendererTest : public ClientAppRenderer::Delegate,
DestroyTest(); DestroyTest();
} }
void RunBigUIntCreateTest() {
uint32_t big_value = 2147483648u;
CefRefPtr<CefV8Value> value = CefV8Value::CreateUInt(big_value);
EXPECT_TRUE(value.get());
EXPECT_TRUE(value->IsUInt());
EXPECT_TRUE(value->IsDouble());
EXPECT_EQ(big_value, value->GetUIntValue());
EXPECT_EQ(big_value, value->GetDoubleValue());
EXPECT_EQ(0, value->GetIntValue());
EXPECT_FALSE(value->IsInt());
EXPECT_FALSE(value->IsUndefined());
EXPECT_FALSE(value->IsArray());
EXPECT_FALSE(value->IsBool());
EXPECT_FALSE(value->IsDate());
EXPECT_FALSE(value->IsFunction());
EXPECT_FALSE(value->IsNull());
EXPECT_FALSE(value->IsObject());
EXPECT_FALSE(value->IsPromise());
EXPECT_FALSE(value->IsString());
DestroyTest();
}
void RunDoubleCreateTest() { void RunDoubleCreateTest() {
CefRefPtr<CefV8Value> value = CefV8Value::CreateDouble(12.1223); CefRefPtr<CefV8Value> value = CefV8Value::CreateDouble(12.1223);
EXPECT_TRUE(value.get()); EXPECT_TRUE(value.get());
@ -3355,7 +3410,9 @@ void CreateV8RendererTests(ClientAppRenderer::DelegateSet& delegates) {
V8_TEST(NullCreate, V8TEST_NULL_CREATE) V8_TEST(NullCreate, V8TEST_NULL_CREATE)
V8_TEST(BoolCreate, V8TEST_BOOL_CREATE) V8_TEST(BoolCreate, V8TEST_BOOL_CREATE)
V8_TEST(IntCreate, V8TEST_INT_CREATE) V8_TEST(IntCreate, V8TEST_INT_CREATE)
V8_TEST(NegativeIntCreate, V8TEST_NEGATIVE_INT_CREATE)
V8_TEST(UIntCreate, V8TEST_UINT_CREATE) V8_TEST(UIntCreate, V8TEST_UINT_CREATE)
V8_TEST(BigUIntCreate, V8TEST_BIG_UINT_CREATE)
V8_TEST(DoubleCreate, V8TEST_DOUBLE_CREATE) V8_TEST(DoubleCreate, V8TEST_DOUBLE_CREATE)
V8_TEST(DateCreate, V8TEST_DATE_CREATE) V8_TEST(DateCreate, V8TEST_DATE_CREATE)
V8_TEST(StringCreate, V8TEST_STRING_CREATE) V8_TEST(StringCreate, V8TEST_STRING_CREATE)