m-lua-scripting: Add WpCollection and WpCollectionManager Lua APIs

This allows using the new collections API in Lua scripts.
This commit is contained in:
Julian Bouzas 2025-12-10 12:06:45 -05:00
parent 3e9d78da1e
commit e89da19cb6
2 changed files with 299 additions and 0 deletions

View file

@ -2179,6 +2179,53 @@ static const luaL_Reg properties_funcs[] = {
{ NULL, NULL }
};
/* WpCollection */
static int
collection_get_name (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
lua_pushstring (L, wp_collection_get_name (c));
return 1;
}
static int
collection_get_metadata (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
WpMetadata *m = wp_collection_get_metadata (c);
if (m)
wplua_pushobject (L, m);
else
lua_pushnil (L);
return 1;
}
static int
collection_contains_global (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
WpGlobalProxy *g = wplua_checkobject (L, 2, WP_TYPE_GLOBAL_PROXY);
lua_pushboolean (L, wp_collection_contains_global (c, g));
return 1;
}
static int
collection_get_global_count (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
lua_pushinteger (L, wp_collection_get_global_count (c));
return 1;
}
static const luaL_Reg collection_funcs[] = {
{ "get_name", collection_get_name },
{ "get_metadata", collection_get_metadata },
{ "contains_global", collection_contains_global },
{ "get_global_count", collection_get_global_count },
{ NULL, NULL }
};
/* WpSettings */
static int
@ -2739,6 +2786,252 @@ static const luaL_Reg permission_manager_funcs[] = {
{ NULL, NULL }
};
/* WpCollectionManager */
static void
on_collection_created (WpCollectionManager *cm, GAsyncResult * res,
GClosure * closure)
{
g_autoptr (GError) error = NULL;
WpCollection *c;
GValue val[2] = { G_VALUE_INIT, G_VALUE_INIT };
int n_vals = 1;
c = wp_collection_manager_create_collection_finish (cm, res, &error);
if (!c) {
g_value_init (&val[1], G_TYPE_STRING);
g_value_set_string (&val[1], error->message);
n_vals = 2;
}
g_value_init (&val[0], WP_TYPE_COLLECTION);
g_value_set_object (&val[0], c);
g_closure_invoke (closure, NULL, n_vals, val, NULL);
g_value_unset (&val[0]);
g_value_unset (&val[1]);
g_closure_invalidate (closure);
g_closure_unref (closure);
}
static int
collection_manager_create_collection (lua_State *L)
{
const char *name = luaL_checkstring (L, 1);
GClosure * closure = wplua_function_to_closure (L, 2);
g_autoptr (WpCollectionManager) cm = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
GValue val[2] = { G_VALUE_INIT, G_VALUE_INIT };
g_value_init (&val[0], WP_TYPE_METADATA);
g_value_set_object (&val[0], NULL);
g_value_init (&val[1], G_TYPE_STRING);
g_value_set_string (&val[1], "Could not find collection manager");
g_closure_invoke (closure, NULL, 2, val, NULL);
g_value_unset (&val[0]);
g_value_unset (&val[1]);
g_closure_invalidate (closure);
g_closure_unref (closure);
return 0;
}
wp_collection_manager_create_collection (cm, name, NULL,
(GAsyncReadyCallback) on_collection_created, closure);
return 0;
}
static int
collection_manager_destroy_collection (lua_State *L)
{
const char *name = luaL_checkstring (L, 1);
g_autoptr (WpCollectionManager) cm = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushboolean (L, FALSE);
return 1;
}
lua_pushboolean (L, wp_collection_manager_destroy_collection (cm, name));
return 1;
}
static int
collection_manager_has_collection (lua_State *L)
{
const char *name = luaL_checkstring (L, 1);
g_autoptr (WpCollectionManager) cm = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushboolean (L, FALSE);
return 1;
}
lua_pushboolean (L, wp_collection_manager_has_collection (cm, name));
return 1;
}
static int
collection_manager_get_collection (lua_State *L)
{
const char *name = luaL_checkstring (L, 1);
g_autoptr (WpCollectionManager) cm = NULL;
WpCollection *c = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushnil (L);
return 1;
}
c = wp_collection_manager_get_collection (cm, name);
if (c)
wplua_pushobject (L, c);
else
lua_pushnil (L);
return 1;
}
static int
collection_manager_get_collection_count (lua_State *L)
{
g_autoptr (WpCollectionManager) cm = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushinteger (L, 0);
return 1;
}
lua_pushinteger (L, wp_collection_manager_get_collection_count (cm));
return 1;
}
static int
collection_manager_iterate_collections (lua_State *L)
{
g_autoptr (WpCollectionManager) cm = NULL;
g_autoptr (WpIterator) it = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushnil (L);
return 1;
}
it = wp_collection_manager_new_collection_iterator (cm);
return push_wpiterator (L, it);
}
static int
collection_manager_collect_global (lua_State *L)
{
WpGlobalProxy *global = wplua_checkobject (L, 1, WP_TYPE_GLOBAL_PROXY);
const char *name = luaL_checkstring (L, 2);
g_autoptr (WpCollectionManager) cm = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushboolean (L, FALSE);
return 1;
}
lua_pushboolean (L, wp_collection_manager_collect_global (cm, global, name));
return 1;
}
static int
collection_manager_drop_global (lua_State *L)
{
WpGlobalProxy *global = wplua_checkobject (L, 1, WP_TYPE_GLOBAL_PROXY);
g_autoptr (WpCollectionManager) cm = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushboolean (L, FALSE);
return 1;
}
lua_pushboolean (L, wp_collection_manager_drop_global (cm, global));
return 1;
}
static int
collection_manager_get_global_collection (lua_State *L)
{
WpGlobalProxy *global = wplua_checkobject (L, 1, WP_TYPE_GLOBAL_PROXY);
g_autoptr (WpCollectionManager) cm = NULL;
WpCollection *c = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushnil (L);
return 1;
}
c = wp_collection_manager_get_global_collection (cm, global);
if (c)
wplua_pushobject (L, c);
else
lua_pushnil (L);
return 1;
}
static int
collection_manager_is_global_collected (lua_State *L)
{
WpGlobalProxy *global = wplua_checkobject (L, 1, WP_TYPE_GLOBAL_PROXY);
const gchar *collection_name = NULL;
if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
collection_name = luaL_checkstring (L, 2);
g_autoptr (WpCollectionManager) cm = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushboolean (L, FALSE);
return 1;
}
lua_pushboolean (L, wp_collection_manager_is_global_collected (cm, global,
collection_name));
return 1;
}
static int
collection_manager_iterate_globals (lua_State *L)
{
const gchar *collection_name = NULL;
if (!lua_isnone (L, 1) && !lua_isnil (L, 1))
collection_name = luaL_checkstring (L, 1);
g_autoptr (WpCollectionManager) cm = NULL;
g_autoptr (WpIterator) it = NULL;
cm = wp_collection_manager_find (get_wp_core (L), NULL);
if (!cm) {
lua_pushnil (L);
return 1;
}
it = wp_collection_manager_new_global_iterator (cm, collection_name);
return push_wpiterator (L, it);
}
static const luaL_Reg collection_manager_funcs[] = {
{ "create_collection", collection_manager_create_collection },
{ "destroy_collection", collection_manager_destroy_collection },
{ "has_collection", collection_manager_has_collection },
{ "get_collection", collection_manager_get_collection },
{ "get_collection_count", collection_manager_get_collection_count },
{ "iterate_collections", collection_manager_iterate_collections },
{ "collect_global", collection_manager_collect_global },
{ "drop_global", collection_manager_drop_global },
{ "get_global_collection", collection_manager_get_global_collection },
{ "is_global_collected", collection_manager_is_global_collected },
{ "iterate_globals", collection_manager_iterate_globals },
{ NULL, NULL }
};
/* WpEventHook */
static int
@ -3205,6 +3498,9 @@ wp_lua_scripting_api_init (lua_State *L)
luaL_newlib (L, event_dispatcher_funcs);
lua_setglobal (L, "WpEventDispatcher");
luaL_newlib (L, collection_manager_funcs);
lua_setglobal (L, "WpCollectionManager");
wp_lua_scripting_pod_init (L);
wp_lua_scripting_json_init (L);
@ -3268,6 +3564,8 @@ wp_lua_scripting_api_init (lua_State *L)
properties_new, properties_funcs);
wplua_register_type_methods (L, WP_TYPE_PERMISSION_MANAGER,
permission_manager_new, permission_manager_funcs);
wplua_register_type_methods (L, WP_TYPE_COLLECTION,
NULL, collection_funcs);
if (!wplua_load_uri (L, URI_API, &error) ||
!wplua_pcall (L, 0, 0, &error)) {

View file

@ -237,6 +237,7 @@ SANDBOX_EXPORT = {
LocalModule = WpImplModule_new,
ImplMetadata = WpImplMetadata_new,
Settings = WpSettings,
CollectionManager = WpCollectionManager,
Conf = WpConf,
JsonUtils = JsonUtils,
ProcUtils = ProcUtils,