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



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

    legacy: Ensure generated GType names are valid
    
    Legacy 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
    underscored.

 installed-tests/js/testLegacyGObject.js | 12 ++++++++++++
 modules/_legacy.js                      |  2 +-
 2 files changed, 13 insertions(+), 1 deletion(-)
---
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) {


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