[gjs: 3/7] arg: Refactor gjs_g_array_new_for_type() to take GITypeTag




commit ead182fc1cb9af761faec329a28eda487daefad3
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Mar 5 16:02:17 2022 -0800

    arg: Refactor gjs_g_array_new_for_type() to take GITypeTag
    
    Rename it garray_new_for_storage_type() to reflect that we are supposed to
    pass the result of g_type_info_get_storage_type(), and to remove the gjs_
    prefix since it is a static function.
    
    This allows calling this function in a future refactor where we don't have
    access to the GITypeInfo pointer.

 gi/arg.cpp | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index d7f294cc0..f8c7ccc9f 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -971,16 +971,11 @@ static bool gjs_array_to_array(JSContext* context, JS::HandleValue array_value,
     }
 }
 
-GJS_JSAPI_RETURN_CONVENTION
-static GArray*
-gjs_g_array_new_for_type(JSContext    *context,
-                         unsigned int  length,
-                         GITypeInfo   *param_info)
-{
+static GArray* garray_new_for_storage_type(unsigned length,
+                                           GITypeTag storage_type) {
     guint element_size;
-    GITypeTag element_type = g_type_info_get_storage_type(param_info);
 
-    switch (element_type) {
+    switch (storage_type) {
     case GI_TYPE_TAG_BOOLEAN:
         element_size = sizeof(gboolean);
         break;
@@ -1024,9 +1019,7 @@ gjs_g_array_new_for_type(JSContext    *context,
         break;
     case GI_TYPE_TAG_VOID:
     default:
-        gjs_throw(context,
-                  "Unhandled GArray element-type %d", element_type);
-        return NULL;
+        g_assert_not_reached();
     }
 
     return g_array_sized_new(true, false, element_size, length);
@@ -1724,10 +1717,8 @@ bool gjs_value_to_g_argument(JSContext* context, JS::HandleValue value,
         if (array_type == GI_ARRAY_TYPE_C) {
             gjs_arg_set(arg, data.release());
         } else if (array_type == GI_ARRAY_TYPE_ARRAY) {
-            GArray *array = gjs_g_array_new_for_type(context, length, param_info);
-
-            if (!array)
-                return false;
+            GITypeTag storage_type = g_type_info_get_storage_type(param_info);
+            GArray* array = garray_new_for_storage_type(length, storage_type);
 
             if (data)
                 g_array_append_vals(array, data, length);


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