[gjs/wip/gobj-kitchen-sink: 12/26] util: Add a flag to gjs_parse_args to allow JS to pass ignored arguments



commit d21b8d314e2509b46cba097a9a0016a907dc5bf2
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Jan 27 18:06:17 2012 -0500

    util: Add a flag to gjs_parse_args to allow JS to pass ignored arguments
    
    Because param_new_internal parses arguments in two steps, we need to make
    it OK for JS to pass more arguments than the totla amount

 gjs/jsapi-util.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
---
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index c495e20..f5dfd06 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -1380,6 +1380,9 @@ gjs_value_to_int64  (JSContext  *context,
  * u: A number, converted into a C "guint32"
  * o: A JavaScript object, as a "JSObject *"
  *
+ * If the first character in the format string is a '!', then JS is allowed
+ * to pass extra arguments that are ignored, to the function.
+ *
  * The '|' character introduces optional arguments.  All format specifiers
  * after a '|' when not specified, do not cause any changes in the C
  * value location.
@@ -1399,6 +1402,7 @@ gjs_parse_args (JSContext  *context,
     guint n_unwind = 0;
 #define MAX_UNWIND_STRINGS 16
     gpointer unwind_strings[MAX_UNWIND_STRINGS];
+    gboolean extra_fine = FALSE;
     guint n_required;
     guint n_total;
     guint consumed_args;
@@ -1407,6 +1411,11 @@ gjs_parse_args (JSContext  *context,
 
     va_start (args, argv);
 
+    if (*format == '!') {
+        extra_fine = TRUE;
+        format++;
+    }
+
     /* Check for optional argument specifier */
     fmt_iter = strchr (format, '|');
     if (fmt_iter) {
@@ -1419,7 +1428,7 @@ gjs_parse_args (JSContext  *context,
         n_required = n_total = strlen (format);
     }
 
-    if (argc < n_required || argc > n_total) {
+    if (argc < n_required || (argc > n_total && !extra_fine)) {
         if (n_required == n_total) {
             gjs_throw(context, "Error invoking %s: Expected %d arguments, got %d", function_name,
                       n_required, argc);



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