[gjs] function: Compute the array length coherently to what we do in cache



commit ba0e51f1dfdb7241cf47494114196a3b9610954e
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Oct 7 03:15:13 2020 +0200

    function: Compute the array length coherently to what we do in cache
    
    When computing an array length to pass to a function call we bother
    some JSapi code to get a value (that also may be of an incorrect type)
    while we already compute this value into the arg-cache depending on the
    GIArgument type.
    
    Since this is the correct way of doing this and avoids lots of unneeded
    checks, let's just expose the function and reuse it.
    
    This is also something that will be quite useful to support BigInt as in
    such case we may create objects instead of numbers, causing this to
    fail.

 gi/arg-cache.cpp |  4 ++--
 gi/arg-cache.h   |  3 +++
 gi/function.cpp  | 15 ++++++---------
 3 files changed, 11 insertions(+), 11 deletions(-)
---
diff --git a/gi/arg-cache.cpp b/gi/arg-cache.cpp
index 2c844411..937f1ade 100644
--- a/gi/arg-cache.cpp
+++ b/gi/arg-cache.cpp
@@ -57,8 +57,8 @@ static_assert(G_N_ELEMENTS(expected_type_names) == ExpectedType::LAST,
 
 // A helper function to retrieve array lengths from a GIArgument (letting the
 // compiler generate good instructions in case of big endian machines)
-[[nodiscard]] static size_t gjs_g_argument_get_array_length(GITypeTag tag,
-                                                            GIArgument* arg) {
+[[nodiscard]] size_t gjs_g_argument_get_array_length(GITypeTag tag,
+                                                     GIArgument* arg) {
     switch (tag) {
         case GI_TYPE_TAG_INT8:
             return gjs_arg_get<int8_t>(arg);
diff --git a/gi/arg-cache.h b/gi/arg-cache.h
index 6b19e6d8..54159df9 100644
--- a/gi/arg-cache.h
+++ b/gi/arg-cache.h
@@ -174,4 +174,7 @@ GJS_JSAPI_RETURN_CONVENTION
 bool gjs_arg_cache_build_instance(JSContext* cx, GjsArgumentCache* self,
                                   GICallableInfo* callable);
 
+[[nodiscard]] size_t gjs_g_argument_get_array_length(GITypeTag tag,
+                                                     GIArgument* arg);
+
 #endif  // GI_ARG_CACHE_H_
diff --git a/gi/function.cpp b/gi/function.cpp
index 76f2781a..4f7077f0 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -478,23 +478,20 @@ bool GjsCallbackTrampoline::callback_closure_inner(
                 gint array_length_pos = g_type_info_get_array_length(&type_info);
                 GIArgInfo array_length_arg;
                 GITypeInfo arg_type_info;
-                JS::RootedValue length(context);
 
                 g_callable_info_load_arg(m_info, array_length_pos,
                                          &array_length_arg);
                 g_arg_info_load_type(&array_length_arg, &arg_type_info);
-                if (!gjs_value_from_g_argument(context, &length, &arg_type_info,
-                                               args[array_length_pos + c_args_offset],
-                                               true))
-                    return false;
+                size_t length = gjs_g_argument_get_array_length(
+                    g_type_info_get_tag(&arg_type_info),
+                    args[array_length_pos + c_args_offset]);
 
                 if (!jsargs.growBy(1))
                     g_error("Unable to grow vector");
 
-                if (!gjs_value_from_explicit_array(context, jsargs[n_jsargs++],
-                                                   &type_info,
-                                                   args[i + c_args_offset],
-                                                   length.toInt32()))
+                if (!gjs_value_from_explicit_array(
+                        context, jsargs[n_jsargs++], &type_info,
+                        args[i + c_args_offset], length))
                     return false;
                 break;
             }


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