lua: allow Conf methods to be indexed or called

Given that we are allowing Conf() to instantiate a new WpConf,
the methods on the class need to be callable in reference to Self while
maintaining API compatibility with being indexable from the table as a
static method.

Allow this by checking if the first argument passed in is userdata.

Signed-off-by: James Calligeros <jcalligeros99@gmail.com>
This commit is contained in:
James Calligeros
2024-04-06 15:44:53 +10:00
parent f769ea8f30
commit d77edf70e9

View File

@@ -1663,13 +1663,24 @@ conf_close (lua_State *L)
static int static int
conf_get_section_as_properties (lua_State *L) conf_get_section_as_properties (lua_State *L)
{ {
const char *section = luaL_checkstring (L, 1); const char *section = NULL;
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L)); g_autoptr (WpConf) conf = NULL;
g_autoptr (WpSpaJson) s = NULL; g_autoptr (WpSpaJson) s = NULL;
g_autoptr (WpProperties) props = NULL; g_autoptr (WpProperties) props = NULL;
int argi = 1;
if (lua_istable (L, 2)) /* check if called as method on object */
props = wplua_table_to_properties (L, 2); if (lua_isuserdata (L, argi)) {
conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
argi++;
} else
conf = wp_core_get_conf (get_wp_core (L));
section = luaL_checkstring (L, argi);
argi++;
if (lua_istable (L, argi))
props = wplua_table_to_properties (L, argi);
else else
props = wp_properties_new_empty (); props = wp_properties_new_empty ();
@@ -1685,9 +1696,20 @@ conf_get_section_as_properties (lua_State *L)
static int static int
conf_get_section_as_object (lua_State *L) conf_get_section_as_object (lua_State *L)
{ {
const char *section = luaL_checkstring (L, 1); const char *section = NULL;
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L)); g_autoptr (WpConf) conf = NULL;
g_autoptr (WpSpaJson) s = NULL; g_autoptr (WpSpaJson) s = NULL;
int argi = 1;
/* check if called as method on object */
if (lua_isuserdata (L, argi)) {
conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
argi++;
} else
conf = wp_core_get_conf (get_wp_core (L));
section = luaL_checkstring (L, argi);
argi++;
if (conf) { if (conf) {
s = wp_conf_get_section (conf, section); s = wp_conf_get_section (conf, section);
@@ -1697,8 +1719,8 @@ conf_get_section_as_object (lua_State *L)
} }
} }
if (lua_istable (L, 2)) if (lua_istable (L, argi))
lua_pushvalue (L, 2); lua_pushvalue (L, argi);
else else
lua_newtable (L); lua_newtable (L);
return 1; return 1;
@@ -1707,9 +1729,20 @@ conf_get_section_as_object (lua_State *L)
static int static int
conf_get_section_as_array (lua_State *L) conf_get_section_as_array (lua_State *L)
{ {
const char *section = luaL_checkstring (L, 1); const char *section = NULL;
g_autoptr (WpConf) conf = wp_core_get_conf (get_wp_core (L)); g_autoptr (WpConf) conf = NULL;
g_autoptr (WpSpaJson) s = NULL; g_autoptr (WpSpaJson) s = NULL;
int argi = 1;
/* check if called as method on object */
if (lua_isuserdata (L, argi)) {
conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
argi++;
} else
conf = wp_core_get_conf (get_wp_core (L));
section = luaL_checkstring (L, argi);
argi++;
if (conf) { if (conf) {
s = wp_conf_get_section (conf, section); s = wp_conf_get_section (conf, section);
@@ -1719,8 +1752,8 @@ conf_get_section_as_array (lua_State *L)
} }
} }
if (lua_istable (L, 2)) if (lua_istable (L, argi))
lua_pushvalue (L, 2); lua_pushvalue (L, argi);
else else
lua_newtable (L); lua_newtable (L);
return 1; return 1;
@@ -1729,15 +1762,25 @@ conf_get_section_as_array (lua_State *L)
static int static int
conf_get_section_as_json (lua_State *L) conf_get_section_as_json (lua_State *L)
{ {
const char *section = luaL_checkstring (L, 1); const char *section = NULL;
g_autoptr (WpConf) conf = NULL; g_autoptr (WpConf) conf = NULL;
g_autoptr (WpSpaJson) s = NULL; g_autoptr (WpSpaJson) s = NULL;
WpSpaJson *fb = NULL; WpSpaJson *fb = NULL;
int argi = 1;
if (lua_isuserdata (L, 2)) /* check if called as method on object */
fb = wplua_checkboxed (L, 2, WP_TYPE_SPA_JSON); if (lua_isuserdata (L, argi)) {
conf = g_object_ref (wplua_checkobject (L, argi, WP_TYPE_CONF));
argi++;
} else
conf = wp_core_get_conf (get_wp_core (L));
section = luaL_checkstring (L, argi);
argi++;
if (lua_isuserdata (L, argi))
fb = wplua_checkboxed (L, argi, WP_TYPE_SPA_JSON);
conf = wp_core_get_conf (get_wp_core (L));
if (conf) { if (conf) {
s = wp_conf_get_section (conf, section); s = wp_conf_get_section (conf, section);
if (!s && fb) if (!s && fb)