[gjs: 1/4] GObject: Support checking if a primitive is instance of an Interface




commit 7722a226211c567f80b80c5b709399790d96872f
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Thu Feb 17 00:54:42 2022 +0100

    GObject: Support checking if a primitive is instance of an Interface
    
    When using instanceof against an interface we assume that the instance
    we pass is an object, but in case that's not the case, we're failing
    while calling GObject.type_is_a instead of just returning false.
    
    So, only perform such test in case we're handling an Object.
    
    Add tests checking this case.
    
    Fixes: #464

 installed-tests/js/testGObjectInterface.js | 23 +++++++++++++++++++++++
 modules/core/overrides/GObject.js          |  3 ++-
 2 files changed, 25 insertions(+), 1 deletion(-)
---
diff --git a/installed-tests/js/testGObjectInterface.js b/installed-tests/js/testGObjectInterface.js
index 46bf4f5c5..ef2d4677d 100644
--- a/installed-tests/js/testGObjectInterface.js
+++ b/installed-tests/js/testGObjectInterface.js
@@ -303,6 +303,29 @@ describe('GObject interface', function () {
             /\[object instance wrapper GType:Gjs_GObjectImplementingGObjectInterface jsobj@0x[a-f0-9]+ 
native@0x[a-f0-9]+\]/);
     });
 
+    it('has instance definition', function () {
+        const obj = new GObjectImplementingGObjectInterface();
+        const obj2 = new ImplementationOfTwoInterfaces();
+        const file = Gio.File.new_for_path('/');
+        expect(obj).toBeInstanceOf(AGObjectInterface);
+        expect(obj).not.toBeInstanceOf(InterfaceRequiringGObjectInterface);
+        expect(obj2).toBeInstanceOf(AGObjectInterface);
+        expect(obj2).toBeInstanceOf(InterfaceRequiringGObjectInterface);
+        expect(new GObject.Object()).not.toBeInstanceOf(AGObjectInterface);
+        expect(file).toBeInstanceOf(Gio.File);
+        expect(file).toBeInstanceOf(GObject.Object);
+    });
+
+    it('has instance definition for non-object type', function () {
+        expect(null).not.toBeInstanceOf(AGObjectInterface);
+        expect(true).not.toBeInstanceOf(AGObjectInterface);
+        expect(undefined).not.toBeInstanceOf(AGObjectInterface);
+        expect(123456).not.toBeInstanceOf(AGObjectInterface);
+        expect(54321n).not.toBeInstanceOf(AGObjectInterface);
+        expect('no way!').not.toBeInstanceOf(AGObjectInterface);
+        expect(new Date()).not.toBeInstanceOf(AGObjectInterface);
+    });
+
     describe('prototype', function () {
         let file, originalDup;
 
diff --git a/modules/core/overrides/GObject.js b/modules/core/overrides/GObject.js
index 8ed662f1f..df6f9dc27 100644
--- a/modules/core/overrides/GObject.js
+++ b/modules/core/overrides/GObject.js
@@ -591,7 +591,8 @@ function _init() {
     });
 
     function interfaceInstanceOf(instance) {
-        if (GObject.Interface.prototype.isPrototypeOf(this.prototype))
+        if (instance && typeof instance === 'object' &&
+            GObject.Interface.prototype.isPrototypeOf(this.prototype))
             return GObject.type_is_a(instance, this);
 
         return false;


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