From 4683e3940363c0b825f3228f41e4f5c8dc555406 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 30 May 2025 13:56:46 -0700 Subject: [PATCH 1/2] Port to girepository-2.0 This is an initial port of lgi to use girepository-2.0 introduced in the GLib 2.80 release. If using 2.85, it will use the new gi_repository_dup_default() function to ensure the repository is using the same repository as applications and libraries such as libpeas. There is still some work to be done, as libpeas tests do not yet pass using this version of lgi. --- lgi/callable.c | 181 +++++++++++++------------- lgi/core.c | 26 ++-- lgi/gi.c | 273 ++++++++++++++++++++------------------- lgi/lgi.h | 4 +- lgi/marshal.c | 340 ++++++++++++++++++++++++------------------------- lgi/object.c | 43 ++++--- meson.build | 2 +- 7 files changed, 429 insertions(+), 440 deletions(-) diff --git a/lgi/callable.c b/lgi/callable.c index dd272ece..fb87d9bb 100644 --- a/lgi/callable.c +++ b/lgi/callable.c @@ -228,7 +228,7 @@ get_ffi_type(Param *param) return &ffi_type_pointer; case PARAM_KIND_ENUM: - return param->ti ? get_simple_ffi_type (g_type_info_get_tag (param->ti)) + return param->ti ? get_simple_ffi_type (gi_type_info_get_tag (param->ti)) : &ffi_type_sint; case PARAM_KIND_TI: @@ -236,26 +236,18 @@ get_ffi_type(Param *param) } /* In case of inout or out parameters, the type is always pointer. */ - GITypeTag tag = g_type_info_get_tag (param->ti); - ffi_type* ffi = g_type_info_is_pointer(param->ti) + GITypeTag tag = gi_type_info_get_tag (param->ti); + ffi_type* ffi = gi_type_info_is_pointer(param->ti) ? &ffi_type_pointer : get_simple_ffi_type (tag); if (ffi == NULL) { /* Something more complex. */ if (tag == GI_TYPE_TAG_INTERFACE) { - GIBaseInfo *ii = g_type_info_get_interface (param->ti); - switch (g_base_info_get_type (ii)) - { - case GI_INFO_TYPE_ENUM: - case GI_INFO_TYPE_FLAGS: - ffi = get_simple_ffi_type (g_enum_info_get_storage_type (ii)); - break; - - default: - break; - } - g_base_info_unref (ii); + GIBaseInfo *ii = gi_type_info_get_interface (param->ti); + if (GI_IS_ENUM_INFO (ii) || GI_IS_FLAGS_INFO (ii)) + ffi = get_simple_ffi_type (gi_enum_info_get_storage_type (GI_ENUM_INFO (ii))); + gi_base_info_unref (ii); } } @@ -267,12 +259,11 @@ get_ffi_type(Param *param) static void callable_mark_array_length (Callable *callable, GITypeInfo *ti) { - gint arg; - if (g_type_info_get_tag (ti) == GI_TYPE_TAG_ARRAY && - g_type_info_get_array_type (ti) == GI_ARRAY_TYPE_C) + if (gi_type_info_get_tag (ti) == GI_TYPE_TAG_ARRAY && + gi_type_info_get_array_type (ti) == GI_ARRAY_TYPE_C) { - arg = g_type_info_get_array_length (ti); - if (arg >= 0 && arg < callable->nargs) + guint arg; + if (gi_type_info_get_array_length_index (ti, &arg) && arg < callable->nargs) callable->params[arg].internal = TRUE; } } @@ -280,14 +271,8 @@ callable_mark_array_length (Callable *callable, GITypeInfo *ti) static void callable_param_init (Param *param) { - param->ti = NULL; - param->has_arg_info =FALSE; - param->internal = FALSE; - param->internal_user_data = FALSE; - param->n_closures = 0; - param->call_scoped_user_data = FALSE; + memset (param, 0, sizeof *param); param->kind = PARAM_KIND_TI; - param->repotype_index = 0; } static Callable * @@ -300,6 +285,7 @@ callable_allocate (lua_State *L, int nargs, ffi_type ***ffi_args) Callable *callable = lua_newuserdata (L, sizeof (Callable) + sizeof (ffi_type) * (nargs + 2) + sizeof (Param) * nargs); + memset (callable, 0, sizeof *callable); lua_pushlightuserdata (L, &callable_mt); lua_rawget (L, LUA_REGISTRYINDEX); lua_setmetatable (L, -2); @@ -336,11 +322,11 @@ callable_get_param (Callable *callable, gint n) if (!param->has_arg_info) { /* Ensure basic fields are initialized. */ - g_callable_info_load_arg (callable->info, n, ¶m->ai); + gi_callable_info_load_arg (callable->info, n, ¶m->ai); param->has_arg_info = TRUE; - param->ti = g_arg_info_get_type (¶m->ai); - param->dir = g_arg_info_get_direction (¶m->ai); - param->transfer = g_arg_info_get_ownership_transfer (¶m->ai); + param->ti = gi_arg_info_get_type_info (¶m->ai); + param->dir = gi_arg_info_get_direction (¶m->ai); + param->transfer = gi_arg_info_get_ownership_transfer (¶m->ai); } return param; } @@ -352,28 +338,28 @@ lgi_callable_create (lua_State *L, GICallableInfo *info, gpointer addr) Param *param, *data_param; ffi_type **ffi_arg, **ffi_args; ffi_type *ffi_retval; - gint nargs, argi, arg; + gint nargs, argi; /* Allocate Callable userdata. */ - nargs = g_callable_info_get_n_args (info); + nargs = gi_callable_info_get_n_args (info); callable = callable_allocate (L, nargs, &ffi_args); - callable->info = g_base_info_ref (info); + callable->info = GI_CALLABLE_INFO (gi_base_info_ref (info)); callable->address = addr; if (GI_IS_FUNCTION_INFO (info)) { /* Get FunctionInfo flags. */ const gchar* symbol; - gint flags = g_function_info_get_flags (info); + gint flags = gi_function_info_get_flags (GI_FUNCTION_INFO (info)); if ((flags & GI_FUNCTION_IS_METHOD) != 0 && (flags & GI_FUNCTION_IS_CONSTRUCTOR) == 0) callable->has_self = 1; - if ((flags & GI_FUNCTION_THROWS) != 0) + if (gi_callable_info_can_throw_gerror (GI_CALLABLE_INFO (info))) callable->throws = 1; /* Resolve symbol (function address). */ - symbol = g_function_info_get_symbol (info); - if (!g_typelib_symbol (g_base_info_get_typelib (info), symbol, - &callable->address)) + symbol = gi_function_info_get_symbol (GI_FUNCTION_INFO (info)); + if (!gi_typelib_symbol (gi_base_info_get_typelib (GI_BASE_INFO (info)), symbol, + &callable->address)) /* Fail with the error message. */ return luaL_error (L, "could not locate %s(%s): %s", lua_tostring (L, -3), symbol, g_module_error ()); @@ -384,9 +370,9 @@ lgi_callable_create (lua_State *L, GICallableInfo *info, gpointer addr) callable->has_self = 1; /* Process return value. */ - callable->retval.ti = g_callable_info_get_return_type (callable->info); + callable->retval.ti = gi_callable_info_get_return_type (callable->info); callable->retval.dir = GI_DIRECTION_OUT; - callable->retval.transfer = g_callable_info_get_caller_owns (callable->info); + callable->retval.transfer = gi_callable_info_get_caller_owns (callable->info); callable->retval.internal = FALSE; callable->retval.repotype_index = 0; ffi_retval = get_ffi_type (&callable->retval); @@ -400,30 +386,36 @@ lgi_callable_create (lua_State *L, GICallableInfo *info, gpointer addr) /* Process the rest of the arguments. */ for (argi = 0; argi < nargs; argi++, ffi_arg++) { + guint arg; + param = callable_get_param (callable, argi); *ffi_arg = (param->dir == GI_DIRECTION_IN) ? get_ffi_type (param) : &ffi_type_pointer; /* Mark closure-related user_data fields as internal. */ - arg = g_arg_info_get_closure (¶m->ai); - data_param = callable_get_param (callable, arg); - /* `arg` is defined also on callbacks, so check for invalid scope - to avoid setting the internal flag on them. */ - if (data_param != NULL && g_arg_info_get_scope (&data_param->ai) == GI_SCOPE_TYPE_INVALID) - { - data_param->internal = TRUE; - if (arg == argi) - data_param->internal_user_data = TRUE; - data_param->n_closures++; - if (g_arg_info_get_scope (¶m->ai) == GI_SCOPE_TYPE_CALL) - data_param->call_scoped_user_data = TRUE; - } + if (gi_arg_info_get_closure_index (¶m->ai, &arg)) + { + data_param = callable_get_param (callable, arg); + /* `arg` is defined also on callbacks, so check for invalid scope + to avoid setting the internal flag on them. */ + if (data_param != NULL && gi_arg_info_get_scope (&data_param->ai) == GI_SCOPE_TYPE_INVALID) + { + data_param->internal = TRUE; + if (arg == (guint)argi) + data_param->internal_user_data = TRUE; + data_param->n_closures++; + if (gi_arg_info_get_scope (¶m->ai) == GI_SCOPE_TYPE_CALL) + data_param->call_scoped_user_data = TRUE; + } + } /* Mark destroy_notify fields as internal. */ - arg = g_arg_info_get_destroy (¶m->ai); - data_param = callable_get_param (callable, arg); - if (data_param != NULL) - data_param->internal = TRUE; + if (gi_arg_info_get_destroy_index (¶m->ai, &arg)) + { + data_param = callable_get_param (callable, arg); + if (data_param != NULL) + data_param->internal = TRUE; + } /* Similarly for array length field. */ callable_mark_array_length (callable, param->ti); @@ -433,7 +425,7 @@ lgi_callable_create (lua_State *L, GICallableInfo *info, gpointer addr) signalize failure by returning nil instead of extra value). */ if (param->dir != GI_DIRECTION_IN - && g_type_info_get_tag (callable->retval.ti) == GI_TYPE_TAG_BOOLEAN) + && gi_type_info_get_tag (callable->retval.ti) == GI_TYPE_TAG_BOOLEAN) callable->ignore_retval = 1; } @@ -441,8 +433,8 @@ lgi_callable_create (lua_State *L, GICallableInfo *info, gpointer addr) crucial for lgi but is missing an array annotation in glib/gobject-introspection < 1.30. */ if (!GLIB_CHECK_VERSION (2, 30, 0) - && !strcmp (g_base_info_get_namespace (info), "GObject") - && !strcmp (g_base_info_get_name (info), "ClosureMarshal")) + && !strcmp (gi_base_info_get_namespace (GI_BASE_INFO (info)), "GObject") + && !strcmp (gi_base_info_get_name (GI_BASE_INFO (info)), "ClosureMarshal")) { callable->is_closure_marshal = 1; callable->params[2].internal = 1; @@ -457,7 +449,7 @@ lgi_callable_create (lua_State *L, GICallableInfo *info, gpointer addr) callable->has_self + nargs + callable->throws, ffi_retval, ffi_args) != FFI_OK) { - lua_concat (L, lgi_type_get_name (L, callable->info)); + lua_concat (L, lgi_type_get_name (L, GI_BASE_INFO (callable->info))); return luaL_error (L, "ffi_prep_cif for `%s' failed", lua_tostring (L, -1)); } @@ -529,7 +521,7 @@ callable_param_parse (lua_State *L, Param *param) /* This is actually an enum, and 'type' field contains numeric type for this enum. Store it into the ti. */ GITypeInfo **ti = luaL_checkudata (L, -1, LGI_GI_INFO); - param->ti = g_base_info_ref (*ti); + param->ti = GI_TYPE_INFO (gi_base_info_ref (*ti)); } lua_pop (L, 1); @@ -546,7 +538,7 @@ callable_param_parse (lua_State *L, Param *param) { /* Expect typeinfo. */ GITypeInfo **pti = lua_touserdata (L, -1); - param->ti = g_base_info_ref (*pti); + param->ti = GI_TYPE_INFO (gi_base_info_ref (*pti)); param->kind = kind; lua_pop (L, 1); } @@ -657,8 +649,8 @@ callable_get (lua_State *L, int narg) static void callable_param_destroy (Param *param) { - if (param->ti) - g_base_info_unref (param->ti); + g_clear_pointer (¶m->ti, gi_base_info_unref); + gi_base_info_clear (¶m->ai); } static int @@ -669,13 +661,13 @@ callable_gc (lua_State *L) /* Unref embedded 'info' field. */ Callable *callable = callable_get (L, 1); if (callable->info) - g_base_info_unref (callable->info); + gi_base_info_unref (callable->info); /* Destroy all params. */ for (i = 0; i < callable->nargs; i++) callable_param_destroy (&callable->params[i]); - callable_param_destroy (&callable->retval ); + callable_param_destroy (&callable->retval); /* Unset the metatable / make the callable unusable */ lua_pushnil (L); @@ -710,7 +702,7 @@ callable_describe (lua_State *L, Callable *callable, FfiClosure *closure) (GI_IS_SIGNAL_INFO (callable->info) ? "sig" : (GI_IS_VFUNC_INFO (callable->info) ? "vfn" : "cbk"))), lua_tostring (L, -1)); - lua_concat (L, lgi_type_get_name (L, callable->info) + 1); + lua_concat (L, lgi_type_get_name (L, GI_BASE_INFO (callable->info)) + 1); } else { @@ -852,12 +844,11 @@ callable_call (lua_State *L) nret = 0; if (callable->has_self) { - GIBaseInfo *parent = g_base_info_get_container (callable->info); - GIInfoType type = g_base_info_get_type (parent); - if (type == GI_INFO_TYPE_OBJECT || type == GI_INFO_TYPE_INTERFACE) + GIBaseInfo *parent = gi_base_info_get_container (GI_BASE_INFO (callable->info)); + if (GI_IS_OBJECT_INFO (parent) || GI_IS_INTERFACE_INFO (parent)) { args[0].v_pointer = - lgi_object_2c (L, 2, g_registered_type_info_get_g_type (parent), + lgi_object_2c (L, 2, gi_registered_type_info_get_g_type (GI_REGISTERED_TYPE_INFO (parent)), FALSE, FALSE, FALSE); nret++; } @@ -912,7 +903,7 @@ callable_call (lua_State *L) 1, callable, ffi_args); /* Special handling for out/caller-alloc structures; we have to manually pre-create them and store them on the stack. */ - else if (callable->info && g_arg_info_is_caller_allocates (¶m->ai) + else if (callable->info && gi_arg_info_is_caller_allocates (¶m->ai) && lgi_marshal_2c_caller_alloc (L, param->ti, &args[argi], 0)) { /* Even when marked as OUT, caller-allocates arguments @@ -957,8 +948,8 @@ callable_call (lua_State *L) nret = 0; if (!callable->ignore_retval && (callable->retval.ti == NULL - || (g_type_info_get_tag (callable->retval.ti) != GI_TYPE_TAG_VOID - || g_type_info_is_pointer (callable->retval.ti)))) + || (gi_type_info_get_tag (callable->retval.ti) != GI_TYPE_TAG_VOID + || gi_type_info_is_pointer (callable->retval.ti)))) { callable_param_2lua (L, &callable->retval, &retval, LGI_PARENT_IS_RETVAL, 1, callable, ffi_args); @@ -996,7 +987,7 @@ callable_call (lua_State *L) for (i = 0; i < callable->nargs; i++, param++) if (!param->internal && param->dir != GI_DIRECTION_IN) { - if (callable->info && g_arg_info_is_caller_allocates (¶m->ai) + if (callable->info && gi_arg_info_is_caller_allocates (¶m->ai) && lgi_marshal_2c_caller_alloc (L, param->ti, NULL, -caller_allocated - nret)) /* Caller allocated parameter is already marshalled and @@ -1041,7 +1032,7 @@ callable_index (lua_State *L) Callable *callable = callable_get (L, 1); const gchar *verb = lua_tostring (L, 2); if (g_strcmp0 (verb, "info") == 0) - return lgi_gi_info_new (L, g_base_info_ref (callable->info)); + return lgi_gi_info_new (L, gi_base_info_ref (callable->info)); else if (g_strcmp0 (verb, "params") == 0) { int index = 1, i; @@ -1062,14 +1053,14 @@ callable_index (lua_State *L) /* Add name. */ if (param->has_arg_info) { - lua_pushstring (L, g_base_info_get_name (¶m->ai)); + lua_pushstring (L, gi_base_info_get_name (GI_BASE_INFO (¶m->ai))); lua_setfield (L, -2, "name"); } /* Add typeinfo. */ if (param->ti) { - lgi_gi_info_new (L, g_base_info_ref (param->ti)); + lgi_gi_info_new (L, gi_base_info_ref (param->ti)); lua_setfield (L, -2, "typeinfo"); } @@ -1127,13 +1118,12 @@ marshal_arguments (lua_State *L, void **args, int callable_index, Callable *call /* Marshall 'self' argument, if it is present. */ if (callable->has_self) { - GIBaseInfo *parent = g_base_info_get_container (callable->info); - GIInfoType type = g_base_info_get_type (parent); + GIBaseInfo *parent = gi_base_info_get_container (GI_BASE_INFO (callable->info)); gpointer addr = ((GIArgument*) args[0])->v_pointer; npos++; - if (type == GI_INFO_TYPE_OBJECT || type == GI_INFO_TYPE_INTERFACE) + if (GI_IS_OBJECT_INFO (parent) || GI_IS_INTERFACE_INFO (parent)) lgi_object_2lua (L, addr, FALSE, FALSE); - else if (type == GI_INFO_TYPE_STRUCT || type == GI_INFO_TYPE_UNION) + else if (GI_IS_STRUCT_INFO (parent) || GI_IS_UNION_INFO (parent)) { lgi_type_get_repotype (L, G_TYPE_INVALID, parent); lgi_record_2lua (L, addr, FALSE, 0); @@ -1199,9 +1189,9 @@ marshal_return_values (lua_State *L, void *ret, void **args, int callable_index, lua_settop(L, lua_gettop (L) + callable->has_self + callable->nargs + 1); /* Marshal return value from Lua. */ - tag = g_type_info_get_tag (callable->retval.ti); + tag = gi_type_info_get_tag (callable->retval.ti); if (tag != GI_TYPE_TAG_VOID - || g_type_info_is_pointer (callable->retval.ti)) + || gi_type_info_is_pointer (callable->retval.ti)) { if (callable->ignore_retval) /* Return value should be ignored on Lua side, so we have @@ -1217,8 +1207,8 @@ marshal_return_values (lua_State *L, void *ret, void **args, int callable_index, if (to_pop != 0) { g_warning ("cbk `%s.%s': return (transfer none) %d, unsafe!", - g_base_info_get_namespace (callable->info), - g_base_info_get_name (callable->info), to_pop); + gi_base_info_get_namespace (GI_BASE_INFO (callable->info)), + gi_base_info_get_name (GI_BASE_INFO (callable->info)), to_pop); lua_pop (L, to_pop); } @@ -1233,8 +1223,8 @@ marshal_return_values (lua_State *L, void *ret, void **args, int callable_index, { gpointer *arg = args[i + callable->has_self]; gboolean caller_alloc = - callable->info && g_arg_info_is_caller_allocates (¶m->ai) - && g_type_info_get_tag (param->ti) == GI_TYPE_TAG_INTERFACE; + callable->info && gi_arg_info_is_caller_allocates (¶m->ai) + && gi_type_info_get_tag (param->ti) == GI_TYPE_TAG_INTERFACE; to_pop = callable_param_2c (L, param, npos, caller_alloc ? LGI_PARENT_CALLER_ALLOC : 0, *arg, callable_index, callable, @@ -1242,9 +1232,9 @@ marshal_return_values (lua_State *L, void *ret, void **args, int callable_index, if (to_pop != 0) { g_warning ("cbk %s.%s: arg `%s' (transfer none) %d, unsafe!", - g_base_info_get_namespace (callable->info), - g_base_info_get_name (callable->info), - g_base_info_get_name (¶m->ai), to_pop); + gi_base_info_get_namespace (GI_BASE_INFO (callable->info)), + gi_base_info_get_name (GI_BASE_INFO (callable->info)), + gi_base_info_get_name (GI_BASE_INFO (¶m->ai)), to_pop); lua_pop (L, to_pop); } @@ -1272,7 +1262,7 @@ marshal_return_error (lua_State *L, void *ret, void **args, Callable *callable) } /* Such function should usually return FALSE, so do it. */ - if (g_type_info_get_tag (callable->retval.ti) == GI_TYPE_TAG_BOOLEAN) + if (gi_type_info_get_tag (callable->retval.ti) == GI_TYPE_TAG_BOOLEAN) *(gboolean *) ret = FALSE; } @@ -1537,7 +1527,7 @@ lgi_closure_create (lua_State *L, gpointer user_data, if (ffi_prep_closure_loc (&closure->ffi_closure, &callable->cif, closure_callback, closure, call_addr) != FFI_OK) { - lua_concat (L, lgi_type_get_name (L, callable->info)); + lua_concat (L, lgi_type_get_name (L, GI_BASE_INFO (callable->info))); luaL_error (L, "failed to prepare closure for `%'", lua_tostring (L, -1)); return NULL; } @@ -1589,3 +1579,4 @@ lgi_callable_init (lua_State *L) luaL_register (L, NULL, callable_api_reg); lua_setfield (L, -2, "callable"); } + diff --git a/lgi/core.c b/lgi/core.c index 44070bf2..0b84fa62 100644 --- a/lgi/core.c +++ b/lgi/core.c @@ -124,23 +124,23 @@ lgi_type_get_name (lua_State *L, GIBaseInfo *info) { GSList *list = NULL, *i; int n = 1; - lua_pushstring (L, g_base_info_get_namespace (info)); + lua_pushstring (L, gi_base_info_get_namespace (info)); - if (g_base_info_get_type (info) == GI_INFO_TYPE_CALLBACK) + if (GI_IS_CALLBACK_INFO (info)) /* Avoid duplicate name for callbacks. */ - info = g_base_info_get_container (info); + info = gi_base_info_get_container (info); /* Add names on the whole path, but in reverse order. */ - for (; info != NULL; info = g_base_info_get_container (info)) + for (; info != NULL; info = gi_base_info_get_container (info)) if (!GI_IS_TYPE_INFO (info)) list = g_slist_prepend (list, info); for (i = list; i != NULL; i = g_slist_next (i)) { - if (g_base_info_get_type (i->data) != GI_INFO_TYPE_TYPE) + if (GI_IS_TYPE_INFO (i->data)) { lua_pushstring (L, "."); - lua_pushstring (L, g_base_info_get_name (i->data)); + lua_pushstring (L, gi_base_info_get_name (i->data)); n += 2; } } @@ -161,7 +161,7 @@ lgi_type_get_repotype (lua_State *L, GType gtype, GIBaseInfo *info) /* Prepare gtype, if not given directly. */ if (gtype == G_TYPE_INVALID && info && GI_IS_REGISTERED_TYPE_INFO (info)) { - gtype = g_registered_type_info_get_g_type (info); + gtype = gi_registered_type_info_get_g_type (GI_REGISTERED_TYPE_INFO (info)); if (gtype == G_TYPE_NONE) gtype = G_TYPE_INVALID; } @@ -182,7 +182,7 @@ lgi_type_get_repotype (lua_State *L, GType gtype, GIBaseInfo *info) lazy-loaded repo tables are not loaded yet. */ if (!info && gtype != G_TYPE_INVALID) { - info = g_irepository_find_by_gtype (NULL, gtype); + info = gi_repository_find_by_gtype (lgi_gi_get_repository (), gtype); lgi_gi_info_new (L, info); } else @@ -193,8 +193,8 @@ lgi_type_get_repotype (lua_State *L, GType gtype, GIBaseInfo *info) { lua_pushlightuserdata (L, &repo); lua_rawget (L, LUA_REGISTRYINDEX); - lua_getfield (L, -1, g_base_info_get_namespace (info)); - lua_getfield (L, -1, g_base_info_get_name (info)); + lua_getfield (L, -1, gi_base_info_get_namespace (info)); + lua_getfield (L, -1, gi_base_info_get_name (info)); lua_replace (L, -5); lua_pop (L, 3); } @@ -296,9 +296,9 @@ core_constant (lua_State *L) /* Get typeinfo of the constant. */ GIArgument val; GIConstantInfo *ci = *(GIConstantInfo **) luaL_checkudata (L, 1, LGI_GI_INFO); - GITypeInfo *ti = g_constant_info_get_type (ci); - lgi_gi_info_new (L, ti); - g_constant_info_get_value (ci, &val); + GITypeInfo *ti = gi_constant_info_get_type_info (ci); + lgi_gi_info_new (L, GI_BASE_INFO (ti)); + gi_constant_info_get_value (ci, &val); lgi_marshal_2lua (L, ti, NULL, GI_DIRECTION_IN, GI_TRANSFER_NOTHING, &val, 0, NULL, NULL); return 1; diff --git a/lgi/gi.c b/lgi/gi.c index e06d8253..dab3a6b8 100644 --- a/lgi/gi.c +++ b/lgi/gi.c @@ -13,6 +13,21 @@ typedef GIBaseInfo *(* InfosItemGet)(GIBaseInfo* info, gint item); +GIRepository * +lgi_gi_get_repository (void) +{ + static GIRepository *repository; + + if (repository == NULL) +#if GLIB_CHECK_VERSION(2, 85, 0) + repository = gi_repository_dup_default (); +#else + repository = gi_repository_new (); +#endif + + return repository; +} + /* Creates new instance of info from given GIBaseInfo pointer. */ int lgi_gi_info_new (lua_State *L, GIBaseInfo *info) @@ -21,18 +36,12 @@ lgi_gi_info_new (lua_State *L, GIBaseInfo *info) { GIBaseInfo **ud_info; - if (g_base_info_get_type (info) == GI_INFO_TYPE_INVALID) - { - g_base_info_unref (info); - lua_pushnil (L); - } - else - { - ud_info = lua_newuserdata (L, sizeof (info)); - *ud_info = info; - luaL_getmetatable (L, LGI_GI_INFO); - lua_setmetatable (L, -2); - } + g_assert (GI_IS_BASE_INFO (info)); + + ud_info = lua_newuserdata (L, sizeof (info)); + *ud_info = info; + luaL_getmetatable (L, LGI_GI_INFO); + lua_setmetatable (L, -2); } else lua_pushnil (L); @@ -50,8 +59,9 @@ lgi_gi_load_function (lua_State *L, int typetable, const char *name) lua_getfield (L, typetable, name); info = lgi_udata_test (L, -1, LGI_GI_INFO); if (info && GI_IS_FUNCTION_INFO (*info)) - g_typelib_symbol (g_base_info_get_typelib (*info), - g_function_info_get_symbol (*info), &symbol); + gi_typelib_symbol (gi_base_info_get_typelib (*info), + gi_function_info_get_symbol (GI_FUNCTION_INFO (*info)), + &symbol); else if (lua_islightuserdata (L, -1)) symbol = lua_touserdata (L, -1); lua_pop (L, 1); @@ -93,10 +103,10 @@ infos_index (lua_State *L) for (n = 0; n < infos->count; n++) { GIBaseInfo *info = infos->item_get (infos->info, n); - if (strcmp (g_base_info_get_name (info), name) == 0) + if (strcmp (gi_base_info_get_name (info), name) == 0) return lgi_gi_info_new (L, info); - g_base_info_unref (info); + gi_base_info_unref (info); } lua_pushnil (L); @@ -108,7 +118,7 @@ static int infos_gc (lua_State *L) { Infos *infos = luaL_checkudata (L, 1, LGI_GI_INFOS); - g_base_info_unref (infos->info); + gi_base_info_unref (infos->info); /* Unset the metatable / make the infos unusable */ lua_pushnil (L); @@ -123,7 +133,7 @@ infos_new (lua_State *L, GIBaseInfo *info, gint count, InfosItemGet item_get) Infos *infos = lua_newuserdata (L, sizeof (Infos)); luaL_getmetatable (L, LGI_GI_INFOS); lua_setmetatable (L, -2); - infos->info = g_base_info_ref (info); + infos->info = gi_base_info_ref (info); infos->count = count; infos->item_get = item_get; return 1; @@ -159,49 +169,47 @@ info_index (lua_State *L) GIBaseInfo **info = luaL_checkudata (L, 1, LGI_GI_INFO); const gchar *prop = luaL_checkstring (L, 2); -#define INFOS(n1, n2) \ - else if (strcmp (prop, #n2 "s") == 0) \ - return infos_new (L, *info, \ - g_ ## n1 ## _info_get_n_ ## n2 ## s (*info), \ - g_ ## n1 ## _info_get_ ## n2); +#define INFOS(n1, n2) \ + else if (strcmp (prop, #n2 "s") == 0) \ + return infos_new (L, GI_BASE_INFO (*info), \ + gi_ ## n1 ## _info_get_n_ ## n2 ## s ((gpointer)*info), \ + (InfosItemGet) gi_ ## n1 ## _info_get_ ## n2); -#define INFOS2(n1, n2, n3) \ - else if (strcmp (prop, #n3) == 0) \ - return infos_new (L, *info, \ - g_ ## n1 ## _info_get_n_ ## n3 (*info), \ - g_ ## n1 ## _info_get_ ## n2); +#define INFOS2(n1, n2, n3) \ + else if (strcmp (prop, #n3) == 0) \ + return infos_new (L, GI_BASE_INFO (*info), \ + gi_ ## n1 ## _info_get_n_ ## n3 ((gpointer)*info), \ + (InfosItemGet) gi_ ## n1 ## _info_get_ ## n2); if (strcmp (prop, "type") == 0) { - switch (g_base_info_get_type (*info)) - { #define H(n1, n2) \ - case GI_INFO_TYPE_ ## n1: \ - lua_pushstring (L, #n2); \ - return 1; - - H(FUNCTION, function) - H(CALLBACK, callback) - H(STRUCT, struct) - H(BOXED, boxed) - H(ENUM, enum) - H(FLAGS, flags) - H(OBJECT, object) - H(INTERFACE, interface) - H(CONSTANT, constant) - H(UNION, union) - H(VALUE, value) - H(SIGNAL, signal) - H(VFUNC, vfunc) - H(PROPERTY, property) - H(FIELD, field) - H(ARG, arg) - H(TYPE, type) - H(UNRESOLVED, unresolved) + else if (GI_IS_ ## n1 ## _INFO (*info)) \ + { \ + lua_pushstring (L, #n2); \ + return 1; \ + } + + if (0) {} + H(FUNCTION, function) + H(CALLBACK, callback) + H(STRUCT, struct) + H(ENUM, enum) + H(FLAGS, flags) + H(OBJECT, object) + H(INTERFACE, interface) + H(CONSTANT, constant) + H(UNION, union) + H(VALUE, value) + H(SIGNAL, signal) + H(VFUNC, vfunc) + H(PROPERTY, property) + H(FIELD, field) + H(ARG, arg) + H(TYPE, type) + H(UNRESOLVED, unresolved) + else g_assert_not_reached (); #undef H - default: - g_assert_not_reached (); - } } #define H(n1, n2) \ @@ -232,12 +240,12 @@ info_index (lua_State *L) { if (strcmp (prop, "name") == 0) { - lua_pushstring (L, g_base_info_get_name (*info)); + lua_pushstring (L, gi_base_info_get_name (*info)); return 1; } else if (strcmp (prop, "namespace") == 0) { - lua_pushstring (L, g_base_info_get_namespace (*info)); + lua_pushstring (L, gi_base_info_get_namespace (*info)); return 1; } } @@ -250,37 +258,37 @@ info_index (lua_State *L) if (strcmp (prop, "deprecated") == 0) { - lua_pushboolean (L, g_base_info_is_deprecated (*info)); + lua_pushboolean (L, gi_base_info_is_deprecated (*info)); return 1; } else if (strcmp (prop, "container") == 0) { - GIBaseInfo *container = g_base_info_get_container (*info); + GIBaseInfo *container = gi_base_info_get_container (*info); if (container) - g_base_info_ref (container); + gi_base_info_ref (container); return lgi_gi_info_new (L, container); } else if (strcmp (prop, "typeinfo") == 0) { GITypeInfo *ti = NULL; if (GI_IS_ARG_INFO (*info)) - ti = g_arg_info_get_type (*info); + ti = gi_arg_info_get_type_info (GI_ARG_INFO (*info)); else if (GI_IS_CONSTANT_INFO (*info)) - ti = g_constant_info_get_type (*info); + ti = gi_constant_info_get_type_info (GI_CONSTANT_INFO (*info)); else if (GI_IS_PROPERTY_INFO (*info)) - ti = g_property_info_get_type (*info); + ti = gi_property_info_get_type_info (GI_PROPERTY_INFO (*info)); else if (GI_IS_FIELD_INFO (*info)) - ti = g_field_info_get_type (*info); + ti = gi_field_info_get_type_info (GI_FIELD_INFO (*info)); if (ti) - return lgi_gi_info_new (L, ti); + return lgi_gi_info_new (L, GI_BASE_INFO (ti)); } if (GI_IS_REGISTERED_TYPE_INFO (*info)) { if (strcmp (prop, "gtype") == 0) { - GType gtype = g_registered_type_info_get_g_type (*info); + GType gtype = gi_registered_type_info_get_g_type (GI_REGISTERED_TYPE_INFO (*info)); if (gtype != G_TYPE_NONE) lua_pushlightuserdata (L, (void *) gtype); else @@ -291,12 +299,12 @@ info_index (lua_State *L) { if (strcmp (prop, "is_gtype_struct") == 0) { - lua_pushboolean (L, g_struct_info_is_gtype_struct (*info)); + lua_pushboolean (L, gi_struct_info_is_gtype_struct (GI_STRUCT_INFO (*info))); return 1; } else if (strcmp (prop, "size") == 0) { - lua_pushinteger (L, g_struct_info_get_size (*info)); + lua_pushinteger (L, gi_struct_info_get_size (GI_STRUCT_INFO (*info))); return 1; } INFOS (struct, field) @@ -306,7 +314,7 @@ info_index (lua_State *L) { if (strcmp (prop, "size") == 0) { - lua_pushinteger (L, g_struct_info_get_size (*info)); + lua_pushinteger (L, gi_struct_info_get_size (GI_STRUCT_INFO (*info))); return 1; } INFOS (union, field) @@ -316,7 +324,7 @@ info_index (lua_State *L) { if (strcmp (prop, "type_struct") == 0) return - lgi_gi_info_new (L, g_interface_info_get_iface_struct (*info)); + lgi_gi_info_new (L, GI_BASE_INFO (gi_interface_info_get_iface_struct (GI_INTERFACE_INFO (*info)))); INFOS (interface, prerequisite) INFOS (interface, vfunc) INFOS (interface, method) @@ -327,9 +335,9 @@ info_index (lua_State *L) else if (GI_IS_OBJECT_INFO (*info)) { if (strcmp (prop, "parent") == 0) - return lgi_gi_info_new (L, g_object_info_get_parent (*info)); + return lgi_gi_info_new (L, GI_BASE_INFO (gi_object_info_get_parent (GI_OBJECT_INFO (*info)))); else if (strcmp (prop, "type_struct") == 0) - return lgi_gi_info_new (L, g_object_info_get_class_struct (*info)); + return lgi_gi_info_new (L, GI_BASE_INFO (gi_object_info_get_class_struct (GI_OBJECT_INFO (*info)))); INFOS (object, interface) INFOS (object, field) INFOS (object, vfunc) @@ -343,16 +351,16 @@ info_index (lua_State *L) if (GI_IS_CALLABLE_INFO (*info)) { if (strcmp (prop, "return_type") == 0) - return lgi_gi_info_new (L, g_callable_info_get_return_type (*info)); + return lgi_gi_info_new (L, GI_BASE_INFO (gi_callable_info_get_return_type (GI_CALLABLE_INFO (*info)))); else if (strcmp (prop, "return_transfer") == 0) - return info_push_transfer (L, g_callable_info_get_caller_owns (*info)); + return info_push_transfer (L, gi_callable_info_get_caller_owns (GI_CALLABLE_INFO (*info))); INFOS (callable, arg); if (GI_IS_SIGNAL_INFO (*info)) { if (strcmp (prop, "flags") == 0) { - GSignalFlags flags = g_signal_info_get_flags (*info); + GSignalFlags flags = gi_signal_info_get_flags (GI_SIGNAL_INFO (*info)); lua_newtable (L); #define H(n1, n2) \ if ((flags & G_SIGNAL_ ## n1) != 0) \ @@ -376,7 +384,7 @@ info_index (lua_State *L) { if (strcmp (prop, "flags") == 0) { - GIFunctionInfoFlags flags = g_function_info_get_flags (*info); + GIFunctionInfoFlags flags = gi_function_info_get_flags (GI_FUNCTION_INFO (*info)); lua_newtable (L); if (0); #define H(n1, n2) \ @@ -386,23 +394,22 @@ info_index (lua_State *L) lua_setfield (L, -2, #n2); \ } H(IS_METHOD, is_method) - H(IS_CONSTRUCTOR, is_constructor) - H(IS_GETTER, is_getter) - H(IS_SETTER, is_setter) - H(WRAPS_VFUNC, wraps_vfunc) - H(THROWS, throws); + H(IS_CONSTRUCTOR, is_constructor) + H(IS_GETTER, is_getter) + H(IS_SETTER, is_setter) + H(WRAPS_VFUNC, wraps_vfunc) #undef H return 1; } } } - if (GI_IS_ENUM_INFO (*info)) + if (GI_IS_ENUM_INFO (*info) || GI_IS_FLAGS_INFO (*info)) { if (strcmp (prop, "storage") == 0) { - GITypeTag tag = g_enum_info_get_storage_type (*info); - lua_pushstring (L, g_type_tag_to_string (tag)); + GITypeTag tag = gi_enum_info_get_storage_type (GI_ENUM_INFO (*info)); + lua_pushstring (L, gi_type_tag_to_string (tag)); return 1; } #if GLIB_CHECK_VERSION (2, 30, 0) @@ -411,7 +418,7 @@ info_index (lua_State *L) INFOS (enum, value) else if (strcmp (prop, "error_domain") == 0) { - const gchar *domain = g_enum_info_get_error_domain (*info); + const gchar *domain = gi_enum_info_get_error_domain (GI_ENUM_INFO (*info)); if (domain != NULL) lua_pushinteger (L, g_quark_from_string (domain)); else @@ -425,7 +432,7 @@ info_index (lua_State *L) { if (strcmp (prop, "value") == 0) { - lua_pushinteger (L, g_value_info_get_value (*info)); + lua_pushinteger (L, gi_value_info_get_value (GI_VALUE_INFO (*info))); return 1; } } @@ -434,9 +441,9 @@ info_index (lua_State *L) { if (strcmp (prop, "direction") == 0) { - GIDirection dir = g_arg_info_get_direction (*info); + GIDirection dir = gi_arg_info_get_direction (GI_ARG_INFO (*info)); if (dir == GI_DIRECTION_OUT) - lua_pushstring (L, g_arg_info_is_caller_allocates (*info) + lua_pushstring (L, gi_arg_info_is_caller_allocates (GI_ARG_INFO (*info)) ? "out-caller-alloc" : "out"); else lua_pushstring (L, dir == GI_DIRECTION_IN ? "in" : "inout"); @@ -444,11 +451,11 @@ info_index (lua_State *L) } if (strcmp (prop, "transfer") == 0) return info_push_transfer (L, - g_arg_info_get_ownership_transfer (*info)); + gi_arg_info_get_ownership_transfer (GI_ARG_INFO (*info))); if (strcmp (prop, "optional") == 0) { - lua_pushboolean (L, g_arg_info_is_optional (*info) - || g_arg_info_may_be_null (*info)); + lua_pushboolean (L, gi_arg_info_is_optional (GI_ARG_INFO (*info)) + || gi_arg_info_may_be_null (GI_ARG_INFO (*info))); return 1; } } @@ -457,20 +464,20 @@ info_index (lua_State *L) { if (strcmp (prop, "flags") == 0) { - lua_pushinteger (L, g_property_info_get_flags (*info)); + lua_pushinteger (L, gi_property_info_get_flags (GI_PROPERTY_INFO (*info))); return 1; } else if (strcmp (prop, "transfer") == 0) return info_push_transfer (L, - g_property_info_get_ownership_transfer (*info)); + gi_property_info_get_ownership_transfer (GI_PROPERTY_INFO (*info))); } if (GI_IS_FIELD_INFO (*info)) { if (strcmp (prop, "flags") == 0) { - GIFieldInfoFlags flags = g_field_info_get_flags (*info); + GIFieldInfoFlags flags = gi_field_info_get_flags (GI_FIELD_INFO (*info)); lua_newtable (L); if (0); #define H(n1, n2) \ @@ -486,27 +493,27 @@ info_index (lua_State *L) } else if (strcmp (prop, "size") == 0) { - lua_pushinteger (L, g_field_info_get_size (*info)); + lua_pushinteger (L, gi_field_info_get_size (GI_FIELD_INFO (*info))); return 1; } else if (strcmp (prop, "offset") == 0) { - lua_pushinteger (L, g_field_info_get_offset (*info)); + lua_pushinteger (L, gi_field_info_get_offset (GI_FIELD_INFO (*info))); return 1; } } if (GI_IS_TYPE_INFO (*info)) { - GITypeTag tag = g_type_info_get_tag (*info); + GITypeTag tag = gi_type_info_get_tag (GI_TYPE_INFO (*info)); if (strcmp (prop, "tag") == 0) { - lua_pushstring (L, g_type_tag_to_string (tag)); + lua_pushstring (L, gi_type_tag_to_string (tag)); return 1; } else if (strcmp (prop, "is_basic") == 0) { - lua_pushboolean (L, G_TYPE_TAG_IS_BASIC (tag)); + lua_pushboolean (L, GI_TYPE_TAG_IS_BASIC (tag)); return 1; } else if (strcmp (prop, "params") == 0) @@ -515,11 +522,11 @@ info_index (lua_State *L) tag == GI_TYPE_TAG_GSLIST || tag == GI_TYPE_TAG_GHASH) { lua_newtable (L); - lgi_gi_info_new (L, g_type_info_get_param_type (*info, 0)); + lgi_gi_info_new (L, GI_BASE_INFO (gi_type_info_get_param_type (GI_TYPE_INFO (*info), 0))); lua_rawseti (L, -2, 1); if (tag == GI_TYPE_TAG_GHASH) { - lgi_gi_info_new (L, g_type_info_get_param_type (*info, 1)); + lgi_gi_info_new (L, GI_BASE_INFO (gi_type_info_get_param_type (GI_TYPE_INFO (*info), 1))); lua_rawseti (L, -2, 2); } return 1; @@ -527,12 +534,12 @@ info_index (lua_State *L) } else if (strcmp (prop, "interface") == 0 && tag == GI_TYPE_TAG_INTERFACE) { - lgi_gi_info_new (L, g_type_info_get_interface (*info)); + lgi_gi_info_new (L, gi_type_info_get_interface (GI_TYPE_INFO (*info))); return 1; } else if (strcmp (prop, "array_type") == 0 && tag == GI_TYPE_TAG_ARRAY) { - switch (g_type_info_get_array_type (*info)) + switch (gi_type_info_get_array_type (GI_TYPE_INFO (*info))) { #define H(n1, n2) \ case GI_ARRAY_TYPE_ ## n1: \ @@ -551,13 +558,13 @@ info_index (lua_State *L) else if (strcmp (prop, "is_zero_terminated") == 0 && tag == GI_TYPE_TAG_ARRAY) { - lua_pushboolean (L, g_type_info_is_zero_terminated (*info)); + lua_pushboolean (L, gi_type_info_is_zero_terminated (GI_TYPE_INFO (*info))); return 1; } else if (strcmp (prop, "array_length") == 0) { - int len = g_type_info_get_array_length (*info); - if (len >= 0) + guint len; + if (gi_type_info_get_array_length_index (GI_TYPE_INFO (*info), &len)) { lua_pushinteger (L, len); return 1; @@ -565,8 +572,8 @@ info_index (lua_State *L) } else if (strcmp (prop, "fixed_size") == 0) { - int size = g_type_info_get_array_fixed_size (*info); - if (size >= 0) + gsize size; + if (gi_type_info_get_array_fixed_size (GI_TYPE_INFO (*info), &size)) { lua_pushinteger (L, size); return 1; @@ -574,7 +581,7 @@ info_index (lua_State *L) } else if (strcmp (prop, "is_pointer") == 0) { - lua_pushboolean (L, g_type_info_is_pointer (*info)); + lua_pushboolean (L, gi_type_info_is_pointer (GI_TYPE_INFO (*info))); return 1; } } @@ -591,7 +598,7 @@ info_eq (lua_State *L) { GIBaseInfo **i1 = luaL_checkudata (L, 1, LGI_GI_INFO); GIBaseInfo **i2 = luaL_checkudata (L, 2, LGI_GI_INFO); - lua_pushboolean (L, g_base_info_equal (*i1, *i2)); + lua_pushboolean (L, gi_base_info_equal (*i1, *i2)); return 1; } @@ -599,7 +606,7 @@ static int info_gc (lua_State *L) { GIBaseInfo **info = luaL_checkudata (L, 1, LGI_GI_INFO); - g_base_info_unref (*info); + gi_base_info_unref (*info); return 0; } @@ -618,7 +625,7 @@ resolver_index (lua_State *L) { gpointer address; GITypelib **typelib = luaL_checkudata (L, 1, LGI_GI_RESOLVER); - if (g_typelib_symbol (*typelib, luaL_checkstring (L, 2), &address)) + if (gi_typelib_symbol (*typelib, luaL_checkstring (L, 2), &address)) { lua_pushlightuserdata (L, address); return 1; @@ -639,7 +646,7 @@ static int namespace_len (lua_State *L) { const gchar *ns = luaL_checkudata (L, 1, LGI_GI_NAMESPACE); - lua_pushinteger (L, g_irepository_get_n_infos (NULL, ns)); + lua_pushinteger (L, gi_repository_get_n_infos (lgi_gi_get_repository (), ns)); return 1; } @@ -650,14 +657,14 @@ namespace_index (lua_State *L) const gchar *prop; if (lua_type (L, 2) == LUA_TNUMBER) { - GIBaseInfo *info = g_irepository_get_info (NULL, ns, + GIBaseInfo *info = gi_repository_get_info (lgi_gi_get_repository (), ns, lua_tointeger (L, 2) - 1); return lgi_gi_info_new (L, info); } prop = luaL_checkstring (L, 2); if (strcmp (prop, "dependencies") == 0) { - gchar **deps = g_irepository_get_dependencies (NULL, ns); + gchar **deps = gi_repository_get_dependencies (lgi_gi_get_repository (), ns, NULL); if (deps == NULL) lua_pushnil (L); else @@ -679,7 +686,7 @@ namespace_index (lua_State *L) } else if (strcmp (prop, "version") == 0) { - lua_pushstring (L, g_irepository_get_version (NULL, ns)); + lua_pushstring (L, gi_repository_get_version (lgi_gi_get_repository (), ns)); return 1; } else if (strcmp (prop, "name") == 0) @@ -692,12 +699,12 @@ namespace_index (lua_State *L) GITypelib **udata = lua_newuserdata (L, sizeof (GITypelib *)); luaL_getmetatable (L, LGI_GI_RESOLVER); lua_setmetatable (L, -2); - *udata = g_irepository_require (NULL, ns, NULL, 0, NULL); + *udata = gi_repository_require (lgi_gi_get_repository (), ns, NULL, 0, NULL); return 1; } else /* Try to lookup the symbol. */ - return lgi_gi_info_new (L, g_irepository_find_by_name (NULL, ns, prop)); + return lgi_gi_info_new (L, gi_repository_find_by_name (lgi_gi_get_repository (), ns, prop)); } static int @@ -727,9 +734,9 @@ gi_require (lua_State *L) GITypelib *typelib; if (typelib_dir == NULL) - typelib = g_irepository_require (NULL, namespace, version, 0, &err); + typelib = gi_repository_require (lgi_gi_get_repository (), namespace, version, 0, &err); else - typelib = g_irepository_require_private (NULL, typelib_dir, namespace, + typelib = gi_repository_require_private (lgi_gi_get_repository (), typelib_dir, namespace, version, 0, &err); if (!typelib) { @@ -764,19 +771,19 @@ gi_index (lua_State *L) { GType gtype = (GType) lua_touserdata (L, 2); GIBaseInfo *info = (gtype != G_TYPE_INVALID) - ? g_irepository_find_by_gtype (NULL, gtype) : NULL; + ? gi_repository_find_by_gtype (lgi_gi_get_repository (), gtype) : NULL; return lgi_gi_info_new (L, info); } else if (lua_type (L, 2) == LUA_TNUMBER) { GQuark domain = (GQuark) lua_tointeger (L, 2); - GIBaseInfo *info = g_irepository_find_by_error_domain (NULL, domain); + GIBaseInfo *info = GI_BASE_INFO (gi_repository_find_by_error_domain (lgi_gi_get_repository (), domain)); return lgi_gi_info_new (L, info); } else { const gchar *ns = luaL_checkstring (L, 2); - if (g_irepository_is_registered (NULL, ns, NULL)) + if (gi_repository_is_registered (lgi_gi_get_repository (), ns, NULL)) return namespace_new (L, ns); } @@ -827,37 +834,37 @@ lgi_gi_init (lua_State *L) } #if !GLIB_CHECK_VERSION(2, 30, 0) -/* Workaround for broken g_struct_info_get_size() for GValue, see +/* Workaround for broken gi_struct_info_get_size() for GValue, see https://bugzilla.gnome.org/show_bug.cgi?id=657040 */ static GIStructInfo *parameter_info = NULL; static GIFieldInfo *parameter_value_info = NULL; -#undef g_struct_info_get_size +#undef gi_struct_info_get_size gsize lgi_struct_info_get_size (GIStructInfo *info) { if (parameter_info == NULL) - parameter_info = g_irepository_find_by_name (NULL, "GObject", "Parameter"); + parameter_info = gi_repository_find_by_name (lgi_gi_get_repository (), "GObject", "Parameter"); if (g_registered_type_info_get_g_type (info) == G_TYPE_VALUE) return sizeof (GValue); - else if (parameter_info && g_base_info_equal (info, parameter_info)) + else if (parameter_info && gi_base_info_equal (info, parameter_info)) return sizeof (GParameter); - return g_struct_info_get_size (info); + return gi_struct_info_get_size (info); } -#undef g_field_info_get_offset +#undef gi_field_info_get_offset gint lgi_field_info_get_offset (GIFieldInfo *info) { if (parameter_value_info == NULL) { if (parameter_info == NULL) - parameter_info = g_irepository_find_by_name (NULL, + parameter_info = gi_repository_find_by_name (lgi_gi_get_repository (), "GObject", "Parameter"); - parameter_value_info = g_struct_info_get_field (parameter_info, 1); + parameter_value_info = gi_struct_info_get_field (parameter_info, 1); } - if (parameter_value_info && g_base_info_equal (info, parameter_value_info)) + if (parameter_value_info && gi_base_info_equal (info, parameter_value_info)) return G_STRUCT_OFFSET (GParameter, value); - return g_field_info_get_offset (info); + return gi_field_info_get_offset (info); } #endif diff --git a/lgi/lgi.h b/lgi/lgi.h index aada2ef7..22a55200 100644 --- a/lgi/lgi.h +++ b/lgi/lgi.h @@ -49,7 +49,7 @@ typedef unsigned long lgi_Unsigned; #include #include #include -#include +#include #include /* Makes sure that Lua stack offset is absolute one, not relative. */ @@ -221,3 +221,5 @@ int lgi_field_info_get_offset (GIFieldInfo *info); in GI 1.32.0. (see https://bugzilla.gnome.org/show_bug.cgi?id=673282) */ gpointer lgi_object_get_function_ptr (GIObjectInfo *info, const gchar *(*getter)(GIObjectInfo *)); + +GIRepository *lgi_gi_get_repository (void); diff --git a/lgi/marshal.c b/lgi/marshal.c index acf86383..2b2625a3 100644 --- a/lgi/marshal.c +++ b/lgi/marshal.c @@ -175,22 +175,22 @@ static void array_get_or_set_length (GITypeInfo *ti, gssize *get_length, gssize set_length, GIBaseInfo *ci, void *args) { - gint param = g_type_info_get_array_length (ti); - if (param >= 0 && ci != NULL) + guint param; + + if (gi_type_info_get_array_length_index (ti, ¶m) && ci != NULL) { GIArgument *val; GITypeInfo *eti; - GIInfoType itype = g_base_info_get_type (ci); - if (itype == GI_INFO_TYPE_FUNCTION || itype == GI_INFO_TYPE_CALLBACK) + if (GI_IS_FUNCTION_INFO (ci) || GI_IS_CALLBACK_INFO (ci)) { GIArgInfo ai; - if (param >= g_callable_info_get_n_args (ci)) + if (param >= gi_callable_info_get_n_args (GI_CALLABLE_INFO (ci))) return; - g_callable_info_load_arg (ci, param, &ai); - eti = g_arg_info_get_type (&ai); - if (g_arg_info_get_direction (&ai) == GI_DIRECTION_IN) + gi_callable_info_load_arg (GI_CALLABLE_INFO (ci), param, &ai); + eti = gi_arg_info_get_type_info (&ai); + if (gi_arg_info_get_direction (&ai) == GI_DIRECTION_IN) /* For input parameters, value is directly pointed to by args table element. */ val = (GIArgument *) ((void **) args)[param]; @@ -198,22 +198,24 @@ array_get_or_set_length (GITypeInfo *ti, gssize *get_length, gssize set_length, /* For output arguments, args table element points to pointer to value. */ val = *(GIArgument **) ((void **) args)[param]; + + gi_base_info_clear (&ai); } - else if (itype == GI_INFO_TYPE_STRUCT || itype == GI_INFO_TYPE_UNION) + else if (GI_IS_STRUCT_INFO (ci) || GI_IS_UNION_INFO (ci)) { GIFieldInfo *fi; - if (param >= g_struct_info_get_n_fields (ci)) + if (param >= gi_struct_info_get_n_fields (GI_STRUCT_INFO (ci))) return; - fi = g_struct_info_get_field (ci, param); - eti = g_field_info_get_type (fi); - val = (GIArgument *) ((char *) args + g_field_info_get_offset (fi)); - g_base_info_unref (fi); + fi = gi_struct_info_get_field (GI_STRUCT_INFO (ci), param); + eti = gi_field_info_get_type_info (fi); + val = (GIArgument *) ((char *) args + gi_field_info_get_offset (fi)); + gi_base_info_unref (fi); } else return; - switch (g_type_info_get_tag (eti)) + switch (gi_type_info_get_tag (eti)) { #define HANDLE_ELT(tag, field) \ case GI_TYPE_TAG_ ## tag: \ @@ -237,7 +239,7 @@ array_get_or_set_length (GITypeInfo *ti, gssize *get_length, gssize set_length, g_assert_not_reached (); } - g_base_info_unref (eti); + gi_base_info_unref (eti); } } @@ -247,9 +249,9 @@ static gssize array_get_elt_size (GITypeInfo *ti, gboolean force_ptr) { gssize size = sizeof (gpointer); - if (!g_type_info_is_pointer (ti) && !force_ptr) + if (!gi_type_info_is_pointer (ti) && !force_ptr) { - switch (g_type_info_get_tag (ti)) + switch (gi_type_info_get_tag (ti)) { #define HANDLE_ELT(nameupper, nametype) \ case GI_TYPE_TAG_ ## nameupper: \ @@ -272,13 +274,12 @@ array_get_elt_size (GITypeInfo *ti, gboolean force_ptr) case GI_TYPE_TAG_INTERFACE: { - GIBaseInfo *info = g_type_info_get_interface (ti); - GIInfoType type = g_base_info_get_type (info); - if (type == GI_INFO_TYPE_STRUCT) - size = g_struct_info_get_size (info); - else if (type == GI_INFO_TYPE_UNION) - size = g_union_info_get_size (info); - g_base_info_unref (info); + GIBaseInfo *info = gi_type_info_get_interface (ti); + if (GI_IS_STRUCT_INFO (info)) + size = gi_struct_info_get_size (GI_STRUCT_INFO (info)); + else if (GI_IS_UNION_INFO (info)) + size = gi_union_info_get_size (GI_UNION_INFO (info)); + gi_base_info_unref (info); break; } @@ -333,8 +334,8 @@ marshal_2c_array (lua_State *L, GITypeInfo *ti, GIArrayType atype, else { /* Get element type info, create guard for it. */ - eti = g_type_info_get_param_type (ti, 0); - lgi_gi_info_new (L, eti); + eti = gi_type_info_get_param_type (ti, 0); + lgi_gi_info_new (L, GI_BASE_INFO (eti)); eti_guard = lua_gettop (L); esize = array_get_elt_size (eti, atype == GI_ARRAY_TYPE_PTR_ARRAY); @@ -363,10 +364,9 @@ marshal_2c_array (lua_State *L, GITypeInfo *ti, GIArrayType atype, luaL_checktype (L, narg, LUA_TTABLE); /* Find out how long array should we allocate. */ - zero_terminated = g_type_info_is_zero_terminated (ti); + zero_terminated = gi_type_info_is_zero_terminated (ti); objlen = lua_objlen (L, narg); - *out_size = g_type_info_get_array_fixed_size (ti); - if (atype != GI_ARRAY_TYPE_C || *out_size < 0) + if (atype != GI_ARRAY_TYPE_C || !gi_type_info_get_array_fixed_size (ti, (gsize *)&out_size)) *out_size = objlen; else if (*out_size < objlen) objlen = *out_size; @@ -434,7 +434,7 @@ marshal_2c_array (lua_State *L, GITypeInfo *ti, GIArrayType atype, according to the array type. */ if (array == NULL) *out_array = NULL; - else + else switch (atype) { case GI_ARRAY_TYPE_C: @@ -498,12 +498,11 @@ marshal_2lua_array (lua_State *L, GITypeInfo *ti, GIDirection dir, else { data = array; - if (g_type_info_is_zero_terminated (ti)) + if (gi_type_info_is_zero_terminated (ti)) len = -1; else { - len = g_type_info_get_array_fixed_size (ti); - if (len == -1) + if (!gi_type_info_get_array_fixed_size (ti, (gsize *)&len)) /* Length of the array is dynamic, get it from other argument. */ len = size; @@ -512,8 +511,8 @@ marshal_2lua_array (lua_State *L, GITypeInfo *ti, GIDirection dir, /* Get array element type info, wrap it in the guard so that we don't leak it. */ - eti = g_type_info_get_param_type (ti, 0); - lgi_gi_info_new (L, eti); + eti = gi_type_info_get_param_type (ti, 0); + lgi_gi_info_new (L, GI_BASE_INFO (eti)); eti_guard = lua_gettop (L); esize = array_get_elt_size (eti, atype == GI_ARRAY_TYPE_PTR_ARRAY); @@ -523,7 +522,7 @@ marshal_2lua_array (lua_State *L, GITypeInfo *ti, GIDirection dir, is workaround for g-ir-scanner bug which might mark elements of uint8 arrays as gconstpointer, thus setting is_pointer=true on it. See https://github.com/pavouk/lgi/issues/57 */ - if (g_type_info_get_tag (eti) == GI_TYPE_TAG_UINT8) + if (gi_type_info_get_tag (eti) == GI_TYPE_TAG_UINT8) { /* UINT8 arrays are marshalled as Lua strings. */ if (len < 0) @@ -611,8 +610,8 @@ marshal_2c_list (lua_State *L, GITypeInfo *ti, GITypeTag list_tag, /* Get list element type info, create guard for it so that we don't leak it. */ - eti = g_type_info_get_param_type (ti, 0); - lgi_gi_info_new (L, eti); + eti = gi_type_info_get_param_type (ti, 0); + lgi_gi_info_new (L, GI_BASE_INFO (eti)); eti_guard = lua_gettop (L); /* Go from back and prepend to the list, which is cheaper than @@ -655,8 +654,8 @@ marshal_2lua_list (lua_State *L, GITypeInfo *ti, GIDirection dir, gint index, eti_guard; /* Get element type info, guard it so that we don't leak it. */ - eti = g_type_info_get_param_type (ti, 0); - lgi_gi_info_new (L, eti); + eti = gi_type_info_get_param_type (ti, 0); + lgi_gi_info_new (L, GI_BASE_INFO (eti)); eti_guard = lua_gettop (L); /* Create table to which we will deserialize the list. */ @@ -714,8 +713,8 @@ marshal_2c_hash (lua_State *L, GITypeInfo *ti, GHashTable **table, int narg, guard = lua_gettop (L) + 1; for (i = 0; i < 2; i++) { - eti[i] = g_type_info_get_param_type (ti, i); - lgi_gi_info_new (L, eti[i]); + eti[i] = gi_type_info_get_param_type (ti, i); + lgi_gi_info_new (L, GI_BASE_INFO (eti[i])); } /* Create the hashtable and guard it so that it is destroyed in @@ -726,7 +725,7 @@ marshal_2c_hash (lua_State *L, GITypeInfo *ti, GHashTable **table, int narg, /* Find out which hash_func and equal_func should be used, according to the type of the key. */ - switch (g_type_info_get_tag (eti[0])) + switch (gi_type_info_get_tag (eti[0])) { case GI_TYPE_TAG_UTF8: case GI_TYPE_TAG_FILENAME: @@ -801,8 +800,8 @@ marshal_2lua_hash (lua_State *L, GITypeInfo *ti, GIDirection dir, guard = lua_gettop (L) + 1; for (i = 0; i < 2; i++) { - eti[i] = g_type_info_get_param_type (ti, i); - lgi_gi_info_new (L, eti[i]); + eti[i] = gi_type_info_get_param_type (ti, i); + lgi_gi_info_new (L, GI_BASE_INFO (eti[i])); } /* Create table to which we will deserialize the hashtable. */ @@ -856,7 +855,7 @@ marshal_2c_callable (lua_State *L, GICallableInfo *ci, GIArgInfo *ai, gint nargs = 0; if (argci != NULL) - nargs = g_callable_info_get_n_args (argci); + nargs = gi_callable_info_get_n_args (argci); /* Check 'nil' in optional case. In this case, return NULL as callback. */ @@ -872,8 +871,8 @@ marshal_2c_callable (lua_State *L, GICallableInfo *ci, GIArgInfo *ai, case). */ if (ai != NULL) { - gint arg = g_arg_info_get_destroy (ai); - if (arg >= 0 && arg < nargs) + guint arg; + if (gi_arg_info_get_destroy_index (ai, &arg) && arg < (guint)nargs) ((GIArgument *) args[arg])->v_pointer = NULL; } return 0; @@ -891,20 +890,19 @@ marshal_2c_callable (lua_State *L, GICallableInfo *ci, GIArgInfo *ai, if (argci != NULL) { - gint arg = g_arg_info_get_closure (ai); + guint arg; /* user_data block is already preallocated from function call. */ g_assert (args != NULL); - if (arg >= 0 && arg < nargs) + if (gi_arg_info_get_closure_index (ai, &arg) && arg < (guint)nargs) { user_data = ((GIArgument *) args[arg])->v_pointer; - arg = g_arg_info_get_destroy (ai); - if (arg >= 0 && arg < nargs) + if (gi_arg_info_get_destroy_index (ai, &arg) && arg < (guint)nargs) ((GIArgument *) args[arg])->v_pointer = lgi_closure_destroy; } } - scope = g_arg_info_get_scope (ai); + scope = gi_arg_info_get_scope (ai); if (user_data == NULL) { /* Closure without user_data block. Create new data block, @@ -934,9 +932,9 @@ lgi_marshal_2c (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, { int nret = 0; gboolean optional = (parent == LGI_PARENT_CALLER_ALLOC) || - (ai == NULL || (g_arg_info_is_optional (ai) || - g_arg_info_may_be_null (ai))); - GITypeTag tag = g_type_info_get_tag (ti); + (ai == NULL || (gi_arg_info_is_optional (ai) || + gi_arg_info_may_be_null (ai))); + GITypeTag tag = gi_type_info_get_tag (ti); GIArgument *arg = target; /* Convert narg stack position to absolute one, because during @@ -1021,15 +1019,13 @@ lgi_marshal_2c (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, case GI_TYPE_TAG_INTERFACE: { - GIBaseInfo *info = g_type_info_get_interface (ti); - GIInfoType type = g_base_info_get_type (info); + GIBaseInfo *info = gi_type_info_get_interface (ti); int info_guard; lgi_gi_info_new (L, info); info_guard = lua_gettop (L); - switch (type) - { - case GI_INFO_TYPE_ENUM: - case GI_INFO_TYPE_FLAGS: + + if (GI_IS_ENUM_INFO (info) || GI_IS_FLAGS_INFO (info)) + { /* If the argument is not numeric, convert to number first. Use enum/flags 'constructor' to do this. */ if (lua_type (L, narg) != LUA_TNUMBER) @@ -1041,56 +1037,51 @@ lgi_marshal_2c (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, } /* Directly store underlying value. */ - marshal_2c_int (L, g_enum_info_get_storage_type (info), arg, narg, + marshal_2c_int (L, gi_enum_info_get_storage_type (GI_ENUM_INFO (info)), arg, narg, optional, parent); /* Remove the temporary value, to keep stack balanced. */ if (narg == -1) lua_pop (L, 1); - break; - - case GI_INFO_TYPE_STRUCT: - case GI_INFO_TYPE_UNION: - { - /* Ideally the g_type_info_is_pointer() should be - sufficient here, but there is some - gobject-introspection quirk that some struct - arguments might not be marked as pointers - (e.g. g_variant_equals(), which has ctype of - gconstpointer, and thus logic in girparser.c which - sets is_pointer attribute fails). Workaround it by - checking also argument type - structs as C function - arguments are always passed as pointers. */ - gboolean by_value = - parent != LGI_PARENT_FORCE_POINTER && - ((!g_type_info_is_pointer (ti) && ai == NULL) || - parent == LGI_PARENT_CALLER_ALLOC); - - lgi_type_get_repotype (L, G_TYPE_INVALID, info); - lgi_record_2c (L, narg, target, by_value, - transfer != GI_TRANSFER_NOTHING, optional, FALSE); - break; - } + } + else if (GI_IS_STRUCT_INFO (info) || GI_IS_UNION_INFO (info)) + { + /* Ideally the gi_type_info_is_pointer() should be + sufficient here, but there is some + gobject-introspection quirk that some struct + arguments might not be marked as pointers + (e.g. g_variant_equals(), which has ctype of + gconstpointer, and thus logic in girparser.c which + sets is_pointer attribute fails). Workaround it by + checking also argument type - structs as C function + arguments are always passed as pointers. */ + gboolean by_value = + parent != LGI_PARENT_FORCE_POINTER && + ((!gi_type_info_is_pointer (ti) && ai == NULL) || + parent == LGI_PARENT_CALLER_ALLOC); - case GI_INFO_TYPE_OBJECT: - case GI_INFO_TYPE_INTERFACE: - { - arg->v_pointer = - lgi_object_2c (L, narg, - g_registered_type_info_get_g_type (info), - optional, FALSE, - transfer != GI_TRANSFER_NOTHING); - break; - } - - case GI_INFO_TYPE_CALLBACK: - nret = marshal_2c_callable (L, info, ai, &arg->v_pointer, narg, + lgi_type_get_repotype (L, G_TYPE_INVALID, info); + lgi_record_2c (L, narg, target, by_value, + transfer != GI_TRANSFER_NOTHING, optional, FALSE); + } + else if (GI_IS_OBJECT_INFO (info) || GI_IS_INTERFACE_INFO (info)) + { + arg->v_pointer = + lgi_object_2c (L, narg, + gi_registered_type_info_get_g_type (GI_REGISTERED_TYPE_INFO (info)), + optional, FALSE, + transfer != GI_TRANSFER_NOTHING); + } + else if (GI_IS_CALLBACK_INFO (info)) + { + nret = marshal_2c_callable (L, GI_CALLABLE_INFO (info), ai, &arg->v_pointer, narg, optional, ci, args); - break; - - default: + } + else + { g_assert_not_reached (); - } + } + lua_remove (L, info_guard); } break; @@ -1098,13 +1089,13 @@ lgi_marshal_2c (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, case GI_TYPE_TAG_ARRAY: { gssize size; - GIArrayType atype = g_type_info_get_array_type (ti); + GIArrayType atype = gi_type_info_get_array_type (ti); nret = marshal_2c_array (L, ti, atype, &arg->v_pointer, &size, narg, optional, transfer); /* Fill in array length argument, if it is specified. */ if (atype == GI_ARRAY_TYPE_C) - array_get_or_set_length (ti, NULL, size, ci, args); + array_get_or_set_length (ti, NULL, size, GI_BASE_INFO (ci), args); break; } @@ -1119,7 +1110,7 @@ lgi_marshal_2c (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, break; case GI_TYPE_TAG_VOID: - if (g_type_info_is_pointer (ti)) + if (gi_type_info_is_pointer (ti)) { /* Check and marshal according to real Lua type. */ if (lua_isnoneornil (L, narg)) @@ -1168,13 +1159,12 @@ lgi_marshal_2c_caller_alloc (lua_State *L, GITypeInfo *ti, GIArgument *val, int pos) { gboolean handled = FALSE; - switch (g_type_info_get_tag (ti)) + switch (gi_type_info_get_tag (ti)) { case GI_TYPE_TAG_INTERFACE: { - GIBaseInfo *ii = g_type_info_get_interface (ti); - GIInfoType type = g_base_info_get_type (ii); - if (type == GI_INFO_TYPE_STRUCT || type == GI_INFO_TYPE_UNION) + GIBaseInfo *ii = gi_type_info_get_interface (ti); + if (GI_IS_STRUCT_INFO (ii) || GI_IS_UNION_INFO (ii)) { if (pos == 0) { @@ -1184,23 +1174,25 @@ lgi_marshal_2c_caller_alloc (lua_State *L, GITypeInfo *ti, GIArgument *val, handled = TRUE; } - g_base_info_unref (ii); + gi_base_info_unref (ii); break; } case GI_TYPE_TAG_ARRAY: { - if (g_type_info_get_array_type (ti) == GI_ARRAY_TYPE_C) + if (gi_type_info_get_array_type (ti) == GI_ARRAY_TYPE_C) { gpointer *array_guard; if (pos == 0) { - gssize elt_size, size; + gssize elt_size; + gsize size; /* Currently only fixed-size arrays are supported. */ elt_size = - array_get_elt_size (g_type_info_get_param_type (ti, 0), FALSE); - size = g_type_info_get_array_fixed_size (ti); + array_get_elt_size (gi_type_info_get_param_type (ti, 0), FALSE); + if (!gi_type_info_get_array_fixed_size (ti, &size)) + g_assert_not_reached (); g_assert (size > 0); /* Allocate underlying array. It is temporary, @@ -1257,7 +1249,7 @@ lgi_marshal_2lua (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, GIDirection dir, GICallableInfo *ci, void *args) { gboolean own = (transfer != GI_TRANSFER_NOTHING); - GITypeTag tag = g_type_info_get_tag (ti); + GITypeTag tag = gi_type_info_get_tag (ti); GIArgument *arg = source; /* Make sure that parent is absolute index so that it is fixed even @@ -1267,7 +1259,7 @@ lgi_marshal_2lua (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, GIDirection dir, switch (tag) { case GI_TYPE_TAG_VOID: - if (g_type_info_is_pointer (ti)) + if (gi_type_info_is_pointer (ti)) /* Marshal pointer to simple lightuserdata. */ lua_pushlightuserdata (L, arg->v_pointer); else @@ -1310,20 +1302,17 @@ lgi_marshal_2lua (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, GIDirection dir, case GI_TYPE_TAG_INTERFACE: { - GIBaseInfo *info = g_type_info_get_interface (ti); - GIInfoType type = g_base_info_get_type (info); + GIBaseInfo *info = gi_type_info_get_interface (ti); int info_guard; lgi_gi_info_new (L, info); info_guard = lua_gettop (L); - switch (type) - { - case GI_INFO_TYPE_ENUM: - case GI_INFO_TYPE_FLAGS: + if (GI_IS_ENUM_INFO (info) || GI_IS_FLAGS_INFO (info)) + { /* Prepare repotable of enum/flags on the stack. */ lgi_type_get_repotype (L, G_TYPE_INVALID, info); /* Unmarshal the numeric value. */ - marshal_2lua_int (L, g_enum_info_get_storage_type (info), + marshal_2lua_int (L, gi_enum_info_get_storage_type (GI_ENUM_INFO (info)), arg, parent); /* Get symbolic value from the table. */ @@ -1331,39 +1320,35 @@ lgi_marshal_2lua (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, GIDirection dir, /* Remove the table from the stack. */ lua_remove (L, -2); - break; - - case GI_INFO_TYPE_STRUCT: - case GI_INFO_TYPE_UNION: - { - gboolean by_ref = parent == LGI_PARENT_FORCE_POINTER || - g_type_info_is_pointer (ti); - if (parent < LGI_PARENT_CALLER_ALLOC && by_ref) - parent = 0; - lgi_type_get_repotype (L, G_TYPE_INVALID, info); - lgi_record_2lua (L, by_ref ? arg->v_pointer : source, - own, parent); - break; - } - - case GI_INFO_TYPE_OBJECT: - case GI_INFO_TYPE_INTERFACE: + } + else if (GI_IS_STRUCT_INFO (info) || GI_IS_UNION_INFO (info)) + { + gboolean by_ref = parent == LGI_PARENT_FORCE_POINTER || + gi_type_info_is_pointer (ti); + if (parent < LGI_PARENT_CALLER_ALLOC && by_ref) + parent = 0; + lgi_type_get_repotype (L, G_TYPE_INVALID, info); + lgi_record_2lua (L, by_ref ? arg->v_pointer : source, + own, parent); + } + else if (GI_IS_OBJECT_INFO (info) || GI_IS_INTERFACE_INFO (info)) + { /* Avoid sinking for input arguments, because it wreaks havoc to input arguments of vfunc callbacks during InitiallyUnowned construction phase. */ lgi_object_2lua (L, arg->v_pointer, own, dir == GI_DIRECTION_IN); - break; - - case GI_INFO_TYPE_CALLBACK: + } + else if (GI_IS_CALLBACK_INFO (info)) + { if (arg->v_pointer == NULL) lua_pushnil (L); else { - lgi_callable_create (L, info, arg->v_pointer); + lgi_callable_create (L, GI_CALLABLE_INFO (info), arg->v_pointer); if (ai != NULL && args != NULL) { - gint closure = g_arg_info_get_closure (ai); - if (closure >= 0) + guint closure; + if (gi_arg_info_get_closure_index (ai, &closure)) { /* Store context associated with the callback to the callback object. */ @@ -1373,21 +1358,22 @@ lgi_marshal_2lua (lua_State *L, GITypeInfo *ti, GIArgInfo *ai, GIDirection dir, } } } - break; + } + else + { + g_assert_not_reached (); + } - default: - g_assert_not_reached (); - } lua_remove (L, info_guard); } break; case GI_TYPE_TAG_ARRAY: { - GIArrayType atype = g_type_info_get_array_type (ti); + GIArrayType atype = gi_type_info_get_array_type (ti); gssize size = -1; - gpointer ptr = g_type_info_is_pointer (ti) ? arg->v_pointer : arg; - array_get_or_set_length (ti, &size, 0, ci, args); + gpointer ptr = gi_type_info_is_pointer (ti) ? arg->v_pointer : arg; + array_get_or_set_length (ti, &size, 0, GI_BASE_INFO (ci), args); marshal_2lua_array (L, ti, dir, atype, transfer, ptr, size, parent); } break; @@ -1424,10 +1410,10 @@ lgi_marshal_field (lua_State *L, gpointer object, gboolean getmode, { GIFieldInfoFlags flags; GIFieldInfo **fi = lua_touserdata (L, field_arg); - pi = g_base_info_get_container (*fi); + pi = gi_base_info_get_container (GI_BASE_INFO (*fi)); /* Check, whether field is readable/writable. */ - flags = g_field_info_get_flags (*fi); + flags = gi_field_info_get_flags (*fi); if ((flags & (getmode ? GI_FIELD_IS_READABLE : GI_FIELD_IS_WRITABLE)) == 0) { @@ -1439,10 +1425,10 @@ lgi_marshal_field (lua_State *L, gpointer object, gboolean getmode, /* Prepare proper error message. */ lua_concat (L, lgi_type_get_name (L, - g_base_info_get_container (*fi))); + gi_base_info_get_container (GI_BASE_INFO (*fi)))); return luaL_error (L, "%s: field `%s' is not %s", lua_tostring (L, -1), - g_base_info_get_name (*fi), + gi_base_info_get_name (GI_BASE_INFO (*fi)), getmode ? "readable" : "writable"); } lua_pop (L, 1); @@ -1450,9 +1436,9 @@ lgi_marshal_field (lua_State *L, gpointer object, gboolean getmode, /* Map GIArgument to proper memory location, get typeinfo of the field and perform actual marshalling. */ - field_addr = (char *) object + g_field_info_get_offset (*fi); - ti = g_field_info_get_type (*fi); - lgi_gi_info_new (L, ti); + field_addr = (char *) object + gi_field_info_get_offset (*fi); + ti = gi_field_info_get_type_info (*fi); + lgi_gi_info_new (L, GI_BASE_INFO (ti)); to_remove = lua_gettop (L); } else @@ -1547,7 +1533,7 @@ lgi_marshal_field (lua_State *L, gpointer object, gboolean getmode, if (getmode) { lgi_marshal_2lua (L, ti, NULL, GI_DIRECTION_OUT, GI_TRANSFER_NOTHING, - field_addr, parent_arg, pi, object); + field_addr, parent_arg, GI_CALLABLE_INFO (pi), object); nret = 1; } else @@ -1609,14 +1595,14 @@ marshal_container_marshaller (lua_State *L) /* Get info and transfer from upvalue. */ ti = lua_touserdata (L, lua_upvalueindex (1)); - tag = g_type_info_get_tag (*ti); + tag = gi_type_info_get_tag (*ti); transfer = lua_tointeger (L, lua_upvalueindex (2)); switch (tag) { case GI_TYPE_TAG_ARRAY: { - GIArrayType atype = g_type_info_get_array_type (*ti); + GIArrayType atype = gi_type_info_get_array_type (*ti); gssize size = -1; if (get_mode) { @@ -1703,7 +1689,7 @@ static int marshal_container (lua_State *L) { GIBaseInfo **info = luaL_checkudata (L, 1, LGI_GI_INFO); - GITypeTag tag = g_type_info_get_tag (*info); + GITypeTag tag = gi_type_info_get_tag (GI_TYPE_INFO (*info)); GITransfer transfer = luaL_checkoption (L, 2, transfers[0], transfers); if (tag == GI_TYPE_TAG_ARRAY || tag == GI_TYPE_TAG_GHASH || tag == GI_TYPE_TAG_GSLIST || tag == GI_TYPE_TAG_GLIST) @@ -1754,19 +1740,19 @@ static int marshal_fundamental (lua_State *L) { /* Find associated baseinfo. */ - GIBaseInfo *info = g_irepository_find_by_gtype (NULL, + GIBaseInfo *info = gi_repository_find_by_gtype (lgi_gi_get_repository (), lgi_type_get_gtype (L, 1)); if (info) { lgi_gi_info_new (L, info); - if (GI_IS_OBJECT_INFO (info) && g_object_info_get_fundamental (info)) + if (GI_IS_OBJECT_INFO (info) && gi_object_info_get_fundamental (GI_OBJECT_INFO (info))) { GIObjectInfoGetValueFunction get_value = - lgi_object_get_function_ptr (info, - g_object_info_get_get_value_function); + lgi_object_get_function_ptr (GI_OBJECT_INFO (info), + gi_object_info_get_get_value_function_name); GIObjectInfoSetValueFunction set_value = - lgi_object_get_function_ptr (info, - g_object_info_get_set_value_function); + lgi_object_get_function_ptr (GI_OBJECT_INFO (info), + gi_object_info_get_set_value_function_name); if (get_value && set_value) { lua_pushlightuserdata (L, get_value); @@ -1855,7 +1841,7 @@ marshal_closure_invoke (lua_State *L) GClosure *closure; GValue *result, *params; gint n_params, i; - + lgi_type_get_repotype (L, G_TYPE_CLOSURE, NULL); lgi_record_2c (L, 1, &closure, FALSE, FALSE, FALSE, FALSE); @@ -1899,11 +1885,11 @@ marshal_closure_set_marshal (lua_State *L) GClosureMarshal marshal; GIBaseInfo *ci; - ci = g_irepository_find_by_name (NULL, "GObject", "ClosureMarshal"); + ci = gi_repository_find_by_name (lgi_gi_get_repository (), "GObject", "ClosureMarshal"); lgi_type_get_repotype (L, G_TYPE_CLOSURE, NULL); lgi_record_2c (L, 1, &closure, FALSE, FALSE, FALSE, FALSE); user_data = lgi_closure_allocate (L, 1); - lgi_callable_create (L, ci, NULL); + lgi_callable_create (L, GI_CALLABLE_INFO (ci), NULL); marshal = lgi_closure_create (L, user_data, 2, FALSE); g_closure_set_marshal (closure, marshal); g_closure_add_invalidate_notifier (closure, user_data, gclosure_destroy); @@ -1916,7 +1902,7 @@ static int marshal_typeinfo (lua_State *L) { GIBaseInfo **info = luaL_checkudata (L, 1, LGI_GI_INFO); - switch (g_type_info_get_tag (*info)) + switch (gi_type_info_get_tag (GI_TYPE_INFO (*info))) { #define HANDLE_INT(upper, type) \ case GI_TYPE_TAG_ ## upper: \ diff --git a/lgi/object.c b/lgi/object.c index c5bad391..2efe2e4f 100644 --- a/lgi/object.c +++ b/lgi/object.c @@ -127,14 +127,14 @@ object_get (lua_State *L, int narg) } /* This is workaround method for broken - g_object_info_get_*_function_pointer() in GI 1.32.0. (see + gi_object_info_get_*_function_pointer() in GI 1.32.0. (see https://bugzilla.gnome.org/show_bug.cgi?id=673282) */ gpointer lgi_object_get_function_ptr (GIObjectInfo *info, const gchar *(*getter)(GIObjectInfo *)) { gpointer func = NULL; - g_base_info_ref (info); + gi_base_info_ref (info); while (info != NULL) { GIBaseInfo *parent; @@ -142,17 +142,17 @@ lgi_object_get_function_ptr (GIObjectInfo *info, /* Try to get the name and the symbol. */ func_name = getter (info); - if (func_name && g_typelib_symbol (g_base_info_get_typelib (info), - func_name, &func)) + if (func_name && gi_typelib_symbol (gi_base_info_get_typelib (GI_BASE_INFO (info)), + func_name, &func)) { - g_base_info_unref (info); + gi_base_info_unref (info); break; } /* Iterate to the parent info. */ - parent = g_object_info_get_parent (info); - g_base_info_unref (info); - info = parent; + parent = GI_BASE_INFO (gi_object_info_get_parent (info)); + gi_base_info_unref (info); + info = GI_OBJECT_INFO (parent); } return func; @@ -187,14 +187,14 @@ object_refsink (lua_State *L, gpointer obj, gboolean no_sink) /* Check whether object has registered fundamental 'ref' function. */ - GIObjectInfo *info = g_irepository_find_by_gtype (NULL, gtype); + GIObjectInfo *info = GI_OBJECT_INFO (gi_repository_find_by_gtype (lgi_gi_get_repository (), gtype)); if (info == NULL) - info = g_irepository_find_by_gtype (NULL, G_TYPE_FUNDAMENTAL (gtype)); - if (info != NULL && g_object_info_get_fundamental (info)) + info = GI_OBJECT_INFO (gi_repository_find_by_gtype (lgi_gi_get_repository (), G_TYPE_FUNDAMENTAL (gtype))); + if (info != NULL && gi_object_info_get_fundamental (info)) { GIObjectInfoRefFunction ref = - lgi_object_get_function_ptr (info, g_object_info_get_ref_function); - g_base_info_unref (info); + lgi_object_get_function_ptr (info, gi_object_info_get_ref_function_name); + gi_base_info_unref (info); if (ref != NULL) { ref (obj); @@ -235,14 +235,14 @@ object_unref (lua_State *L, gpointer obj) /* Some other fundamental type, check, whether it has registered custom unref method. */ - GIObjectInfo *info = g_irepository_find_by_gtype (NULL, gtype); + GIObjectInfo *info = GI_OBJECT_INFO (gi_repository_find_by_gtype (lgi_gi_get_repository (), gtype)); if (info == NULL) - info = g_irepository_find_by_gtype (NULL, G_TYPE_FUNDAMENTAL (gtype)); - if (info != NULL && g_object_info_get_fundamental (info)) + info = GI_OBJECT_INFO (gi_repository_find_by_gtype (lgi_gi_get_repository (), G_TYPE_FUNDAMENTAL (gtype))); + if (info != NULL && gi_object_info_get_fundamental (info)) { GIObjectInfoUnrefFunction unref = - lgi_object_get_function_ptr (info, g_object_info_get_unref_function); - g_base_info_unref (info); + lgi_object_get_function_ptr (info, gi_object_info_get_unref_function_name); + gi_base_info_unref (info); if (unref != NULL) { unref (obj); @@ -545,6 +545,7 @@ object_new (lua_State *L) lua_toboolean (L, 3)); else { + G_GNUC_BEGIN_IGNORE_DEPRECATIONS /* Normally Lua code uses GObject.Object.new(), which maps directly to g_object_newv(), but for some reason GOI < 1.0 does not export this method in the typelib. */ @@ -557,8 +558,8 @@ object_new (lua_State *L) luaL_checktype (L, 2, LUA_TTABLE); /* Find BaseInfo of GParameter. */ - gparam_info = g_irepository_find_by_name (NULL, "GObject", "Parameter"); - *lgi_guard_create (L, (GDestroyNotify) g_base_info_unref) = gparam_info; + gparam_info = gi_repository_find_by_name (lgi_gi_get_repository (), "GObject", "Parameter"); + *lgi_guard_create (L, (GDestroyNotify) gi_base_info_unref) = gparam_info; /* Prepare array of GParameter structures. */ size = lua_objlen (L, 2); @@ -575,6 +576,8 @@ object_new (lua_State *L) /* Create the object and return it. */ return lgi_object_2lua (L, g_object_newv (gtype, size, params), TRUE, FALSE); + + G_GNUC_END_IGNORE_DEPRECATIONS } } diff --git a/meson.build b/meson.build index e619e336..6c762ef4 100644 --- a/meson.build +++ b/meson.build @@ -74,7 +74,7 @@ else lua_path = join_paths(get_option('datadir'), 'lua', lua_abi_version) endif -gi_dep = dependency('gobject-introspection-1.0') +gi_dep = dependency('girepository-2.0', version: '>= 2.80') gi_datadir = gi_dep.get_pkgconfig_variable('gidatadir') install_data('lgi.lua', install_dir: lua_path) From a833e1107a3c068f491931043ec4b10f4ffa8ed4 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Fri, 20 Jun 2025 14:30:53 -0700 Subject: [PATCH 2/2] Update lgi/marshal.c Co-authored-by: Victoria Lacroix --- lgi/marshal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lgi/marshal.c b/lgi/marshal.c index 2b2625a3..87337024 100644 --- a/lgi/marshal.c +++ b/lgi/marshal.c @@ -366,7 +366,7 @@ marshal_2c_array (lua_State *L, GITypeInfo *ti, GIArrayType atype, /* Find out how long array should we allocate. */ zero_terminated = gi_type_info_is_zero_terminated (ti); objlen = lua_objlen (L, narg); - if (atype != GI_ARRAY_TYPE_C || !gi_type_info_get_array_fixed_size (ti, (gsize *)&out_size)) + if (atype != GI_ARRAY_TYPE_C || !gi_type_info_get_array_fixed_size (ti, (gsize *)out_size)) *out_size = objlen; else if (*out_size < objlen) objlen = *out_size;