summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2020-05-14 21:57:14 +0200
committerPhilip Chimento <philip.chimento@gmail.com>2020-10-12 20:00:14 -0700
commit641e62160fb067101fb5b50dc1c5929a26143faa (patch)
tree62617c6939c114b5ade7898186782486a3286c26
parent7130f9500b17406f22c599af5fbcae06fa92df4d (diff)
downloadgjs-641e62160fb067101fb5b50dc1c5929a26143faa.tar.gz
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.
-rw-r--r--gi/arg-types-inl.h5
-rw-r--r--gi/arg.cpp39
-rw-r--r--gi/js-value-inl.h8
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
@@ -937,42 +937,6 @@ static bool gjs_array_to_flat_struct_array(JSContext* cx,
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,
gpointer array,
unsigned length,
@@ -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) {