[gjs] jsapi-util: Add a maybe prefix flag for gjs_parse_args



commit ef4e523b75144da4a1e61308e8fbe0d269da8893
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Jan 2 15:13:37 2013 -0500

    jsapi-util: Add a maybe prefix flag for gjs_parse_args
    
    This allows users to have "maybe" values where the C value
    is treated as NULL.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=691039

 gjs/jsapi-util.c         |   36 +++++++++++++++++++++++-------------
 modules/gettext-native.c |    4 ++--
 2 files changed, 25 insertions(+), 15 deletions(-)
---
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 39194f4..3198ec7 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -1117,7 +1117,6 @@ gjs_value_to_int64  (JSContext  *context,
  *
  * b: A boolean
  * s: A string, converted into UTF-8
- * z: Like 's', but may be null in JavaScript (which appears as NULL in C)
  * F: A string, converted into "filename encoding" (i.e. active locale)
  * i: A number, will be converted to a C "gint32"
  * u: A number, converted into a C "guint32"
@@ -1130,6 +1129,9 @@ gjs_value_to_int64  (JSContext  *context,
  * The '|' character introduces optional arguments.  All format specifiers
  * after a '|' when not specified, do not cause any changes in the C
  * value location.
+ *
+ * A prefix character '?' means that the next value may be null, in
+ * which case the C value %NULL is returned.
  */
 JSBool
 gjs_parse_args (JSContext  *context,
@@ -1164,6 +1166,8 @@ gjs_parse_args (JSContext  *context,
         case '|':
             n_required = n_total;
             continue;
+        case '?':
+            continue;
         default:
             break;
         }
@@ -1213,6 +1217,16 @@ gjs_parse_args (JSContext  *context,
 
         js_value = argv[consumed_args];
 
+        if (*fmt_iter == '?') {
+            fmt_iter++;
+
+            if (JSVAL_IS_NULL (js_value)) {
+                gpointer *arg = arg_location;
+                *arg = NULL;
+                goto got_value;
+            }
+        }
+
         switch (*fmt_iter) {
         case 'b': {
             if (!JSVAL_IS_BOOLEAN(js_value)) {
@@ -1232,21 +1246,16 @@ gjs_parse_args (JSContext  *context,
             }
         }
             break;
-        case 's':
-        case 'z': {
+        case 's': {
             char **arg = arg_location;
 
-            if (*fmt_iter == 'z' && JSVAL_IS_NULL(js_value)) {
-                *arg = NULL;
+            if (gjs_string_to_utf8 (context, js_value, arg)) {
+                unwind_strings[n_unwind++] = *arg;
+                g_assert(n_unwind < MAX_UNWIND_STRINGS);
             } else {
-                if (gjs_string_to_utf8 (context, js_value, arg)) {
-                    unwind_strings[n_unwind++] = *arg;
-                    g_assert(n_unwind < MAX_UNWIND_STRINGS);
-                } else {
-                    /* Our error message is going to be more useful */
-                    JS_ClearPendingException(context);
-                    arg_error_message = "Couldn't convert to string";
-                }
+                /* Our error message is going to be more useful */
+                JS_ClearPendingException(context);
+                arg_error_message = "Couldn't convert to string";
             }
         }
             break;
@@ -1307,6 +1316,7 @@ gjs_parse_args (JSContext  *context,
             g_assert_not_reached ();
         }
 
+    got_value:
         if (arg_error_message != NULL) {
             gjs_throw(context, "Error invoking %s, at argument %d (%s): %s", function_name,
                       consumed_args+1, argname, arg_error_message);
diff --git a/modules/gettext-native.c b/modules/gettext-native.c
index a38040c..b286f15 100644
--- a/modules/gettext-native.c
+++ b/modules/gettext-native.c
@@ -106,7 +106,7 @@ gjs_dgettext(JSContext *context,
     JSBool result;
     jsval retval;
 
-    if (!gjs_parse_args (context, "dgettext", "zs", argc, argv,
+    if (!gjs_parse_args (context, "dgettext", "?ss", argc, argv,
                          "domain", &domain, "msgid", &msgid))
       return JS_FALSE;
 
@@ -161,7 +161,7 @@ gjs_dngettext(JSContext *context,
     JSBool result;
     jsval retval;
 
-    if (!gjs_parse_args (context, "dngettext", "zssu", argc, argv,
+    if (!gjs_parse_args (context, "dngettext", "?sssu", argc, argv,
                          "domain", &domain, "msgid1", &msgid1,
                          "msgid2", &msgid2, "n", &n))
       return JS_FALSE;



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