[gjs: 6/12] GObject: Guard against classes with GType name 'Gjs_'
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 6/12] GObject: Guard against classes with GType name 'Gjs_'
- Date: Fri, 5 Mar 2021 05:05:09 +0000 (UTC)
commit 276ef4153b73d54b5ed1d9a2ece921a6f71147b3
Author: Philip Chimento <philip chimento gmail com>
Date: Thu Feb 18 18:39:02 2021 -0800
GObject: Guard against classes with GType name 'Gjs_'
Previously, if an anonymous class expression was passed to
GObject.registerClass(), then the GType name would become 'Gjs_', meaning
that only one of these classes could ever be registered in the same
process.
Instead, use a name based on a UUID when the class expression is
anonymous. I chose a UUID, and not a sequence number, for example, so that
it is random and cannot be the same in any two given runs.
installed-tests/js/testGObjectClass.js | 11 +++++++++++
modules/core/overrides/GObject.js | 5 ++++-
2 files changed, 15 insertions(+), 1 deletion(-)
---
diff --git a/installed-tests/js/testGObjectClass.js b/installed-tests/js/testGObjectClass.js
index dd0f2dca..4cf3a867 100644
--- a/installed-tests/js/testGObjectClass.js
+++ b/installed-tests/js/testGObjectClass.js
@@ -153,6 +153,8 @@ const MyCustomInit = GObject.registerClass(class MyCustomInit extends GObject.Ob
}
});
+const NoName = GObject.registerClass(class extends GObject.Object {});
+
describe('GObject class with decorator', function () {
let myInstance;
beforeEach(function () {
@@ -337,6 +339,15 @@ describe('GObject class with decorator', function () {
expect(obj instanceof MyObject).toBeTruthy();
});
+ it('handles anonymous class expressions', function () {
+ const obj = new NoName();
+ expect(obj instanceof NoName).toBeTruthy();
+
+ const NoName2 = GObject.registerClass(class extends GObject.Object {});
+ const obj2 = new NoName2();
+ expect(obj2 instanceof NoName2).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/core/overrides/GObject.js b/modules/core/overrides/GObject.js
index 640a4fc6..8058a2ec 100644
--- a/modules/core/overrides/GObject.js
+++ b/modules/core/overrides/GObject.js
@@ -4,7 +4,7 @@
// SPDX-FileCopyrightText: 2017 Philip Chimento <philip chimento gmail com>, <philip endlessm com>
const Gi = imports._gi;
-const GjsPrivate = imports.gi.GjsPrivate;
+const {GjsPrivate, GLib} = imports.gi;
const {_checkAccessors} = imports._common;
const Legacy = imports._legacy;
@@ -151,6 +151,9 @@ function _createGTypeName(klass) {
gtypeClassName = `${callerBasename}_${gtypeClassName}`;
}
+ if (gtypeClassName === '')
+ gtypeClassName = `anonymous_${GLib.uuid_string_random()}`;
+
return sanitizeGType(`Gjs_${gtypeClassName}`);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]