m-lua-scripting: add WpConf Lua API

This commit is contained in:
Julian Bouzas
2022-12-22 13:36:00 -05:00
parent cb80cb750b
commit a7a7e614db
2 changed files with 171 additions and 0 deletions

View File

@@ -1473,6 +1473,173 @@ impl_module_new (lua_State *L)
}
}
/* WpConf */
static int
conf_get_section (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
const char *section;
g_autoptr (WpSpaJson) fb = NULL;
g_autoptr (WpSpaJson) s = NULL;
g_return_val_if_fail (conf, 0);
section = luaL_checkstring (L, 1);
if (lua_isuserdata (L, 2)) {
WpSpaJson *v = wplua_checkboxed (L, 2, WP_TYPE_SPA_JSON);
if (v)
fb = wp_spa_json_ref (v);
}
s = wp_conf_get_section (conf, section, g_steal_pointer (&fb));
if (s)
wplua_pushboxed (L, WP_TYPE_SPA_JSON, g_steal_pointer (&s));
else
lua_pushnil (L);
return 1;
}
static int
conf_get_value (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
const char *section;
const char *key;
g_autoptr (WpSpaJson) fb = NULL;
g_autoptr (WpSpaJson) v = NULL;
g_return_val_if_fail (conf, 0);
section = luaL_checkstring (L, 1);
key = luaL_checkstring (L, 2);
if (lua_isuserdata (L, 3)) {
WpSpaJson *v = wplua_checkboxed (L, 3, WP_TYPE_SPA_JSON);
if (v)
fb = wp_spa_json_ref (v);
}
v = wp_conf_get_value (conf, section, key, g_steal_pointer (&fb));
if (v)
wplua_pushboxed (L, WP_TYPE_SPA_JSON, g_steal_pointer (&v));
else
lua_pushnil (L);
return 1;
}
static int
conf_get_value_boolean (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
const char *section;
const char *key;
gboolean fb;
g_return_val_if_fail (conf, 0);
section = luaL_checkstring (L, 1);
key = luaL_checkstring (L, 2);
fb = lua_toboolean (L, 3) ? TRUE : FALSE;
lua_pushboolean (L, wp_conf_get_value_boolean (conf, section, key, fb));
return 1;
}
static int
conf_get_value_int (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
const char *section;
const char *key;
gint fb;
g_return_val_if_fail (conf, 0);
section = luaL_checkstring (L, 1);
key = luaL_checkstring (L, 2);
fb = luaL_checkinteger (L, 3);
lua_pushinteger (L, wp_conf_get_value_int (conf, section, key, fb));
return 1;
}
static int
conf_get_value_float (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
const char *section;
const char *key;
float fb;
g_return_val_if_fail (conf, 0);
section = luaL_checkstring (L, 1);
key = luaL_checkstring (L, 2);
fb = lua_tonumber (L, 3);
lua_pushnumber (L, wp_conf_get_value_float (conf, section, key, fb));
return 1;
}
static int
conf_get_value_string (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
const char *section;
const char *key;
const char *fb;
g_autofree gchar *str = NULL;
g_return_val_if_fail (conf, 0);
section = luaL_checkstring (L, 1);
key = luaL_checkstring (L, 2);
fb = luaL_checkstring (L, 3);
str = wp_conf_get_value_string (conf, section, key, fb);
lua_pushstring (L, str);
return 1;
}
static int
conf_apply_rules (lua_State *L)
{
g_autoptr (WpConf) conf = wp_conf_get_instance (get_wp_core (L));
g_autoptr (WpProperties) ap = NULL;
const char *section;
g_autoptr (WpProperties) mp = NULL;
g_autoptr (WpSpaJson) fb = NULL;
g_return_val_if_fail (conf, 0);
ap = wp_properties_new_empty ();
section = luaL_checkstring (L, 1);
mp = wplua_table_to_properties (L, 2);
if (lua_isuserdata (L, 3)) {
WpSpaJson *v = wplua_checkboxed (L, 3, WP_TYPE_SPA_JSON);
if (v)
fb = wp_spa_json_ref (v);
}
lua_pushboolean (L, wp_conf_apply_rules (conf, section, mp, ap,
g_steal_pointer (&fb)));
wplua_properties_to_table (L, ap);
return 2;
}
static const luaL_Reg conf_methods[] = {
{ "get_section", conf_get_section },
{ "get_value", conf_get_value },
{ "get_value_boolean", conf_get_value_boolean },
{ "get_value_int", conf_get_value_int },
{ "get_value_float", conf_get_value_float },
{ "get_value_string", conf_get_value_string },
{ "apply_rules", conf_apply_rules },
{ NULL, NULL }
};
/* WpSettings */
static int
settings_get (lua_State *L)
{
@@ -2304,6 +2471,9 @@ wp_lua_scripting_api_init (lua_State *L)
luaL_newlib (L, plugin_funcs);
lua_setglobal (L, "WpPlugin");
luaL_newlib (L, conf_methods);
lua_setglobal (L, "WpConf");
luaL_newlib (L, settings_methods);
lua_setglobal (L, "WpSettings");

View File

@@ -218,6 +218,7 @@ SANDBOX_EXPORT = {
LocalModule = WpImplModule_new,
ImplMetadata = WpImplMetadata_new,
Settings = WpSettings,
Conf = WpConf,
SimpleEventHook = WpSimpleEventHook_new,
AsyncEventHook = WpAsyncEventHook_new,
}