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



commit d454a398e8a3177442c0b51ec51318ffce76b819
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..326325a 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 ignore_trailing_args = 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 == '!') {
+        ignore_trailing_args = 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 && !ignore_trailing_args)) {
         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]