[gjs/wip/ptomato/classes: 3/8] class: Move to ES6 classes in internal code



commit fd9257a234f34d2c052caffd41e0fff68714c226
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Jul 23 21:10:38 2017 -0700

    class: Move to ES6 classes in internal code
    
    Where possible, move usage of Lang.Class within GJS to use ES6 classes.
    That is currently possible for any classes which don't inherit from
    GObject classes and don't implement Lang.Interfaces.
    
    Remove the documentation for the Lang.Class class framework, as it's now
    become outdated.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=785652

 doc/Class_Framework.md            |   98 -------------------------------------
 installed-tests/js/minijasmine.js |   30 +++++------
 installed-tests/js/testGDBus.js   |   92 ++++++++++++++++------------------
 installed-tests/js/testLang.js    |   12 ++---
 installed-tests/js/testSignals.js |    7 +--
 5 files changed, 65 insertions(+), 174 deletions(-)
---
diff --git a/installed-tests/js/minijasmine.js b/installed-tests/js/minijasmine.js
index b03c205..6af74e3 100644
--- a/installed-tests/js/minijasmine.js
+++ b/installed-tests/js/minijasmine.js
@@ -48,19 +48,17 @@ Lang.copyProperties(jasmineInterface, window);
 
 // Reporter that outputs according to the Test Anything Protocol
 // See http://testanything.org/tap-specification.html
