From 3215a8457fcedcbc400a9447d06c7f54b0af35a4 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 3 Sep 2009 19:18:55 +0400 Subject: [PATCH] * Make lua api object oriented --- src/lua/lua_common.c | 63 +++++++++++++++++++++++++++---------------- src/lua/lua_common.h | 2 ++ src/lua/lua_config.c | 16 +++++------ src/lua/lua_message.c | 6 ++--- src/lua/lua_task.c | 16 +++++------ 5 files changed, 61 insertions(+), 42 deletions(-) diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 907f7e6b5..0480ea1b6 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -28,18 +28,21 @@ #define MODULE_INIT_FUNC "module_init" lua_State *L = NULL; +const luaL_reg null_reg[] = { + {NULL, NULL} +}; /* Logger methods */ -LUA_FUNCTION_DEF(logger, err); -LUA_FUNCTION_DEF(logger, warn); -LUA_FUNCTION_DEF(logger, info); -LUA_FUNCTION_DEF(logger, debug); +LUA_FUNCTION_DEF(logger, _err); +LUA_FUNCTION_DEF(logger, _warn); +LUA_FUNCTION_DEF(logger, _info); +LUA_FUNCTION_DEF(logger, _debug); static const struct luaL_reg loggerlib_m[] = { - LUA_INTERFACE_DEF(logger, err), - LUA_INTERFACE_DEF(logger, warn), - LUA_INTERFACE_DEF(logger, info), - LUA_INTERFACE_DEF(logger, debug), + LUA_INTERFACE_DEF(logger, _err), + LUA_INTERFACE_DEF(logger, _warn), + LUA_INTERFACE_DEF(logger, _info), + LUA_INTERFACE_DEF(logger, _debug), {NULL, NULL} }; @@ -89,7 +92,7 @@ lua_set_table_index (lua_State *L, const char *index, const char *value) /*** Logger interface ***/ static int -lua_logger_err (lua_State *L) +lua_logger__err (lua_State *L) { const char *msg; msg = luaL_checkstring (L, 2); @@ -98,7 +101,7 @@ lua_logger_err (lua_State *L) } static int -lua_logger_warn (lua_State *L) +lua_logger__warn (lua_State *L) { const char *msg; msg = luaL_checkstring (L, 2); @@ -107,7 +110,7 @@ lua_logger_warn (lua_State *L) } static int -lua_logger_info (lua_State *L) +lua_logger__info (lua_State *L) { const char *msg; msg = luaL_checkstring (L, 2); @@ -116,7 +119,7 @@ lua_logger_info (lua_State *L) } static int -lua_logger_debug (lua_State *L) +lua_logger__debug (lua_State *L) { const char *msg; msg = luaL_checkstring (L, 2); @@ -127,11 +130,24 @@ lua_logger_debug (lua_State *L) /*** Init functions ***/ +int +luaopen_rspamd (lua_State *L) +{ + luaL_openlib(L, "rspamd", null_reg, 0); + /* make version string available to scripts */ + lua_pushstring(L, "_VERSION"); + lua_pushstring(L, RVERSION); + lua_rawset(L, -3); + + return 1; +} + int luaopen_logger (lua_State *L) { - lua_newclass (L, "Rspamd.logger", loggerlib_m); - luaL_openlib (L, "logger", loggerlib_m, 0); + + lua_newclass (L, "rspamd{logger}", loggerlib_m); + luaL_openlib (L, NULL, null_reg, 0); return 1; } @@ -142,13 +158,14 @@ init_lua () if (L == NULL) { L = lua_open (); luaL_openlibs (L); - - luaopen_task (L); - luaopen_message (L); - luaopen_logger (L); - luaopen_config (L); - luaopen_metric (L); - luaopen_textpart (L); + + (void)luaopen_rspamd (L); + (void)luaopen_logger (L); + (void)luaopen_config (L); + (void)luaopen_metric (L); + (void)luaopen_task (L); + (void)luaopen_textpart (L); + (void)luaopen_message (L); } } @@ -172,7 +189,7 @@ init_lua_filters (struct config_file *cfg) /* Call module init function */ pcfg = lua_newuserdata (L, sizeof (struct config_file *)); - lua_setclass (L, "Rspamd.config", -1); + lua_setclass (L, "rspamd{config}", -1); *pcfg = cfg; lua_setglobal (L, "rspamd_config"); /* do the call (1 arguments, 1 result) */ @@ -194,7 +211,7 @@ lua_call_filter (const char *function, struct worker_task *task) lua_getglobal (L, function); ptask = lua_newuserdata (L, sizeof (struct worker_task *)); - lua_setclass (L, "Rspamd.task", -1); + lua_setclass (L, "rspamd{task}", -1); *ptask = task; if (lua_pcall (L, 1, 1, 0) != 0) { diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index bcb69a009..886960ab3 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -12,6 +12,8 @@ #define LUA_FUNCTION_DEF(class, name) static int lua_##class##_##name(lua_State *L) #define LUA_INTERFACE_DEF(class, name) { #name, lua_##class##_##name } +extern const luaL_reg null_reg[]; + void lua_newclass (lua_State *L, const char *classname, const struct luaL_reg *func); void lua_setclass (lua_State *L, const char *classname, int objidx); void lua_set_table_index (lua_State *L, const char *index, const char *value); diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index c385d24f7..0879cf835 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -48,7 +48,7 @@ static const struct luaL_reg metriclib_m[] = { static struct config_file * lua_check_config (lua_State *L) { - void *ud = luaL_checkudata (L, 1, "Rspamd.config"); + void *ud = luaL_checkudata (L, 1, "rspamd{config}"); luaL_argcheck (L, ud != NULL, 1, "'config' expected"); return *((struct config_file **)ud); } @@ -56,7 +56,7 @@ lua_check_config (lua_State *L) static struct metric * lua_check_metric (lua_State *L) { - void *ud = luaL_checkudata (L, 1, "Rspamd.metric"); + void *ud = luaL_checkudata (L, 1, "rspamd{metric}"); luaL_argcheck (L, ud != NULL, 1, "'metric' expected"); return *((struct metric **)ud); } @@ -128,7 +128,7 @@ lua_config_get_metric (lua_State *L) metric = g_hash_table_lookup (cfg->metrics, name); if (metric) { pmetric = lua_newuserdata (L, sizeof (struct metric *)); - lua_setclass (L, "Rspamd.metric", -1); + lua_setclass (L, "rspamd{metric}", -1); *pmetric = metric; return 1; } @@ -154,7 +154,7 @@ lua_metric_symbol_callback (struct worker_task *task, gpointer ud) lua_getglobal (cd->L, cd->name); ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *)); - lua_setclass (cd->L, "Rspamd.task", -1); + lua_setclass (cd->L, "rspamd{task}", -1); *ptask = task; if (lua_pcall(cd->L, 1, 1, 0) != 0) { @@ -187,8 +187,8 @@ lua_metric_register_symbol (lua_State *L) int luaopen_config (lua_State *L) { - lua_newclass (L, "Rspamd.config", configlib_m); - luaL_openlib (L, "config", configlib_m, 0); + lua_newclass (L, "rspamd{config}", configlib_m); + luaL_openlib (L, NULL, null_reg, 0); return 1; } @@ -196,8 +196,8 @@ luaopen_config (lua_State *L) int luaopen_metric (lua_State *L) { - lua_newclass (L, "Rspamd.metric", configlib_m); - luaL_openlib (L, "metric", configlib_m, 0); + lua_newclass (L, "rspamd{metric}", configlib_m); + luaL_openlib (L, NULL, null_reg, 0); return 1; } diff --git a/src/lua/lua_message.c b/src/lua/lua_message.c index 60daf357b..a99fa070c 100644 --- a/src/lua/lua_message.c +++ b/src/lua/lua_message.c @@ -87,7 +87,7 @@ static const struct luaL_reg msglib_m[] = { static GMimeMessage * lua_check_message (lua_State *L) { - void *ud = luaL_checkudata (L, 1, "Rspamd.message"); + void *ud = luaL_checkudata (L, 1, "rspamd{message}"); luaL_argcheck (L, ud != NULL, 1, "'message' expected"); return *((GMimeMessage **)ud); } @@ -166,8 +166,8 @@ lua_message_set_header (lua_State *L) int luaopen_message (lua_State *L) { - lua_newclass (L, "Rspamd.message", msglib_m); - luaL_openlib (L, "message", msglib_m, 0); + lua_newclass (L, "rspamd{message}", msglib_m); + luaL_openlib (L, NULL, null_reg, 0); return 1; } diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 2f55f0d71..e89572144 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -58,7 +58,7 @@ static const struct luaL_reg textpartlib_m[] = { static struct worker_task * lua_check_task (lua_State *L) { - void *ud = luaL_checkudata (L, 1, "Rspamd.task"); + void *ud = luaL_checkudata (L, 1, "rspamd{task}"); luaL_argcheck (L, ud != NULL, 1, "'task' expected"); return *((struct worker_task **)ud); } @@ -66,7 +66,7 @@ lua_check_task (lua_State *L) static struct mime_text_part * lua_check_textpart (lua_State *L) { - void *ud = luaL_checkudata (L, 1, "Rspamd.textpart"); + void *ud = luaL_checkudata (L, 1, "rspamd{textpart}"); luaL_argcheck (L, ud != NULL, 1, "'textpart' expected"); return *((struct mime_text_part **)ud); } @@ -81,7 +81,7 @@ lua_task_get_message (lua_State *L) if (task != NULL) { /* XXX write handler for message object */ pmsg = lua_newuserdata (L, sizeof (GMimeMessage *)); - lua_setclass (L, "Rspamd.message", -1); + lua_setclass (L, "rspamd{message}", -1); *pmsg = task->message; } return 1; @@ -135,7 +135,7 @@ lua_task_get_text_parts (lua_State *L) while (cur) { part = cur->data; ppart = lua_newuserdata (L, sizeof (struct mime_text_part *)); - lua_setclass (L, "Rspamd.textpart", -1); + lua_setclass (L, "rspamd{textpart}", -1); *ppart = part; cur = g_list_next (cur); } @@ -209,8 +209,8 @@ lua_textpart_get_fuzzy (lua_State *L) int luaopen_task (lua_State *L) { - lua_newclass (L, "Rspamd.task", tasklib_m); - luaL_openlib (L, "task", tasklib_m, 0); + lua_newclass (L, "rspamd{task}", tasklib_m); + luaL_openlib (L, NULL, null_reg, 0); return 1; } @@ -218,8 +218,8 @@ luaopen_task (lua_State *L) int luaopen_textpart (lua_State *L) { - lua_newclass (L, "Rspamd.textpart", textpartlib_m); - luaL_openlib (L, "textpart", textpartlib_m, 0); + lua_newclass (L, "rspamd{textpart}", textpartlib_m); + luaL_openlib (L, NULL, null_reg, 0); return 1; } -- 2.39.5