[gjs/gnome-40: 4/30] object: Pass the instance pointer to toggle notify




commit 841b0a6bb1259e9a88685395a6948efc56b6af43
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Wed Mar 24 03:26:11 2021 +0100

    object: Pass the instance pointer to toggle notify
    
    There's no need to lookup the object pointer again during toggle notify
    callback as we can just use the current instance, as in case the
    underlying gobject is destroyed earlier we'd still remove the toggle
    notification on dispose notify.
    
    (cherry-picked from commit 9b0232c0)

 gi/object.cpp | 20 +++++++++-----------
 gi/object.h   |  2 ++
 2 files changed, 11 insertions(+), 11 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 39ce7f5f..75b29be3 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1084,17 +1084,14 @@ static void wrapped_gobj_dispose_notify(
                         where_the_object_was);
 }
 
-static void wrapped_gobj_toggle_notify(void*, GObject* gobj,
-                                       gboolean is_last_ref);
-
 void
 ObjectInstance::gobj_dispose_notify(void)
 {
     m_gobj_disposed = true;
 
     if (m_uses_toggle_ref) {
-        g_object_remove_toggle_ref(m_ptr, wrapped_gobj_toggle_notify, nullptr);
-        wrapped_gobj_toggle_notify(nullptr, m_ptr, TRUE);
+        g_object_remove_toggle_ref(m_ptr, wrapped_gobj_toggle_notify, this);
+        wrapped_gobj_toggle_notify(this, m_ptr, TRUE);
     }
 }
 
@@ -1222,10 +1219,11 @@ toggle_handler(GObject               *gobj,
     }
 }
 
-static void wrapped_gobj_toggle_notify(void*, GObject* gobj,
-                                       gboolean is_last_ref) {
+void ObjectInstance::wrapped_gobj_toggle_notify(void* instance, GObject* gobj,
+                                                gboolean is_last_ref) {
     bool is_main_thread;
     bool toggle_up_queued, toggle_down_queued;
+    auto* self = static_cast<ObjectInstance*>(instance);
 
     GjsContextPrivate* gjs = GjsContextPrivate::from_current_context();
     if (gjs->destroying()) {
@@ -1281,7 +1279,7 @@ static void wrapped_gobj_toggle_notify(void*, GObject* gobj,
                         toggle_up_queued? "up" : "down");
             }
 
-            ObjectInstance::for_gobject(gobj)->toggle_down();
+            self->toggle_down();
         } else {
             toggle_queue.enqueue(gobj, ToggleQueue::DOWN, toggle_handler);
         }
@@ -1296,7 +1294,7 @@ static void wrapped_gobj_toggle_notify(void*, GObject* gobj,
                 g_error("toggling up object %s that's already queued to toggle up\n",
                         G_OBJECT_TYPE_NAME(gobj));
             }
-            ObjectInstance::for_gobject(gobj)->toggle_up();
+            self->toggle_up();
         } else {
             toggle_queue.enqueue(gobj, ToggleQueue::UP, toggle_handler);
         }
@@ -1311,7 +1309,7 @@ ObjectInstance::release_native_object(void)
         m_ptr.release();
     else if (m_uses_toggle_ref)
         g_object_remove_toggle_ref(m_ptr.release(), wrapped_gobj_toggle_notify,
-                                   nullptr);
+                                   this);
     else
         m_ptr = nullptr;
 }
@@ -1459,7 +1457,7 @@ ObjectInstance::ensure_uses_toggle_ref(JSContext *cx)
      */
     m_uses_toggle_ref = true;
     switch_to_rooted(cx);
-    g_object_add_toggle_ref(m_ptr, wrapped_gobj_toggle_notify, nullptr);
+    g_object_add_toggle_ref(m_ptr, wrapped_gobj_toggle_notify, this);
 
     /* We now have both a ref and a toggle ref, we only want the toggle ref.
      * This may immediately remove the GC root we just added, since refcount
diff --git a/gi/object.h b/gi/object.h
index d3f1ebf5..9e10b676 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -474,6 +474,8 @@ class ObjectInstance : public GIWrapperInstance<ObjectBase, ObjectPrototype,
     void gobj_dispose_notify(void);
     static void context_dispose_notify(void* data,
                                        GObject* where_the_object_was);
+    static void wrapped_gobj_toggle_notify(void* instance, GObject* gobj,
+                                           gboolean is_last_ref);
 };
 
 GJS_JSAPI_RETURN_CONVENTION


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