[gjs/wip/ptomato/mozjs31prep] js: Root gjs_define_function()



commit 9abc94a00f711ada541eb01258adea64c0396409
Author: Philip Chimento <philip endlessm com>
Date:   Thu Oct 27 19:07:02 2016 -0700

    js: Root gjs_define_function()
    
    Starting from gjs_define_function() in function.cpp and working outwards,
    we cause another cascade of GC rooting changes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/boxed.cpp       |   16 +++++++---------
 gi/enumeration.cpp |    6 +++---
 gi/enumeration.h   |    6 +++---
 gi/function.cpp    |   22 ++++++++++------------
 gi/function.h      |    8 ++++----
 gi/interface.cpp   |    2 +-
 gi/object.cpp      |    8 ++++----
 gi/object.h        |    8 ++++----
 gi/union.cpp       |    8 +++-----
 9 files changed, 39 insertions(+), 45 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 642c86a..60b3337 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -70,10 +70,10 @@ extern struct JSClass gjs_boxed_class;
 GJS_DEFINE_PRIV_FROM_JS(Boxed, gjs_boxed_class)
 
 static bool
-gjs_define_static_methods(JSContext    *context,
-                          JSObject     *constructor,
-                          GType         gtype,
-                          GIStructInfo *boxed_info)
+gjs_define_static_methods(JSContext       *context,
+                          JS::HandleObject constructor,
+                          GType            gtype,
+                          GIStructInfo    *boxed_info)
 {
     int i;
     int n_methods;
@@ -146,7 +146,6 @@ boxed_new_resolve(JSContext *context,
                                                 name);
 
         if (method_info != NULL) {
-            JSObject *boxed_proto;
             const char *method_name;
 
 #if GJS_VERBOSE_ENABLE_GI_USAGE
@@ -161,15 +160,14 @@ boxed_new_resolve(JSContext *context,
                           g_base_info_get_namespace( (GIBaseInfo*) priv->info),
                           g_base_info_get_name( (GIBaseInfo*) priv->info));
 
-                boxed_proto = obj;
-
-                if (gjs_define_function(context, boxed_proto, priv->gtype,
+                /* obj is the Boxed prototype */
+                if (gjs_define_function(context, obj, priv->gtype,
                                         (GICallableInfo *)method_info) == NULL) {
                     g_base_info_unref( (GIBaseInfo*) method_info);
                     goto out;
                 }
 
-                objp.set(boxed_proto); /* we defined the prop in object_proto */
+                objp.set(obj); /* we defined the prop in object_proto */
             }
 
             g_base_info_unref( (GIBaseInfo*) method_info);
diff --git a/gi/enumeration.cpp b/gi/enumeration.cpp
index 6e48f86..1b2361c 100644
--- a/gi/enumeration.cpp
+++ b/gi/enumeration.cpp
@@ -115,9 +115,9 @@ gjs_define_enum_values(JSContext       *context,
 }
 
 bool
-gjs_define_enum_static_methods(JSContext    *context,
-                               JSObject     *constructor,
-                               GIEnumInfo   *enum_info)
+gjs_define_enum_static_methods(JSContext       *context,
+                               JS::HandleObject constructor,
+                               GIEnumInfo      *enum_info)
 {
     int i, n_methods;
 
diff --git a/gi/enumeration.h b/gi/enumeration.h
index 542772c..a573fad 100644
--- a/gi/enumeration.h
+++ b/gi/enumeration.h
@@ -37,9 +37,9 @@ bool gjs_define_enum_values(JSContext       *context,
                             JS::HandleObject in_object,
                             GIEnumInfo      *info);
 
-bool    gjs_define_enum_static_methods (JSContext    *context,
-                                        JSObject     *constructor,
-                                        GIEnumInfo   *enum_info);
+bool gjs_define_enum_static_methods(JSContext       *context,
+                                    JS::HandleObject constructor,
+                                    GIEnumInfo      *enum_info);
 
 bool gjs_define_enumeration(JSContext       *context,
                             JS::HandleObject in_object,
diff --git a/gi/function.cpp b/gi/function.cpp
index 3dff737..638026f 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1718,23 +1718,22 @@ function_new(JSContext      *context,
 }
 
 JSObject*
-gjs_define_function(JSContext      *context,
-                    JSObject       *in_object,
-                    GType           gtype,
-                    GICallableInfo *info)
+gjs_define_function(JSContext       *context,
+                    JS::HandleObject in_object,
+                    GType            gtype,
+                    GICallableInfo  *info)
 {
-    JSObject *function = NULL;
     GIInfoType info_type;
     gchar *name;
     bool free_name;
 
     info_type = g_base_info_get_type((GIBaseInfo *)info);
 
-    JS_BeginRequest(context);
+    JSAutoRequest ar(context);
 
-    function = function_new(context, gtype, info);
+    JS::RootedObject function(context, function_new(context, gtype, info));
     if (function == NULL)
-        goto out;
+        return NULL;
 
     if (info_type == GI_INFO_TYPE_FUNCTION) {
         name = (gchar *) g_base_info_get_name((GIBaseInfo*) info);
@@ -1746,8 +1745,9 @@ gjs_define_function(JSContext      *context,
         g_assert_not_reached ();
     }
 
-    if (!JS_DefineProperty(context, in_object, name,
-                           JS::ObjectValue(*function),
+    /* COMPAT: JS_DefineProperty is overloaded to take JS::HandleObject in 31 */
+    JS::RootedValue v_function(context, JS::ObjectValue(*function));
+    if (!JS_DefineProperty(context, in_object, name, v_function,
                            NULL, NULL,
                            GJS_MODULE_PROP_FLAGS)) {
         gjs_debug(GJS_DEBUG_GFUNCTION, "Failed to define function");
@@ -1757,8 +1757,6 @@ gjs_define_function(JSContext      *context,
     if (free_name)
         g_free(name);
 
- out:
-    JS_EndRequest(context);
     return function;
 }
 
diff --git a/gi/function.h b/gi/function.h
index 507e553..f11a262 100644
--- a/gi/function.h
+++ b/gi/function.h
@@ -62,10 +62,10 @@ GjsCallbackTrampoline* gjs_callback_trampoline_new(JSContext      *context,
 void gjs_callback_trampoline_unref(GjsCallbackTrampoline *trampoline);
 void gjs_callback_trampoline_ref(GjsCallbackTrampoline *trampoline);
 
-JSObject* gjs_define_function   (JSContext      *context,
-                                 JSObject       *in_object,
-                                 GType           gtype,
-                                 GICallableInfo *info);
+JSObject *gjs_define_function(JSContext       *context,
+                              JS::HandleObject in_object,
+                              GType            gtype,
+                              GICallableInfo  *info);
 
 bool      gjs_invoke_c_function_uncached(JSContext       *context,
                                          GIFunctionInfo  *info,
diff --git a/gi/interface.cpp b/gi/interface.cpp
index d4bd9bc..699a7fc 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -71,7 +71,7 @@ interface_finalize(JSFreeOp *fop,
 
 static bool
 gjs_define_static_methods(JSContext       *context,
-                          JSObject        *constructor,
+                          JS::HandleObject constructor,
                           GType            gtype,
                           GIInterfaceInfo *info)
 {
diff --git a/gi/object.cpp b/gi/object.cpp
index 83f91c9..188afce 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1814,10 +1814,10 @@ JSFunctionSpec gjs_object_instance_proto_funcs[] = {
 };
 
 bool
-gjs_object_define_static_methods(JSContext    *context,
-                                 JSObject     *constructor,
-                                 GType         gtype,
-                                 GIObjectInfo *object_info)
+gjs_object_define_static_methods(JSContext       *context,
+                                 JS::HandleObject constructor,
+                                 GType            gtype,
+                                 GIObjectInfo    *object_info)
 {
     GIStructInfo *gtype_struct;
     int i;
diff --git a/gi/object.h b/gi/object.h
index cc089dc..2e7e538 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -58,10 +58,10 @@ bool      gjs_typecheck_is_object(JSContext       *context,
 
 void      gjs_object_prepare_shutdown   (JSContext     *context);
 
-bool      gjs_object_define_static_methods(JSContext    *context,
-                                           JSObject     *constructor,
-                                           GType         gtype,
-                                           GIObjectInfo *object_info);
+bool gjs_object_define_static_methods(JSContext       *context,
+                                      JS::HandleObject constructor,
+                                      GType            gtype,
+                                      GIObjectInfo    *object_info);
 
 G_END_DECLS
 
diff --git a/gi/union.cpp b/gi/union.cpp
index c422124..a0016b4 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -93,7 +93,6 @@ union_new_resolve(JSContext *context,
                                                name);
 
         if (method_info != NULL) {
-            JSObject *union_proto;
             const char *method_name;
 
 #if GJS_VERBOSE_ENABLE_GI_USAGE
@@ -108,9 +107,8 @@ union_new_resolve(JSContext *context,
                           g_base_info_get_namespace( (GIBaseInfo*) priv->info),
                           g_base_info_get_name( (GIBaseInfo*) priv->info));
 
-                union_proto = obj;
-
-                if (gjs_define_function(context, union_proto,
+                /* obj is union proto */
+                if (gjs_define_function(context, obj,
                                         g_registered_type_info_get_g_type(priv->info),
                                         method_info) == NULL) {
                     g_base_info_unref( (GIBaseInfo*) method_info);
@@ -118,7 +116,7 @@ union_new_resolve(JSContext *context,
                     goto out;
                 }
 
-                objp.set(union_proto); /* we defined the prop in object_proto */
+                objp.set(obj); /* we defined the prop in object_proto */
             }
 
             g_base_info_unref( (GIBaseInfo*) method_info);


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