[template-glib] eval: add GType.is_a(GType) builtin



commit 615ed73a63caf7d5259f944a87cfb24aa009bb35
Author: Christian Hergert <chergert redhat com>
Date:   Thu May 5 16:22:25 2022 -0700

    eval: add GType.is_a(GType) builtin
    
    This allows you to do handy things like check types with inheritance such
    as the following:
    
     typeof(widget).is_a(typeof(Gtk.Native))

 src/tmpl-expr-eval.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 tests/test1.script   |  6 ++++++
 2 files changed, 51 insertions(+)
---
diff --git a/src/tmpl-expr-eval.c b/src/tmpl-expr-eval.c
index d39e060..a5244e9 100644
--- a/src/tmpl-expr-eval.c
+++ b/src/tmpl-expr-eval.c
@@ -929,6 +929,51 @@ tmpl_expr_gi_call_eval (TmplExprGiCall  *node,
       goto cleanup;
     }
 
+  if (G_VALUE_HOLDS_GTYPE (&left))
+    {
+      if (FALSE) {}
+      else if (g_str_equal (node->name, "is_a"))
+        {
+          if (node->params != NULL)
+            {
+              GValue param1 = G_VALUE_INIT;
+
+              if (!tmpl_expr_eval_internal (node->params, scope, &param1, error))
+                goto cleanup;
+
+              if (!G_VALUE_HOLDS_GTYPE (&param1))
+                {
+                  g_set_error (error,
+                               TMPL_ERROR,
+                               TMPL_ERROR_TYPE_MISMATCH,
+                               "%s is not a GType",
+                               G_VALUE_TYPE_NAME (&param1));
+                  TMPL_CLEAR_VALUE (&param1);
+                  goto cleanup;
+                }
+
+              g_value_init (return_value, G_TYPE_BOOLEAN);
+              g_value_set_boolean (return_value,
+                                   g_type_is_a (g_value_get_gtype (&left),
+                                                g_value_get_gtype (&param1)));
+
+              TMPL_CLEAR_VALUE (&param1);
+
+              ret = TRUE;
+
+              goto cleanup;
+            }
+        }
+
+      g_set_error (error,
+                   TMPL_ERROR,
+                   TMPL_ERROR_GI_FAILURE,
+                   "No such method %s of GType",
+                   node->name);
+
+      goto cleanup;
+    }
+
   repository = g_irepository_get_default ();
 
   if (G_VALUE_HOLDS (&left, TMPL_TYPE_TYPELIB) &&
diff --git a/tests/test1.script b/tests/test1.script
index e079290..0d50adc 100644
--- a/tests/test1.script
+++ b/tests/test1.script
@@ -59,6 +59,10 @@ assert(myfunc() == 27);
 # test out construction
 group = Gio.SimpleActionGroup.new()
 assert(group)
+assert(group != null)
+assert(typeof(group).is_a(typeof(Gio.ActionGroup)))
+assert(typeof(group).is_a(typeof(Gio.ActionMap)))
+assert(!typeof(group).is_a(typeof(Gio.Action)))
 
 t1 = typeof(group)
 t2 = typeof(Gio.SimpleActionGroup)
@@ -76,5 +80,7 @@ assert(action != null)
 assert(typeof(action) != null)
 assert(typeof(action) == typeof(Gio.SimpleAction))
 assert(typeof(action) != typeof(Gio.SimpleActionGroup))
+assert(typeof(action).is_a(typeof(Gio.Action)))
+assert(!typeof(action).is_a(typeof(Gio.ActionMap)))
 
 1234;


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