[gjs/cairo: 1/12] [gi] Add arg overrides



commit 0863e3631a903af7e56944503dd2cec3c8ad1a55
Author: Johan Dahlin <johan gnome org>
Date:   Thu Feb 18 11:15:19 2010 -0200

    [gi] Add arg overrides
    
    Adds arg overrides, which is a way to specificy that certain gi types
    should use custom create/get and release functions. This is useful
    to be able to integrate types from third-party libraries.
    It makes no sense to override basic types as they are already handled
    perfectly well by gjs itself. This also avoids the performance penalty
    since we don't have to check all argument types.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=610357

 Makefile-gi.am |    2 +
 gi/arg.c       |   13 ++++-
 gi/override.c  |  160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gi/override.h  |   77 +++++++++++++++++++++++++++
 4 files changed, 251 insertions(+), 1 deletions(-)
---
diff --git a/Makefile-gi.am b/Makefile-gi.am
index 108069c..5aad834 100644
--- a/Makefile-gi.am
+++ b/Makefile-gi.am
@@ -20,6 +20,7 @@ nobase_gjsgiinclude_HEADERS =	\
 	gi/keep-alive.h	\
 	gi/ns.h	        \
 	gi/object.h	\
+	gi/override.h	\
 	gi/param.h	\
 	gi/repo.h	\
 	gi/union.h	\
@@ -34,6 +35,7 @@ libgjs_gi_la_SOURCES =	\
 	gi/keep-alive.c	\
 	gi/ns.c	\
 	gi/object.c	\
+	gi/override.c	\
 	gi/param.c	\
         gi/repo.c	\
 	gi/union.c	\
diff --git a/gi/arg.c b/gi/arg.c
index 78878e2..b3d6469 100644
--- a/gi/arg.c
+++ b/gi/arg.c
@@ -25,6 +25,7 @@
 
 #include "arg.h"
 #include "object.h"
+#include "override.h"
 #include "boxed.h"
 #include "union.h"
 #include "value.h"
