[gjs] function: Add argument names to our function's toString



commit 0023abb891d72136a3422dec0742aa9ccfd50bed
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri May 25 14:11:07 2012 -0400

    function: Add argument names to our function's toString
    
    It's sometimes hard to know what each argument is when translated to JS. This
    should provide a helpful debugging hint for those new to the GNOME JS dev
    process, and also help with debugging hard errors about wrong argument types.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=676833

 gi/function.c |   46 +++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 39 insertions(+), 7 deletions(-)
---
diff --git a/gi/function.c b/gi/function.c
index 576c755..7edd496 100644
--- a/gi/function.c
+++ b/gi/function.c
@@ -1316,6 +1316,9 @@ function_to_string (JSContext *context,
     JSObject *self;
     jsval retval;
     JSBool ret = JS_FALSE;
+    int i, n_args, n_jsargs;
+    GString *arg_names_str;
+    gchar *arg_names;
 
     self = JS_THIS_OBJECT(context, vp);
     if (!self) {
@@ -1324,21 +1327,50 @@ function_to_string (JSContext *context,
     }
 
     priv = priv_from_js (context, self);
-
     if (priv == NULL) {
         string = "function () {\n}";
         free = FALSE;
-    } else if (g_base_info_get_type(priv->info) == GI_INFO_TYPE_FUNCTION) {
-        string = g_strdup_printf("function %s(){\n\t/* proxy for native symbol %s(); */\n}",
+        goto out;
+    }
+
+    free = TRUE;
+
+    n_args = g_callable_info_get_n_args(priv->info);
+    n_jsargs = 0;
+    arg_names_str = g_string_new("");
+    for (i = 0; i < n_args; i++) {
+        GIArgInfo arg_info;
+
+        if (priv->param_types[i] == PARAM_SKIPPED)
+            continue;
+
+        g_callable_info_load_arg(priv->info, i, &arg_info);
+
+        if (g_arg_info_get_direction(&arg_info) == GI_DIRECTION_OUT)
+            continue;
+
+        if (n_jsargs > 0)
+            g_string_append(arg_names_str, ", ");
+
+        n_jsargs++;
+        g_string_append(arg_names_str, g_base_info_get_name(&arg_info));
+    }
+    arg_names = g_string_free(arg_names_str, FALSE);
+
+    if (g_base_info_get_type(priv->info) == GI_INFO_TYPE_FUNCTION) {
+        string = g_strdup_printf("function %s(%s) {\n\t/* proxy for native symbol %s(); */\n}",
                                  g_base_info_get_name ((GIBaseInfo *) priv->info),
+                                 arg_names,
                                  g_function_info_get_symbol ((GIFunctionInfo *) priv->info));
-        free = TRUE;
     } else {
-        string = g_strdup_printf("function %s(){\n\t/* proxy for native symbol */\n}",
-                                 g_base_info_get_name ((GIBaseInfo *) priv->info));
-        free = TRUE;
+        string = g_strdup_printf("function %s(%s) {\n\t/* proxy for native symbol */\n}",
+                                 g_base_info_get_name ((GIBaseInfo *) priv->info),
+                                 arg_names);
     }
 
+    g_free(arg_names);
+
+ out:
     if (gjs_string_from_utf8(context, string, -1, &retval)) {
         JS_SET_RVAL(context, vp, retval);
         ret = JS_TRUE;



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