[gjs/wip/gcampax/70-arg-cache: 6/10] arg-cache: Factor out code for handling null pointers
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/gcampax/70-arg-cache: 6/10] arg-cache: Factor out code for handling null pointers
- Date: Sat, 1 Aug 2020 05:20:25 +0000 (UTC)
commit 896c74bceebca29c411b3b5195d7b4b09ef6cbe9
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 188e558d..0d204ae6 100644
--- a/gi/arg-cache.cpp
+++ b/gi/arg-cache.cpp
@@ -476,17 +476,20 @@ static bool gjs_marshal_gtype_in_in(JSContext* cx, GjsArgumentCache* self,
cx, gtype_obj, &gjs_arg_member<GType, GI_TYPE_TAG_GTYPE>(arg));
}
+// 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);
+ gjs_arg_unset<void*>(arg);
+ 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);
-
- gjs_arg_unset<void*>(arg);
- return true;
- }
+ if (value.isNull())
+ return self->handle_nullable(cx, arg);
if (!value.isString())
return report_typeof_mismatch(cx, self->arg_name, value,
@@ -584,13 +587,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);
-
- gjs_arg_unset<void*>(arg);
- return true;
- }
+ if (value.isNull())
+ return self->handle_nullable(cx, arg);
GType gtype = self->contents.object.gtype;
@@ -615,13 +613,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);
-
- gjs_arg_unset<void*>(arg);
- return true;
- }
+ if (value.isNull())
+ return self->handle_nullable(cx, arg);
GType gtype = self->contents.object.gtype;
g_assert(gtype != G_TYPE_NONE);
@@ -639,13 +632,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);
-
- gjs_arg_unset<void*>(arg);
- return true;
- }
+ if (value.isNull())
+ return self->handle_nullable(cx, arg);
if (!(JS_TypeOfValue(cx, value) == JSTYPE_FUNCTION))
return report_typeof_mismatch(cx, self->arg_name, value,
@@ -664,13 +652,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);
-
- gjs_arg_unset<void*>(arg);
- return true;
- }
+ if (value.isNull())
+ return self->handle_nullable(cx, arg);
if (!value.isObject())
return report_gtype_mismatch(cx, self->arg_name, value, G_TYPE_BYTES);
@@ -692,13 +675,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);
-
- gjs_arg_unset<void*>(arg);
- 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]