[gjs: 4/5] arg-cache: Save unsigned state as argument flag, sharing the bit with FILENAME




commit 8d42570e00404c15b05fc43bad2e66ecd61c1e60
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Oct 14 15:52:45 2020 +0200

    arg-cache: Save unsigned state as argument flag, sharing the bit with FILENAME
    
    There won't be any case in which a string can be unsigned, or when a
    number is a filename, so we can reuse the same bit safely.

 gi/arg-cache.cpp | 15 ++++++++++-----
 gi/arg-cache.h   |  1 -
 gi/arg.h         |  3 ++-
 3 files changed, 12 insertions(+), 7 deletions(-)
---
diff --git a/gi/arg-cache.cpp b/gi/arg-cache.cpp
index ffa9b7cf..62b98343 100644
--- a/gi/arg-cache.cpp
+++ b/gi/arg-cache.cpp
@@ -7,6 +7,7 @@
 #include <inttypes.h>
 #include <stdint.h>
 #include <string.h>
+#include <limits>
 
 #include <ffi.h>
 #include <girepository.h>
@@ -489,7 +490,7 @@ static bool gjs_marshal_enum_in_in(JSContext* cx, GjsArgumentCache* self,
     // Unpack the values from their uint32_t bitfield. See note in
     // gjs_arg_cache_build_enum_bounds().
     int64_t min, max;
-    if (self->is_unsigned) {
+    if (self->flags & GjsArgumentFlags::UNSIGNED) {
         min = self->contents.enum_type.enum_min;
         max = self->contents.enum_type.enum_max;
     } else {
@@ -503,7 +504,7 @@ static bool gjs_marshal_enum_in_in(JSContext* cx, GjsArgumentCache* self,
         return false;
     }
 
-    if (self->is_unsigned)
+    if (self->flags & GjsArgumentFlags::UNSIGNED)
         gjs_arg_set<unsigned, GI_TYPE_TAG_INTERFACE>(arg, number);
     else
         gjs_arg_set<int, GI_TYPE_TAG_INTERFACE>(arg, number);
@@ -1300,8 +1301,8 @@ bool gjs_arg_cache_build_return(JSContext*, GjsArgumentCache* self,
 
 static void gjs_arg_cache_build_enum_bounds(GjsArgumentCache* self,
                                             GIEnumInfo* enum_info) {
-    int64_t min = G_MAXINT64;
-    int64_t max = G_MININT64;
+    int64_t min = std::numeric_limits<int64_t>::max();
+    int64_t max = std::numeric_limits<int64_t>::min();
     int n = g_enum_info_get_n_values(enum_info);
     for (int i = 0; i < n; i++) {
         GjsAutoValueInfo value_info = g_enum_info_get_value(enum_info, i);
@@ -1320,7 +1321,11 @@ static void gjs_arg_cache_build_enum_bounds(GjsArgumentCache* self,
     // whether we have to compare them as signed.
     self->contents.enum_type.enum_min = static_cast<uint32_t>(min);
     self->contents.enum_type.enum_max = static_cast<uint32_t>(max);
-    self->is_unsigned = min >= 0 && max > G_MAXINT32;
+
+    if (min >= 0 && max > std::numeric_limits<int32_t>::max())
+        self->flags = (self->flags | GjsArgumentFlags::UNSIGNED);
+    else
+        self->flags = (self->flags & ~GjsArgumentFlags::UNSIGNED);
 }
 
 static void gjs_arg_cache_build_flags_mask(GjsArgumentCache* self,
diff --git a/gi/arg-cache.h b/gi/arg-cache.h
index b45b6dbc..67c0e849 100644
--- a/gi/arg-cache.h
+++ b/gi/arg-cache.h
@@ -43,7 +43,6 @@ struct GjsArgumentCache {
     uint8_t arg_pos;
     GITransfer transfer : 2;
     GjsArgumentFlags flags : 5;
-    bool is_unsigned : 1;  // number and enum only
 
     union {
         // for explicit array only
diff --git a/gi/arg.h b/gi/arg.h
index 049da403..6c91f0b8 100644
--- a/gi/arg.h
+++ b/gi/arg.h
@@ -36,7 +36,8 @@ enum class GjsArgumentFlags : uint8_t {
     SKIP_IN = 1 << 2,
     SKIP_OUT = 1 << 3,
     SKIP_ALL = SKIP_IN | SKIP_OUT,
-    FILENAME = 1 << 4,
+    FILENAME = 1 << 4,  //  Sharing the bit with UNSIGNED, used only for strings
+    UNSIGNED = 1 << 4,  //  Sharing the bit with FILENAME, used only for enums
 };
 
 [[nodiscard]] char* gjs_argument_display_name(const char* arg_name,


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