m-lua-scripting: Add WpCollection and WpImplCollection 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 fd8d36e73b
commit bfdec9e653
2 changed files with 165 additions and 0 deletions

View file

@ -2179,6 +2179,166 @@ 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_size (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
lua_pushinteger (L, wp_collection_get_size (c));
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);
guint32 global_id = wp_proxy_get_bound_id (WP_PROXY (g));
lua_pushboolean (L, wp_collection_contains_global (c, global_id));
return 1;
}
static int
collection_collect_global (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
WpGlobalProxy *g = wplua_checkobject (L, 2, WP_TYPE_GLOBAL_PROXY);
guint32 global_id = wp_proxy_get_bound_id (WP_PROXY (g));
wp_collection_collect_global (c, global_id);
return 0;
}
static int
collection_drop_global (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
WpGlobalProxy *g = wplua_checkobject (L, 2, WP_TYPE_GLOBAL_PROXY);
guint32 global_id = wp_proxy_get_bound_id (WP_PROXY (g));
wp_collection_drop_global (c, global_id);
return 0;
}
static int
collection_iterate (lua_State *L)
{
WpCollection *c = wplua_checkobject (L, 1, WP_TYPE_COLLECTION);
WpIterator *it = wp_collection_new_iterator (c);
return push_wpiterator (L, it);
}
static const luaL_Reg collection_funcs[] = {
{ "get_name", collection_get_name },
{ "get_size", collection_get_size },
{ "contains_global", collection_contains_global },
{ "collect_global", collection_collect_global },
{ "drop_global", collection_drop_global },
{ "iterate", collection_iterate },
{ NULL, NULL }
};
/* WpImplCollection */
static int
impl_collection_new (lua_State *L)
{
const char *name = luaL_checkstring (L, 1);
g_autoptr (WpProperties) props = NULL;
if (lua_istable (L, 2))
props = wplua_table_to_properties (L, 2);
else if (!lua_isnone (L, 2) && !lua_isnil (L, 2))
props = wp_properties_ref (wplua_checkboxed (L, 2, WP_TYPE_PROPERTIES));
else
props = wp_properties_new_empty ();
wplua_pushobject (L, wp_impl_collection_new (get_wp_export_core (L),
name, wp_properties_ref (props)));
return 1;
}
static int
impl_collection_get_properties (lua_State *L)
{
WpImplCollection *c = wplua_checkobject (L, 1, WP_TYPE_IMPL_COLLECTION);
WpProperties *props = wp_impl_collection_get_properties (c);
wplua_pushboxed (L, WP_TYPE_PROPERTIES, props);
return 1;
}
static int
impl_collection_get_name (lua_State *L)
{
WpImplCollection *c = wplua_checkobject (L, 1, WP_TYPE_IMPL_COLLECTION);
lua_pushstring (L, wp_impl_collection_get_name (c));
return 1;
}
static int
impl_collection_get_size (lua_State *L)
{
WpImplCollection *c = wplua_checkobject (L, 1, WP_TYPE_IMPL_COLLECTION);
lua_pushinteger (L, wp_impl_collection_get_size (c));
return 1;
}
static int
impl_collection_contains_global (lua_State *L)
{
WpImplCollection *c = wplua_checkobject (L, 1, WP_TYPE_IMPL_COLLECTION);
WpGlobalProxy *g = wplua_checkobject (L, 2, WP_TYPE_GLOBAL_PROXY);
guint32 global_id = wp_proxy_get_bound_id (WP_PROXY (g));
lua_pushboolean (L, wp_impl_collection_contains_global (c, global_id));
return 1;
}
static int
impl_collection_collect_global (lua_State *L)
{
WpImplCollection *c = wplua_checkobject (L, 1, WP_TYPE_IMPL_COLLECTION);
guint32 global_id = luaL_checkinteger (L, 2);
wp_impl_collection_collect_global (c, global_id);
return 0;
}
static int
impl_collection_drop_global (lua_State *L)
{
WpImplCollection *c = wplua_checkobject (L, 1, WP_TYPE_IMPL_COLLECTION);
guint32 global_id = luaL_checkinteger (L, 2);
wp_impl_collection_drop_global (c, global_id);
return 0;
}
static int
impl_collection_iterate (lua_State *L)
{
WpImplCollection *c = wplua_checkobject (L, 1, WP_TYPE_IMPL_COLLECTION);
WpIterator *it = wp_impl_collection_new_iterator (c);
return push_wpiterator (L, it);
}
static const luaL_Reg impl_collection_funcs[] = {
{ "get_properties", impl_collection_get_properties },
{ "get_name", impl_collection_get_name },
{ "get_size", impl_collection_get_size },
{ "contains_global", impl_collection_contains_global },
{ "collect_global", impl_collection_collect_global },
{ "drop_global", impl_collection_drop_global },
{ "iterate", impl_collection_iterate },
{ NULL, NULL }
};
/* WpSettings */
static int
@ -3284,6 +3444,10 @@ 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);
wplua_register_type_methods (L, WP_TYPE_IMPL_COLLECTION,
impl_collection_new, impl_collection_funcs);
if (!wplua_load_uri (L, URI_API, &error) ||
!wplua_pcall (L, 0, 0, &error)) {

View file

@ -236,6 +236,7 @@ SANDBOX_EXPORT = {
State = WpState_new,
LocalModule = WpImplModule_new,
ImplMetadata = WpImplMetadata_new,
ImplCollection = WpImplCollection_new,
Settings = WpSettings,
Conf = WpConf,
JsonUtils = JsonUtils,