[gjs: 1/10] repo: Factor out new function



commit fc79130033d7fe63c097d06cf114a8dfe398d359
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Nov 23 22:14:28 2018 -0500

    repo: Factor out new function
    
    The combination of gjs_lookup_generic_prototype() plus
    JS_NewObjectWithGivenProto() occurs often enough to be a separate
    function.

 gi/boxed.cpp  | 18 +++---------------
 gi/gerror.cpp |  4 +---
 gi/repo.cpp   |  9 +++++++++
 gi/repo.h     |  3 +++
 gi/union.cpp  |  9 ++-------
 5 files changed, 18 insertions(+), 25 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 7bbb42b4..287a6204 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -447,17 +447,10 @@ bool BoxedInstance::get_nested_interface_object(
         return false;
     }
 
-    JS::RootedObject proto(
-        context, gjs_lookup_generic_prototype(context, interface_info));
-
-    if (!proto)
-        return false;
-
     offset = g_field_info_get_offset (field_info);
 
-    JS::RootedObject obj(context, JS_NewObjectWithGivenProto(
-                                      context, JS_GetClass(proto), proto));
-
+    JS::RootedObject obj(context, gjs_new_object_with_generic_prototype(
+                                      context, interface_info));
     if (!obj)
         return false;
 
@@ -963,12 +956,7 @@ JSObject* gjs_boxed_from_c_struct(JSContext* cx, GIStructInfo* info,
                       "Wrapping struct %s %p with JSObject",
                       g_base_info_get_name((GIBaseInfo *)info), gboxed);
 
-    JS::RootedObject proto(cx, gjs_lookup_generic_prototype(cx, info));
-    if (!proto)
-        return nullptr;
-
-    JS::RootedObject obj(
-        cx, JS_NewObjectWithGivenProto(cx, JS_GetClass(proto), proto));
+    JS::RootedObject obj(cx, gjs_new_object_with_generic_prototype(cx, info));
     if (!obj)
         return nullptr;
 
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 7501de4a..88641918 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -382,10 +382,8 @@ gjs_error_from_gerror(JSContext             *context,
                       "Wrapping struct %s with JSObject",
                       g_base_info_get_name((GIBaseInfo *)info));
 
-    JS::RootedObject proto(context, gjs_lookup_generic_prototype(context, info));
-
     JS::RootedObject obj(context,
-        JS_NewObjectWithGivenProto(context, JS_GetClass(proto), proto));
+                         gjs_new_object_with_generic_prototype(context, info));
     if (!obj)
         return nullptr;
 
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 9c2e848c..9ce7aa41 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -780,3 +780,12 @@ gjs_lookup_generic_prototype(JSContext  *context,
 
     return &value.toObject();
 }
+
+JSObject* gjs_new_object_with_generic_prototype(JSContext* cx,
+                                                GIBaseInfo* info) {
+    JS::RootedObject proto(cx, gjs_lookup_generic_prototype(cx, info));
+    if (!proto)
+        return nullptr;
+
+    return JS_NewObjectWithGivenProto(cx, JS_GetClass(proto), proto);
+}
diff --git a/gi/repo.h b/gi/repo.h
index fbf7357c..d5e3226b 100644
--- a/gi/repo.h
+++ b/gi/repo.h
@@ -57,6 +57,9 @@ JSObject *  gjs_lookup_generic_constructor      (JSContext      *context,
 GJS_JSAPI_RETURN_CONVENTION
 JSObject *  gjs_lookup_generic_prototype        (JSContext      *context,
                                                  GIBaseInfo     *info);
+GJS_JSAPI_RETURN_CONVENTION
+JSObject* gjs_new_object_with_generic_prototype(JSContext* cx,
+                                                GIBaseInfo* info);
 
 GJS_JSAPI_RETURN_CONVENTION
 bool gjs_define_info(JSContext       *context,
diff --git a/gi/union.cpp b/gi/union.cpp
index c0639d39..320096cc 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -233,13 +233,8 @@ gjs_union_from_c_union(JSContext    *context,
                       "Wrapping union %s %p with JSObject",
                       g_base_info_get_name((GIBaseInfo *)info), gboxed);
 
-    JS::RootedObject proto(context,
-        gjs_lookup_generic_prototype(context, (GIUnionInfo*) info));
-    if (!proto)
-        return nullptr;
-
-    JS::RootedObject obj(
-        context, JS_NewObjectWithGivenProto(context, JS_GetClass(proto), proto));
+    JS::RootedObject obj(context,
+                         gjs_new_object_with_generic_prototype(context, info));
     if (!obj)
         return nullptr;
 


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