[gjs/wip/ptomato/mozjs31prep] WIP - typesafe gjs_parse_call_args()



commit 77622e8d607fbdd9bb2eed054717c3064452becf
Author: Philip Chimento <philip endlessm com>
Date:   Fri Oct 14 19:03:12 2016 -0700

    WIP - typesafe gjs_parse_call_args()
    
    I thought this was going to be beautiful, but it is a holy mess of
    templates.

 Makefile-test.am                  |    1 +
 gi/object.cpp                     |   36 ++--
 gjs/byteArray.cpp                 |    4 +-
 gjs/coverage.cpp                  |    4 +-
 gjs/jsapi-util.cpp                |  571 +++++++++++++++++++++++--------------
 gjs/jsapi-util.h                  |   21 +-
 modules/cairo-context.cpp         |  102 ++++----
 modules/cairo-gradient.cpp        |   22 +-
 modules/cairo-image-surface.cpp   |   12 +-
 modules/cairo-linear-gradient.cpp |   10 +-
 modules/cairo-pdf-surface.cpp     |    8 +-
 modules/cairo-ps-surface.cpp      |    8 +-
 modules/cairo-radial-gradient.cpp |   14 +-
 modules/cairo-region.cpp          |   17 +-
 modules/cairo-solid-pattern.cpp   |   18 +-
 modules/cairo-surface-pattern.cpp |   14 +-
 modules/cairo-surface.cpp         |    4 +-
 modules/cairo-svg-surface.cpp     |    8 +-
 modules/system.cpp                |   18 +-
 test/gjs-test-call-args.cpp       |   61 ++++
 test/gjs-tests-add-funcs.h        |    2 +
 21 files changed, 580 insertions(+), 375 deletions(-)
---
diff --git a/Makefile-test.am b/Makefile-test.am
index bd6beb4..428fc8f 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -92,6 +92,7 @@ gjs_tests_LDADD =             \
 gjs_tests_SOURCES =                                    \
        test/gjs-tests.cpp                              \
        test/gjs-tests-add-funcs.h                      \
+       test/gjs-test-call-args.cpp                     \
        test/gjs-test-coverage.cpp                      \
        mock-js-resources.c                             \
        $(NULL)
