[gjs] Allow null/undefined to be passed in to callbacks



commit e2fe272a26e17a413b74178d48136be1108ff2a0
Author: Johan Dahlin <johan gnome org>
Date:   Thu Dec 3 19:33:21 2009 -0200

    Allow null/undefined to be passed in to callbacks
    
    Some functions do not require a callback to be passed in,
    instead NULL is accepted, the same should be supported in gjs
    by allowing null/undefined.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=603735

 gi/function.c                  |    5 +++++
 test/js/testEverythingBasic.js |    3 +++
 2 files changed, 8 insertions(+), 0 deletions(-)
---
diff --git a/gi/function.c b/gi/function.c
index 5d403e8..09bb09f 100644
--- a/gi/function.c
+++ b/gi/function.c
@@ -382,6 +382,11 @@ gjs_callback_from_arguments(JSContext *context,
     if (is_notify)
         goto out;
 
+    if (JSVAL_IS_NULL(argv[*argv_pos]) || JSVAL_IS_VOID(argv[*argv_pos])) {
+        *closure = NULL;
+        return TRUE;
+    }
+
     invoke_info = gjs_callback_invoke_prepare(context,
                                               argv[*argv_pos],
                                               (GICallableInfo*)interface_info);
diff --git a/test/js/testEverythingBasic.js b/test/js/testEverythingBasic.js
index 755ad45..40d5710 100644
--- a/test/js/testEverythingBasic.js
+++ b/test/js/testEverythingBasic.js
@@ -190,6 +190,9 @@ function testCallback() {
                        return 42;
                    };
     assertEquals('Callback', Everything.test_callback(callback), 42);
+
+    assertEquals('CallbackNull', Everything.test_callback(null), 0);
+    assertEquals('CallbackUndefined', Everything.test_callback(undefined), 0);
 }
 
 function testCallbackUserData() {



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