From: antonm@chromium.org Date: Fri, 2 Oct 2009 15:51:07 +0000 (+0000) Subject: Add a method to convert unsigned C integer into V8 Integer. X-Git-Tag: upstream/4.7.83~23196 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8925e8af0a77ea9dca069f65bdee5881144773ee;p=platform%2Fupstream%2Fv8.git Add a method to convert unsigned C integer into V8 Integer. Review URL: http://codereview.chromium.org/260002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3013 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- diff --git a/include/v8.h b/include/v8.h index 4992d75..6d9370f 100644 --- a/include/v8.h +++ b/include/v8.h @@ -1063,6 +1063,7 @@ class V8EXPORT Number : public Primitive { class V8EXPORT Integer : public Number { public: static Local New(int32_t value); + static inline Local New(uint32_t value); int64_t Value() const; static inline Integer* Cast(v8::Value* obj); private: @@ -3026,6 +3027,16 @@ Number* Number::Cast(v8::Value* value) { } +Local Integer::New(uint32_t value) { + // If highest bit is not set, chances are it's SMI. + bool could_be_smi = (value & (1 << 31)) == 0; + if (could_be_smi) { + return Integer::New(static_cast(value)); + } + return Local::Cast(Number::New(value)); +} + + Integer* Integer::Cast(v8::Value* value) { #ifdef V8_ENABLE_CHECKS CheckCast(value); diff --git a/src/checks.h b/src/checks.h index b302e5b..a5d24c5 100644 --- a/src/checks.h +++ b/src/checks.h @@ -80,6 +80,25 @@ static inline void CheckEqualsHelper(const char* file, int line, } } +// Helper function used by the CHECK_EQ function when given int64_t +// arguments. Should not be called directly. +static inline void CheckEqualsHelper(const char* file, int line, + const char* expected_source, + int64_t expected, + const char* value_source, + int64_t value) { + if (expected != value) { + // Sorry, printing int64_t in a fanky hex way, + // that's our mother tongue after all :) + V8_Fatal(file, line, + "CHECK_EQ(%s, %s) failed\n#" + " Expected: 0x%08x%08x\n# Found: 0x%08x%08x", + expected_source, value_source, + uint32_t(expected >> 32), uint32_t(expected), + uint32_t(value >> 32), uint32_t(value)); + } +} + // Helper function used by the CHECK_NE function when given int // arguments. Should not be called directly. diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index f430cbd..5b0a2d4 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -702,6 +702,75 @@ THREADED_TEST(PropertyHandler) { } +THREADED_TEST(TinyInteger) { + v8::HandleScope scope; + LocalContext env; + int32_t value = 239; + Local value_obj = v8::Integer::New(value); + CHECK_EQ(int64_t(value), value_obj->Value()); +} + + +THREADED_TEST(BigSmiInteger) { + v8::HandleScope scope; + LocalContext env; + int32_t value = (1 << 30) - 1; + CHECK(i::Smi::IsValid(value)); + CHECK(!i::Smi::IsValid(value + 1)); + Local value_obj = v8::Integer::New(value); + CHECK_EQ(int64_t(value), value_obj->Value()); +} + + +THREADED_TEST(BigInteger) { + v8::HandleScope scope; + LocalContext env; + int32_t value = (1 << 30) + 1; + CHECK(!i::Smi::IsValid(value)); + Local value_obj = v8::Integer::New(value); + CHECK_EQ(int64_t(value), value_obj->Value()); +} + + +THREADED_TEST(TinyUnsignedInteger) { + v8::HandleScope scope; + LocalContext env; + uint32_t value = 239; + Local value_obj = v8::Integer::New(value); + CHECK_EQ(int64_t(value), value_obj->Value()); +} + + +THREADED_TEST(BigUnsignedSmiInteger) { + v8::HandleScope scope; + LocalContext env; + uint32_t value = (1 << 30) - 1; + CHECK(i::Smi::IsValid(value)); + CHECK(!i::Smi::IsValid(value + 1)); + Local value_obj = v8::Integer::New(value); + CHECK_EQ(int64_t(value), value_obj->Value()); +} + + +THREADED_TEST(BigUnsignedInteger) { + v8::HandleScope scope; + LocalContext env; + uint32_t value = (1 << 30) + 1; + CHECK(!i::Smi::IsValid(value)); + Local value_obj = v8::Integer::New(value); + CHECK_EQ(int64_t(value), value_obj->Value()); +} + + +THREADED_TEST(OutOfSignedRangeUnsignedInteger) { + v8::HandleScope scope; + LocalContext env; + uint32_t value = uint32_t(0xffffffff); + Local value_obj = v8::Integer::New(value); + CHECK_EQ(int64_t(value), value_obj->Value()); +} + + THREADED_TEST(Number) { v8::HandleScope scope; LocalContext env;