[gjs: 2/4] interface: Return gracefully when comparing an interface with a primitive




commit 20e994736c486539e158ab83fe3817cdfe4361c9
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Thu Feb 17 00:59:45 2022 +0100

    interface: Return gracefully when comparing an interface with a primitive
    
    The interface implementation for `instanceof` was assuming that we were
    always compare an interface with an object, but this may not be the case
    (like in case of comparing a null value).
    
    So, do not assert in this case, but just return gracefully false.
    
    Fixes: #464

 gi/interface.cpp                           |  7 ++++++-
 installed-tests/js/testGObjectInterface.js | 10 ++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
---
diff --git a/gi/interface.cpp b/gi/interface.cpp
index 4b1ed5fad..7ee729888 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -153,7 +153,12 @@ bool InterfacePrototype::has_instance_impl(JSContext* cx,
                                            const JS::CallArgs& args) {
     // This method is never called directly, so no need for error messages.
     g_assert(args.length() == 1);
-    g_assert(args[0].isObject());
+
+    if (!args[0].isObject()) {
+        args.rval().setBoolean(false);
+        return true;
+    }
+
     JS::RootedObject instance(cx, &args[0].toObject());
     bool isinstance = ObjectBase::typecheck(cx, instance, nullptr, m_gtype,
                                             GjsTypecheckNoThrow());
diff --git a/installed-tests/js/testGObjectInterface.js b/installed-tests/js/testGObjectInterface.js
index ef2d4677d..72ee657ac 100644
--- a/installed-tests/js/testGObjectInterface.js
+++ b/installed-tests/js/testGObjectInterface.js
@@ -326,6 +326,16 @@ describe('GObject interface', function () {
         expect(new Date()).not.toBeInstanceOf(AGObjectInterface);
     });
 
+    it('has instance definition for non-object type for native interface', function () {
+        expect(null).not.toBeInstanceOf(Gio.File);
+        expect(true).not.toBeInstanceOf(Gio.File);
+        expect(undefined).not.toBeInstanceOf(Gio.File);
+        expect(12345).not.toBeInstanceOf(Gio.File);
+        expect(54321n).not.toBeInstanceOf(Gio.File);
+        expect('no way!').not.toBeInstanceOf(Gio.File);
+        expect(new Date()).not.toBeInstanceOf(Gio.File);
+    });
+
     describe('prototype', function () {
         let file, originalDup;
 


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