[gjs/wip/fmuellner/legacy-gtype-names] GObject: Ensure generated GType names are valid



commit e1ea03a0214a3d9bf91977a0cbe771a9004834b7
Author: Florian Müllner <fmuellner gnome org>
Date:   Mon Sep 3 15:34:21 2018 +0200

    GObject: Ensure generated GType names are valid
    
    JS classes accept names that aren't valid GType names. Usually this
    is something the programmer can take into account, however sometimes
    a parent class isn't under their control and can therefore change
    unexpectedly (gnome-shell extensions are a primary example).
    
    Handle this case gracefully by replacing any invalid characters with
    underscores.
    
    https://gitlab.gnome.org/GNOME/gjs/merge_requests/229

 installed-tests/js/testGObjectClass.js  |  9 +++++++++
 installed-tests/js/testLegacyGObject.js | 12 ++++++++++++
 modules/_legacy.js                      |  2 +-
 modules/overrides/GObject.js            |  2 +-
 4 files changed, 23 insertions(+), 2 deletions(-)
---
diff --git a/installed-tests/js/testGObjectClass.js b/installed-tests/js/testGObjectClass.js
index 4371f598..680c96b1 100644
--- a/installed-tests/js/testGObjectClass.js
+++ b/installed-tests/js/testGObjectClass.js
@@ -139,6 +139,8 @@ const Derived = GObject.registerClass(class Derived extends MyObject {
     }
 });
 
+const Cla$$ = GObject.registerClass(class Cla$$ extends MyObject {});
+
 const MyCustomInit = GObject.registerClass(class MyCustomInit extends GObject.Object {
     _instance_init() {
         this.foo = true;
@@ -292,6 +294,13 @@ describe('GObject class with decorator', function () {
         expect(derived.readwrite).toEqual('yes');
     });
 
+    it('can have any valid class name', function () {
+        let obj = new Cla$$();
+
+        expect(obj instanceof Cla$$).toBeTruthy();
+        expect(obj instanceof MyObject).toBeTruthy();
+    });
+
     it('calls its _instance_init() function while chaining up in constructor', function () {
         let instance = new MyCustomInit();
         expect(instance.foo).toBeTruthy();
diff --git a/installed-tests/js/testLegacyGObject.js b/installed-tests/js/testLegacyGObject.js
index 38cd6ea2..87ef31a5 100644
--- a/installed-tests/js/testLegacyGObject.js
+++ b/installed-tests/js/testLegacyGObject.js
@@ -164,6 +164,11 @@ const Derived = new Lang.Class({
     }
 });
 
+const OddlyNamed = new Lang.Class({
+    Name: 'Legacy.OddlyNamed',
+    Extends: MyObject
+});
+
 const MyCustomInit = new Lang.Class({
     Name: 'MyCustomInit',
     Extends: GObject.Object,
@@ -316,6 +321,13 @@ describe('GObject class', function () {
         expect(derived.readwrite).toEqual('yes');
     });
 
+    it('can have any valid Lang.Class name', function () {
+        let obj = new OddlyNamed();
+
+        expect(obj instanceof OddlyNamed).toBeTruthy();
+        expect(obj instanceof MyObject).toBeTruthy();
+    });
+
     it('calls its _instance_init() function while chaining up in constructor', function () {
         let instance = new MyCustomInit();
         expect(instance.foo).toBeTruthy();
diff --git a/modules/_legacy.js b/modules/_legacy.js
index 4dcde002..8d50c352 100644
--- a/modules/_legacy.js
+++ b/modules/_legacy.js
@@ -434,7 +434,7 @@ function defineGObjectLegacyObjects(GObject) {
         if (params.GTypeName)
             return params.GTypeName;
         else
-            return 'Gjs_' + params.Name;
+            return 'Gjs_' + params.Name.replace(/[^a-z0-9_+-]/gi, '_');
     }
 
     function _getGObjectInterfaces(interfaces) {
diff --git a/modules/overrides/GObject.js b/modules/overrides/GObject.js
index 15bbea8f..8181b815 100644
--- a/modules/overrides/GObject.js
+++ b/modules/overrides/GObject.js
@@ -110,7 +110,7 @@ function _createSignals(gtype, signals) {
 function _createGTypeName(klass) {
     if (klass.hasOwnProperty(GTypeName))
         return klass[GTypeName];
-    return `Gjs_${klass.name}`;
+    return `Gjs_${klass.name.replace(/[^a-z0-9+_-]/gi, '_')}`;
 }
 
 function _propertiesAsArray(klass) {


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