[gjs: 2/3] private: Allow passing type flags to register_type
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 2/3] private: Allow passing type flags to register_type
- Date: Sat, 27 Apr 2019 03:56:28 +0000 (UTC)
commit 022b6681f7e8b035581d74d737f083baa40c5f9a
Author: Florian Müllner <fmuellner gnome org>
Date: Fri Apr 26 21:12:52 2019 +0000
private: Allow passing type flags to register_type
Unlike Lang.Class, GObject.registerClass currently lacks a way for
defining abstract classes. Make this possible by allowing for an
optional GTypeFlags value.
https://gitlab.gnome.org/GNOME/gjs/merge_requests/290
gi/private.cpp | 8 +++++---
installed-tests/js/testGObjectClass.js | 9 +++++++++
modules/_legacy.js | 3 ++-
modules/overrides/GObject.js | 7 ++++++-
4 files changed, 22 insertions(+), 5 deletions(-)
---
diff --git a/gi/private.cpp b/gi/private.cpp
index 4cc29f74..e53e2871 100644
--- a/gi/private.cpp
+++ b/gi/private.cpp
@@ -252,9 +252,11 @@ static bool gjs_register_type(JSContext* cx, unsigned argc, JS::Value* vp) {
JSAutoRequest ar(cx);
JS::UniqueChars name;
+ GTypeFlags type_flags;
JS::RootedObject parent(cx), interfaces(cx), properties(cx);
- if (!gjs_parse_call_args(cx, "register_type", argv, "osoo", "parent",
- &parent, "name", &name, "interfaces", &interfaces,
+ if (!gjs_parse_call_args(cx, "register_type", argv, "osioo", "parent",
+ &parent, "name", &name, "flags", &type_flags,
+ "interfaces", &interfaces,
"properties", &properties))
return false;
@@ -296,7 +298,7 @@ static bool gjs_register_type(JSContext* cx, unsigned argc, JS::Value* vp) {
type_info.instance_size = query.instance_size;
GType instance_type = g_type_register_static(
- parent_priv->gtype(), name.get(), &type_info, GTypeFlags(0));
+ parent_priv->gtype(), name.get(), &type_info, type_flags);
g_type_set_qdata(instance_type, ObjectBase::custom_type_quark(),
GINT_TO_POINTER(1));
diff --git a/installed-tests/js/testGObjectClass.js b/installed-tests/js/testGObjectClass.js
index 680c96b1..1bd75285 100644
--- a/installed-tests/js/testGObjectClass.js
+++ b/installed-tests/js/testGObjectClass.js
@@ -114,6 +114,11 @@ const MyObject = GObject.registerClass({
}
});
+const MyAbstractObject = GObject.registerClass({
+ GTypeFlags: GObject.TypeFlags.ABSTRACT
+}, class MyAbstractObject extends GObject.Object {
+});
+
const MyApplication = GObject.registerClass({
Signals: { 'custom': { param_types: [ GObject.TYPE_INT ] } },
}, class MyApplication extends Gio.Application {
@@ -158,6 +163,10 @@ describe('GObject class with decorator', function () {
expect (() => GObject.registerClass(class Bar extends Foo {})).toThrow();
});
+ it('throws an error when used with an abstract class', function() {
+ expect (() => new MyAbstractObject()).toThrow();
+ });
+
it('constructs with default values for properties', function () {
expect(myInstance.readwrite).toEqual('foo');
expect(myInstance.readonly).toEqual('bar');
diff --git a/modules/_legacy.js b/modules/_legacy.js
index 8d50c352..8c4b22c3 100644
--- a/modules/_legacy.js
+++ b/modules/_legacy.js
@@ -513,6 +513,7 @@ function defineGObjectLegacyObjects(GObject) {
let name = params.Name;
let gtypename = _createGTypeName(params);
+ let gflags = params.Abstract ? GObject.TypeFlags.ABSTRACT : 0;
if (!params.Extends)
params.Extends = GObject.Object;
@@ -530,7 +531,7 @@ function defineGObjectLegacyObjects(GObject) {
delete params.Properties;
let newClass = Gi.register_type(parent.prototype, gtypename,
- gobjectInterfaces, propertiesArray);
+ gflags, gobjectInterfaces, propertiesArray);
// See Class.prototype._construct for the reasoning
// behind this direct prototype set.
diff --git a/modules/overrides/GObject.js b/modules/overrides/GObject.js
index 8181b815..c6ea6ad9 100644
--- a/modules/overrides/GObject.js
+++ b/modules/overrides/GObject.js
@@ -27,6 +27,7 @@ const Legacy = imports._legacy;
let GObject;
var GTypeName = Symbol('GType name');
+var GTypeFlags = Symbol('GType flags');
var interfaces = Symbol('GObject interfaces');
var properties = Symbol('GObject properties');
var requires = Symbol('GObject interface requires');
@@ -58,6 +59,8 @@ function registerClass(klass) {
klass = arguments[1];
if ('GTypeName' in metaInfo)
klass[GTypeName] = metaInfo.GTypeName;
+ if ('GTypeFlags' in metaInfo)
+ klass[GTypeFlags] = metaInfo.GTypeFlags;
if ('Implements' in metaInfo)
klass[interfaces] = metaInfo.Implements;
if ('Properties' in metaInfo)
@@ -334,6 +337,8 @@ function _init() {
GObject.Object._classInit = function(klass) {
let gtypename = _createGTypeName(klass);
+ let gflags = klass.hasOwnProperty(GTypeFlags) ?
+ klass[GTypeFlags] : 0;
let gobjectInterfaces = klass.hasOwnProperty(interfaces) ?
klass[interfaces] : [];
let propertiesArray = _propertiesAsArray(klass);
@@ -341,7 +346,7 @@ function _init() {
let gobjectSignals = klass.hasOwnProperty(signals) ?
klass[signals] : [];
- let newClass = Gi.register_type(parent.prototype, gtypename,
+ let newClass = Gi.register_type(parent.prototype, gtypename, gflags,
gobjectInterfaces, propertiesArray);
Object.setPrototypeOf(newClass, parent);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]