[gjs] jsapi-util: Add a gjs_parse_call_args variant, that takes JS::CallArgs



commit d49152677b756d81872ea129df44508ff2b0fe29
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Thu Jan 2 17:29:11 2014 -0500

    jsapi-util: Add a gjs_parse_call_args variant, that takes JS::CallArgs
    
    JS::CallArgs is now a recommended replacement for the argc/vp pair, and
    in the future, JSNative will take a JS::CallArgs directly, instead of a
    argc/vp pair. For now, use the semi-deprecated "array" member to share
    the same code as the old argc/vp interface.
    
    When upstream deprecates argc/vp, we'll port everything to JS::CallArgs.
    
    This is because I spend almost half an hour finding a bug with the old
    interface (I called JS_SET_RVAL on the argv instead of vp), and they
    recommended that I use JS::CallArgs instead.

 gjs/jsapi-util.cpp |  119 +++++++++++++++++++++++++++++++--------------------
 gjs/jsapi-util.h   |    6 +++
 2 files changed, 78 insertions(+), 47 deletions(-)
---
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 3b54cb0..44f4534 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -865,51 +865,16 @@ gjs_value_to_int64  (JSContext  *context,
     }
 }
 
-/**
- * gjs_parse_args:
- * @context:
- * @function_name: The name of the function being called
- * @format: Printf-like format specifier containing the expected arguments
- * @argc: Number of JavaScript arguments
- * @argv: JavaScript argument array
- * @Varargs: for each character in @format, a pair of a char * which is the name
- * of the argument, and a pointer to a location to store the value. The type of
- * value stored depends on the format character, as described below.
- *
- * This function is inspired by Python's PyArg_ParseTuple for those
- * familiar with it.  It takes a format specifier which gives the
- * types of the expected arguments, and a list of argument names and
- * value location pairs.  The currently accepted format specifiers are:
- *
- * b: A boolean
- * s: A string, converted into UTF-8
- * 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"
- * t: A 64-bit number, converted into a C "gint64" by way of gjs_value_to_int64()
- * 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.
- *
- * 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,
-                const char *function_name,
-                const char *format,
-                unsigned   argc,
-                jsval     *argv,
-                ...)
+static JSBool
+gjs_parse_args_valist (JSContext  *context,
+                       const char *function_name,
+                       const char *format,
+                       unsigned    argc,
+                       jsval      *argv,
+                       va_list     args)
 {
     guint i;
     const char *fmt_iter;
-    va_list args;
     guint n_unwind = 0;
 #define MAX_UNWIND_STRINGS 16
     gpointer unwind_strings[MAX_UNWIND_STRINGS];
@@ -920,8 +885,6 @@ gjs_parse_args (JSContext  *context,
 
     JS_BeginRequest(context);
 
-    va_start (args, argv);
-
     if (*format == '!') {
         ignore_trailing_args = TRUE;
         format++;
@@ -1092,13 +1055,10 @@ gjs_parse_args (JSContext  *context,
         consumed_args++;
     }
 
-    va_end (args);
-
     JS_EndRequest(context);
     return JS_TRUE;
 
  error_unwind:
-    va_end (args);
     /* We still own the strings in the error case, free any we converted */
     for (i = 0; i < n_unwind; i++) {
         g_free (unwind_strings[i]);
@@ -1107,6 +1067,71 @@ gjs_parse_args (JSContext  *context,
     return JS_FALSE;
 }
 
+/**
+ * gjs_parse_args:
+ * @context:
+ * @function_name: The name of the function being called
+ * @format: Printf-like format specifier containing the expected arguments
+ * @argc: Number of JavaScript arguments
+ * @argv: JavaScript argument array
+ * @Varargs: for each character in @format, a pair of a char * which is the name
+ * of the argument, and a pointer to a location to store the value. The type of
+ * value stored depends on the format character, as described below.
+ *
+ * This function is inspired by Python's PyArg_ParseTuple for those
+ * familiar with it.  It takes a format specifier which gives the
+ * types of the expected arguments, and a list of argument names and
+ * value location pairs.  The currently accepted format specifiers are:
+ *
+ * b: A boolean
+ * s: A string, converted into UTF-8
+ * 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"
+ * t: A 64-bit number, converted into a C "gint64" by way of gjs_value_to_int64()
+ * 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.
+ *
+ * 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,
+                const char *function_name,
+                const char *format,
+                unsigned    argc,
+                jsval      *argv,
+                ...)
+{
+    va_list args;
+    JSBool ret;
+    va_start (args, argv);
+    ret = gjs_parse_args_valist (context, function_name, format, argc, argv, args);
+    va_end (args);
+    return ret;
+}
+
+JSBool
+gjs_parse_call_args (JSContext    *context,
+                     const char   *function_name,
+                     const char   *format,
+                     JS::CallArgs &call_args,
+                     ...)
+{
+    va_list args;
+    JSBool ret;
+    va_start (args, call_args);
+    ret = gjs_parse_args_valist (context, function_name, format, call_args.length(), call_args.array(), 
args);
+    va_end (args);
+    return ret;
+}
+
 #ifdef __linux__
 static void
 _linux_get_self_process_size (gulong *vm_size,
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index e133b16..607d157 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -351,6 +351,12 @@ JSBool      gjs_parse_args                   (JSContext  *context,
                                               jsval     *argv,
                                               ...);
 
+JSBool      gjs_parse_call_args              (JSContext    *context,
+                                              const char   *function_name,
+                                              const char   *format,
+                                              JS::CallArgs &args,
+                                              ...);
+
 GjsRootedArray*   gjs_rooted_array_new        (void);
 void              gjs_rooted_array_append     (JSContext        *context,
                                                GjsRootedArray *array,


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