[gjs/wip/ptomato/mozjs31: 21/29] object: Use PersistentRooted in object_init_list



commit ce6135413e388a90f61ce790b57b40d8e386e973
Author: Philip Chimento <philip endlessm com>
Date:   Wed Nov 2 18:00:47 2016 -0700

    object: Use PersistentRooted in object_init_list
    
    Previously using JS_AddObjectRoot and JS_RemoveObjectRoot to keep objects
    alive from the time that the JS Object is created until the GObject
    instance_init function runs. Now we have JS::PersistentRootedObject which
    accomplishes the same thing but with a cleaner API.
    
    (The API of JS_AddObjectRoot and friends changes in mozjs31 to take only
    JS::Heap-wrapped objects, so it had to change one way or the other.)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=751252

 gi/object.cpp |   18 +++++++-----------
 1 files changed, 7 insertions(+), 11 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 5f02bdb..9da5754 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -24,6 +24,7 @@
 #include <config.h>
 
 #include <memory>
+#include <stack>
 #include <string.h>
 #include <vector>
 
@@ -89,7 +90,7 @@ enum {
     PROP_JS_HANDLED,
 };
 
-static GSList *object_init_list;
+static std::stack<JS::PersistentRootedObject> object_init_list;
 static GHashTable *class_init_properties;
 
 extern struct JSClass gjs_object_instance_class;
@@ -1245,10 +1246,7 @@ object_instance_init (JSContext                  *context,
        down.
     */
     if (g_type_get_qdata(gtype, gjs_is_custom_type_quark())) {
-        // COMPAT: Replace with JS::PersistentRootedObject in mozjs31
-        object_init_list = g_slist_prepend(object_init_list, object.get());
-        JS_AddNamedObjectRoot(context, (JSObject **) &object_init_list->data,
-                              "object_init_list");
+        object_init_list.emplace(context, object);
     }
 
     gobj = (GObject*) g_object_newv(gtype, params.size(), &params[0]);
@@ -2389,7 +2387,7 @@ gjs_object_constructor (GType                  type,
 {
     GObject *gobj = NULL;
 
-    if (object_init_list) {
+    if (!object_init_list.empty()) {
         GType parent_type = g_type_parent(type);
 
         /* The object is being constructed from JS:
@@ -2580,13 +2578,13 @@ gjs_object_custom_init(GTypeInstance *instance,
     JSContext *context;
     ObjectInstance *priv;
 
-    if (!object_init_list)
+    if (object_init_list.empty())
       return;
 
     gjs_context = gjs_context_get_current();
     context = (JSContext*) gjs_context_get_native_context(gjs_context);
 
-    JS::RootedObject object(context, (JSObject*) object_init_list->data);
+    JS::RootedObject object(context, object_init_list.top().get());
     priv = (ObjectInstance*) JS_GetPrivate(object);
 
     if (priv->gtype != G_TYPE_FROM_INSTANCE (instance)) {
@@ -2596,9 +2594,7 @@ gjs_object_custom_init(GTypeInstance *instance,
         return;
     }
 
-    JS_RemoveObjectRoot(context, (JSObject **) &object_init_list->data);
-    object_init_list = g_slist_delete_link(object_init_list,
-                                           object_init_list);
+    object_init_list.pop();
 
     associate_js_gobject(context, object, G_OBJECT (instance));
 


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