lua: implement EventDispatcher.push_event

This commit is contained in:
George Kiagiadakis
2022-03-24 18:18:20 +02:00
committed by Julian Bouzas
parent 5c2565c9b1
commit 4c7cb5145d
2 changed files with 58 additions and 0 deletions

View File

@@ -1685,6 +1685,60 @@ static const luaL_Reg event_methods[] = {
{ NULL, NULL }
};
/* WpEventDispatcher */
static int
event_dispatcher_push_event (lua_State *L)
{
const gchar *type = NULL;
gint priority = 0;
WpProperties *properties = NULL;
GObject *source = NULL;
GObject *subject = NULL;
luaL_checktype (L, 1, LUA_TTABLE);
lua_pushliteral (L, "type");
if (lua_gettable (L, 1) != LUA_TSTRING)
luaL_error (L, "EventDispatcher.push_event: expected 'type' as string");
type = lua_tostring (L, -1);
// not popping to keep the string allocated
lua_pushliteral (L, "priority");
if (lua_gettable (L, 1) != LUA_TNUMBER)
luaL_error (L, "EventDispatcher.push_event: expected 'priority' as number");
priority = lua_tointeger (L, -1);
lua_pop (L, 1);
lua_pushliteral (L, "properties");
if (lua_gettable (L, 1) != LUA_TNIL) {
luaL_checktype (L, -1, LUA_TTABLE);
properties = wplua_table_to_properties (L, -1);
}
lua_pop (L, 1);
lua_pushliteral (L, "source");
if (lua_gettable (L, 1) != LUA_TNIL)
source = wplua_checkobject (L, -1, G_TYPE_OBJECT);
lua_pop (L, 1);
lua_pushliteral (L, "subject");
if (lua_gettable (L, 1) != LUA_TNIL)
subject = wplua_checkobject (L, -1, G_TYPE_OBJECT);
lua_pop (L, 1);
g_autoptr (WpEventDispatcher) dispatcher =
wp_event_dispatcher_get_instance (get_wp_core (L));
WpEvent *event = wp_event_new (type, priority, properties, source, subject);
wp_event_dispatcher_push_event (dispatcher, event);
return 0;
}
static const luaL_Reg event_dispatcher_funcs[] = {
{ "push_event", event_dispatcher_push_event },
{ NULL, NULL }
};
/* WpEventHook */
static int
@@ -1993,6 +2047,9 @@ wp_lua_scripting_api_init (lua_State *L)
luaL_newlib (L, settings_methods);
lua_setglobal (L, "WpSettings");
luaL_newlib (L, event_dispatcher_funcs);
lua_setglobal (L, "WpEventDispatcher");
wp_lua_scripting_pod_init (L);
wp_lua_scripting_json_init (L);

View File

@@ -196,6 +196,7 @@ SANDBOX_EXPORT = {
Log = WpLog,
Core = WpCore,
Plugin = WpPlugin,
EventDispatcher = WpEventDispatcher,
ObjectManager = WpObjectManager_new,
Interest = WpObjectInterest_new,
SessionItem = WpSessionItem_new,