[gjs] arg: Convert GValue arrays using auto function



commit 641e62160fb067101fb5b50dc1c5929a26143faa
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Thu May 14 21:57:14 2020 +0200

    arg: Convert GValue arrays using auto function
    
    GValue arrays are parsed as the other kinds of arrays, once we set up a
    proper js_value_to_c() for it, we can just rely on the default
    implementation.

 gi/arg-types-inl.h |  5 +++++
 gi/arg.cpp         | 39 ++-------------------------------------
 gi/js-value-inl.h  |  8 ++++++++
 3 files changed, 15 insertions(+), 37 deletions(-)
---
diff --git a/gi/arg-types-inl.h b/gi/arg-types-inl.h
index 16922189..8f9e0952 100644
--- a/gi/arg-types-inl.h
+++ b/gi/arg-types-inl.h
@@ -92,4 +92,9 @@ constexpr inline const char* static_type_name<gboolean, GI_TYPE_TAG_BOOLEAN>() {
     return "boolean";
 }
 
+template <>
+constexpr inline const char* static_type_name<GValue>() {
+    return "GValue";
+}
+
 }  // namespace Gjs
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 2ba60f55..17100dbb 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -935,42 +935,6 @@ static bool gjs_array_to_flat_struct_array(JSContext* cx,
     return true;
 }
 
-GJS_JSAPI_RETURN_CONVENTION
-static bool
-gjs_array_to_flat_gvalue_array(JSContext   *context,
-                               JS::Value    array_value,
-                               unsigned int length,
-                               void       **arr_p)
-{
-    GValue *values = g_new0(GValue, length);
-    unsigned int i;
-    bool result = true;
-    JS::RootedObject array(context, array_value.toObjectOrNull());
-    JS::RootedValue elem(context);
-
-    for (i = 0; i < length; i ++) {
-        elem = JS::UndefinedValue();
-
-        if (!JS_GetElement(context, array, i, &elem)) {
-            g_free(values);
-            gjs_throw(context,
-                      "Missing array element %u",
-                      i);
-            return false;
-        }
-
-        result = gjs_value_to_g_value(context, elem, &values[i]);
-
-        if (!result)
-            break;
-    }
-
-    if (result)
-        *arr_p = values;
-
-    return result;
-}
-
 GJS_JSAPI_RETURN_CONVENTION
 static bool
 gjs_array_from_flat_gvalue_array(JSContext             *context,
@@ -1060,7 +1024,8 @@ static bool gjs_array_to_array(JSContext* context, JS::HandleValue array_value,
 
     /* Special case for GValue "flat arrays" */
     if (is_gvalue_flat_array(param_info, element_type))
-        return gjs_array_to_flat_gvalue_array(context, array_value, length, arr_p);
+        return gjs_array_to_auto_array<GValue>(context, array_value, length,
+                                               arr_p);
 
     switch (element_type) {
     case GI_TYPE_TAG_UTF8:
diff --git a/gi/js-value-inl.h b/gi/js-value-inl.h
index 84c25107..77c0aab0 100644
--- a/gi/js-value-inl.h
+++ b/gi/js-value-inl.h
@@ -12,6 +12,7 @@
 #include <js/Conversions.h>
 
 #include "gi/gtype.h"
+#include "gi/value.h"
 #include "gjs/macros.h"
 
 namespace Gjs {
@@ -164,6 +165,13 @@ GJS_JSAPI_RETURN_CONVENTION inline bool js_value_to_c<GI_TYPE_TAG_GTYPE>(
     return true;
 }
 
+template <>
+GJS_JSAPI_RETURN_CONVENTION inline bool js_value_to_c(
+    JSContext* cx, const JS::HandleValue& value, GValue* out) {
+    *out = G_VALUE_INIT;
+    return gjs_value_to_g_value(cx, value, out);
+}
+
 template <typename WantedType, typename T>
 GJS_JSAPI_RETURN_CONVENTION inline bool js_value_to_c_checked(
     JSContext* cx, const JS::HandleValue& value, T* out, bool* out_of_range) {


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