[gjs/wip/ptomato/mozjs31: 4/12] js: Root gjs_init_dynamic_class()



commit e5287453133a866c34f5165115d69ca9318fdc3e
Author: Philip Chimento <philip endlessm com>
Date:   Wed Oct 26 19:40:14 2016 -0700

    js: Root gjs_init_dynamic_class()
    
    Starting in jsapi-dynamic-class.cpp and working backwards, we cause
    another cascade of GC rooting changes.

 gi/boxed.cpp                |   14 ++++++------
 gi/boxed.h                  |    2 +-
 gi/fundamental.cpp          |   19 ++++++----------
 gi/fundamental.h            |    4 +-
 gi/gerror.cpp               |   15 +++++++------
 gi/gerror.h                 |    2 +-
 gi/interface.cpp            |    6 ++--
 gi/interface.h              |    2 +-
 gi/object.cpp               |   50 +++++++++++++++++++------------------------
 gi/object.h                 |    2 +-
 gi/param.cpp                |   12 +++++-----
 gi/param.h                  |    4 +-
 gi/repo.cpp                 |   14 +++++++-----
 gi/repo.h                   |   10 +++++---
 gi/union.cpp                |   14 ++++++------
 gi/union.h                  |    6 ++--
 gjs/jsapi-dynamic-class.cpp |   29 ++++++++++--------------
 gjs/jsapi-util.h            |   14 ++++++------
 18 files changed, 104 insertions(+), 115 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 7fd594b..3e035e1 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -1121,13 +1121,12 @@ boxed_fill_prototype_info(JSContext *context,
 }
 
 void