-const TapReporter = new Lang.Class({
-    Name: 'TapReporter',
-
-    _init: function () {
+class TapReporter {
+    constructor() {
         this._failedSuites = [];
         this._specCount = 0;
-    },
+    }
 
-    jasmineStarted: function (info) {
+    jasmineStarted(info) {
         print('1..' + info.totalSpecsDefined);
-    },
+    }
 
-    jasmineDone: function () {
+    jasmineDone() {
         this._failedSuites.forEach(failure => {
             failure.failedExpectations.forEach(result => {
                 print('not ok - An error was thrown outside a test');
@@ -69,9 +67,9 @@ const TapReporter = new Lang.Class({
         });
 
         window._jasmineMain.quit();
-    },
+    }
 
-    suiteDone: function (result) {
+    suiteDone(result) {
         if (result.failedExpectations && result.failedExpectations.length > 0) {
             window._jasmineRetval = 1;
             this._failedSuites.push(result);
@@ -80,13 +78,13 @@ const TapReporter = new Lang.Class({
         if (result.status === 'disabled') {
             print('# Suite was disabled:', result.fullName);
         }
-    },
+    }
 
-    specStarted: function () {
+    specStarted() {
         this._specCount++;
-    },
+    }
 
-    specDone: function (result) {
+    specDone(result) {
         let tap_report;
         if (result.status === 'failed') {
             window._jasmineRetval = 1;
@@ -110,7 +108,7 @@ const TapReporter = new Lang.Class({
                 print(stackTrace.split('\n').map((str) => '#   ' + str).join('\n'));
             });
         }
-    },
-});
+    }
+}
 
 window._jasmineEnv.addReporter(new TapReporter());
diff --git a/installed-tests/js/testGDBus.js b/installed-tests/js/testGDBus.js
index cf24c13..11cd739 100644
--- a/installed-tests/js/testGDBus.js
+++ b/installed-tests/js/testGDBus.js
@@ -81,131 +81,127 @@ var TestIface = '<node> \
 </interface> \
 </node>';
 
-/* Test is the actual object exporting the dbus methods */
-function Test() {
-    this._init();
-}
-
 const PROP_READ_WRITE_INITIAL_VALUE = 58;
 const PROP_WRITE_ONLY_INITIAL_VALUE = "Initial value";
 
-Test.prototype = {
-    _init: function(){
+/* Test is the actual object exporting the dbus methods */
+class Test {
+    constructor() {
         this._propWriteOnly = PROP_WRITE_ONLY_INITIAL_VALUE;
         this._propReadWrite = PROP_READ_WRITE_INITIAL_VALUE;
 
         this._impl = Gio.DBusExportedObject.wrapJSObject(TestIface, this);
         this._impl.export(Gio.DBus.session, '/org/gnome/gjs/Test');
-    },
+    }
 
-    frobateStuff: function(args) {
+    frobateStuff(args) {
         return { hello: new GLib.Variant('s', 'world') };
-    },
+    }
 
-    nonJsonFrobateStuff: function(i) {
+    nonJsonFrobateStuff(i) {
         if (i == 42) {
             return "42 it is!";
         } else {
             return "Oops";
         }
-    },
+    }
 
-    alwaysThrowException: function() {
+    alwaysThrowException() {
         throw Error("Exception!");
-    },
+    }
 
-    thisDoesNotExist: function () {
+    thisDoesNotExist() {
         /* We'll remove this later! */
-    },
+    }
 
-    noInParameter: function() {
+    noInParameter() {
         return "Yes!";
-    },
+    }
 
-    multipleInArgs: function(a, b, c, d, e) {
+    multipleInArgs(a, b, c, d, e) {
         return a + " " + b + " " + c + " " + d + " " + e;
-    },
+    }
 
-    emitSignal: function() {
+    emitSignal() {
         this._impl.emit_signal('signalFoo', GLib.Variant.new('(s)', [ "foobar" ]));
-    },
+    }
 
-    noReturnValue: function() {
+    noReturnValue() {
         /* Empty! */
-    },
+    }
 
     /* The following two functions have identical return values
      * in JS, but the bus message will be different.
      * multipleOutValues is "sss", while oneArrayOut is "as"
      */
-    multipleOutValues: function() {
+    multipleOutValues() {
         return [ "Hello", "World", "!" ];
-    },
+    }
 
-    oneArrayOut: function() {
+    oneArrayOut() {
         return [ "Hello", "World", "!" ];
-    },
+    }
 
     /* Same thing again. In this case multipleArrayOut is "asas",
      * while arrayOfArrayOut is "aas".
      */
-    multipleArrayOut: function() {
+    multipleArrayOut() {
         return [[ "Hello", "World" ], [ "World", "Hello" ]];
-    },
+    }
 
-    arrayOfArrayOut: function() {
+    arrayOfArrayOut() {
         return [[ "Hello", "World" ], [ "World", "Hello" ]];
-    },
+    }
 
-    arrayOutBadSig: function() {
+    arrayOutBadSig() {
         return Symbol('Hello World!');
-    },
+    }
 
-    byteArrayEcho: function(binaryString) {
+    byteArrayEcho(binaryString) {
         return binaryString;
-    },
+    }
 
-    byteEcho: function(aByte) {
+    byteEcho(aByte) {
         return aByte;
-    },
+    }
 
-    dictEcho: function(dict) {
+    dictEcho(dict) {
         return dict;
-    },
+    }
 
     /* This one is implemented asynchronously. Returns
      * the input arguments */
-    echoAsync: function(parameters, invocation) {
+    echoAsync(parameters, invocation) {
         var [someString, someInt] = parameters;
         GLib.idle_add(GLib.PRIORITY_DEFAULT, function() {
             invocation.return_value(new GLib.Variant('(si)', [someString, someInt]));
             return false;
         });
-    },
+    }
 
     // boolean
     get PropReadOnly() {
         return true;
-    },
+    }
 
     // string
     set PropWriteOnly(value) {
         this._propWriteOnly = value;
-    },
+    }
 
     // variant
     get PropReadWrite() {
         return new GLib.Variant('s', this._propReadWrite.toString());
-    },
+    }
 
     set PropReadWrite(value) {
         this._propReadWrite = value.deep_unpack();
-    },
+    }
 
-    structArray: function () {
+    structArray() {
         return [[128, 123456], [42, 654321]];
     }
-};
+}
 
 const ProxyClass = Gio.DBusProxy.makeProxyWrapper(TestIface);
 
diff --git a/installed-tests/js/testLang.js b/installed-tests/js/testLang.js
index 1e0d307..13735e6 100644
--- a/installed-tests/js/testLang.js
+++ b/installed-tests/js/testLang.js
@@ -53,16 +53,14 @@ describe('Lang module', function () {
 
 
     describe('bind()', function () {
-        const Obj = new Lang.Class({
-            Name: 'Obj',
-            callback: function () {
-                return true;
-            },
-        });
         let o;
 
         beforeEach(function () {
-            o = new Obj();
+            o = {
+                callback() {
+                    return true;
+                }
+            };
             spyOn(o, 'callback').and.callThrough();
         });
 
diff --git a/installed-tests/js/testSignals.js b/installed-tests/js/testSignals.js
index 5944b29..58d3530 100644
--- a/installed-tests/js/testSignals.js
+++ b/installed-tests/js/testSignals.js
@@ -8,14 +8,11 @@ const Foo = new Lang.Class({
     _init: function () {},
 });
 
-describe('Object with signals', function () {
+describe('Legacy object with signals', function () {
     testSignals(Foo);
 });
 
-const FooWithoutSignals = new Lang.Class({
-    Name: 'FooWithoutSignals',
-    _init: function () {},
-});
+class FooWithoutSignals {}
 Signals.addSignalMethods(FooWithoutSignals.prototype);
 
 describe('Object with signals added', function () {


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