@@ -747,6 +748,10 @@ gjs_value_to_g_argument(JSContext      *context,
             GIInfoType interface_type;
             GType gtype;
 
+            if (gjs_arg_override_convert_to_g_argument(context, value, type_info, arg_name,
+                                                       arg_type, transfer, may_be_null, arg))
+                return JS_TRUE;
+
             interface_info = g_type_info_get_interface(type_info);
             g_assert(interface_info != NULL);
 
@@ -1368,6 +1373,9 @@ gjs_value_from_g_argument (JSContext  *context,
             GIInfoType interface_type;
             GType gtype;
 
+            if (gjs_arg_override_convert_from_g_argument(context, value_p, type_info, arg))
+                return JS_TRUE;
+
             interface_info = g_type_info_get_interface(type_info);
             g_assert(interface_info != NULL);
 
@@ -1635,7 +1643,7 @@ gjs_g_arg_release_internal(JSContext  *context,
                            GArgument  *arg)
 {
     JSBool failed;
-    
+
     g_assert(transfer != GI_TRANSFER_NOTHING);
 
     failed = JS_FALSE;
@@ -1675,6 +1683,9 @@ gjs_g_arg_release_internal(JSContext  *context,
             GIInfoType interface_type;
             GType gtype;
 
+            if (gjs_arg_override_release_g_argument(context, transfer, type_info, arg))
+                return JS_TRUE;
+
             interface_info = g_type_info_get_interface(type_info);
             g_assert(interface_info != NULL);
 
diff --git a/gi/override.c b/gi/override.c
new file mode 100644
index 0000000..c7f261c
--- /dev/null
+++ b/gi/override.c
@@ -0,0 +1,160 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2010  litl, LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <config.h>
+
+#include <string.h>
+#include <gjs/gjs.h>
+#include <girepository.h>
+
+#include "arg.h"
+#include "override.h"
+
+static GHashTable *arg_overrides_table = NULL;
+
+JSBool
+gjs_arg_override_register(const char *namespace,
+                          const char *type_name,
+                          GjsArgOverrideInfo *info)
+{
+    char *canonical_name;
+
+    g_return_val_if_fail(info != NULL, JS_FALSE);
+    g_return_val_if_fail(info->to_func != NULL, JS_FALSE);
+    g_return_val_if_fail(info->from_func != NULL, JS_FALSE);
+
+    canonical_name = g_strdup_printf("%s.%s", namespace, type_name);
+    if (!arg_overrides_table) {
+        // FIXME: look into hasing on GITypeInfo instead.
+        arg_overrides_table = g_hash_table_new_full(
+            g_str_hash, g_str_equal,
+            (GDestroyNotify)g_free,
+            NULL);
+    }
+    g_hash_table_insert(arg_overrides_table, canonical_name, info);
+    return JS_TRUE;
+}
+
+static GjsArgOverrideInfo *
+gjs_arg_override_lookup(GITypeInfo *type_info)
+{
+    const char *type_name;
+    const char *namespace;
+    char *canonical;
+    GIBaseInfo *base_info;
+
+    base_info = g_type_info_get_interface(type_info);
+    if (!base_info)
+        return NULL;
+    namespace = g_base_info_get_namespace(base_info);
+    type_name = g_base_info_get_name(base_info);
+    g_base_info_unref(base_info);
+
+    /* silly optimiazation, since this is rather frequent
+     * CairoContext -> "Cairo.Context" hash
+     */
+    {
+        int namespace_len = strlen(namespace);
+        int type_name_len = strlen(type_name);
+        int canonical_len = namespace_len + 1 + type_name_len;
+        canonical = g_alloca(sizeof(char) * canonical_len + 1);
+        memcpy(canonical, namespace, namespace_len);
+        canonical[namespace_len] = '.';
+        memcpy(canonical + namespace_len + 1, type_name, type_name_len);
+        canonical[canonical_len] = '\0';
+    }
+
+    return (GjsArgOverrideInfo*)g_hash_table_lookup(arg_overrides_table, canonical);
+}
+
+JSBool
+gjs_arg_override_convert_to_g_argument(JSContext      *context,
+                                       jsval           value,
+                                       GITypeInfo     *type_info,
+                                       const char     *arg_name,
+                                       GjsArgumentType argument_type,
+                                       GITransfer      transfer,
+                                       gboolean        may_be_null,
+                                       GArgument      *arg)
+{
+    GjsArgOverrideInfo *override;
+
+    if (!arg_overrides_table)
+        return JS_FALSE;
+
+    override = gjs_arg_override_lookup(type_info);
+    if (!override)
+        return JS_FALSE;
+
+    if (!override->to_func(context, value, type_info, arg_name,
+                           argument_type, transfer, may_be_null, arg))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
+JSBool
+gjs_arg_override_convert_from_g_argument(JSContext  *context,
+                                         jsval      *value_p,
+                                         GITypeInfo *type_info,
+                                         GArgument  *arg)
+{
+    GjsArgOverrideInfo *override;
+
+    if (!arg_overrides_table)
+        return JS_FALSE;
+
+    override = gjs_arg_override_lookup(type_info);
+    if (!override)
+        return JS_FALSE;
+
+    if (!override->from_func(context, value_p, type_info, arg))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
+JSBool
+gjs_arg_override_release_g_argument(JSContext  *context,
+                                    GITransfer  transfer,
+                                    GITypeInfo *type_info,
+                                    GArgument  *arg)
+{
+    GjsArgOverrideInfo *override;
+
+    if (!arg_overrides_table)
+        return JS_FALSE;
+
+    override = gjs_arg_override_lookup(type_info);
+    if (!override || !override->release_func)
+        return JS_FALSE;
+
+    if (!override->release_func)
+        return JS_TRUE;
+
+    if (!override->release_func(context, transfer, type_info, arg))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
diff --git a/gi/override.h b/gi/override.h
new file mode 100644
index 0000000..250071b
--- /dev/null
+++ b/gi/override.h
@@ -0,0 +1,77 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (c) 2010  litl, LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifndef __GJS_OVERRIDE_H__
+#define __GJS_OVERRIDE_H__
+
+#include <girepository.h>
+#include <gjs/gjs.h>
+#include "arg.h"
+
+typedef JSBool (*GjsArgOverrideToGArgumentFunc) (JSContext      *context,
+                                                 jsval           value,
+                                                 GITypeInfo     *type_info,
+                                                 const char     *arg_name,
+                                                 GjsArgumentType argument_type,
+                                                 GITransfer      transfer,
+                                                 gboolean        may_be_null,
+                                                 GArgument      *arg);
+
+typedef JSBool (*GjsArgOverrideFromGArgumentFunc) (JSContext  *context,
+                                                   jsval      *value_p,
+                                                   GITypeInfo *type_info,
+                                                   GArgument  *arg);
+typedef JSBool (*GjsArgOverrideReleaseGArgumentFunc) (JSContext  *context,
+                                                      GITransfer  transfer,
+                                                      GITypeInfo *type_info,
+                                                      GArgument  *arg);
+
+typedef struct {
+    GjsArgOverrideToGArgumentFunc to_func;
+    GjsArgOverrideFromGArgumentFunc from_func;
+    GjsArgOverrideReleaseGArgumentFunc release_func;
+} GjsArgOverrideInfo;
+
+JSBool gjs_arg_override_register(const char         *namespace,
+                                 const char         *type_name,
+                                 GjsArgOverrideInfo *info);
+
+
+JSBool gjs_arg_override_convert_to_g_argument  (JSContext  *context,
+                                                jsval           value,
+                                                GITypeInfo     *type_info,
+                                                const char     *arg_name,
+                                                GjsArgumentType argument_type,
+                                                GITransfer      transfer,
+                                                gboolean        may_be_null,
+                                                GArgument      *arg);
+JSBool gjs_arg_override_convert_from_g_argument(JSContext  *context,
+                                                jsval      *value_p,
+                                                GITypeInfo *type_info,
+                                                GArgument  *arg);
+JSBool gjs_arg_override_release_g_argument(JSContext  *context,
+                                           GITransfer  transfer,
+                                           GITypeInfo *type_info,
+                                           GArgument  *arg);
+
+#endif /* __GJS_OVERRIDE_H__ */



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