[gjs] arg: add support for arrays of flat structures (to JS)



commit 967d6962943cc5101eb9347fe960d605e92493b9
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Wed Jul 31 11:15:23 2013 +0200

    arg: add support for arrays of flat structures (to JS)
    
    Add support for marshalling from C to JS arrays not of pointer
    to structures but of actual structures, as for example used
    by gdk_keymap_get_entries_for_keyval().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=704842

 gi/arg.cpp                    |   38 +++++++++++++++++++++++++++++++++++++-
 installed-tests/js/testGtk.js |    9 +++++++++
 2 files changed, 46 insertions(+), 1 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 6fbcaa5..7cb92bd 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -255,6 +255,11 @@ type_needs_out_release(GITypeInfo *type_info,
             needs_release = FALSE;
             break;
 
+        case GI_INFO_TYPE_STRUCT:
+        case GI_INFO_TYPE_UNION:
+            needs_release = g_type_info_is_pointer (type_info);
+            break;
+
         default:
             needs_release = TRUE;
         }
@@ -2157,11 +2162,42 @@ gjs_array_from_carray_internal (JSContext  *context,
         case GI_TYPE_TAG_DOUBLE:
           ITERATE(double);
           break;
+        case GI_TYPE_TAG_INTERFACE: {
+          GIBaseInfo *interface_info;
+          GIInfoType info_type;
+
+          interface_info = g_type_info_get_interface (param_info);
+          info_type = g_base_info_get_type (interface_info);
+
+          if ((info_type == GI_INFO_TYPE_STRUCT ||
+               info_type == GI_INFO_TYPE_UNION) &&
+              !g_type_info_is_pointer (param_info)) {
+              gsize struct_size;
+
+              if (info_type == GI_INFO_TYPE_UNION)
+                  struct_size = g_union_info_get_size ((GIUnionInfo*)interface_info);
+              else
+                  struct_size = g_struct_info_get_size ((GIStructInfo*)interface_info);
+
+              for (i = 0; i < length; i++) {
+                  arg.v_pointer = ((char*)array) + struct_size;
+
+                  if (!gjs_value_from_g_argument(context, &elem, param_info, &arg, TRUE))
+                      goto finally;
+                  if (!JS_DefineElement(context, obj, i, elem, NULL, NULL,
+                                        JSPROP_ENUMERATE))
+                      goto finally;
+              }
+
+              break;
+          }
+
+          /* fallthrough */
+        }
         case GI_TYPE_TAG_GTYPE:
         case GI_TYPE_TAG_UTF8:
         case GI_TYPE_TAG_FILENAME:
         case GI_TYPE_TAG_ARRAY:
-        case GI_TYPE_TAG_INTERFACE:
         case GI_TYPE_TAG_GLIST:
         case GI_TYPE_TAG_GSLIST:
         case GI_TYPE_TAG_GHASH:
diff --git a/installed-tests/js/testGtk.js b/installed-tests/js/testGtk.js
index b324653..fa32d19 100755
--- a/installed-tests/js/testGtk.js
+++ b/installed-tests/js/testGtk.js
@@ -1,6 +1,7 @@
 #!/usr/bin/env gjs
 
 const ByteArray = imports.byteArray;
+const Gdk = imports.gi.Gdk;
 const Gio = imports.gi.Gio;
 const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
@@ -92,4 +93,12 @@ function testGtk() {
     validateTemplate(new MyComplexGtkSubclassFromResource());
 }
 
+// https://bugzilla.gnome.org/show_bug.cgi?id=704842
+function testGdkKeymap() {
+    Gtk.init(null);
+
+    let keymap = Gdk.Keymap.get_default();
+    JSUnit.assertNotEquals(keymap.get_entries_for_keyval(Gdk.KEY_a), null);
+}
+
 JSUnit.gjstestRun(this, JSUnit.setUp, JSUnit.tearDown);


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