From f769ea8f30c6ea4639c47c4a6dec3f21884ad400 Mon Sep 17 00:00:00 2001 From: James Calligeros Date: Wed, 3 Apr 2024 22:19:09 +1000 Subject: [PATCH] lua: add constructor and file ops for WpConf This exposes the ability to load a SPA-JSON representation of a WpConf object from an arbitrary file on disk to the Lua API Signed-off-by: James Calligeros --- modules/module-lua-scripting/api/api.c | 46 ++++++++++++++++++++++++ modules/module-lua-scripting/api/api.lua | 3 ++ 2 files changed, 49 insertions(+) diff --git a/modules/module-lua-scripting/api/api.c b/modules/module-lua-scripting/api/api.c index 46df75b5..4eb889b9 100644 --- a/modules/module-lua-scripting/api/api.c +++ b/modules/module-lua-scripting/api/api.c @@ -1618,6 +1618,48 @@ impl_module_new (lua_State *L) /* WpConf */ + +static int +conf_new (lua_State *L) +{ + const char *path = luaL_checkstring (L, 1); + WpProperties *p = NULL; + WpConf *conf = NULL; + + if (lua_istable (L, 2)) { + p = wplua_table_to_properties (L, 2); + } + + conf = wp_conf_new (path, p); + if (conf) { + wplua_pushobject (L, conf); + } else + lua_pushnil (L); + return 1; +} + +static int +conf_open (lua_State *L) +{ + WpConf *conf = wplua_checkobject (L, 1, WP_TYPE_CONF); + g_autoptr (GError) err = NULL; + + if (wp_conf_open (conf, &err)) { + lua_pushnil (L); + } else + lua_pushstring (L, err->message); + return 1; +} + +static int +conf_close (lua_State *L) +{ + WpConf *conf = wplua_checkobject (L, 1, WP_TYPE_CONF); + + wp_conf_close (conf); + return 0; +} + static int conf_get_section_as_properties (lua_State *L) { @@ -1712,6 +1754,8 @@ conf_get_section_as_json (lua_State *L) } static const luaL_Reg conf_methods[] = { + { "open", conf_open }, + { "close", conf_close }, { "get_section_as_properties", conf_get_section_as_properties }, { "get_section_as_object", conf_get_section_as_object }, { "get_section_as_array", conf_get_section_as_array }, @@ -2764,6 +2808,8 @@ wp_lua_scripting_api_init (lua_State *L) async_event_hook_new, NULL); wplua_register_type_methods (L, WP_TYPE_TRANSITION, NULL, transition_methods); + wplua_register_type_methods (L, WP_TYPE_CONF, + conf_new, conf_methods); if (!wplua_load_uri (L, URI_API, &error) || !wplua_pcall (L, 0, 0, &error)) { diff --git a/modules/module-lua-scripting/api/api.lua b/modules/module-lua-scripting/api/api.lua index a000d96c..b53686c2 100644 --- a/modules/module-lua-scripting/api/api.lua +++ b/modules/module-lua-scripting/api/api.lua @@ -184,6 +184,9 @@ local Feature = { }, } +-- Allow calling Conf() to instantiate a new WpConf +WpConf["__new"] = WpConf_new + SANDBOX_EXPORT = { Debug = Debug, Id = Id,