Commit e1ea03a0 authored by Florian Müllner's avatar Florian Müllner

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.

GNOME/gjs!229
parent d32e4a94
......@@ -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();
......
......@@ -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();
......
......@@ -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) {
......
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment