From 911276b2630fcc43df0ac53dbc0adb699be94227 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Thu, 27 Oct 2016 20:02:25 +0200 Subject: degesch: add introspection for "app_context" --- degesch.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/degesch.c b/degesch.c index d6cfdec..4f42fc5 100644 --- a/degesch.c +++ b/degesch.c @@ -2110,6 +2110,18 @@ struct app_context } *g_ctx; +static struct ispect_field g_ctx_ispect_fields[] = +{ + ISPECT_( app_context, global_buffer, REF, buffer ) + ISPECT_( app_context, current_buffer, REF, buffer ) + {} +}; + +static struct ispect g_ctx_ispect = +{ + .fields = g_ctx_ispect_fields, +}; + static int * filter_color_cube_for_acceptable_nick_colors (size_t *len) { @@ -8570,7 +8582,9 @@ lua_weak_push (struct lua_plugin *plugin, void *object, wrapper->plugin = plugin; wrapper->info = info; wrapper->object = object; - wrapper->weak_ref = info->ref (object, lua_weak_invalidate, wrapper); + wrapper->weak_ref = NULL; + if (info->ref) + wrapper->weak_ref = info->ref (object, lua_weak_invalidate, wrapper); lua_cache_store (L, object, -1); } @@ -8581,7 +8595,8 @@ lua_weak_gc (lua_State *L, const struct lua_weak_info *info) if (wrapper->object) { lua_cache_invalidate (L, wrapper->object); - info->unref (wrapper->object, &wrapper->weak_ref); + if (info->unref) + info->unref (wrapper->object, &wrapper->weak_ref); wrapper->object = NULL; } return 0; @@ -8616,6 +8631,14 @@ LUA_WEAK_DECLARE (channel, XLUA_CHANNEL_METATABLE) LUA_WEAK_DECLARE (buffer, XLUA_BUFFER_METATABLE) LUA_WEAK_DECLARE (server, XLUA_SERVER_METATABLE) +// The global context is kind of fake and don't have any ref-counting, +// however it's still very much an object +static struct lua_weak_info lua_ctx_info = +{ + .name = PROGRAM_NAME, + .ispect = &g_ctx_ispect, +}; + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static int @@ -9730,8 +9753,18 @@ lua_plugin_connect (lua_State *L) return 1; } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +static int +lua_ctx_gc (lua_State *L) +{ + return lua_weak_gc (L, &lua_ctx_info); +} + static luaL_Reg lua_plugin_library[] = { + // These are global functions: + { "parse", lua_plugin_parse }, { "hook_input", lua_plugin_hook_input }, { "hook_irc", lua_plugin_hook_irc }, @@ -9739,6 +9772,10 @@ static luaL_Reg lua_plugin_library[] = { "hook_timer", lua_plugin_hook_timer }, { "setup_config", lua_plugin_setup_config }, { "connect", lua_plugin_connect }, + + // And these are methods: + { "__gc", lua_ctx_gc }, + // TODO: get_prompt_ignore_markers() { NULL, NULL }, }; @@ -9908,7 +9945,6 @@ lua_plugin_reg_finish (lua_State *L, struct lua_weak_info *info) lua_setfield (L, -2, "__index"); lua_pushcfunction (L, lua_plugin_property_set); lua_setfield (L, -2, "__newindex"); - lua_pop (L, 1); } @@ -9948,12 +9984,17 @@ lua_plugin_load (struct app_context *ctx, const char *filename, plugin->ctx = ctx; plugin->L = L; - // Register the degesch library with "plugin" as an upvalue luaL_checkversion (L); - luaL_newlibtable (L, lua_plugin_library); + + // Register the degesch library as a singleton with "plugin" as an upvalue + // (mostly historical, but rather convenient) + luaL_newmetatable (L, lua_ctx_info.name); lua_pushlightuserdata (L, plugin); luaL_setfuncs (L, lua_plugin_library, 1); - lua_setglobal (L, PROGRAM_NAME); + lua_plugin_reg_finish (L, &lua_ctx_info); + + lua_weak_push (plugin, ctx, &lua_ctx_info); + lua_setglobal (L, lua_ctx_info.name); // Create metatables for our objects lua_plugin_reg_meta (L, XLUA_HOOK_METATABLE, lua_hook_table); -- cgit v1.2.3-70-g09d2