diff --git a/gi/object.cpp b/gi/object.cpp
index e6c0189..785a295 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -2235,8 +2235,7 @@ gjs_hook_up_vfunc(JSContext *cx,
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
     gchar *name;
-    JS::RootedObject object(cx);
-    JSObject *function;
+    JS::RootedObject object(cx), function(cx);
     ObjectInstance *priv;
     GType gtype, info_gtype;
     GIObjectInfo *info;
@@ -2244,11 +2243,10 @@ gjs_hook_up_vfunc(JSContext *cx,
     gpointer implementor_vtable;
     GIFieldInfo *field_info;
 
-    if (!gjs_parse_call_args(cx, "hook_up_vfunc",
-                        "oso", argv,
-                        "object", object.address(),
-                        "name", &name,
-                        "function", &function))
+    if (!gjs_parse_call_args(cx, "hook_up_vfunc", argv, "oso",
+                             "object", &object,
+                             "name", &name,
+                             "function", &function))
         return false;
 
     if (!do_base_typecheck(cx, object, true))
@@ -2493,9 +2491,9 @@ gjs_override_property(JSContext *cx,
     GParamSpec *new_pspec;
     GType gtype;
 
-    if (!gjs_parse_call_args(cx, "override_property", "so", args,
+    if (!gjs_parse_call_args(cx, "override_property", args, "so",
                              "name", &name,
-                             "type", type.address()))
+                             "type", &type))
         return false;
 
     if ((gtype = gjs_gtype_get_actual_gtype(cx, type)) == G_TYPE_INVALID) {
@@ -2750,7 +2748,8 @@ gjs_register_interface(JSContext *cx,
 {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
     char *name = NULL;
-    JSObject *constructor, *interfaces, *properties, *module;
+    JS::RootedObject interfaces(cx), properties(cx);
+    JSObject *constructor, *module;
     guint32 i, n_interfaces, n_properties;
     GType *iface_types;
     GType interface_type;
@@ -2770,7 +2769,7 @@ gjs_register_interface(JSContext *cx,
         NULL, /* instance_init */
     };
 
-    if (!gjs_parse_call_args(cx, "register_interface", "soo", args,
+    if (!gjs_parse_call_args(cx, "register_interface", args, "soo",
                              "name", &name,
                              "interfaces", &interfaces,
                              "properties", &properties))
@@ -2829,8 +2828,8 @@ gjs_register_type(JSContext *cx,
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
     gchar *name;
-    JS::RootedObject parent(cx);
-    JSObject *constructor, *interfaces, *properties, *module;
+    JS::RootedObject parent(cx), interfaces(cx), properties(cx);
+    JSObject *constructor, *module;
     GType instance_type, parent_type;
     GTypeQuery query;
     GTypeModule *type_module;
@@ -2855,12 +2854,11 @@ gjs_register_type(JSContext *cx,
 
     JS_BeginRequest(cx);
 
-    if (!gjs_parse_call_args(cx, "register_type",
-                        "osoo", argv,
-                        "parent", parent.address(),
-                        "name", &name,
-                        "interfaces", &interfaces,
-                        "properties", &properties))
+    if (!gjs_parse_call_args(cx, "register_type", argv, "osoo",
+                             "parent", &parent,
+                             "name", &name,
+                             "interfaces", &interfaces,
+                             "properties", &properties))
         goto out;
 
     if (!parent)
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 2882ab9..fe440af 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -712,8 +712,8 @@ from_gbytes_func(JSContext *context,
     GBytes *gbytes;
     ByteArrayInstance *priv;
 
-    if (!gjs_parse_call_args(context, "overrides_gbytes_to_array", "o", argv,
-                             "bytes", bytes_obj.address()))
+    if (!gjs_parse_call_args(context, "overrides_gbytes_to_array", argv, "o",
+                             "bytes", &bytes_obj))
         return false;
 
     if (!gjs_typecheck_boxed(context, bytes_obj, NULL, G_TYPE_BYTES, true))
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index d7546bd..3bfbe3c 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -1382,7 +1382,7 @@ get_filename_from_filename_as_js_string(JSContext    *context,
                                         JS::CallArgs &args) {
     char *filename = NULL;
 
-    if (!gjs_parse_call_args(context, "getFileContents", "s", args,
+    if (!gjs_parse_call_args(context, "getFileContents", args, "s",
                              "filename", &filename))
         return NULL;
 
@@ -1481,7 +1481,7 @@ coverage_get_file_contents(JSContext *context,
     JSString *script_jsstr;
     GError *error = NULL;
 
-    if (!gjs_parse_call_args(context, "getFileContents", "s", args,
+    if (!gjs_parse_call_args(context, "getFileContents", args, "s",
                              "filename", &filename))
         goto out;
 
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 8e40dc9..799d40e 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -832,231 +832,290 @@ gjs_value_to_int64(JSContext      *context,
     return JS::ToInt64(context, val, (int64_t *) result);
 }
 
-static bool
-gjs_parse_args_valist (JSContext  *context,
-                       const char *function_name,
-                       const char *format,
-                       unsigned    argc,
-                       JS::Value  *argv,
-                       va_list     args)
+template <typename T>
+static void
+assign(JSContext      *cx,
+       const char      c,
+       bool            nullable,
+       JS::HandleValue value,
+       T               ref)
 {
-    guint i;
-    const char *fmt_iter;
-    guint n_unwind = 0;
-#define MAX_UNWIND_STRINGS 16
-    gpointer unwind_strings[MAX_UNWIND_STRINGS];
-    bool ignore_trailing_args = false;
-    guint n_required = 0;
-    guint n_total = 0;
-    guint consumed_args;
+    throw "Wrong type for c, got typeid(T).name";
+}
 
-    JS_BeginRequest(context);
+static void
+assign(JSContext      *cx,
+       const char      c,
+       bool            nullable,
+       JS::HandleValue value,
+       bool           *ref)
+{
+    if (c != 'b')
+        throw "Wrong type for c, got bool*";
+    if (!value.isBoolean())
+        throw "Not a boolean";
+    if (nullable)
+        throw "Invalid format string combination ?b";
+    *ref = value.toBoolean();
+}
 
-    if (*format == '!') {
-        ignore_trailing_args = true;
-        format++;
+static void
+assign(JSContext              *cx,
+       const char              c,
+       bool                    nullable,
+       JS::HandleValue         value,
+       JS::MutableHandleObject ref)
+{
+    if (c != 'o')
+        throw "Wrong type for c, got JS::MutableHandleObject";
+    if (nullable && value.isNull()) {
+        ref.set(NULL);
+        return;
     }
+    if (!value.isObject())
+        throw "Not an object";
+    ref.set(&value.toObject());
+}
 
-    for (fmt_iter = format; *fmt_iter; fmt_iter++) {
-        switch (*fmt_iter) {
-        case '|':
-            n_required = n_total;
-            continue;
-        case '?':
-            continue;
-        default:
-            break;
+static void
+assign(JSContext      *cx,
+       const char      c,
+       bool            nullable,
+       JS::HandleValue value,
+       char          **ref)
+{
+    if (nullable && (c == 's' || c == 'F') && value.isNull()) {
+        *ref = NULL;
+        return;
+    }
+    if (c == 's') {
+        if (!gjs_string_to_utf8(cx, value, ref)) {
+            /* Our error message is going to be more useful */
+            JS_ClearPendingException(cx);
+            throw "Couldn't convert to string";
         }
+    } else if (c == 'F') {
+        if (!gjs_string_to_filename(cx, value, ref)) {
+            /* Our error message is going to be more useful */
+            JS_ClearPendingException(cx);
+            throw "Couldn't convert to filename";
+        }
+    } else {
+        throw "Wrong type for c, got char**";
+    }
+}
 
-        n_total++;
+static void
+assign(JSContext      *cx,
+       const char      c,
+       bool            nullable,
+       JS::HandleValue value,
+       int32_t        *ref)
+{
+    if (c != 'i')
+        throw "Wrong type for c, got int32_t*";
+    if (nullable)
+        throw "Invalid format string combination ?i";
+    if (!JS::ToInt32(cx, value, ref)) {
+        /* Our error message is going to be more useful */
+        JS_ClearPendingException(cx);
+        throw "Couldn't convert to integer";
     }
+}
 
-    if (n_required == 0)
-        n_required = n_total;
+static void
+assign(JSContext      *cx,
+       const char      c,
+       bool            nullable,
+       JS::HandleValue value,
+       uint32_t       *ref)
+{
+    double num;
+
+    if (c != 'u')
+        throw "Wrong type for c, got uint32_t*";
+    if (nullable)
+        throw "Invalid format string combination ?u";
+    if (!value.isNumber() || !JS::ToNumber(cx, value, &num)) {
+        /* Our error message is going to be more useful */
+        JS_ClearPendingException(cx);
+        throw "Couldn't convert to unsigned integer";
+    }
+    if (num > G_MAXUINT32 || num < 0)
+        throw "Value is out of range";
+    *ref = num;
+}
 
-    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);
-        } else {
-            gjs_throw(context, "Error invoking %s: Expected minimum %d arguments (and %d optional), got %d", 
function_name,
-                      n_required, n_total - n_required, argc);
-        }
-        goto error_unwind;
+static void
+assign(JSContext       *cx,
+       const char       c,
+       bool             nullable,
+       JS::HandleValue& value,
+       int64_t         *ref)
+{
+    if (c != 't')
+        throw "Wrong type for c, got int64_t*";
+    if (nullable)
+        throw "Invalid format string combination ?t";
+    if (!JS::ToInt64(cx, value, ref)) {
+        /* Our error message is going to be more useful */
+        JS_ClearPendingException(cx);
+        throw "Couldn't convert to 64-bit integer";
     }
+}
 
-    /* We have 3 iteration variables here.
-     * @i: The current integer position in fmt_args
-     * @fmt_iter: A pointer to the character in fmt_args
-     * @consumed_args: How many arguments we've taken from argv
-     *
-     * consumed_args can currently be different from 'i' because of the '|' character.
-     */
-    for (i = 0, consumed_args = 0, fmt_iter = format; *fmt_iter; fmt_iter++, i++) {
-        const char *argname;
-        gpointer arg_location;
-        JS::Value js_value;
-        const char *arg_error_message = NULL;
+static void
+assign(JSContext      *cx,
+       const char      c,
+       bool            nullable,
+       JS::HandleValue value,
+       double         *ref)
+{
+    if (c != 'f')
+        throw "Wrong type for c, got double*";
+    if (nullable)
+        throw "Invalid format string combination ?f";
+    if (!JS::ToNumber(cx, value, ref)) {
+        /* Our error message is going to be more useful */
+        JS_ClearPendingException(cx);
+        throw "Couldn't convert to double";
+    }
+}
 
-        if (*fmt_iter == '|')
-            continue;
+static bool
+check_nullable(const char*& fchar,
+               const char*& fmt_string)
+{
+    if (*fchar != '?')
+        return false;
 
-        if (consumed_args == argc) {
-            break;
-        }
+    fchar++;
+    fmt_string++;
+    g_assert(((void) "Invalid format string, parameter required after '?'",
+              *fchar != '\0'));
+    return true;
+}
 
-        argname = va_arg (args, char *);
-        arg_location = va_arg (args, gpointer);
+template<typename T>
+static void
+free_if_necessary(T param_ref) {}
 
-        g_return_val_if_fail (argname != NULL, false);
-        g_return_val_if_fail (arg_location != NULL, false);
+static void
+free_if_necessary(char **param_ref)
+{
+    g_free(*param_ref);
+}
 
-        js_value = argv[consumed_args];
+template<typename T, typename... Args>
+static bool
+parse_call_args_helper(JSContext    *cx,
+                       const char   *function_name,
+                       JS::CallArgs& args,
+                       bool          ignore_trailing_args,
+                       const char   *fmt_required,
+                       const char   *fmt_optional,
+                       unsigned      param_ix,
+                       const char   *param_name,
+                       T             param_ref,
+                       Args       ...params)
+{
+    bool retval, nullable = false;
+    const char *fchar = fmt_required;
 
-        if (*fmt_iter == '?') {
-            fmt_iter++;
+    g_return_val_if_fail (param_name != NULL, false);
 
-            if (js_value.isNull()) {
-                gpointer *arg = (gpointer*) arg_location;
-                *arg = NULL;
-                goto got_value;
-            }
-        }
+    if (*fchar != '\0') {
+        nullable = check_nullable(fchar, fmt_required);
+        fmt_required++;
+    } else {
+        fchar = fmt_optional;
+        g_assert(((void) "Wrong number of parameters passed to gjs_parse_call_args()",
+                  *fchar != '\0'));
+        nullable = check_nullable(fchar, fmt_optional);
+        fmt_optional++;
+    }
 
-        switch (*fmt_iter) {
-        case 'b': {
-            if (!js_value.isBoolean()) {
-                arg_error_message = "Not a boolean";
-            } else {
-                bool *arg = (bool *) arg_location;
-                *arg = js_value.toBoolean();
-            }
-        }
-            break;
-        case 'o': {
-            if (!js_value.isObject()) {
-                arg_error_message = "Not an object";
-            } else {
-                JSObject **arg = (JSObject**) arg_location;
-                *arg = &js_value.toObject();
-            }
-        }
-            break;
-        case 's': {
-            char **arg = (char**) arg_location;
+    try {
+        assign(cx, *fchar, nullable, args.handleOrUndefinedAt(param_ix), param_ref);
+    } catch (const char *message) {
+        gjs_throw(cx, "Error invoking %s, at argument %d (%s): %s",
+                  function_name, param_ix, param_name, message);
+        return false;
+    }
 
-            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";
-            }
-        }
-            break;
-        case 'F': {
-            char **arg = (char**) arg_location;
+    retval = parse_call_args_helper(cx, function_name, args,
+                                    ignore_trailing_args,
+                                    fmt_required, fmt_optional, param_ix++,
+                                    params...);
 
-            if (gjs_string_to_filename (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 filename";
-            }
-        }
-            break;
-        case 'i': {
-            if (!JS::ToInt32(context, js_value, (int32_t *) arg_location)) {
-                /* Our error message is going to be more useful */
-                JS_ClearPendingException(context);
-                arg_error_message = "Couldn't convert to integer";
-            }
-        }
-            break;
-        case 'u': {
-            gdouble num;
-            if (!js_value.isNumber() || !JS::ToNumber(context, js_value, &num)) {
-                /* Our error message is going to be more useful */
-                JS_ClearPendingException(context);
-                arg_error_message = "Couldn't convert to unsigned integer";
-            } else if (num > G_MAXUINT32 || num < 0) {
-                arg_error_message = "Value is out of range";
-            } else {
-                *((guint32*) arg_location) = num;
-            }
-        }
-            break;
-        case 't': {
-            if (!JS::ToInt64(context, js_value, (int64_t *) arg_location)) {
-                /* Our error message is going to be more useful */
-                JS_ClearPendingException(context);
-                arg_error_message = "Couldn't convert to 64-bit integer";
-            }
-        }
-            break;
-        case 'f': {
-            double num;
-            if (!JS::ToNumber(context, js_value, &num)) {
-                /* Our error message is going to be more useful */
-                JS_ClearPendingException(context);
-                arg_error_message = "Couldn't convert to double";
-            } else {
-                *((double*) arg_location) = num;
-            }
-        }
-            break;
-        default:
-            g_assert_not_reached ();
-        }
+    /* We still own the strings in the error case, free any we converted */
+    if (!retval)
+        free_if_necessary(param_ref);
+    return retval;
+}
 
-    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);
-            goto error_unwind;
-        }
+template<typename T>
+static bool
+parse_call_args_helper(JSContext    *cx,
+                       const char   *function_name,
+                       JS::CallArgs& args,
+                       bool          ignore_trailing_args,
+                       const char   *fmt_required,
+                       const char   *fmt_optional,
+                       unsigned      param_ix,
+                       const char   *param_name,
+                       T             param_ref)
+{
+    bool nullable = false;
+    const char *fchar = fmt_required;
 
-        consumed_args++;
-    }
+    g_return_val_if_fail (param_name != NULL, false);
 
-    JS_EndRequest(context);
-    return true;
+    if (*fchar != '\0') {
+        nullable = check_nullable(fchar, fmt_required);
+        fmt_required++;
+    } else {
+        fchar = fmt_optional;
+        g_assert(((void) "Wrong number of parameters passed to gjs_parse_call_args()",
+                  *fchar != '\0'));
+        nullable = check_nullable(fchar, fmt_optional);
+        fmt_optional++;
+    }
 
- error_unwind:
-    /* 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]);
+    try {
+        assign(cx, *fchar, nullable, args.handleOrUndefinedAt(param_ix), param_ref);
+    } catch (const char *message) {
+        gjs_throw(cx, "Error invoking %s, at argument %d (%s): %s",
+                  function_name, param_ix, param_name, message);
+        return false;
     }
-    JS_EndRequest(context);
-    return false;
+
+    return true;
 }
 
 /**
- * gjs_parse_args:
+ * gjs_parse_call_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.
+ * @args: #JS::CallArgs from #JSNative function
+ * @params: for each character in @format, a pair of const char * which is the
+ * name of the argument, and a location to store the value. The type of
+ * location argument 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"
- * o: A JavaScript object, as a "JSObject *"
+ * b: A boolean (pass a bool *)
+ * s: A string, converted into UTF-8 (pass a char **)
+ * F: A string, converted into "filename encoding" (i.e. active locale) (pass
+ *   a char **)
+ * i: A number, will be converted to a 32-bit int (pass an int32_t *)
+ * u: A number, converted into a 32-bit unsigned int (pass a uint32_t *)
+ * t: A 64-bit number, converted into a 64-bit int (pass an int64_t *)
+ * o: A JavaScript object (pass a JS::MutableHandleObject)
  *
  * If the first character in the format string is a '!', then JS is allowed
  * to pass extra arguments that are ignored, to the function.
@@ -1065,40 +1124,124 @@ gjs_parse_args_valist (JSContext  *context,
  * 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.
+ * A prefix character '?' in front of 's', 'F', or 'o' means that the next value
+ * may be null. For 's' or 'F' a null pointer is returned, for 'o' the handle is
+ * set to null.
  */
+template<typename... Args>
 bool
-gjs_parse_args (JSContext  *context,
-                const char *function_name,
-                const char *format,
-                unsigned    argc,
-                JS::Value  *argv,
-                ...)
+gjs_parse_call_args(JSContext    *cx,
+                    const char   *function_name,
+                    JS::CallArgs& args,
+                    const char   *format,
+                    Args       ...params)
 {
-    va_list args;
-    bool ret;
-    va_start (args, argv);
-    ret = gjs_parse_args_valist (context, function_name, format, argc, argv, args);
-    va_end (args);
-    return ret;
-}
+    const char *fmt_iter, *fmt_required, *fmt_optional;
+    unsigned n_required = 0, n_total = 0;
+    bool ignore_trailing_args = false, retval;
 
-bool
-gjs_parse_call_args (JSContext    *context,
-                     const char   *function_name,
-                     const char   *format,
-                     JS::CallArgs &call_args,
-                     ...)
-{
-    va_list args;
-    bool 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;
+    JSAutoRequest ar(cx);
+
+    if (*format == '!') {
+        ignore_trailing_args = true;
+        format++;
+    }
+
+    for (fmt_iter = format; *fmt_iter; fmt_iter++) {
+        switch (*fmt_iter) {
+        case '|':
+            n_required = n_total;
+            continue;
+        case '?':
+            continue;
+        default:
+            break;
+        }
+
+        n_total++;
+    }
+
+    if (n_required == 0)
+        n_required = n_total;
+
+    g_assert(((void)"Wrong number of parameters passed to gjs_parse_call_args()",
+              sizeof...(Args) == n_required));
+
+    // COMPAT: In future, use args.requireAtLeast()
+    if (args.length() < n_required ||
+        (args.length() > n_total && !ignore_trailing_args)) {
+        if (n_required == n_total) {
+            gjs_throw(cx, "Error invoking %s: Expected %d arguments, got %d",
+                      function_name, n_required, args.length());
+        } else {
+            gjs_throw(cx,
+                      "Error invoking %s: Expected minimum %d arguments (and %d optional), got %d",
+                      function_name, n_required, n_total - n_required,
+                      args.length());
+        }
+        return false;
+    }
+
+    char **parts = g_strsplit(format, "|", 2);
+    fmt_required = parts[0];
+    fmt_optional = parts[1];  /* may be NULL */
+
+    retval = parse_call_args_helper(cx, function_name, args, ignore_trailing_args,
+                                    fmt_required, fmt_optional, 0, params...);
+
+    g_strfreev(parts);
+    return retval;
 }
 
+#define COMMA ,
+#define GENERATE_TEMPLATE_SPECIALIZATION(types) \
+    template bool gjs_parse_call_args(JSContext *, const char *, \
+                                      JS::CallArgs&, const char *, types);
+#define GENERATE_TEMPLATE_SPECIALIZATION_1(t1) \
+    GENERATE_TEMPLATE_SPECIALIZATION(const char * COMMA t1)
+#define GENERATE_TEMPLATE_SPECIALIZATION_2(t1, t2) \
+    GENERATE_TEMPLATE_SPECIALIZATION(const char * COMMA t1 COMMA \
+                                     const char * COMMA t2)
+#define GENERATE_TEMPLATE_SPECIALIZATION_3(t1, t2, t3) \
+    GENERATE_TEMPLATE_SPECIALIZATION(const char * COMMA t1 COMMA \
+                                     const char * COMMA t2 COMMA \
+                                     const char * COMMA t3)
+#define GENERATE_TEMPLATE_SPECIALIZATION_4(t1, t2, t3, t4) \
+    GENERATE_TEMPLATE_SPECIALIZATION(const char * COMMA t1 COMMA \
+                                     const char * COMMA t2 COMMA \
+                                     const char * COMMA t3 COMMA \
+                                     const char * COMMA t4)
+#define GENERATE_TEMPLATE_SPECIALIZATION_5(t1, t2, t3, t4, t5) \
+    GENERATE_TEMPLATE_SPECIALIZATION(const char * COMMA t1 COMMA \
+                                     const char * COMMA t2 COMMA \
+                                     const char * COMMA t3 COMMA \
+                                     const char * COMMA t4 COMMA \
+                                     const char * COMMA t5)
+#define GENERATE_TEMPLATE_SPECIALIZATION_6(t1, t2, t3, t4, t5, t6) \
+    GENERATE_TEMPLATE_SPECIALIZATION(const char * COMMA t1 COMMA \
+                                     const char * COMMA t2 COMMA \
+                                     const char * COMMA t3 COMMA \
+                                     const char * COMMA t4 COMMA \
+                                     const char * COMMA t5 COMMA \
+                                     const char * COMMA t6)
+
+GENERATE_TEMPLATE_SPECIALIZATION_6(double *, double *, double *, double *, double *, double *)
+GENERATE_TEMPLATE_SPECIALIZATION_5(double *, double *, double *, double *, double *)
+GENERATE_TEMPLATE_SPECIALIZATION_4(double *, double *, double *, double *)
+GENERATE_TEMPLATE_SPECIALIZATION_3(char **,  double *, double *)
+GENERATE_TEMPLATE_SPECIALIZATION_3(double *, double *, double *)
+GENERATE_TEMPLATE_SPECIALIZATION_2(double *, double *)
+GENERATE_TEMPLATE_SPECIALIZATION_1(double *)
+
+#undef GENERATE_TEMPLATE_SPECIALIZATION_6
+#undef GENERATE_TEMPLATE_SPECIALIZATION_5
+#undef GENERATE_TEMPLATE_SPECIALIZATION_4
+#undef GENERATE_TEMPLATE_SPECIALIZATION_3
+#undef GENERATE_TEMPLATE_SPECIALIZATION_2
+#undef GENERATE_TEMPLATE_SPECIALIZATION_1
+#undef GENERATE_TEMPLATE_SPECIALIZATION
+#undef COMMA
+
 #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 b394fec..a8fc60a 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -447,19 +447,6 @@ bool        gjs_value_to_int64               (JSContext       *context,
                                               gint64          *result)
 G_GNUC_DEPRECATED_FOR(JS::ToInt64);
 
-bool        gjs_parse_args                   (JSContext  *context,
-                                              const char *function_name,
-                                              const char *format,
-                                              unsigned   argc,
-                                              JS::Value  *argv,
-                                              ...);
-
-bool        gjs_parse_call_args              (JSContext    *context,
-                                              const char   *function_name,
-                                              const char   *format,
-                                              JS::CallArgs &args,
-                                              ...);
-
 GjsRootedArray*   gjs_rooted_array_new        (void)
 G_GNUC_DEPRECATED_FOR(JS::AutoValueVector);
 void              gjs_rooted_array_append     (JSContext        *context,
@@ -553,4 +540,12 @@ const char * gjs_strip_unix_shebang(const char *script,
 
 G_END_DECLS
 
+/* Templates must have C++ linkage */
+template<typename...Args>
+bool gjs_parse_call_args(JSContext    *cx,
+                         const char   *function_name,
+                         JS::CallArgs& args,
+                         const char   *format,
+                         Args       ...params);
+
 #endif  /* __GJS_JSAPI_UTIL_H__ */
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 70e5c71..016e82b 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -74,8 +74,8 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 #define _GJS_CAIRO_CONTEXT_DEFINE_FUNC2FFAFF(method, cfunc, n1, n2)        \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     double arg1, arg2;                                                     \
-    if (!gjs_parse_call_args(context, #method, "ff", argv,                 \
-                        #n1, &arg1, #n2, &arg2))                           \
+    if (!gjs_parse_call_args(context, #method, argv, "ff",                 \
+                             #n1, &arg1, #n2, &arg2))                      \
         return false;                                                      \
     cfunc(cr, &arg1, &arg2);                                               \
     if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                        \
@@ -142,8 +142,8 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 #define _GJS_CAIRO_CONTEXT_DEFINE_FUNC1(method, cfunc, fmt, t1, n1)        \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     t1 arg1;                                                               \
-    if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
-                        #n1, &arg1))                                       \
+    if (!gjs_parse_call_args(context, #method, argv, fmt,                  \
+                             #n1, &arg1))                                  \
         return false;                                                      \
     cfunc(cr, arg1);                                                       \
     argv.rval().setUndefined();                                            \
@@ -153,8 +153,8 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     t1 arg1;                                                               \
     t2 arg2;                                                               \
-    if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
-                        #n1, &arg1, #n2, &arg2))                           \
+    if (!gjs_parse_call_args(context, #method, argv, fmt,                  \
+                             #n1, &arg1, #n2, &arg2))                      \
         return false;                                                      \
     cfunc(cr, arg1, arg2);                                                 \
     argv.rval().setUndefined();                                            \
@@ -165,8 +165,8 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     t1 arg1;                                                               \
     t2 arg2;                                                               \
     cairo_bool_t ret;                                                      \
-    if (!gjs_parse_call_args(context, #method, fmt, argv,                 \
-                        #n1, &arg1, #n2, &arg2))                           \
+    if (!gjs_parse_call_args(context, #method, argv, fmt,                  \
+                             #n1, &arg1, #n2, &arg2))                      \
         return false;                                                      \
     ret = cfunc(cr, arg1, arg2);                                           \
     argv.rval().setBoolean(ret);                                           \
@@ -177,8 +177,8 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     t1 arg1;                                                               \
     t2 arg2;                                                               \
     t3 arg3;                                                               \
-    if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
-                        #n1, &arg1, #n2, &arg2, #n3, &arg3))               \
+    if (!gjs_parse_call_args(context, #method, argv, fmt,                  \
+                             #n1, &arg1, #n2, &arg2, #n3, &arg3))          \
         return false;                                                      \
     cfunc(cr, arg1, arg2, arg3);                                           \
     argv.rval().setUndefined();                                            \
@@ -190,8 +190,9 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     t2 arg2;                                                               \
     t3 arg3;                                                               \
     t4 arg4;                                                               \
-    if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
-                        #n1, &arg1, #n2, &arg2, #n3, &arg3, #n4, &arg4))   \
+    if (!gjs_parse_call_args(context, #method, argv, fmt,                  \
+                             #n1, &arg1, #n2, &arg2,                       \
+                             #n3, &arg3, #n4, &arg4))                      \
         return false;                                                      \
     cfunc(cr, arg1, arg2, arg3, arg4);                                     \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -203,9 +204,9 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     t3 arg3;                                                               \
     t4 arg4;                                                               \
     t5 arg5;                                                               \
-    if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
-                        #n1, &arg1, #n2, &arg2, #n3, &arg3,                \
-                        #n4, &arg4, #n5, &arg5))                           \
+    if (!gjs_parse_call_args(context, #method, argv, fmt,                  \
+                             #n1, &arg1, #n2, &arg2, #n3, &arg3,           \
+                             #n4, &arg4, #n5, &arg5))                      \
         return false;                                                      \
     cfunc(cr, arg1, arg2, arg3, arg4, arg5);                               \
     argv.rval().setUndefined();                                            \
@@ -219,9 +220,9 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     t4 arg4;                                                               \
     t5 arg5;                                                               \
     t6 arg6;                                                               \
-    if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
-                        #n1, &arg1, #n2, &arg2, #n3, &arg3,                \
-                        #n4, &arg4, #n5, &arg5, #n6, &arg6))               \
+    if (!gjs_parse_call_args(context, #method, argv, fmt,                  \
+                             #n1, &arg1, #n2, &arg2, #n3, &arg3,           \
+                             #n4, &arg4, #n5, &arg5, #n6, &arg6))          \
         return false;                                                      \
     cfunc(cr, arg1, arg2, arg3, arg4, arg5, arg6);                         \
     argv.rval().setUndefined();                                            \
@@ -257,14 +258,14 @@ _gjs_cairo_context_construct_internal(JSContext       *context,
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_context)
 {
     GJS_NATIVE_CONSTRUCTOR_VARIABLES(cairo_context)
-    JSObject *surface_wrapper;
     cairo_surface_t *surface;
     cairo_t *cr;
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_context);
 
-    if (!gjs_parse_call_args(context, "Context", "o", argv,
-                        "surface", &surface_wrapper))
+    JS::RootedObject surface_wrapper(context);
+    if (!gjs_parse_call_args(context, "Context", argv, "o",
+                             "surface", &surface_wrapper))
         return false;
 
     surface = gjs_cairo_surface_get_surface(context, surface_wrapper);
@@ -407,12 +408,12 @@ appendPath_func(JSContext *context,
                 JS::Value *vp)
 {
     GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
-    JSObject *path_wrapper;
+    JS::RootedObject path_wrapper(context);
     cairo_path_t *path;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "path", "o", argv,
-                        "path", &path_wrapper))
+    if (!gjs_parse_call_args(context, "path", argv, "o",
+                             "path", &path_wrapper))
         return false;
 
     path = gjs_cairo_path_get_path(context, path_wrapper);
@@ -435,7 +436,7 @@ copyPath_func(JSContext *context,
     cairo_path_t *path;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "", "", argv))
+    if (!gjs_parse_call_args(context, "", argv, ""))
         return false;
 
     path = cairo_copy_path(cr);
@@ -452,7 +453,7 @@ copyPathFlat_func(JSContext *context,
     cairo_path_t *path;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "", "", argv))
+    if (!gjs_parse_call_args(context, "", argv, ""))
         return false;
 
     path = cairo_copy_path_flat(cr);
@@ -466,12 +467,12 @@ mask_func(JSContext *context,
           JS::Value *vp)
 {
     GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
-    JSObject *pattern_wrapper;
+    JS::RootedObject pattern_wrapper(context);
     cairo_pattern_t *pattern;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "mask", "o", argv,
-                        "pattern", &pattern_wrapper))
+    if (!gjs_parse_call_args(context, "mask", argv, "o",
+                             "pattern", &pattern_wrapper))
         return false;
 
     pattern = gjs_cairo_pattern_get_pattern(context, pattern_wrapper);
@@ -495,15 +496,15 @@ maskSurface_func(JSContext *context,
                  JS::Value *vp)
 {
     GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
-    JSObject *surface_wrapper;
+    JS::RootedObject surface_wrapper(context);
     double x, y;
     cairo_surface_t *surface;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "maskSurface", "off", argv,
-                        "surface", &surface_wrapper,
-                        "x", &x,
-                        "y", &y))
+    if (!gjs_parse_call_args(context, "maskSurface", argv, "off",
+                             "surface", &surface_wrapper,
+                             "x", &x,
+                             "y", &y))
         return false;
 
     surface = gjs_cairo_surface_get_surface(context, surface_wrapper);
@@ -535,8 +536,9 @@ setDash_func(JSContext *context,
     guint len;
     GArray *dashes_c = NULL;
 
-    if (!gjs_parse_call_args(context, "setDash", "of", argv,
-                        "dashes", dashes.address(), "offset", &offset))
+    if (!gjs_parse_call_args(context, "setDash", argv, "of",
+                             "dashes", &dashes,
+                             "offset", &offset))
         return false;
 
 
@@ -587,12 +589,12 @@ setSource_func(JSContext *context,
                JS::Value *vp)
 {
     GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
-    JSObject *pattern_wrapper;
+    JS::RootedObject pattern_wrapper(context);
     cairo_pattern_t *pattern;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "setSource", "o", argv,
-                        "pattern", &pattern_wrapper))
+    if (!gjs_parse_call_args(context, "setSource", argv, "o",
+                             "pattern", &pattern_wrapper))
         return false;
 
     pattern = gjs_cairo_pattern_get_pattern(context, pattern_wrapper);
@@ -617,15 +619,15 @@ setSourceSurface_func(JSContext *context,
                       JS::Value *vp)
 {
     GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
-    JSObject *surface_wrapper;
+    JS::RootedObject surface_wrapper(context);
     double x, y;
     cairo_surface_t *surface;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "setSourceSurface", "off", argv,
-                        "surface", &surface_wrapper,
-                        "x", &x,
-                        "y", &y))
+    if (!gjs_parse_call_args(context, "setSourceSurface", argv, "off",
+                             "surface", &surface_wrapper,
+                             "x", &x,
+                             "y", &y))
         return false;
 
     surface = gjs_cairo_surface_get_surface(context, surface_wrapper);
@@ -653,8 +655,8 @@ showText_func(JSContext *context,
     char *utf8;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "showText", "s", argv,
-                        "utf8", &utf8))
+    if (!gjs_parse_call_args(context, "showText", argv, "s",
+                             "utf8", &utf8))
         return false;
 
     cairo_show_text(cr, utf8);
@@ -679,10 +681,10 @@ selectFontFace_func(JSContext *context,
     cairo_font_weight_t weight;
     cairo_t *cr = priv ? priv->cr : NULL;
 
-    if (!gjs_parse_call_args(context, "selectFontFace", "sii", argv,
-                        "family", &family,
-                        "slang", &slant,
-                        "weight", &weight))
+    if (!gjs_parse_call_args(context, "selectFontFace", argv, "sii",
+                             "family", &family,
+                             "slang", &slant,
+                             "weight", &weight))
         return false;
 
     cairo_select_font_face(cr, family, slant, weight);
diff --git a/modules/cairo-gradient.cpp b/modules/cairo-gradient.cpp
index eca60dc..61ba3a2 100644
--- a/modules/cairo-gradient.cpp
+++ b/modules/cairo-gradient.cpp
@@ -52,11 +52,11 @@ addColorStopRGB_func(JSContext *context,
     double offset, red, green, blue;
     cairo_pattern_t *pattern;
 
-    if (!gjs_parse_call_args(context, "addColorStopRGB", "ffff", argv,
-                        "offset", &offset,
-                        "red", &red,
-                        "green", &green,
-                        "blue", &blue))
+    if (!gjs_parse_call_args(context, "addColorStopRGB", argv, "ffff",
+                             "offset", &offset,
+                             "red", &red,
+                             "green", &green,
+                             "blue", &blue))
         return false;
 
     pattern = gjs_cairo_pattern_get_pattern(context, obj);
@@ -79,12 +79,12 @@ addColorStopRGBA_func(JSContext *context,
     double offset, red, green, blue, alpha;
     cairo_pattern_t *pattern;
 
-    if (!gjs_parse_call_args(context, "addColorStopRGBA", "fffff", argv,
-                        "offset", &offset,
-                        "red", &red,
-                        "green", &green,
-                        "blue", &blue,
-                        "alpha", &alpha))
+    if (!gjs_parse_call_args(context, "addColorStopRGBA", argv, "fffff",
+                             "offset", &offset,
+                             "red", &red,
+                             "green", &green,
+                             "blue", &blue,
+                             "alpha", &alpha))
         return false;
 
     pattern = gjs_cairo_pattern_get_pattern(context, obj);
diff --git a/modules/cairo-image-surface.cpp b/modules/cairo-image-surface.cpp
index 6de5682..2853e1a 100644
--- a/modules/cairo-image-surface.cpp
+++ b/modules/cairo-image-surface.cpp
@@ -38,10 +38,10 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_image_surface)
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_image_surface);
 
     // create_for_data optional parameter
-    if (!gjs_parse_call_args(context, "ImageSurface", "iii", argv,
-                        "format", &format,
-                        "width", &width,
-                        "height", &height))
+    if (!gjs_parse_call_args(context, "ImageSurface", argv, "iii",
+                             "format", &format,
+                             "width", &width,
+                             "height", &height))
         return false;
 
     surface = cairo_image_surface_create((cairo_format_t) format, width, height);
@@ -77,8 +77,8 @@ createFromPNG_func(JSContext *context,
     char *filename;
     cairo_surface_t *surface;
 
-    if (!gjs_parse_call_args(context, "createFromPNG", "s", argv,
-                        "filename", &filename))
+    if (!gjs_parse_call_args(context, "createFromPNG", argv, "s",
+                             "filename", &filename))
         return false;
 
     surface = cairo_image_surface_create_from_png(filename);
diff --git a/modules/cairo-linear-gradient.cpp b/modules/cairo-linear-gradient.cpp
index b4ff0c6..58e502d 100644
--- a/modules/cairo-linear-gradient.cpp
+++ b/modules/cairo-linear-gradient.cpp
@@ -37,11 +37,11 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_linear_gradient)
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_linear_gradient);
 
-    if (!gjs_parse_call_args(context, "LinearGradient", "ffff", argv,
-                        "x0", &x0,
-                        "y0", &y0,
-                        "x1", &x1,
-                        "y1", &y1))
+    if (!gjs_parse_call_args(context, "LinearGradient", argv, "ffff",
+                             "x0", &x0,
+                             "y0", &y0,
+                             "x1", &x1,
+                             "y1", &y1))
         return false;
 
     pattern = cairo_pattern_create_linear(x0, y0, x1, y1);
diff --git a/modules/cairo-pdf-surface.cpp b/modules/cairo-pdf-surface.cpp
index cbbb415..7bc10bb 100644
--- a/modules/cairo-pdf-surface.cpp
+++ b/modules/cairo-pdf-surface.cpp
@@ -41,10 +41,10 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_pdf_surface)
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_pdf_surface);
 
-    if (!gjs_parse_call_args(context, "PDFSurface", "sff", argv,
-                        "filename", &filename,
-                        "width", &width,
-                        "height", &height))
+    if (!gjs_parse_call_args(context, "PDFSurface", argv, "Fff",
+                             "filename", &filename,
+                             "width", &width,
+                             "height", &height))
         return false;
 
     surface = cairo_pdf_surface_create(filename, width, height);
diff --git a/modules/cairo-ps-surface.cpp b/modules/cairo-ps-surface.cpp
index dee145d..b5df8c1 100644
--- a/modules/cairo-ps-surface.cpp
+++ b/modules/cairo-ps-surface.cpp
@@ -41,10 +41,10 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_ps_surface)
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_ps_surface);
 
-    if (!gjs_parse_call_args(context, "PSSurface", "sff", argv,
-                        "filename", &filename,
-                        "width", &width,
-                        "height", &height))
+    if (!gjs_parse_call_args(context, "PSSurface", argv, "Fff",
+                             "filename", &filename,
+                             "width", &width,
+                             "height", &height))
         return false;
 
     surface = cairo_ps_surface_create(filename, width, height);
diff --git a/modules/cairo-radial-gradient.cpp b/modules/cairo-radial-gradient.cpp
index ca373f0..2e114bd 100644
--- a/modules/cairo-radial-gradient.cpp
+++ b/modules/cairo-radial-gradient.cpp
@@ -37,13 +37,13 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_radial_gradient)
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_radial_gradient);
 
-    if (!gjs_parse_call_args(context, "RadialGradient", "ffffff", argv,
-                        "cx0", &cx0,
-                        "cy0", &cy0,
-                        "radius0", &radius0,
-                        "cx1", &cx1,
-                        "cy1", &cy1,
-                        "radius1", &radius1))
+    if (!gjs_parse_call_args(context, "RadialGradient", argv, "ffffff",
+                             "cx0", &cx0,
+                             "cy0", &cy0,
+                             "radius0", &radius0,
+                             "cx1", &cx1,
+                             "cy1", &cy1,
+                             "radius1", &radius1))
         return false;
 
     pattern = cairo_pattern_create_radial(cx0, cy0, radius0, cx1, cy1, radius1);
diff --git a/modules/cairo-region.cpp b/modules/cairo-region.cpp
index 65e33cb..1687ded 100644
--- a/modules/cairo-region.cpp
+++ b/modules/cairo-region.cpp
@@ -70,8 +70,8 @@ fill_rectangle(JSContext *context, JSObject *obj,
         PRELUDE;                                                \
         JS::RootedObject other_obj(context);                    \
         cairo_region_t *other_region;                           \
-        if (!gjs_parse_call_args(context, #method, "o", argv,   \
-                                 "other_region", other_obj.address())) \
+        if (!gjs_parse_call_args(context, #method, argv, "o",   \
+                                 "other_region", &other_obj))   \
             return false;                                       \
                                                                 \
         other_region = get_region(context, other_obj);          \
@@ -88,10 +88,10 @@ fill_rectangle(JSContext *context, JSObject *obj,
                             JS::Value *vp)                      \
     {                                                           \
         PRELUDE;                                                \
-        JSObject *rect_obj;                                     \
+        JS::RootedObject rect_obj(context);                     \
         cairo_rectangle_int_t rect;                             \
-        if (!gjs_parse_call_args(context, #method, "o", argv,   \
-                            "rect", &rect_obj))                 \
+        if (!gjs_parse_call_args(context, #method, argv, "o",   \
+                                 "rect", &rect_obj))            \
             return false;                                       \
                                                                 \
         if (!fill_rectangle(context, rect_obj, &rect))          \
@@ -171,7 +171,7 @@ num_rectangles_func(JSContext *context,
     PRELUDE;
     int n_rects;
 
-    if (!gjs_parse_call_args(context, "num_rectangles", "", argv))
+    if (!gjs_parse_call_args(context, "num_rectangles", argv, ""))
         return false;
 
     n_rects = cairo_region_num_rectangles(this_region);
@@ -189,7 +189,8 @@ get_rectangle_func(JSContext *context,
     JSObject *rect_obj;
     cairo_rectangle_int_t rect;
 
-    if (!gjs_parse_call_args(context, "get_rectangle", "i", argv, "rect", &i))
+    if (!gjs_parse_call_args(context, "get_rectangle", argv, "i",
+                             "rect", &i))
         return false;
 
     cairo_region_get_rectangle(this_region, i, &rect);
@@ -243,7 +244,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_region)
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_region);
 
-    if (!gjs_parse_call_args(context, "Region", "", argv))
+    if (!gjs_parse_call_args(context, "Region", argv, ""))
         return false;
 
     region = cairo_region_create();
diff --git a/modules/cairo-solid-pattern.cpp b/modules/cairo-solid-pattern.cpp
index 5bb4a8b..f2308c6 100644
--- a/modules/cairo-solid-pattern.cpp
+++ b/modules/cairo-solid-pattern.cpp
@@ -50,10 +50,10 @@ createRGB_func(JSContext *context,
     cairo_pattern_t *pattern;
     JSObject *pattern_wrapper;
 
-    if (!gjs_parse_call_args(context, "createRGB", "fff", argv,
-                        "red", &red,
-                        "green", &green,
-                        "blue", &blue))
+    if (!gjs_parse_call_args(context, "createRGB", argv, "fff",
+                             "red", &red,
+                             "green", &green,
+                             "blue", &blue))
         return false;
 
     pattern = cairo_pattern_create_rgb(red, green, blue);
@@ -78,11 +78,11 @@ createRGBA_func(JSContext *context,
     cairo_pattern_t *pattern;
     JSObject *pattern_wrapper;
 
-    if (!gjs_parse_call_args(context, "createRGBA", "ffff", argv,
-                        "red", &red,
-                        "green", &green,
-                        "blue", &blue,
-                        "alpha", &alpha))
+    if (!gjs_parse_call_args(context, "createRGBA", argv, "ffff",
+                             "red", &red,
+                             "green", &green,
+                             "blue", &blue,
+                             "alpha", &alpha))
         return false;
 
     pattern = cairo_pattern_create_rgba(red, green, blue, alpha);
diff --git a/modules/cairo-surface-pattern.cpp b/modules/cairo-surface-pattern.cpp
index 750b061..5dd3089 100644
--- a/modules/cairo-surface-pattern.cpp
+++ b/modules/cairo-surface-pattern.cpp
@@ -32,14 +32,14 @@ GJS_DEFINE_PROTO("CairoSurfacePattern", cairo_surface_pattern, JSCLASS_BACKGROUN
 GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_surface_pattern)
 {
     GJS_NATIVE_CONSTRUCTOR_VARIABLES(cairo_surface_pattern)
-    JSObject *surface_wrapper;
     cairo_surface_t *surface;
     cairo_pattern_t *pattern;
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_surface_pattern);
 
-    if (!gjs_parse_call_args(context, "SurfacePattern", "o", argv,
-                        "surface", &surface_wrapper))
+    JS::RootedObject surface_wrapper(context);
+    if (!gjs_parse_call_args(context, "SurfacePattern", argv, "o",
+                             "surface", &surface_wrapper))
         return false;
 
     surface = gjs_cairo_surface_get_surface(context, surface_wrapper);
@@ -83,8 +83,8 @@ setExtend_func(JSContext *context,
     cairo_extend_t extend;
     cairo_pattern_t *pattern;
 
-    if (!gjs_parse_call_args(context, "setExtend", "i", argv,
-                        "extend", &extend))
+    if (!gjs_parse_call_args(context, "setExtend", argv, "i",
+                             "extend", &extend))
         return false;
 
     pattern = gjs_cairo_pattern_get_pattern(context, obj);
@@ -131,8 +131,8 @@ setFilter_func(JSContext *context,
     cairo_filter_t filter;
     cairo_pattern_t *pattern;
 
-    if (!gjs_parse_call_args(context, "setFilter", "i", argv,
-                        "filter", &filter))
+    if (!gjs_parse_call_args(context, "setFilter", argv, "i",
+                             "filter", &filter))
         return false;
 
     pattern = gjs_cairo_pattern_get_pattern(context, obj);
diff --git a/modules/cairo-surface.cpp b/modules/cairo-surface.cpp
index 673219b..a192db9 100644
--- a/modules/cairo-surface.cpp
+++ b/modules/cairo-surface.cpp
@@ -66,8 +66,8 @@ writeToPNG_func(JSContext *context,
     char *filename;
     cairo_surface_t *surface;
 
-    if (!gjs_parse_call_args(context, "writeToPNG", "s", argv,
-                        "filename", &filename))
+    if (!gjs_parse_call_args(context, "writeToPNG", argv, "F",
+                             "filename", &filename))
         return false;
 
     surface = gjs_cairo_surface_get_surface(context, obj);
diff --git a/modules/cairo-svg-surface.cpp b/modules/cairo-svg-surface.cpp
index f305285..6f069ca 100644
--- a/modules/cairo-svg-surface.cpp
+++ b/modules/cairo-svg-surface.cpp
@@ -41,10 +41,10 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_svg_surface)
 
     GJS_NATIVE_CONSTRUCTOR_PRELUDE(cairo_svg_surface);
 
-    if (!gjs_parse_call_args(context, "SVGSurface", "sff", argv,
-                        "filename", &filename,
-                        "width", &width,
-                        "height", &height))
+    if (!gjs_parse_call_args(context, "SVGSurface", argv, "Fff",
+                             "filename", &filename,
+                             "width", &width,
+                             "height", &height))
         return false;
 
     surface = cairo_svg_surface_create(filename, width, height);
diff --git a/modules/system.cpp b/modules/system.cpp
index 8ecf5e4..900c628 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -38,14 +38,15 @@ gjs_address_of(JSContext *context,
                JS::Value *vp)
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JSObject *target_obj;
+    JS::RootedObject target_obj(context);
     bool ret;
     char *pointer_string;
 
-    if (!gjs_parse_call_args(context, "addressOf", "o", argv, "object", &target_obj))
+    if (!gjs_parse_call_args(context, "addressOf", argv, "o",
+                             "object", &target_obj))
         return false;
 
-    pointer_string = g_strdup_printf("%p", target_obj);
+    pointer_string = g_strdup_printf("%p", target_obj.get());
 
     ret = gjs_string_from_utf8(context, pointer_string, -1, argv.rval());
 
@@ -62,8 +63,8 @@ gjs_refcount(JSContext *context,
     JS::RootedObject target_obj(context);
     GObject *obj;
 
-    if (!gjs_parse_call_args(context, "refcount", "o", argv,
-                             "object", target_obj.address()))
+    if (!gjs_parse_call_args(context, "refcount", argv, "o",
+                             "object", &target_obj))
         return false;
 
     if (!gjs_typecheck_object(context, target_obj, G_TYPE_OBJECT, true))
@@ -83,7 +84,7 @@ gjs_breakpoint(JSContext *context,
                JS::Value *vp)
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    if (!gjs_parse_call_args(context, "breakpoint", "", argv))
+    if (!gjs_parse_call_args(context, "breakpoint", argv, ""))
         return false;
     G_BREAKPOINT();
     argv.rval().setUndefined();
@@ -96,7 +97,7 @@ gjs_gc(JSContext *context,
        JS::Value *vp)
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    if (!gjs_parse_call_args(context, "gc", "", argv))
+    if (!gjs_parse_call_args(context, "gc", argv, ""))
         return false;
     JS_GC(JS_GetRuntime(context));
     argv.rval().setUndefined();
@@ -110,7 +111,8 @@ gjs_exit(JSContext *context,
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
     gint32 ecode;
-    if (!gjs_parse_call_args(context, "exit", "i", argv, "ecode", &ecode))
+    if (!gjs_parse_call_args(context, "exit", argv, "i",
+                             "ecode", &ecode))
         return false;
     exit(ecode);
     return true;
diff --git a/test/gjs-test-call-args.cpp b/test/gjs-test-call-args.cpp
new file mode 100644
index 0000000..71ac293
--- /dev/null
+++ b/test/gjs-test-call-args.cpp
@@ -0,0 +1,61 @@
+#include <string.h>
+
+#include <glib.h>
+
+#include "gjs/compat.h"
+
+/* The class of the global object. */
+static JSClass global_class = {
+    "global",
+    JSCLASS_GLOBAL_FLAGS,
+    JS_PropertyStub,
+    JS_DeletePropertyStub,
+    JS_PropertyStub,
+    JS_StrictPropertyStub,
+    JS_EnumerateStub,
+    JS_ResolveStub,
+    JS_ConvertStub
+};
+
+static void
+test_one_of_each_type(void)
+{
+    JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS);
+    g_assert_nonnull(rt);
+
+    JSContext *cx = JS_NewContext(rt, 8192);
+    g_assert_nonnull(cx);
+
+    {
+        JSAutoRequest ar(cx);
+
+        JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class, nullptr));
+        g_assert_nonnull(global);
+
+        JS::RootedValue rval(cx);
+
+        {
+            JSAutoCompartment ac(cx, global);
+            JS_InitStandardClasses(cx, global);
+
+            const char *script = "'hello'+'world, it is '+new Date()";
+            const char *filename = "noname";
+            int lineno = 1;
+            bool ok = JS_EvaluateScript(cx, global, script, strlen(script), filename, lineno, 
rval.address());
+            g_assert_true(ok);
+        }
+
+        JSString *str = rval.toString();
+        printf("%s\n", JS_EncodeString(cx, str));
+    }
+
+    JS_DestroyContext(cx);
+    JS_DestroyRuntime(rt);
+    JS_ShutDown();
+}
+
+void
+gjs_test_add_tests_for_parse_call_args(void)
+{
+    g_test_add_func("/callargs/blah", test_one_of_each_type);
+}
diff --git a/test/gjs-tests-add-funcs.h b/test/gjs-tests-add-funcs.h
index a7414a6..23ae846 100644
--- a/test/gjs-tests-add-funcs.h
+++ b/test/gjs-tests-add-funcs.h
@@ -22,4 +22,6 @@
 
 void gjs_test_add_tests_for_coverage ();
 
+void gjs_test_add_tests_for_parse_call_args(void);
+
 #endif


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