[gjs/wip/ptomato/classes: 10/10] legacy class: Add name property to class object



commit 271dc2064f3417333910cfbee81ef62d3b682650
Author: Philip Chimento <philip endlessm com>
Date:   Tue Aug 8 17:32:09 2017 +0200

    legacy class: Add name property to class object
    
    Instead of the __name__ property on instances, add a name property to
    class objects. The __name__ property continues to work, for backwards
    compatibility in case any code used it (unlikely), but this is compatible
    with ES6 classes.

 NEWS                                    |    8 ++++++++
 installed-tests/js/testLegacyClass.js   |    8 ++++++++
 installed-tests/js/testLegacyGObject.js |   12 ++++++++++--
 modules/_legacy.js                      |   26 ++++++++++++++++++++++++++
 4 files changed, 52 insertions(+), 2 deletions(-)
---
diff --git a/NEWS b/NEWS
index 059b7e4..2e865e6 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,11 @@
+NEXT
+----
+
+- Deprecation: The private "__name__" property on Lang.Class instances is
+  now discouraged. Code should not have been using this anyway, but if it did
+  then it should use the "name" property on the class (this.__name__ should
+  become this.constructor.name), which is compatible with ES6 classes.
+
 Version 1.49.90
 ---------------
 
diff --git a/installed-tests/js/testLegacyClass.js b/installed-tests/js/testLegacyClass.js
index 5675796..6805532 100644
--- a/installed-tests/js/testLegacyClass.js
+++ b/installed-tests/js/testLegacyClass.js
@@ -216,6 +216,10 @@ describe('Class framework', function () {
         expect(newMagic instanceof MagicBase).toBeTruthy();
     });
 
+    it('has a name', function () {
+        expect(Magic.name).toEqual('Magic');
+    });
+
     it('reports a sensible value for toString()', function () {
         let newMagic = new MagicBase();
         expect(newMagic.toString()).toEqual('[object MagicBase]');
@@ -424,6 +428,10 @@ describe('An interface', function () {
         expect(InterfaceRequiringOtherInterface instanceof Lang.Interface).toBeTruthy();
     });
 
+    it('has a name', function () {
+        expect(AnInterface.name).toEqual('AnInterface');
+    });
+
     it('cannot be instantiated', function () {
         expect(() => new AnInterface()).toThrow();
     });
diff --git a/installed-tests/js/testLegacyGObject.js b/installed-tests/js/testLegacyGObject.js
index f79e63e..35f82fb 100644
--- a/installed-tests/js/testLegacyGObject.js
+++ b/installed-tests/js/testLegacyGObject.js
@@ -214,6 +214,10 @@ describe('GObject class', function () {
         expect(myInstance3.construct).toEqual('quz');
     });
 
+    it('has a name', function () {
+        expect(MyObject.name).toEqual('MyObject');
+    });
+
     // the following would (should) cause a CRITICAL:
     // myInstance.readonly = 'val';
     // myInstance.construct = 'val';
@@ -513,6 +517,10 @@ describe('GObject interface', function () {
         expect(() => new AGObjectInterface()).toThrow();
     });
 
+    it('has a name', function () {
+        expect(AGObjectInterface.name).toEqual('AGObjectInterface');
+    });
+
     it('reports its type name', function () {
         expect(AGObjectInterface.$gtype.name).toEqual('ArbitraryGTypeName');
     });
@@ -527,8 +535,8 @@ describe('GObject interface', function () {
     it('is implemented by a GObject class with the correct class object', function () {
         let obj = new GObjectImplementingGObjectInterface();
         expect(obj.constructor).toEqual(GObjectImplementingGObjectInterface);
-        expect(obj.constructor.toString())
-            .toEqual('[object GObjectClass for GObjectImplementingGObjectInterface]');
+        expect(obj.constructor.name)
+            .toEqual('GObjectImplementingGObjectInterface');
     });
 
     it('can be implemented by a class also implementing a Lang.Interface', function () {
diff --git a/modules/_legacy.js b/modules/_legacy.js
index 6f73ade..99cef8c 100644
--- a/modules/_legacy.js
+++ b/modules/_legacy.js
@@ -149,6 +149,12 @@ Class.prototype._construct = function(params) {
             value: interfaces,
         },
     });
+    Object.defineProperty(newClass, 'name', {
+        writable: false,
+        configurable: true,
+        enumerable: false,
+        value: name,
+    });
 
     interfaces.forEach((iface) => {
         iface._check(newClass.prototype);
@@ -313,6 +319,12 @@ Interface.prototype._construct = function (params) {
         enumerable: false,
         value: this.constructor,
     });
+    Object.defineProperty(newInterface, 'name', {
+        writable: false,
+        configurable: true,
+        enumerable: false,
+        value: params.Name,
+    });
 
     return newInterface;
 };
@@ -544,6 +556,13 @@ function defineGObjectLegacyObjects(GObject) {
                     value: interfaces,
                 },
             });
+            // Overwrite the C++-set class name, as if it were an ES6 class
+            Object.defineProperty(newClass, 'name', {
+                writable: false,
+                configurable: true,
+                enumerable: false,
+                value: name,
+            });
 
             interfaces.forEach((iface) => {
                 if (iface instanceof Interface)
@@ -605,6 +624,13 @@ function defineGObjectLegacyObjects(GObject) {
             enumerable: false,
             value: this.constructor,
         });
+        // Overwrite the C++-set class name, as if it were an ES6 class
+        Object.defineProperty(newInterface, 'name', {
+            writable: false,
+            configurable: true,
+            enumerable: false,
+            value: params.Name,
+        });
 
         return newInterface;
     };


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