[gjs/wip/gcampax/70-arg-cache: 9/14] arg-cache: Factor out code for handling null pointers



commit 4615da9f3f105190a9d79eeb4cc59b0308a60ffd
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jul 18 10:25:45 2020 -0700

    arg-cache: Factor out code for handling null pointers
    
    It's common to translate a JS null to a C null if the argument is
    nullable and report an error otherwise.

 gi/arg-cache.cpp | 62 ++++++++++++++++++--------------------------------------
 gi/arg-cache.h   |  3 +++
 2 files changed, 23 insertions(+), 42 deletions(-)
---
diff --git a/gi/arg-cache.cpp b/gi/arg-cache.cpp
index ce999b52..8b6c861b 100644
--- a/gi/arg-cache.cpp
+++ b/gi/arg-cache.cpp
@@ -472,17 +472,20 @@ static bool gjs_marshal_gtype_in_in(JSContext* cx, GjsArgumentCache* self,
     return gjs_gtype_get_actual_gtype(cx, gtype_obj, &arg->v_ulong);
 }
 
+// Common code for most types that are pointers on the C side
+bool GjsArgumentCache::handle_nullable(JSContext* cx, GIArgument* arg) {
+    if (!nullable)
+        return report_invalid_null(cx, arg_name);
+    arg->v_pointer = nullptr;
+    return true;
+}
+
 GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_marshal_string_in_in(JSContext* cx, GjsArgumentCache* self,
                                      GjsFunctionCallState*, GIArgument* arg,
                                      JS::HandleValue value) {
-    if (value.isNull()) {
-        if (!self->nullable)
-            return report_invalid_null(cx, self->arg_name);
-
-        arg->v_pointer = nullptr;
-        return true;
-    }
+    if (value.isNull())
+        return self->handle_nullable(cx, arg);
 
     if (!value.isString())
         return report_primitive_type_mismatch(cx, self->arg_name, value,
@@ -580,13 +583,8 @@ GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_marshal_boxed_in_in(JSContext* cx, GjsArgumentCache* self,
                                     GjsFunctionCallState*, GIArgument* arg,
                                     JS::HandleValue value) {
-    if (value.isNull()) {
-        if (!self->nullable)
-            return report_invalid_null(cx, self->arg_name);
-
-        arg->v_pointer = nullptr;
-        return true;
-    }
+    if (value.isNull())
+        return self->handle_nullable(cx, arg);
 
     GType gtype = self->contents.object.gtype;
 
@@ -612,13 +610,8 @@ GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_marshal_union_in_in(JSContext* cx, GjsArgumentCache* self,
                                     GjsFunctionCallState*, GIArgument* arg,
                                     JS::HandleValue value) {
-    if (value.isNull()) {
-        if (!self->nullable)
-            return report_invalid_null(cx, self->arg_name);
-
-        arg->v_pointer = nullptr;
-        return true;
-    }
+    if (value.isNull())
+        return self->handle_nullable(cx, arg);
 
     GType gtype = self->contents.object.gtype;
     g_assert(gtype != G_TYPE_NONE);
@@ -637,13 +630,8 @@ GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_marshal_gclosure_in_in(JSContext* cx, GjsArgumentCache* self,
                                        GjsFunctionCallState*, GIArgument* arg,
                                        JS::HandleValue value) {
-    if (value.isNull()) {
-        if (!self->nullable)
-            return report_invalid_null(cx, self->arg_name);
-
-        arg->v_pointer = nullptr;
-        return true;
-    }
+    if (value.isNull())
+        return self->handle_nullable(cx, arg);
 
     if (!(JS_TypeOfValue(cx, value) == JSTYPE_FUNCTION))
         return report_primitive_type_mismatch(cx, self->arg_name, value,
@@ -662,13 +650,8 @@ GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_marshal_gbytes_in_in(JSContext* cx, GjsArgumentCache* self,
                                      GjsFunctionCallState*, GIArgument* arg,
                                      JS::HandleValue value) {
-    if (value.isNull()) {
-        if (!self->nullable)
-            return report_invalid_null(cx, self->arg_name);
-
-        arg->v_pointer = nullptr;
-        return true;
-    }
+    if (value.isNull())
+        return self->handle_nullable(cx, arg);
 
     if (!value.isObject())
         return report_object_primitive_type_mismatch(cx, self->arg_name, value,
@@ -691,13 +674,8 @@ GJS_JSAPI_RETURN_CONVENTION
 static bool gjs_marshal_object_in_in(JSContext* cx, GjsArgumentCache* self,
                                      GjsFunctionCallState*, GIArgument* arg,
                                      JS::HandleValue value) {
-    if (value.isNull()) {
-        if (!self->nullable)
-            return report_invalid_null(cx, self->arg_name);
-
-        arg->v_pointer = nullptr;
-        return true;
-    }
+    if (value.isNull())
+        return self->handle_nullable(cx, arg);
 
     GType gtype = self->contents.object.gtype;
     g_assert(gtype != G_TYPE_NONE);
diff --git a/gi/arg-cache.h b/gi/arg-cache.h
index ff5d4d3b..2a43a2be 100644
--- a/gi/arg-cache.h
+++ b/gi/arg-cache.h
@@ -101,6 +101,9 @@ struct GjsArgumentCache {
         // out caller allocates (FIXME: should be in object)
         size_t caller_allocates_size;
     } contents;
+
+    GJS_JSAPI_RETURN_CONVENTION
+    bool handle_nullable(JSContext* cx, GIArgument* arg);
 };
 
 // This is a trick to print out the sizes of the structs at compile time, in


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