-gjs_define_boxed_class(JSContext    *context,
-                       JSObject     *in_object,
-                       GIBoxedInfo  *info)
+gjs_define_boxed_class(JSContext       *context,
+                       JS::HandleObject in_object,
+                       GIBoxedInfo     *info)
 {
     const char *constructor_name;
-    JSObject *prototype;
-    JS::RootedObject constructor(context);
+    JS::RootedObject prototype(context), constructor(context);
     JS::Value value;
     Boxed *priv;
 
@@ -1139,7 +1138,7 @@ gjs_define_boxed_class(JSContext    *context,
     constructor_name = g_base_info_get_name( (GIBaseInfo*) info);
 
     if (!gjs_init_class_dynamic(context, in_object,
-                                NULL, /* parent prototype */
+                                JS::NullPtr(), /* parent prototype */
                                 g_base_info_get_namespace( (GIBaseInfo*) info),
                                 constructor_name,
                                 &gjs_boxed_class,
@@ -1168,7 +1167,8 @@ gjs_define_boxed_class(JSContext    *context,
     JS_SetPrivate(prototype, priv);
 
     gjs_debug(GJS_DEBUG_GBOXED, "Defined class %s prototype is %p class %p in object %p",
-              constructor_name, prototype, JS_GetClass(prototype), in_object);
+              constructor_name, prototype.get(), JS_GetClass(prototype),
+              in_object.get());
 
     priv->can_allocate_directly = struct_is_simple (priv->info);
 
diff --git a/gi/boxed.h b/gi/boxed.h
index 85b7791..3e8e0e2 100644
--- a/gi/boxed.h
+++ b/gi/boxed.h
@@ -42,7 +42,7 @@ typedef enum {
 typedef GIStructInfo GIBoxedInfo;
 
 void      gjs_define_boxed_class       (JSContext             *context,
-                                        JSObject              *in_object,
+                                        JS::HandleObject       in_object,
                                         GIBoxedInfo           *info);
 
 void*     gjs_c_struct_from_boxed      (JSContext             *context,
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 34a881a..f2e6a99 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -579,7 +579,7 @@ gjs_lookup_fundamental_prototype(JSContext    *context,
                                  GIObjectInfo *info,
                                  GType         gtype)
 {
-    JSObject *in_object;
+    JS::RootedObject in_object(context);
     const char *constructor_name;
 
     if (info) {
@@ -640,16 +640,15 @@ gjs_lookup_fundamental_prototype_from_gtype(JSContext *context,
 
 bool
 gjs_define_fundamental_class(JSContext              *context,
-                             JSObject               *in_object,
+                             JS::HandleObject        in_object,
                              GIObjectInfo           *info,
                              JS::MutableHandleObject constructor,
-                             JSObject              **prototype_p)
+                             JS::MutableHandleObject prototype)
 {
     const char *constructor_name;
-    JSObject *prototype;
     JS::Value value;
     jsid js_constructor_name = JSID_VOID;
-    JSObject *parent_proto;
+    JS::RootedObject parent_proto(context);
     Fundamental *priv;
     GType parent_gtype;
     GType gtype;
@@ -665,7 +664,6 @@ gjs_define_fundamental_class(JSContext              *context,
 
     gtype = g_registered_type_info_get_g_type (info);
     parent_gtype = g_type_parent(gtype);
-    parent_proto = NULL;
     if (parent_gtype != G_TYPE_INVALID)
         parent_proto = gjs_lookup_fundamental_prototype_from_gtype(context,
                                                                    parent_gtype);
@@ -690,7 +688,7 @@ gjs_define_fundamental_class(JSContext              *context,
                                 NULL,
                                 /* funcs of constructor, MyConstructor.myfunc() */
                                 NULL,
-                                &prototype,
+                                prototype,
                                 constructor)) {
         gjs_log_exception(context);
         g_error("Can't init class %s", constructor_name);
@@ -716,8 +714,8 @@ gjs_define_fundamental_class(JSContext              *context,
 
     gjs_debug(GJS_DEBUG_GFUNDAMENTAL,
               "Defined class %s prototype is %p class %p in object %p constructor %s.%s.%s",
-              constructor_name, prototype, JS_GetClass(prototype),
-              in_object,
+              constructor_name, prototype.get(), JS_GetClass(prototype),
+              in_object.get(),
               constructor_info != NULL ? g_base_info_get_namespace(constructor_info) : "unknown",
               constructor_info != NULL ? g_base_info_get_name(g_base_info_get_container(constructor_info)) : 
"unknown",
               constructor_info != NULL ? g_base_info_get_name(constructor_info) : "unknown");
@@ -736,9 +734,6 @@ gjs_define_fundamental_class(JSContext              *context,
     JS_DefineProperty(context, constructor, "$gtype", value,
                       NULL, NULL, JSPROP_PERMANENT);
 
-    if (prototype_p)
-        *prototype_p = prototype;
-
     return true;
 }
 
diff --git a/gi/fundamental.h b/gi/fundamental.h
index b631005..3f3dc6d 100644
--- a/gi/fundamental.h
+++ b/gi/fundamental.h
@@ -33,10 +33,10 @@
 G_BEGIN_DECLS
 
 bool gjs_define_fundamental_class(JSContext              *context,
-                                  JSObject               *in_object,
+                                  JS::HandleObject        in_object,
                                   GIObjectInfo           *info,
                                   JS::MutableHandleObject constructor,
-                                  JSObject              **prototype_p);
+                                  JS::MutableHandleObject prototype);
 
 JSObject* gjs_object_from_g_fundamental      (JSContext     *context,
                                               GIObjectInfo  *info,
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 4c38953..6e1d214 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -309,14 +309,13 @@ static JSFunctionSpec gjs_error_constructor_funcs[] = {
 };
 
 void
-gjs_define_error_class(JSContext    *context,
-                       JSObject     *in_object,
-                       GIEnumInfo   *info)
+gjs_define_error_class(JSContext       *context,
+                       JS::HandleObject in_object,
+                       GIEnumInfo      *info)
 {
     const char *constructor_name;
     GIBoxedInfo *glib_error_info;
-    JSObject *prototype, *parent_proto;
-    JS::RootedObject constructor(context);
+    JS::RootedObject prototype(context), constructor(context);
     Error *priv;
 
     /* See the comment in gjs_define_boxed_class() for an
@@ -328,7 +327,8 @@ gjs_define_error_class(JSContext    *context,
 
     g_irepository_require(NULL, "GLib", "2.0", (GIRepositoryLoadFlags) 0, NULL);
     glib_error_info = (GIBoxedInfo*) g_irepository_find_by_name(NULL, "GLib", "Error");
-    parent_proto = gjs_lookup_generic_prototype(context, glib_error_info);
+    JS::RootedObject parent_proto(context,
+        gjs_lookup_generic_prototype(context, glib_error_info));
     g_base_info_unref((GIBaseInfo*)glib_error_info);
 
     if (!gjs_init_class_dynamic(context, in_object,
@@ -360,7 +360,8 @@ gjs_define_error_class(JSContext    *context,
     JS_SetPrivate(prototype, priv);
 
     gjs_debug(GJS_DEBUG_GBOXED, "Defined class %s prototype is %p class %p in object %p",
-              constructor_name, prototype, JS_GetClass(prototype), in_object);
+              constructor_name, prototype.get(), JS_GetClass(prototype),
+              in_object.get());
 
     gjs_define_enum_values(context, constructor, priv->info);
     gjs_define_enum_static_methods(context, constructor, priv->info);
diff --git a/gi/gerror.h b/gi/gerror.h
index 46e2862..e114c30 100644
--- a/gi/gerror.h
+++ b/gi/gerror.h
@@ -33,7 +33,7 @@
 G_BEGIN_DECLS
 
 void      gjs_define_error_class       (JSContext             *context,
-                                        JSObject              *in_object,
+                                        JS::HandleObject       in_object,
                                         GIEnumInfo            *info);
 GError*   gjs_gerror_from_error        (JSContext             *context,
                                         JS::HandleObject       obj);
diff --git a/gi/interface.cpp b/gi/interface.cpp
index 9377e89..d4bd9bc 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -182,7 +182,7 @@ JSFunctionSpec gjs_interface_proto_funcs[] = {
 
 bool
 gjs_define_interface_class(JSContext              *context,
-                           JSObject               *in_object,
+                           JS::HandleObject        in_object,
                            GIInterfaceInfo        *info,
                            GType                   gtype,
                            JS::MutableHandleObject constructor)
@@ -190,14 +190,14 @@ gjs_define_interface_class(JSContext              *context,
     Interface *priv;
     const char *constructor_name;
     const char *ns;
-    JSObject *prototype;
+    JS::RootedObject prototype(context);
     JS::Value value;
 
     ns = gjs_get_names_from_gtype_and_gi_info(gtype, (GIBaseInfo *) info,
                                               &constructor_name);
 
     if (!gjs_init_class_dynamic(context, in_object,
-                                NULL,
+                                JS::NullPtr(),
                                 ns,
                                 constructor_name,
                                 &gjs_interface_class,
diff --git a/gi/interface.h b/gi/interface.h
index ba5a6f1..d28ec89 100644
--- a/gi/interface.h
+++ b/gi/interface.h
@@ -33,7 +33,7 @@
 G_BEGIN_DECLS
 
 bool gjs_define_interface_class(JSContext              *context,
-                                JSObject               *in_object,
+                                JS::HandleObject        in_object,
                                 GIInterfaceInfo        *info,
                                 GType                   gtype,
                                 JS::MutableHandleObject constructor);
diff --git a/gi/object.cpp b/gi/object.cpp
index 54412d5..83f91c9 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1454,7 +1454,7 @@ gjs_lookup_object_constructor_from_info(JSContext    *context,
                                         GIObjectInfo *info,
                                         GType         gtype)
 {
-    JSObject *in_object;
+    JS::RootedObject in_object(context);
     const char *constructor_name;
     JS::Value value;
 
@@ -1871,14 +1871,13 @@ gjs_object_define_static_methods(JSContext    *context,
 
 void
 gjs_define_object_class(JSContext              *context,
-                        JSObject               *in_object,
+                        JS::HandleObject        in_object,
                         GIObjectInfo           *info,
                         GType                   gtype,
                         JS::MutableHandleObject constructor)
 {
     const char *constructor_name;
-    JSObject *prototype;
-    JSObject *parent_proto;
+    JS::RootedObject prototype(context), parent_proto(context);
 
     JS::Value value;
     ObjectInstance *priv;
@@ -1928,7 +1927,6 @@ gjs_define_object_class(JSContext              *context,
      * JavaScript is SO AWESOME
      */
 
-    parent_proto = NULL;
     parent_type = g_type_parent(gtype);
     if (parent_type != G_TYPE_INVALID)
        parent_proto = gjs_lookup_object_prototype(context, parent_type);
@@ -1964,7 +1962,8 @@ gjs_define_object_class(JSContext              *context,
     JS_SetPrivate(prototype, priv);
 
     gjs_debug(GJS_DEBUG_GOBJECT, "Defined class %s prototype %p class %p in object %p",
-              constructor_name, prototype, JS_GetClass(prototype), in_object);
+              constructor_name, prototype.get(), JS_GetClass(prototype),
+              in_object.get());
 
     if (info)
         gjs_object_define_static_methods(context, constructor, gtype, info);
@@ -2717,8 +2716,6 @@ gjs_register_interface(JSContext *cx,
 {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
     char *name = NULL;
-    JS::RootedObject interfaces(cx), properties(cx), constructor(cx);
-    JSObject *module;
     guint32 i, n_interfaces, n_properties;
     GType *iface_types;
     GType interface_type;
@@ -2738,6 +2735,7 @@ gjs_register_interface(JSContext *cx,
         NULL, /* instance_init */
     };
 
+    JS::RootedObject interfaces(cx), properties(cx);
     if (!gjs_parse_call_args(cx, "register_interface", args, "soo",
                              "name", &name,
                              "interfaces", &interfaces,
@@ -2782,8 +2780,11 @@ gjs_register_interface(JSContext *cx,
         g_type_interface_add_prerequisite(interface_type, iface_types[i]);
 
     /* create a custom JSClass */
-    if ((module = gjs_lookup_private_namespace(cx)) == NULL)
+    JS::RootedObject module(cx, gjs_lookup_private_namespace(cx));
+    if (module == NULL)
         return false;  /* error will have been thrown already */
+
+    JS::RootedObject constructor(cx);
     gjs_define_interface_class(cx, module, NULL, interface_type, &constructor);
 
     args.rval().setObject(*constructor);
@@ -2797,8 +2798,6 @@ gjs_register_type(JSContext *cx,
 {
     JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
     gchar *name;
-    JS::RootedObject parent(cx), interfaces(cx), properties(cx), constructor(cx);
-    JSObject *module;
     GType instance_type, parent_type;
     GTypeQuery query;
     GTypeModule *type_module;
@@ -2819,37 +2818,37 @@ gjs_register_type(JSContext *cx,
     };
     guint32 i, n_interfaces, n_properties;
     GType *iface_types;
-    bool retval = false;
 
-    JS_BeginRequest(cx);
+    JSAutoRequest ar(cx);
 
+    JS::RootedObject parent(cx), interfaces(cx), properties(cx);
     if (!gjs_parse_call_args(cx, "register_type", argv, "osoo",
                              "parent", &parent,
                              "name", &name,
                              "interfaces", &interfaces,
                              "properties", &properties))
-        goto out;
+        return false;
 
     if (!parent)
-        goto out;
+        return false;
 
     if (!do_base_typecheck(cx, parent, true))
-        goto out;
+        return false;
 
     if (!validate_interfaces_and_properties_args(cx, interfaces, properties,
                                                  &n_interfaces, &n_properties))
-        goto out;
+        return false;
 
     iface_types = (GType*) g_alloca(sizeof(GType) * n_interfaces);
 
     /* We do interface addition in two passes so that any failure
        is caught early, before registering the GType (which we can't undo) */
     if (!get_interface_gtypes(cx, interfaces, n_interfaces, iface_types))
-        goto out;
+        return false;
 
     if (g_type_from_name(name) != G_TYPE_INVALID) {
         gjs_throw (cx, "Type name %s is already registered", name);
-        goto out;
+        return false;
     }
 
     parent_priv = priv_from_js(cx, parent);
@@ -2862,7 +2861,7 @@ gjs_register_type(JSContext *cx,
     g_type_query_dynamic_safe(parent_type, &query);
     if (G_UNLIKELY (query.type == 0)) {
         gjs_throw (cx, "Cannot inherit from a non-gjs dynamic type [bug 687184]");
-        goto out;
+        return false;
     }
 
     type_info.class_size = query.class_size;
@@ -2880,23 +2879,18 @@ gjs_register_type(JSContext *cx,
     g_type_set_qdata (instance_type, gjs_is_custom_type_quark(), GINT_TO_POINTER (1));
 
     if (!save_properties_for_class_init(cx, properties, n_properties, instance_type))
-        goto out;
+        return false;
 
     for (i = 0; i < n_interfaces; i++)
         gjs_add_interface(instance_type, iface_types[i]);
 
     /* create a custom JSClass */
-    module = gjs_lookup_private_namespace(cx);
+    JS::RootedObject module(cx, gjs_lookup_private_namespace(cx)), constructor(cx);
     gjs_define_object_class(cx, module, NULL, instance_type, &constructor);
 
     argv.rval().setObject(*constructor);
 
-    retval = true;
-
-out:
-    JS_EndRequest(cx);
-
-    return retval;
+    return true;
 }
 
 static JSBool
diff --git a/gi/object.h b/gi/object.h
index 2906a96..cc089dc 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -32,7 +32,7 @@
 G_BEGIN_DECLS
 
 void gjs_define_object_class(JSContext              *context,
-                             JSObject               *in_object,
+                             JS::HandleObject        in_object,
                              GIObjectInfo           *info,
                              GType                   gtype,
                              JS::MutableHandleObject constructor);
diff --git a/gi/param.cpp b/gi/param.cpp
index 179988f..c0b75eb 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -211,19 +211,18 @@ gjs_lookup_param_prototype(JSContext    *context)
 }
 
 void
-gjs_define_param_class(JSContext    *context,
-                       JSObject     *in_object)
+gjs_define_param_class(JSContext       *context,
+                       JS::HandleObject in_object)
 {
     const char *constructor_name;
-    JSObject *prototype;
     JS::Value value;
-    JS::RootedObject constructor(context);
+    JS::RootedObject prototype(context), constructor(context);
     GIObjectInfo *info;
 
     constructor_name = "ParamSpec";
 
     if (!gjs_init_class_dynamic(context, in_object,
-                                NULL,
+                                JS::NullPtr(),
                                 "GObject",
                                 constructor_name,
                                 &gjs_param_class,
@@ -250,7 +249,8 @@ gjs_define_param_class(JSContext    *context,
     g_base_info_unref( (GIBaseInfo*) info);
 
     gjs_debug(GJS_DEBUG_GPARAM, "Defined class %s prototype is %p class %p in object %p",
-              constructor_name, prototype, JS_GetClass(prototype), in_object);
+              constructor_name, prototype.get(), JS_GetClass(prototype),
+              in_object.get());
 }
 
 JSObject*
diff --git a/gi/param.h b/gi/param.h
index c957818..107f66b 100644
--- a/gi/param.h
+++ b/gi/param.h
@@ -31,8 +31,8 @@
 
 G_BEGIN_DECLS
 
-void        gjs_define_param_class     (JSContext  *context,
-                                        JSObject   *in_object);
+void gjs_define_param_class(JSContext       *context,
+                            JS::HandleObject in_object);
 
 GParamSpec *gjs_g_param_from_param (JSContext       *context,
                                     JS::HandleObject obj);
diff --git a/gi/repo.cpp b/gi/repo.cpp
index b712bd2..1706f35 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -434,10 +434,10 @@ _gjs_log_info_usage(GIBaseInfo *info)
 #endif /* GJS_VERBOSE_ENABLE_GI_USAGE */
 
 bool
-gjs_define_info(JSContext  *context,
-                JSObject   *in_object,
-                GIBaseInfo *info,
-                bool       *defined)
+gjs_define_info(JSContext       *context,
+                JS::HandleObject in_object,
+                GIBaseInfo      *info,
+                bool            *defined)
 {
 #if GJS_VERBOSE_ENABLE_GI_USAGE
     _gjs_log_info_usage(info);
@@ -466,8 +466,10 @@ gjs_define_info(JSContext  *context,
                 gjs_define_object_class(context, in_object,
                                         (GIObjectInfo *) info, gtype, &ignored);
             } else if (G_TYPE_IS_INSTANTIATABLE(gtype)) {
-                JS::RootedObject ignored(context);
-                if (!gjs_define_fundamental_class(context, in_object, (GIObjectInfo*)info, &ignored, NULL)) {
+                JS::RootedObject ignored1(context), ignored2(context);
+                if (!gjs_define_fundamental_class(context, in_object,
+                                                  (GIObjectInfo*)info,
+                                                  &ignored1, &ignored2)) {
                     gjs_throw (context,
                                "Unsupported fundamental class creation for type %s",
                                g_type_name(gtype));
diff --git a/gi/repo.h b/gi/repo.h
index 0a4200b..a3a38fc 100644
--- a/gi/repo.h
+++ b/gi/repo.h
@@ -50,10 +50,12 @@ JSObject *  gjs_lookup_generic_constructor      (JSContext      *context,
                                                  GIBaseInfo     *info);
 JSObject *  gjs_lookup_generic_prototype        (JSContext      *context,
                                                  GIBaseInfo     *info);
-bool        gjs_define_info                     (JSContext      *context,
-                                                 JSObject       *in_object,
-                                                 GIBaseInfo     *info,
-                                                 bool           *defined);
+
+bool gjs_define_info(JSContext       *context,
+                     JS::HandleObject in_object,
+                     GIBaseInfo      *info,
+                     bool            *defined);
+
 char*       gjs_camel_from_hyphen               (const char     *hyphen_name);
 char*       gjs_hyphen_from_camel               (const char     *camel_name);
 
diff --git a/gi/union.cpp b/gi/union.cpp
index 43b2a04..c422124 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -322,16 +322,15 @@ JSFunctionSpec gjs_union_proto_funcs[] = {
 };
 
 bool
-gjs_define_union_class(JSContext    *context,
-                       JSObject     *in_object,
-                       GIUnionInfo  *info)
+gjs_define_union_class(JSContext       *context,
+                       JS::HandleObject in_object,
+                       GIUnionInfo     *info)
 {
     const char *constructor_name;
-    JSObject *prototype;
     JS::Value value;
     Union *priv;
     GType gtype;
-    JS::RootedObject constructor(context);
+    JS::RootedObject prototype(context), constructor(context);
 
     /* For certain unions, we may be able to relax this in the future by
      * directly allocating union memory, as we do for structures in boxed.c
@@ -350,7 +349,7 @@ gjs_define_union_class(JSContext    *context,
     constructor_name = g_base_info_get_name( (GIBaseInfo*) info);
 
     if (!gjs_init_class_dynamic(context, in_object,
-                                NULL,
+                                JS::NullPtr(),
                                 g_base_info_get_namespace( (GIBaseInfo*) info),
                                 constructor_name,
                                 &gjs_union_class,
@@ -376,7 +375,8 @@ gjs_define_union_class(JSContext    *context,
     JS_SetPrivate(prototype, priv);
 
     gjs_debug(GJS_DEBUG_GBOXED, "Defined class %s prototype is %p class %p in object %p",
-              constructor_name, prototype, JS_GetClass(prototype), in_object);
+              constructor_name, prototype.get(), JS_GetClass(prototype),
+              in_object.get());
 
     value = JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, gtype));
     JS_DefineProperty(context, constructor, "$gtype", value,
diff --git a/gi/union.h b/gi/union.h
index fabc065..4f676d5 100644
--- a/gi/union.h
+++ b/gi/union.h
@@ -31,9 +31,9 @@
 
 G_BEGIN_DECLS
 
-bool      gjs_define_union_class       (JSContext    *context,
-                                        JSObject     *in_object,
-                                        GIUnionInfo  *info);
+bool gjs_define_union_class(JSContext       *context,
+                            JS::HandleObject in_object,
+                            GIUnionInfo     *info);
 
 void     *gjs_c_union_from_union(JSContext       *context,
                                  JS::HandleObject obj);
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index f85c57e..4ac1071 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -37,8 +37,8 @@
 
 bool
 gjs_init_class_dynamic(JSContext              *context,
-                       JSObject               *in_object,
-                       JSObject               *parent_proto,
+                       JS::HandleObject        in_object,
+                       JS::HandleObject        parent_proto,
                        const char             *ns_name,
                        const char             *class_name,
                        JSClass                *clasp,
@@ -48,13 +48,11 @@ gjs_init_class_dynamic(JSContext              *context,
                        JSFunctionSpec         *proto_fs,
                        JSPropertySpec         *static_ps,
                        JSFunctionSpec         *static_fs,
-                       JSObject              **prototype_p,
+                       JS::MutableHandleObject prototype,
                        JS::MutableHandleObject constructor)
 {
-    JSObject *global;
     /* Force these variables on the stack, so the conservative GC will
        find them */
-    JSObject * volatile prototype;
     JSFunction * volatile constructor_fun;
     char *full_function_name = NULL;
     bool res = false;
@@ -68,7 +66,7 @@ gjs_init_class_dynamic(JSContext              *context,
 
     JS_BeginRequest(context);
 
-    global = gjs_get_import_global(context);
+    JS::RootedObject global(context, gjs_get_import_global(context));
 
     /* Class initalization consists of three parts:
        - building a prototype
@@ -86,7 +84,7 @@ gjs_init_class_dynamic(JSContext              *context,
      * constructor is not found (and it won't be found, because we never call
      * JS_InitClass).
      */
-    prototype = JS_NewObject(context, clasp, parent_proto, global);
+    prototype.set(JS_NewObject(context, clasp, parent_proto, global));
     if (!prototype)
         goto out;
 
@@ -121,12 +119,8 @@ gjs_init_class_dynamic(JSContext              *context,
                            JS_PropertyStub, JS_StrictPropertyStub, GJS_MODULE_PROP_FLAGS))
         goto out;
 
-    if (prototype_p)
-        *prototype_p = prototype;
-
     res = true;
 
-    prototype = NULL;
     constructor_fun = NULL;
 
  out:
@@ -146,18 +140,19 @@ format_dynamic_class_name (const char *name)
 }
 
 bool
-gjs_typecheck_instance(JSContext *context,
-                       JSObject  *obj,
-                       JSClass   *static_clasp,
-                       bool       throw_error)
+gjs_typecheck_instance(JSContext       *context,
+                       JS::HandleObject obj,
+                       JSClass         *static_clasp,
+                       bool             throw_error)
 {
     if (!JS_InstanceOf(context, obj, static_clasp, NULL)) {
         if (throw_error) {
-            JSClass *obj_class = JS_GetClass(obj);
+            const JSClass *obj_class = JS_GetClass(obj);
 
             gjs_throw_custom(context, "TypeError",
                              "Object %p is not a subclass of %s, it's a %s",
-                             obj, static_clasp->name, format_dynamic_class_name (obj_class->name));
+                             obj.get(), static_clasp->name,
+                             format_dynamic_class_name(obj_class->name));
         }
 
         return false;
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 41d0f20..172f5e3 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -318,8 +318,8 @@ bool gjs_object_require_converted_property_value(JSContext       *context,
                                                  uint32_t        *value);
 
 bool gjs_init_class_dynamic(JSContext              *context,
-                            JSObject               *in_object,
-                            JSObject               *parent_proto,
+                            JS::HandleObject        in_object,
+                            JS::HandleObject        parent_proto,
                             const char             *ns_name,
                             const char             *class_name,
                             JSClass                *clasp,
@@ -329,7 +329,7 @@ bool gjs_init_class_dynamic(JSContext              *context,
                             JSFunctionSpec         *fs,
                             JSPropertySpec         *static_ps,
                             JSFunctionSpec         *static_fs,
-                            JSObject              **prototype_p,
+                            JS::MutableHandleObject prototype,
                             JS::MutableHandleObject constructor);
 
 void gjs_throw_constructor_error             (JSContext       *context);
@@ -337,10 +337,10 @@ void gjs_throw_constructor_error             (JSContext       *context);
 void gjs_throw_abstract_constructor_error(JSContext    *context,
                                           JS::CallArgs& args);
 
-bool        gjs_typecheck_instance            (JSContext  *context,
-                                               JSObject   *obj,
-                                               JSClass    *static_clasp,
-                                               bool        _throw);
+bool gjs_typecheck_instance(JSContext       *context,
+                            JS::HandleObject obj,
+                            JSClass         *static_clasp,
+                            bool             throw_error);
 
 JSObject *gjs_construct_object_dynamic(JSContext       *context,
                                        JS::HandleObject proto,


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