[gjs/wip/ptomato/mozjs45prep: 3/23] jsapi-util-args: Functions with only optional args



commit e93d71fbfd359bbd3db9eec7b7f1420357971b3f
Author: Philip Chimento <philip endlessm com>
Date:   Wed Mar 15 12:10:11 2017 -0700

    jsapi-util-args: Functions with only optional args
    
    An oversight in the original implementation: arguments were not
    considered optional if all the arguments were optional.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780106

 gjs/jsapi-util-args.h       |    5 +++--
 test/gjs-test-call-args.cpp |   14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
---
diff --git a/gjs/jsapi-util-args.h b/gjs/jsapi-util-args.h
index 665aeb6..b6c78bf 100644
--- a/gjs/jsapi-util-args.h
+++ b/gjs/jsapi-util-args.h
@@ -366,7 +366,7 @@ gjs_parse_call_args(JSContext    *cx,
 {
     const char *fmt_iter, *fmt_required, *fmt_optional;
     unsigned n_required = 0, n_total = 0;
-    bool ignore_trailing_args = false, retval;
+    bool optional_args = false, ignore_trailing_args = false, retval;
     char **parts;
 
     if (*format == '!') {
@@ -378,6 +378,7 @@ gjs_parse_call_args(JSContext    *cx,
         switch (*fmt_iter) {
         case '|':
             n_required = n_total;
+            optional_args = true;
             continue;
         case '?':
             continue;
@@ -386,7 +387,7 @@ gjs_parse_call_args(JSContext    *cx,
         }
     }
 
-    if (n_required == 0)
+    if (!optional_args)
         n_required = n_total;
 
     g_assert(((void) "Wrong number of parameters passed to gjs_parse_call_args()",
diff --git a/test/gjs-test-call-args.cpp b/test/gjs-test-call-args.cpp
index 9eea273..1adebb8 100644
--- a/test/gjs-test-call-args.cpp
+++ b/test/gjs-test-call-args.cpp
@@ -134,6 +134,13 @@ JSNATIVE_TEST_FUNC_BEGIN(optional_args_only_required)
     g_assert_cmpint(val3, ==, false);
 JSNATIVE_TEST_FUNC_END
 
+JSNATIVE_TEST_FUNC_BEGIN(only_optional_args)
+    int val1, val2;
+    retval = gjs_parse_call_args(cx, "onlyOptionalArgs", args, "|ii",
+                                 "val1", &val1,
+                                 "val2", &val2);
+JSNATIVE_TEST_FUNC_END
+
 JSNATIVE_TEST_FUNC_BEGIN(unsigned_enum_arg)
     test_enum_t val;
     retval = gjs_parse_call_args(cx, "unsignedEnumArg", args, "i",
@@ -225,6 +232,7 @@ static JSFunctionSpec native_test_funcs[] = {
     JS_FS("oneOfEachType", one_of_each_type, 0, 0),
     JS_FS("optionalArgsAll", optional_args_all, 0, 0),
     JS_FS("optionalArgsOnlyRequired", optional_args_only_required, 0, 0),
+    JS_FS("onlyOptionalArgs", only_optional_args, 0, 0),
     JS_FS("unsignedEnumArg", unsigned_enum_arg, 0, 0),
     JS_FS("signedEnumArg", signed_enum_arg, 0, 0),
     JS_FS("oneOfEachNullableType", one_of_each_nullable_type, 0, 0),
@@ -339,6 +347,12 @@ gjs_test_add_tests_for_parse_call_args(void)
     ADD_CALL_ARGS_TEST("signed-enum-types-work", "signedEnumArg(-1)");
     ADD_CALL_ARGS_TEST("one-of-each-nullable-type-works",
                        "oneOfEachNullableType(null, null, null)");
+    ADD_CALL_ARGS_TEST("passing-no-arguments-when-all-optional",
+                       "onlyOptionalArgs()");
+    ADD_CALL_ARGS_TEST("passing-some-arguments-when-all-optional",
+                       "onlyOptionalArgs(1)");
+    ADD_CALL_ARGS_TEST("passing-all-arguments-when-all-optional",
+                       "onlyOptionalArgs(1, 1)");
     ADD_CALL_ARGS_TEST_XFAIL("allocated-args-are-freed-on-error",
                              "unwindFreeTest('', '', {}, 1, -1)"
                              "//*Value * is out of range");


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