[gjs] jsapi-util: Return JSObject from create_proto



commit 06e37d73e4bc1523b063942aff4ace96245c6092
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Apr 9 16:48:13 2017 -0700

    jsapi-util: Return JSObject from create_proto
    
    The return value is a prototype object, so there's no use in returning
    JS::Value here only to convert it to an object in the calling code.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=614413

 gi/gtype.cpp            |    4 +-
 gjs/jsapi-util.h        |   39 ++++++++++++--------------
 modules/cairo-private.h |   28 +++++++++---------
 modules/cairo.cpp       |   71 ++++++++++++++--------------------------------
 4 files changed, 56 insertions(+), 86 deletions(-)
---
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index 61708e6..be84ae9 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -34,7 +34,7 @@
 static bool weak_pointer_callback = false;
 static std::set<GType> weak_pointer_list;
 
-static JS::Value
+static JSObject *
 gjs_gtype_create_proto(JSContext       *context,
                        JS::HandleObject module,
                        const char      *proto_name,
@@ -155,7 +155,7 @@ gjs_gtype_create_gtype_wrapper (JSContext *context,
     /* put constructor for GIRepositoryGType() in the global namespace */
     JS::RootedObject global(context, gjs_get_import_global(context));
     JS::RootedObject proto(context,
-        gjs_gtype_create_proto(context, global, "GIRepositoryGType", JS::NullPtr()).toObjectOrNull());
+        gjs_gtype_create_proto(context, global, "GIRepositoryGType", JS::NullPtr()));
 
     auto heap_wrapper =
         static_cast<JS::Heap<JSObject *> *>(g_type_get_qdata(gtype, gjs_get_gtype_wrapper_quark()));
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index aa0bd25..0e537c7 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -215,46 +215,43 @@ static struct JSClass gjs_##cname##_class = { \
     NULL,  /* convert */                                                       \
     gjs_##cname##_finalize                                                     \
 }; \
