aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-11-24 21:52:16 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2015-11-24 21:52:31 +0100
commit4a287a724e257dbc9cd9b8a0c48bd15451acd2fe (patch)
tree71e5968ed9f1bbb2712c7e0df13933a1d6a8e1ce
parent87e1236b3076539eeba108c04483e5429f176a3b (diff)
downloadxK-4a287a724e257dbc9cd9b8a0c48bd15451acd2fe.tar.gz
xK-4a287a724e257dbc9cd9b8a0c48bd15451acd2fe.tar.xz
xK-4a287a724e257dbc9cd9b8a0c48bd15451acd2fe.zip
degesch: Lua: add a "server" property to buffers
-rw-r--r--degesch.c138
1 files changed, 78 insertions, 60 deletions
diff --git a/degesch.c b/degesch.c
index 90f441c..4cba07c 100644
--- a/degesch.c
+++ b/degesch.c
@@ -7362,40 +7362,6 @@ struct lua_buffer
struct weak_ref_link *weak_ref; ///< A weak reference link
};
-static int
-lua_buffer_gc (lua_State *L)
-{
- struct lua_buffer *wrapper = luaL_checkudata (L, 1, XLUA_BUFFER_METATABLE);
- if (wrapper->buffer)
- {
- lua_cache_invalidate (L, wrapper->buffer);
- buffer_weak_unref (wrapper->buffer, &wrapper->weak_ref);
- wrapper->buffer = NULL;
- }
- return 0;
-}
-
-static int
-lua_buffer_log (lua_State *L)
-{
- struct lua_buffer *wrapper = luaL_checkudata (L, 1, XLUA_BUFFER_METATABLE);
- luaL_argcheck (L, wrapper->buffer, 1, "dead reference used");
- const char *message = luaL_checkstring (L, 2);
-
- struct buffer *buffer = wrapper->buffer;
- log_full (wrapper->plugin->ctx, buffer->server, buffer,
- BUFFER_LINE_STATUS, "#s", message);
- return 0;
-}
-
-static luaL_Reg lua_buffer_table[] =
-{
- // TODO: some useful methods or values
- { "__gc", lua_buffer_gc },
- { "log", lua_buffer_log },
- { NULL, NULL }
-};
-
static void
lua_buffer_invalidate (void *object, void *user_data)
{
@@ -7433,6 +7399,84 @@ struct lua_server
struct weak_ref_link *weak_ref; ///< A weak reference link
};
+static void
+lua_server_invalidate (void *object, void *user_data)
+{
+ struct lua_server *wrapper = user_data;
+ wrapper->server = NULL;
+ wrapper->weak_ref = NULL;
+ // This can in theory call the GC, order isn't arbitrary here
+ lua_cache_invalidate (wrapper->plugin->L, object);
+}
+
+static void
+lua_plugin_push_server (struct lua_plugin *plugin, struct server *server)
+{
+ lua_State *L = plugin->L;
+ if (lua_cache_get (L, server))
+ return;
+
+ struct lua_server *wrapper = lua_newuserdata (L, sizeof *wrapper);
+ luaL_setmetatable (L, XLUA_SERVER_METATABLE);
+ wrapper->plugin = plugin;
+ wrapper->server = server;
+ wrapper->weak_ref = server_weak_ref
+ (server, lua_server_invalidate, wrapper);
+ lua_cache_store (L, server, -1);
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+static int
+lua_buffer_gc (lua_State *L)
+{
+ struct lua_buffer *wrapper = luaL_checkudata (L, 1, XLUA_BUFFER_METATABLE);
+ if (wrapper->buffer)
+ {
+ lua_cache_invalidate (L, wrapper->buffer);
+ buffer_weak_unref (wrapper->buffer, &wrapper->weak_ref);
+ wrapper->buffer = NULL;
+ }
+ return 0;
+}
+
+static int
+lua_buffer_get_server (lua_State *L)
+{
+ struct lua_buffer *wrapper = luaL_checkudata (L, 1, XLUA_BUFFER_METATABLE);
+ luaL_argcheck (L, wrapper->buffer, 1, "dead reference used");
+
+ if (wrapper->buffer->server)
+ lua_plugin_push_server (wrapper->plugin, wrapper->buffer->server);
+ else
+ lua_pushnil (L);
+ return 1;
+}
+
+static int
+lua_buffer_log (lua_State *L)
+{
+ struct lua_buffer *wrapper = luaL_checkudata (L, 1, XLUA_BUFFER_METATABLE);
+ luaL_argcheck (L, wrapper->buffer, 1, "dead reference used");
+ const char *message = luaL_checkstring (L, 2);
+
+ struct buffer *buffer = wrapper->buffer;
+ log_full (wrapper->plugin->ctx, buffer->server, buffer,
+ BUFFER_LINE_STATUS, "#s", message);
+ return 0;
+}
+
+static luaL_Reg lua_buffer_table[] =
+{
+ // TODO: some useful methods or values
+ { "__gc", lua_buffer_gc },
+ { "get_server", lua_buffer_get_server },
+ { "log", lua_buffer_log },
+ { NULL, NULL }
+};
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
static int
lua_server_gc (lua_State *L)
{
@@ -7479,32 +7523,6 @@ static luaL_Reg lua_server_table[] =
{ NULL, NULL }
};
-static void
-lua_server_invalidate (void *object, void *user_data)
-{
- struct lua_server *wrapper = user_data;
- wrapper->server = NULL;
- wrapper->weak_ref = NULL;
- // This can in theory call the GC, order isn't arbitrary here
- lua_cache_invalidate (wrapper->plugin->L, object);
-}
-
-static void
-lua_plugin_push_server (struct lua_plugin *plugin, struct server *server)
-{
- lua_State *L = plugin->L;
- if (lua_cache_get (L, server))
- return;
-
- struct lua_server *wrapper = lua_newuserdata (L, sizeof *wrapper);
- luaL_setmetatable (L, XLUA_SERVER_METATABLE);
- wrapper->plugin = plugin;
- wrapper->server = server;
- wrapper->weak_ref = server_weak_ref
- (server, lua_server_invalidate, wrapper);
- lua_cache_store (L, server, -1);
-}
-
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#define XLUA_HOOK_METATABLE "hook" ///< Identifier for the Lua metatable