[gjs] arg: Simpler is_safe_to_store_in_double()



commit 44aa96511f38c4ca818aeece3f2bdb6dfddd02fe
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Mar 6 21:30:02 2017 -0800

    arg: Simpler is_safe_to_store_in_double()
    
    It could be considered misleading not to simply warn if the value being
    marshalled is > 2^53. In addition, warning only when the value itself is
    truncated is difficult, though not impossible, to do in a cross-platform
    way.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779399

 gi/arg.cpp |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index d1194ef..93f7219 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -2576,7 +2576,8 @@ template<typename T>
 static bool
 is_safe_to_store_in_double(T number)
 {
-    return static_cast<T>(static_cast<double>(number)) == number;
+    static T max_safe_int = T(1) << std::numeric_limits<double>::digits;
+    return (std::abs(number) <= max_safe_int);
 }
 
 bool
@@ -2616,14 +2617,14 @@ gjs_value_from_g_argument (JSContext             *context,
     case GI_TYPE_TAG_INT64:
         if (!is_safe_to_store_in_double(arg->v_int64))
             g_warning("Value %" G_GINT64_FORMAT " cannot be safely stored in "
-                      "a JS Number and will be rounded", arg->v_int64);
+                      "a JS Number and may be rounded", arg->v_int64);
         value_p.setNumber(static_cast<double>(arg->v_int64));
         break;
 
     case GI_TYPE_TAG_UINT64:
         if (!is_safe_to_store_in_double(arg->v_uint64))
             g_warning("Value %" G_GUINT64_FORMAT " cannot be safely stored in "
-                      "a JS Number and will be rounded", arg->v_uint64);
+                      "a JS Number and may be rounded", arg->v_uint64);
         value_p.setNumber(static_cast<double>(arg->v_uint64));
         break;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]