-JS::Value                                                                      \
+JSObject *                                                                     \
 gjs_##cname##_create_proto(JSContext *context,                                 \
                            JS::HandleObject module,                            \
                            const char      *proto_name,                        \
                            JS::HandleObject parent)                            \
 { \
-    JS::RootedValue rval(context);                                             \
+    JS::RootedObject rval(context);                                            \
     JS::RootedObject global(context, gjs_get_import_global(context));          \
     JS::RootedId class_name(context,                                           \
         gjs_intern_string_to_id(context, gjs_##cname##_class.name));           \
     bool found = false;                                                        \
     if (!JS_AlreadyHasOwnPropertyById(context, global, class_name, &found))    \
-        return JS::NullValue(); \
-    if (found) {                                                               \
-        if (!JS_GetPropertyById(context, global, class_name, &rval))           \
-            return JS::NullValue();                                            \
-        return rval;                                                           \
-    }                                                                          \
-    gjs_##cname##_prototype.init(context);                                     \
-    gjs_##cname##_prototype =                                                  \
-        JS_InitClass(context, global, parent, &gjs_##cname##_class, ctor,      \
-                     0, &gjs_##cname##_proto_props[0],                         \
-                     &gjs_##cname##_proto_funcs[0],                            \
-                     nullptr, nullptr);                                        \
-    if (!gjs_##cname##_prototype) {                                            \
-        return JS::NullValue(); \
+        return nullptr;                                                        \
+    if (!found) {                                                              \
+        gjs_##cname##_prototype.init(context);                                 \
+        gjs_##cname##_prototype =                                              \
+            JS_InitClass(context, global, parent, &gjs_##cname##_class, ctor,  \
+                         0, &gjs_##cname##_proto_props[0],                     \
+                         &gjs_##cname##_proto_funcs[0],                        \
+                         nullptr, nullptr);                                    \
+        if (!gjs_##cname##_prototype)                                          \
+            return nullptr;                                                    \
     } \
     if (!gjs_object_require_property( \
             context, global, NULL, \
             class_name, &rval)) { \
-        return JS::NullValue(); \
+        return nullptr;                                                        \
     } \
-    if (!JS_DefineProperty(context, module, proto_name, \
+    if (found)                                                                 \
+        return rval;                                                           \
+    if (!JS_DefineProperty(context, module, proto_name,                        \
                            rval, GJS_MODULE_PROP_FLAGS))                       \
-        return JS::NullValue(); \
+        return nullptr;                                                        \
     if (gtype != G_TYPE_NONE) { \
-        JS::RootedObject rval_obj(context, &rval.toObject());                  \
         JS::RootedObject gtype_obj(context,                                    \
             gjs_gtype_create_gtype_wrapper(context, gtype));                   \
-        JS_DefineProperty(context, rval_obj, "$gtype", gtype_obj,              \
+        JS_DefineProperty(context, rval, "$gtype", gtype_obj,                  \
                           JSPROP_PERMANENT);                                   \
     } \
     return rval; \
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index c80b87f..2435ed3 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -30,14 +30,14 @@ bool             gjs_cairo_check_status                 (JSContext       *contex
                                                          cairo_status_t   status,
                                                          const char      *name);
 
-JS::Value gjs_cairo_region_create_proto(JSContext       *context,
+JSObject *gjs_cairo_region_create_proto(JSContext       *cx,
                                         JS::HandleObject module,
                                         const char      *proto_name,
                                         JS::HandleObject parent);
 
 void             gjs_cairo_region_init                  (JSContext       *context);
 
-JS::Value gjs_cairo_context_create_proto(JSContext       *context,
+JSObject *gjs_cairo_context_create_proto(JSContext       *cx,
                                          JS::HandleObject module,
                                          const char      *proto_name,
                                          JS::HandleObject parent);
@@ -51,7 +51,7 @@ void             gjs_cairo_surface_init                 (JSContext       *contex
 
 
 /* cairo_path_t */
-JS::Value gjs_cairo_path_create_proto(JSContext       *context,
+JSObject *gjs_cairo_path_create_proto(JSContext       *cx,
                                       JS::HandleObject module,
                                       const char      *proto_name,
                                       JS::HandleObject parent);
@@ -62,7 +62,7 @@ cairo_path_t *   gjs_cairo_path_get_path                (JSContext       *contex
                                                          JSObject        *path_wrapper);
 
 /* surface */
-JS::Value gjs_cairo_surface_create_proto(JSContext       *context,
+JSObject *gjs_cairo_surface_create_proto(JSContext       *cx,
                                          JS::HandleObject module,
                                          const char      *proto_name,
                                          JS::HandleObject parent);
@@ -78,7 +78,7 @@ cairo_surface_t* gjs_cairo_surface_get_surface          (JSContext       *contex
                                                          JSObject        *object);
 
 /* image surface */
-JS::Value gjs_cairo_image_surface_create_proto(JSContext       *context,
+JSObject *gjs_cairo_image_surface_create_proto(JSContext       *cx,
                                                JS::HandleObject module,
                                                const char      *proto_name,
                                                JS::HandleObject parent);
@@ -91,7 +91,7 @@ JSObject *       gjs_cairo_image_surface_from_surface   (JSContext       *contex
 
 /* postscript surface */
 #ifdef CAIRO_HAS_PS_SURFACE
-JS::Value gjs_cairo_ps_surface_create_proto(JSContext       *context,
+JSObject *gjs_cairo_ps_surface_create_proto(JSContext       *cx,
                                             JS::HandleObject module,
                                             const char      *proto_name,
                                             JS::HandleObject parent);
@@ -101,7 +101,7 @@ JSObject *       gjs_cairo_ps_surface_from_surface       (JSContext       *conte
 
 /* pdf surface */
 #ifdef CAIRO_HAS_PDF_SURFACE
-JS::Value gjs_cairo_pdf_surface_create_proto(JSContext       *context,
+JSObject *gjs_cairo_pdf_surface_create_proto(JSContext       *cx,
                                              JS::HandleObject module,
                                              const char      *proto_name,
                                              JS::HandleObject parent);
@@ -111,7 +111,7 @@ JSObject *       gjs_cairo_pdf_surface_from_surface     (JSContext       *contex
 
 /* svg surface */
 #ifdef CAIRO_HAS_SVG_SURFACE
-JS::Value gjs_cairo_svg_surface_create_proto(JSContext       *context,
+JSObject *gjs_cairo_svg_surface_create_proto(JSContext       *cx,
                                              JS::HandleObject module,
                                              const char      *proto_name,
                                              JS::HandleObject parent);
@@ -120,7 +120,7 @@ JSObject *       gjs_cairo_svg_surface_from_surface     (JSContext       *contex
                                                          cairo_surface_t *surface);
 
 /* pattern */
-JS::Value gjs_cairo_pattern_create_proto(JSContext       *context,
+JSObject *gjs_cairo_pattern_create_proto(JSContext       *cx,
                                          JS::HandleObject module,
                                          const char      *proto_name,
                                          JS::HandleObject parent);
@@ -136,13 +136,13 @@ cairo_pattern_t* gjs_cairo_pattern_get_pattern          (JSContext       *contex
                                                          JSObject        *object);
 
 /* gradient */
-JS::Value gjs_cairo_gradient_create_proto(JSContext       *context,
+JSObject *gjs_cairo_gradient_create_proto(JSContext       *cx,
                                           JS::HandleObject module,
                                           const char      *proto_name,
                                           JS::HandleObject parent);
 
 /* linear gradient */
-JS::Value gjs_cairo_linear_gradient_create_proto(JSContext       *context,
+JSObject *gjs_cairo_linear_gradient_create_proto(JSContext       *cx,
                                                  JS::HandleObject module,
                                                  const char      *proto_name,
                                                  JS::HandleObject parent);
@@ -151,7 +151,7 @@ JSObject *       gjs_cairo_linear_gradient_from_pattern (JSContext       *contex
                                                          cairo_pattern_t *pattern);
 
 /* radial gradient */
-JS::Value gjs_cairo_radial_gradient_create_proto(JSContext       *context,
+JSObject *gjs_cairo_radial_gradient_create_proto(JSContext       *cx,
                                                  JS::HandleObject module,
                                                  const char      *proto_name,
                                                  JS::HandleObject parent);
@@ -160,7 +160,7 @@ JSObject *       gjs_cairo_radial_gradient_from_pattern (JSContext       *contex
                                                          cairo_pattern_t *pattern);
 
 /* surface pattern */
-JS::Value gjs_cairo_surface_pattern_create_proto(JSContext       *context,
+JSObject *gjs_cairo_surface_pattern_create_proto(JSContext       *cx,
                                                  JS::HandleObject module,
                                                  const char      *proto_name,
                                                  JS::HandleObject parent);
@@ -169,7 +169,7 @@ JSObject *       gjs_cairo_surface_pattern_from_pattern (JSContext       *contex
                                                          cairo_pattern_t *pattern);
 
 /* solid pattern */
-JS::Value gjs_cairo_solid_pattern_create_proto(JSContext       *context,
+JSObject *gjs_cairo_solid_pattern_create_proto(JSContext       *cx,
                                                JS::HandleObject module,
                                                const char      *proto_name,
                                                JS::HandleObject parent);
diff --git a/modules/cairo.cpp b/modules/cairo.cpp
index f3e4647..02c202f 100644
--- a/modules/cairo.cpp
+++ b/modules/cairo.cpp
@@ -59,90 +59,63 @@ bool
 gjs_js_define_cairo_stuff(JSContext              *context,
                           JS::MutableHandleObject module)
 {
-    JS::Value obj;
-    JS::RootedObject surface_proto(context), pattern_proto(context),
-        gradient_proto(context);
-
     module.set(JS_NewObject(context, NULL));
 
-    obj = gjs_cairo_region_create_proto(context, module,
-                                        "Region", JS::NullPtr());
-    if (obj.isNull())
+    if (!gjs_cairo_region_create_proto(context, module, "Region", JS::NullPtr()))
         return false;
     gjs_cairo_region_init(context);
 
-    obj = gjs_cairo_context_create_proto(context, module,
-                                         "Context", JS::NullPtr());
-    if (obj.isNull())
+    if (!gjs_cairo_context_create_proto(context, module, "Context", JS::NullPtr()))
         return false;
     gjs_cairo_context_init(context);
-    gjs_cairo_surface_init(context);
 
-    obj = gjs_cairo_surface_create_proto(context, module,
-                                         "Surface", JS::NullPtr());
-    if (obj.isNull())
+    JS::RootedObject surface_proto(context,
+        gjs_cairo_surface_create_proto(context, module, "Surface", JS::NullPtr()));
+    if (!surface_proto)
         return false;
-    surface_proto = &obj.toObject();
+    gjs_cairo_surface_init(context);
 
-    obj = gjs_cairo_image_surface_create_proto(context, module,
-                                               "ImageSurface", surface_proto);
-    if (obj.isNull())
+    JS::RootedObject image_surface_proto(context,
+        gjs_cairo_image_surface_create_proto(context, module, "ImageSurface", surface_proto));
+    if (!image_surface_proto)
         return false;
-
-    JS::RootedObject image_surface_proto(context, &obj.toObject());
     gjs_cairo_image_surface_init(context, image_surface_proto);
 
 #if CAIRO_HAS_PS_SURFACE
-    obj = gjs_cairo_ps_surface_create_proto(context, module,
-                                            "PSSurface", surface_proto);
-    if (obj.isNull())
+    if (!gjs_cairo_ps_surface_create_proto(context, module, "PSSurface", surface_proto))
         return false;
 #endif
 
 #if CAIRO_HAS_PDF_SURFACE
-    obj = gjs_cairo_pdf_surface_create_proto(context, module,
-                                             "PDFSurface", surface_proto);
-    if (obj.isNull())
+    if (!gjs_cairo_pdf_surface_create_proto(context, module, "PDFSurface", surface_proto))
         return false;
 #endif
 
 #if CAIRO_HAS_SVG_SURFACE
-    obj = gjs_cairo_svg_surface_create_proto(context, module,
-                                             "SVGSurface", surface_proto);
-    if (obj.isNull())
+    if (!gjs_cairo_svg_surface_create_proto(context, module, "SVGSurface", surface_proto))
         return false;
 #endif
 
-    obj = gjs_cairo_pattern_create_proto(context, module,
-                                         "Pattern", JS::NullPtr());
-    if (obj.isNull())
+    JS::RootedObject pattern_proto(context,
+        gjs_cairo_pattern_create_proto(context, module, "Pattern", JS::NullPtr()));
+    if (!pattern_proto)
         return false;
-    pattern_proto = &obj.toObject();
 
-    obj = gjs_cairo_gradient_create_proto(context, module,
-                                         "Gradient", pattern_proto);
-    if (obj.isNull())
+    JS::RootedObject gradient_proto(context,
+        gjs_cairo_gradient_create_proto(context, module, "Gradient", pattern_proto));
+    if (!gradient_proto)
         return false;
-    gradient_proto = &obj.toObject();
 
-    obj = gjs_cairo_linear_gradient_create_proto(context, module,
-                                                 "LinearGradient", gradient_proto);
-    if (obj.isNull())
+    if (!gjs_cairo_linear_gradient_create_proto(context, module, "LinearGradient", gradient_proto))
         return false;
 
-    obj = gjs_cairo_radial_gradient_create_proto(context, module,
-                                                 "RadialGradient", gradient_proto);
-    if (obj.isNull())
+    if (!gjs_cairo_radial_gradient_create_proto(context, module, "RadialGradient", gradient_proto))
         return false;
 
-    obj = gjs_cairo_surface_pattern_create_proto(context, module,
-                                                 "SurfacePattern", pattern_proto);
-    if (obj.isNull())
+    if (!gjs_cairo_surface_pattern_create_proto(context, module, "SurfacePattern", pattern_proto))
         return false;
 
-    obj = gjs_cairo_solid_pattern_create_proto(context, module,
-                                               "SolidPattern", pattern_proto);
-    if (obj.isNull())
+    if (!gjs_cairo_solid_pattern_create_proto(context, module, "SolidPattern", pattern_proto))
         return false;
 
     return true;


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