[gjs] [gi] Add support for foreign structs



commit 6111cb23f0fa10600f09d71b2d3cdf479d4577cb
Author: Johan Dahlin <johan gnome org>
Date:   Thu Mar 25 10:07:20 2010 -0300

    [gi] Add support for foreign structs
    
    Add support for foreign structs, which is a way to specificy that structs
    should use custom create/get/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       |   19 +++++-
 gi/foreign.c   |  195 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gi/foreign.h   |   76 ++++++++++++++++++++++
 gi/repo.c      |    1 +
 5 files changed, 290 insertions(+), 3 deletions(-)
---
diff --git a/Makefile-gi.am b/Makefile-gi.am
index 108069c..1bdf56c 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/foreign.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/foreign.c	\
 	gi/param.c	\
         gi/repo.c	\
 	gi/union.c	\
diff --git a/gi/arg.c b/gi/arg.c
index 78878e2..0595cc9 100644
--- a/gi/arg.c
+++ b/gi/arg.c
@@ -25,6 +25,7 @@
 
 #include "arg.h"
 #include "object.h"
+#include "foreign.h"
 #include "boxed.h"
 #include "union.h"
 #include "value.h"
@@ -753,8 +754,13 @@ gjs_value_to_g_argument(JSContext      *context,
             interface_type = g_base_info_get_type(interface_info);
 
             switch(interface_type) {
-
             case GI_INFO_TYPE_STRUCT:
+                if (g_struct_info_is_foreign((GIStructInfo*)interface_info)) {
+                    return gjs_struct_foreign_convert_to_g_argument(
+                            context, value, type_info, arg_name,
+                            arg_type, transfer, may_be_null, arg);
+                }
+                /* fall through */
             case GI_INFO_TYPE_ENUM:
             case GI_INFO_TYPE_OBJECT:
             case GI_INFO_TYPE_INTERFACE:
@@ -765,7 +771,6 @@ gjs_value_to_g_argument(JSContext      *context,
                 gtype = g_registered_type_info_get_g_type
                     ((GIRegisteredTypeInfo*)interface_info);
                 break;
-
             case GI_INFO_TYPE_VALUE:
                 /* Special case for GValues */
                 gtype = G_TYPE_VALUE;
@@ -1402,6 +1407,9 @@ gjs_value_from_g_argument (JSContext  *context,
                     value = INT_TO_JSVAL(arg->v_int);
 
                 goto out;
+            } else if (interface_type == GI_INFO_TYPE_STRUCT &&
+                       g_struct_info_is_foreign((GIStructInfo*)interface_info)) {
+                return gjs_struct_foreign_convert_from_g_argument(context, value_p, type_info, arg);
             }
 
             /* Everything else is a pointer type, NULL is the easy case */
@@ -1635,7 +1643,7 @@ gjs_g_arg_release_internal(JSContext  *context,
                            GArgument  *arg)
 {
     JSBool failed;
-    
+
     g_assert(transfer != GI_TRANSFER_NOTHING);
 
     failed = JS_FALSE;
@@ -1680,6 +1688,11 @@ gjs_g_arg_release_internal(JSContext  *context,
 
             interface_type = g_base_info_get_type(interface_info);
 
+            if (interface_type == GI_INFO_TYPE_STRUCT &&
+                g_struct_info_is_foreign((GIStructInfo*)interface_info))
+                return gjs_struct_foreign_release_g_argument(context,
+                        transfer, type_info, arg);
+
             if (interface_type == GI_INFO_TYPE_ENUM || interface_type == GI_INFO_TYPE_FLAGS)
                 goto out;
 
diff --git a/gi/foreign.c b/gi/foreign.c
new file mode 100644
index 0000000..a64efb4
--- /dev/null
+++ b/gi/foreign.c
@@ -0,0 +1,195 @@
+/* -*- 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 "foreign.h"
+
+static struct {
+    char *namespace;
+    char *module; // relative to "imports."
+    gboolean loaded;
+} foreign_modules[] = {
+    { "cairo", "cairo", FALSE },
+    { NULL }
+};
+
+static GHashTable* foreign_structs_table = NULL;
+
+static GHashTable*
+get_foreign_structs(void)
+{
+    // FIXME: look into hasing on GITypeInfo instead.
+    if (!foreign_structs_table) {
+        foreign_structs_table = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                     (GDestroyNotify)g_free,
+                                     NULL);
+    }
+
+    return foreign_structs_table;
+}
+
+static JSBool
+gjs_foreign_load_foreign_module(JSContext *context,
+                                const gchar *namespace)
+{
+    int i;
+
+    for (i = 0; foreign_modules[i].namespace; ++i) {
+        int code;
+        GError *error = NULL;
+        char *script;
+
+        if (strcmp(namespace, foreign_modules[i].namespace) != 0)
+            continue;
+
+        if (foreign_modules[i].loaded) {
+            return JS_TRUE;
+        }
+
+        // FIXME: Find a way to check if a module is imported
+        //        and only execute this statement if isn't
+        script = g_strdup_printf("imports.%s;", namespace);
+        if (!gjs_context_eval(JS_GetContextPrivate(context), script, strlen(script),
+                              "<internal>", &code,
+                              &error)) {
+            g_printerr("ERROR: %s\n", error->message);
+            g_free(script);
+            g_error_free(error);
+            return JS_FALSE;
+        }
+        g_free(script);
+        foreign_modules[i].loaded = TRUE;
+    }
+
+    return JS_TRUE;
+}
+
+JSBool
+gjs_struct_foreign_register(const char *namespace,
+                            const char *type_name,
+                            GjsForeignInfo *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);
+    g_hash_table_insert(get_foreign_structs(), canonical_name, info);
+    return JS_TRUE;
+}
+
+static GjsForeignInfo *
+gjs_struct_foreign_lookup(JSContext  *context,
+                          GITypeInfo *type_info)
+{
+    GIBaseInfo *base_info;
+    GjsForeignInfo *retval = NULL;
+    GHashTable *hash_table;
+
+    base_info = g_type_info_get_interface(type_info);
+    if (base_info) {
+        char *key = g_strdup_printf("%s.%s", g_base_info_get_namespace(base_info),
+                                    g_base_info_get_name(base_info));
+        hash_table = get_foreign_structs();
+        retval = (GjsForeignInfo*)g_hash_table_lookup(hash_table, key);
+        if (!retval) {
+            if (gjs_foreign_load_foreign_module(context, g_base_info_get_namespace(base_info))) {
+                retval = (GjsForeignInfo*)g_hash_table_lookup(hash_table, key);
+            }
+        }
+        g_base_info_unref(base_info);
+        g_free(key);
+    }
+    return retval;
+}
+
+JSBool
+gjs_struct_foreign_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)
+{
+    GjsForeignInfo *foreign;
+
+    foreign = gjs_struct_foreign_lookup(context, type_info);
+    if (!foreign)
+        return JS_FALSE;
+
+    if (!foreign->to_func(context, value, type_info, arg_name,
+                           argument_type, transfer, may_be_null, arg))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
+JSBool
+gjs_struct_foreign_convert_from_g_argument(JSContext  *context,
+                                           jsval      *value_p,
+                                           GITypeInfo *type_info,
+                                           GArgument  *arg)
+{
+    GjsForeignInfo *foreign;
+
+    foreign = gjs_struct_foreign_lookup(context, type_info);
+    if (!foreign)
+        return JS_FALSE;
+
+    if (!foreign->from_func(context, value_p, type_info, arg))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
+JSBool
+gjs_struct_foreign_release_g_argument(JSContext  *context,
+                                      GITransfer  transfer,
+                                      GITypeInfo *type_info,
+                                      GArgument  *arg)
+{
+    GjsForeignInfo *foreign;
+
+    foreign = gjs_struct_foreign_lookup(context, type_info);
+    if (!foreign)
+        return JS_FALSE;
+
+    if (!foreign->release_func)
+        return JS_TRUE;
+
+    if (!foreign->release_func(context, transfer, type_info, arg))
+        return JS_FALSE;
+
+    return JS_TRUE;
+}
+
diff --git a/gi/foreign.h b/gi/foreign.h
new file mode 100644
index 0000000..3611590
--- /dev/null
+++ b/gi/foreign.h
@@ -0,0 +1,76 @@
+/* -*- 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;
+} GjsForeignInfo;
+
+JSBool  gjs_struct_foreign_register                (const char         *namespace,
+                                                    const char         *type_name,
+                                                    GjsForeignInfo *info);
+
+JSBool  gjs_struct_foreign_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_struct_foreign_convert_from_g_argument (JSContext          *context,
+                                                    jsval              *value_p,
+                                                    GITypeInfo         *type_info,
+                                                    GArgument          *arg);
+JSBool  gjs_struct_foreign_release_g_argument      (JSContext          *context,
+                                                    GITransfer          transfer,
+                                                    GITypeInfo         *type_info,
+                                                    GArgument          *arg);
+
+#endif /* __GJS_OVERRIDE_H__ */
diff --git a/gi/repo.c b/gi/repo.c
index 747161f..4f073ba 100644
--- a/gi/repo.c
+++ b/gi/repo.c
@@ -31,6 +31,7 @@
 #include "union.h"
 #include "enumeration.h"
 #include "arg.h"
+#include "foreign.h"
 
 #include <gjs/mem.h>
 



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