[gjs] enumeration: add support for static methods



commit 58798198e83f0ee1925a9adee70cc5f6b0cdd8cb
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Tue Feb 25 14:37:38 2014 +0100

    enumeration: add support for static methods
    
    gobject-introspection has supported these for a while, and it
    costs nothing for us to support them as well. In addition, it
    makes a lot simpler to have documentation (because we can simply
    skip all the backward compatibility functions)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=725143

 gi/enumeration.cpp                        |   36 +++++++++++++++++++++++++++++
 gi/enumeration.h                          |    3 ++
 gi/gerror.cpp                             |    1 +
 installed-tests/js/testEverythingBasic.js |    3 ++
 4 files changed, 43 insertions(+), 0 deletions(-)
---
diff --git a/gi/enumeration.cpp b/gi/enumeration.cpp
index 0fb0132..598a11a 100644
--- a/gi/enumeration.cpp
+++ b/gi/enumeration.cpp
@@ -29,6 +29,7 @@
 #include <gjs/compat.h>
 #include "repo.h"
 #include "gtype.h"
+#include "function.h"
 
 #include <util/log.h>
 
@@ -139,6 +140,40 @@ gjs_define_enum_values(JSContext    *context,
     return JS_TRUE;
 }
 
+JSBool
+gjs_define_enum_static_methods(JSContext    *context,
+                               JSObject     *constructor,
+                               GIEnumInfo   *enum_info)
+{
+    int i, n_methods;
+
+    n_methods = g_enum_info_get_n_methods(enum_info);
+
+    for (i = 0; i < n_methods; i++) {
+        GIFunctionInfo *meth_info;
+        GIFunctionInfoFlags flags;
+
+        meth_info = g_enum_info_get_method(enum_info, i);
+        flags = g_function_info_get_flags(meth_info);
+
+        g_warn_if_fail(!(flags & GI_FUNCTION_IS_METHOD));
+        /* Anything that isn't a method we put on the prototype of the
+         * constructor.  This includes <constructor> introspection
+         * methods, as well as the forthcoming "static methods"
+         * support.  We may want to change this to use
+         * GI_FUNCTION_IS_CONSTRUCTOR and GI_FUNCTION_IS_STATIC or the
+         * like in the near future.
+         */
+        if (!(flags & GI_FUNCTION_IS_METHOD)) {
+            gjs_define_function(context, constructor, G_TYPE_NONE,
+                                (GICallableInfo *)meth_info);
+        }
+
+        g_base_info_unref((GIBaseInfo*) meth_info);
+    }
+
+    return JS_TRUE;
+}
 
 JSBool
 gjs_define_enumeration(JSContext    *context,
@@ -172,6 +207,7 @@ gjs_define_enumeration(JSContext    *context,
 
     if (!gjs_define_enum_values(context, enum_obj, info))
         return JS_FALSE;
+    gjs_define_enum_static_methods (context, enum_obj, info);
 
     gjs_debug(GJS_DEBUG_GENUM,
               "Defining %s.%s as %p",
diff --git a/gi/enumeration.h b/gi/enumeration.h
index afb3431..89f6d46 100644
--- a/gi/enumeration.h
+++ b/gi/enumeration.h
@@ -35,6 +35,9 @@ G_BEGIN_DECLS
 JSBool    gjs_define_enum_values       (JSContext    *context,
                                         JSObject     *in_object,
                                         GIEnumInfo   *info);
+JSBool    gjs_define_enum_static_methods(JSContext    *context,
+                                         JSObject     *constructor,
+                                         GIEnumInfo   *enum_info);
 JSBool    gjs_define_enumeration       (JSContext    *context,
                                         JSObject     *in_object,
                                         GIEnumInfo   *info);
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 0856955..6174e04 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -404,6 +404,7 @@ gjs_define_error_class(JSContext    *context,
               constructor_name, prototype, JS_GetClass(prototype), in_object);
 
     gjs_define_enum_values(context, constructor, priv->info);
+    gjs_define_enum_static_methods(context, constructor, priv->info);
 }
 
 static GIEnumInfo *
diff --git a/installed-tests/js/testEverythingBasic.js b/installed-tests/js/testEverythingBasic.js
index 7305592..7043d63 100644
--- a/installed-tests/js/testEverythingBasic.js
+++ b/installed-tests/js/testEverythingBasic.js
@@ -409,6 +409,9 @@ function testEnumParam() {
 
     JSUnit.assertNotUndefined("Enum $gtype", Everything.TestEnumUnsigned.$gtype);
     JSUnit.assertTrue("Enum $gtype enumerable", "$gtype" in Everything.TestEnumUnsigned);
+
+    JSUnit.assertEquals(Number(Everything.TestError), Everything.TestError.quark());
+    JSUnit.assertEquals('value4', Everything.TestEnum.param(Everything.TestEnum.VALUE4));
 }
 
 function testSignal() {


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