[gjs/wip/gdbus-2: 11/13] GDBus: use gio-style asyncs for the new bindings



commit 22eba902546f50147058fd5a17a7930565395eda
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sat Jun 16 18:38:17 2012 +0200

    GDBus: use gio-style asyncs for the new bindings
    
    Split asynchronous result collection into a MethodFinish function,
    so that exceptions can be caught in the usual way instead of being
    passed as objects to the functions. Also, remove the flags parameter
    and make the cancellable argument compulsory, matching the code
    generated by gdbus-codegen.
    Finally, make proxy initialization explicit, reducing the amount
    of magic done by the bindings.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=669350

 modules/overrides/Gio.js |   82 +++++++++++-----
 test/js/testGDBus2.js    |  240 +++++++++++++++++++++++++++++-----------------
 2 files changed, 209 insertions(+), 113 deletions(-)
---
diff --git a/modules/overrides/Gio.js b/modules/overrides/Gio.js
index 0c157dd..37b1384 100644
--- a/modules/overrides/Gio.js
+++ b/modules/overrides/Gio.js
@@ -37,6 +37,48 @@ function _signatureLength(sig) {
     return counter;
 }
 
+function _gioStyleProxyFinish(result) {
+    return this.call_finish(result).deep_unpack();
+}
+
+function _gioStyleProxyInvoker(methodName, sync, inSignature, arg_array) {
+    var asyncCallback, cancellable;
+
+    /* Convert arg_array to a *real* array */
+    arg_array = Array.prototype.slice.call(arg_array);
+
+    var signatureLength = inSignature.length;
+    var numberArgs = sync ? signatureLength + 2 : signatureLength + 1;
+
+    if (arg_array.length < numberArgs) {
+        throw new Error("Wrong number of arguments passed for method: " + methodName +
+                       ". Expected " + numberArgs + ", got " + arg_array.length);
+    }
+
+    if (!sync)
+        asyncCallback = arg_array.pop();
+    cancellable = arg_array.pop();
+
+    var params = GLib.Variant.new('(' + inSignature.join('') + ')', arg_array);
+    if (!sync) {
+        this.call(methodName,
+                  params,
+                  Gio.DBusCallFlags.NONE,
+                  -1,
+                  cancellable,
+                  asyncCallback);
+
+        // silent a warning
+        return undefined;
+    } else {
+        return this.call_sync(methodName,
+                              params,
+                              Gio.DBusCallFlags.NONE,
+                              -1,
+                              cancellable).deep_unpack();
+    }
+}
+
 function _proxyInvoker(methodName, sync, inSignature, arg_array) {
     var replyFunc;
     var flags = 0;
@@ -207,31 +249,8 @@ const DBusProxyClass = new Lang.Class({
             params.g_interface_name = this.Interface.name;
             params.g_interface_info = this.Interface;
 
-            let asyncCallback, cancellable = null;
-            if ('g_async_callback' in params) {
-                asyncCallback = params.g_async_callback;
-                delete params.g_async_callback;
-            }
-            if ('g_cancellable' in params) {
-                cancellable = params.g_cancellable;
-                delete params.g_cancellable;
-            }
-
             this.parent(params);
 
-            if (asyncCallback) {
-                this.init_async(GLib.PRIORITY_DEFAULT, cancellable, function(initable, result) {
-                    try {
-                        initable.init_finish(result);
-                        asyncCallback(initable, null);
-                    } catch(e) {
-                        asyncCallback(null, e);
-                    }
-                });
-            } else {
-                this.init(cancellable);
-            }
-
             this.connect('g-signal', _convertToNativeSignal);
         }
 
@@ -242,14 +261,27 @@ const DBusProxyClass = new Lang.Class({
         this._addDBusConvenience(classParams);
     },
 
+    _makeGioStyleProxyMethod: function(method, sync) {
+        var name = method.name;
+        var inArgs = method.in_args;
+        var inSignature = [ ];
+        for (var i = 0; i < inArgs.length; i++)
+            inSignature.push(inArgs[i].signature);
+
+        return this.wrapFunction(method, function() {
+            return _gioStyleProxyInvoker.call(this, name, sync, inSignature, arguments);
+        });
+    },
+
     _addDBusConvenience: function(classParams) {
         let info = classParams.Interface;
 
         let i, methods = info.methods;
         for (i = 0; i < methods.length; i++) {
             var method = methods[i];
-            this.prototype[method.name + 'Remote'] = _makeProxyMethod.call(this, methods[i], false, true);
-            this.prototype[method.name + 'Sync'] = _makeProxyMethod.call(this, methods[i], true, true);
+            this.prototype[method.name + 'Remote'] = this._makeGioStyleProxyMethod(methods[i], false);
+            this.prototype[method.name + 'Finish'] = this.wrapFunction(method.name + 'Finish', _gioStyleProxyFinish);
+            this.prototype[method.name + 'Sync'] = this._makeGioStyleProxyMethod(methods[i], true);
         }
 
         let properties = info.properties;
diff --git a/test/js/testGDBus2.js b/test/js/testGDBus2.js
index 7255285..96d16f6 100644
--- a/test/js/testGDBus2.js
+++ b/test/js/testGDBus2.js
@@ -232,106 +232,133 @@ function testInitStuff() {
     var theError;
     proxy = new TestProxy({ g_connection: Gio.DBus.session,
                             g_name: 'org.gnome.gjs.Test',
-                            g_object_path: '/org/gnome/gjs/Test',
-                            g_async_callback: function (obj, error) {
-                                theError = error;
-                                proxy = obj;
-
-                                Mainloop.quit('testGDBus');
-                            } });
+                            g_object_path: '/org/gnome/gjs/Test'
+                          });
+    proxy.init_async(GLib.PRIORITY_DEFAULT, null, function (obj, result) {
+        try {
+            obj.init_finish(result);
+        } catch(error) {
+            theError = error;
+            proxy = obj;
+        }
 
-    log(typeof(proxy._init) + " " + typeof(proxy._construct) + " " + typeof(proxy.frobateStuffRemote));
+        Mainloop.quit('testGDBus');
+    });
 
     Mainloop.run('testGDBus');
 
-    assertNull(theError);
-    assertNotNull(proxy);
+    assertUndefined(theError);
+    assertNotUndefined(proxy);
 }
 
 function testFrobateStuff() {
     let theResult, theExcp;
-    proxy.frobateStuffRemote({}, function(result, excp) {
-        theResult = result;
-        theExcp = excp;
+    proxy.frobateStuffRemote({}, null, function(proxy, result) {
+        try {
+            [theResult] = proxy.frobateStuffFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
 
-    assertNull(theExcp);
-    assertEquals("world", theResult[0].hello.deep_unpack());
+    assertUndefined(theExcp);
+    assertEquals("world", theResult.hello.deep_unpack());
 }
 
 /* excp must be exactly the exception thrown by the remote method
    (more or less) */
 function testThrowException() {
     let theResult, theExcp;
-    proxy.alwaysThrowExceptionRemote({}, function(result, excp) {
-        theResult = result;
-        theExcp = excp;
+    proxy.alwaysThrowExceptionRemote({}, null, function(proxy, result) {
+        try {
+            [theResult] = proxy.alwaysThrowExceptionFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
 
-    assertNull(theResult);
-    assertNotNull(theExcp);
+    assertUndefined(theResult);
+    assertNotUndefined(theExcp);
 }
 
 function testNonJsonFrobateStuff() {
     let theResult, theExcp;
-    proxy.nonJsonFrobateStuffRemote(42, function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.nonJsonFrobateStuffRemote(42, null, function(proxy, result) {
+        try {
+            [theResult] = proxy.nonJsonFrobateStuffFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
 
     assertEquals("42 it is!", theResult);
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 function testNoInParameter() {
     let theResult, theExcp;
-    proxy.noInParameterRemote(function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.noInParameterRemote(null, function(proxy, result) {
+        try {
+            [theResult] = proxy.noInParameterFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
 
     assertEquals("Yes!", theResult);
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 function testMultipleInArgs() {
     let theResult, theExcp;
-    proxy.multipleInArgsRemote(1, 2, 3, 4, 5, function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.multipleInArgsRemote(1, 2, 3, 4, 5, null, function(proxy, result) {
+        try {
+            [theResult] = proxy.multipleInArgsFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
 
     assertEquals("1 2 3 4 5", theResult);
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 function testNoReturnValue() {
     let theResult, theExcp;
-    proxy.noReturnValueRemote(function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.noReturnValueRemote(null, function(proxy, result) {
+        try {
+            [theResult] = proxy.noReturnValueFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
 
     assertEquals(undefined, theResult);
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 function testEmitSignal() {
@@ -345,18 +372,20 @@ function testEmitSignal() {
 
                                      proxy.disconnectSignal(id);
                                  });
-    proxy.emitSignalRemote(function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
-        if (excp)
-            log("Signal emission exception: " + excp);
+    proxy.emitSignalRemote(null, function(proxy, result) {
+        try {
+            [theResult] = proxy.emitSignalFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
 
     assertUndefined('result should be undefined', theResult);
-    assertNull('no exception set', theExcp);
+    assertUndefined('no exception set', theExcp);
     assertEquals('number of signals received', signalReceived, 1);
     assertEquals('signal argument', signalArgument, "foobar");
 
@@ -364,9 +393,13 @@ function testEmitSignal() {
 
 function testMultipleOutValues() {
     let theResult, theExcp;
-    proxy.multipleOutValuesRemote(function(result, excp) {
-        theResult = result;
-        theExcp = excp;
+    proxy.multipleOutValuesRemote(null, function(proxy, result) {
+        try {
+            theResult = proxy.multipleOutValuesFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
@@ -375,14 +408,18 @@ function testMultipleOutValues() {
     assertEquals("Hello", theResult[0]);
     assertEquals("World", theResult[1]);
     assertEquals("!", theResult[2]);
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 function testOneArrayOut() {
     let theResult, theExcp;
-    proxy.oneArrayOutRemote(function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.oneArrayOutRemote(null, function(proxy, result) {
+        try {
+            [theResult] = proxy.oneArrayOutFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
@@ -391,14 +428,18 @@ function testOneArrayOut() {
     assertEquals("Hello", theResult[0]);
     assertEquals("World", theResult[1]);
     assertEquals("!", theResult[2]);
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 function testArrayOfArrayOut() {
     let theResult, theExcp;
-    proxy.arrayOfArrayOutRemote(function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.arrayOfArrayOutRemote(null, function(proxy, result) {
+        try {
+            [theResult] = proxy.arrayOfArrayOutFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
@@ -413,14 +454,18 @@ function testArrayOfArrayOut() {
     assertEquals("World", a2[0]);
     assertEquals("Hello", a2[1]);;
 
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 function testMultipleArrayOut() {
     let theResult, theExcp;
-    proxy.multipleArrayOutRemote(function(result, excp) {
-        theResult = result;
-        theExcp = excp;
+    proxy.multipleArrayOutRemote(null, function(proxy, result) {
+        try {
+            theResult = proxy.multipleArrayOutFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
@@ -435,7 +480,7 @@ function testMultipleArrayOut() {
     assertEquals("World", a2[0]);
     assertEquals("Hello", a2[1]);;
 
-    assertNull(theExcp);
+    assertUndefined(theExcp);
 }
 
 /* We are returning an array but the signature says it's an integer,
@@ -443,31 +488,38 @@ function testMultipleArrayOut() {
  */
 function testArrayOutBadSig() {
     let theResult, theExcp;
-    proxy.arrayOutBadSigRemote(function(result, excp) {
-        theResult = result;
-        theExcp = excp;
+    proxy.arrayOutBadSigRemote(null, function(proxy, result) {
+        try {
+            theResult = proxy.arrayOutBadSigFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
-    assertNull(theResult);
-    assertNotNull(theExcp);
+    assertUndefined(theResult);
+    assertNotUndefined(theExcp);
 }
 
 function testAsyncImplementation() {
     let someString = "Hello world!";
     let someInt = 42;
     let theResult, theExcp;
-    proxy.echoRemote(someString, someInt,
-                     function(result, excp) {
-                         theResult = result;
-                         theExcp = excp;
-                         Mainloop.quit('testGDBus');
-                     });
+    proxy.echoRemote(someString, someInt, null, function(proxy, result) {
+        try {
+            theResult = proxy.echoFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
+        Mainloop.quit('testGDBus');
+    });
 
     Mainloop.run('testGDBus');
-    assertNull(theExcp);
-    assertNotNull(theResult);
+    assertUndefined(theExcp);
+    assertNotUndefined(theResult);
     assertEquals(theResult[0], someString);
     assertEquals(theResult[1], someInt);
 }
@@ -476,31 +528,39 @@ function testBytes() {
     let someBytes = [ 0, 63, 234 ];
     let theResult, theExcp;
     for (let i = 0; i < someBytes.length; ++i) {
-        theResult = null;
-        theExcp = null;
-        proxy.byteEchoRemote(someBytes[i], function(result, excp) {
-            [theResult] = result;
-            theExcp = excp;
+        theResult = undefined;
+        theExcp = undefined;
+        proxy.byteEchoRemote(someBytes[i], null, function(proxy, result) {
+            try {
+                [theResult] = proxy.byteEchoFinish(result);
+            } catch(excp) {
+                theExcp = excp;
+            }
+
             Mainloop.quit('testGDBus');
         });
 
         Mainloop.run('testGDBus');
-        assertNull(theExcp);
-        assertNotNull(theResult);
+        assertUndefined(theExcp);
+        assertNotUndefined(theResult);
         assertEquals(someBytes[i], theResult);
     }
 }
 
 function testStructArray() {
     let theResult, theExcp;
-    proxy.structArrayRemote(function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.structArrayRemote(null, function(proxy, result) {
+        try {
+            [theResult] = proxy.structArrayFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
     Mainloop.run('testGDBus');
-    assertNull(theExcp);
-    assertNotNull(theResult);
+    assertUndefined(theExcp);
+    assertNotUndefined(theResult);
     assertEquals(theResult[0][0], 128);
     assertEquals(theResult[0][1], 123456);
     assertEquals(theResult[1][0], 42);
@@ -516,15 +576,19 @@ function testDictSignatures() {
         aDoubleBeforeAndAfter: GLib.Variant.new('d', 10.5),
     };
     let theResult, theExcp;
-    proxy.dictEchoRemote(someDict, function(result, excp) {
-        [theResult] = result;
-        theExcp = excp;
+    proxy.dictEchoRemote(someDict, null, function(proxy, result) {
+        try {
+            [theResult] = proxy.dictEchoFinish(result);
+        } catch(excp) {
+            theExcp = excp;
+        }
+
         Mainloop.quit('testGDBus');
     });
 
     Mainloop.run('testGDBus');
-    assertNull(theExcp);
-    assertNotNull(theResult);
+    assertUndefined(theExcp);
+    assertNotUndefined(theResult);
 
     // verify the fractional part was dropped off int
     assertEquals(11, theResult['anInteger'].deep_unpack());



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