]> source.dussan.org Git - rspamd.git/commitdiff
* More fixes to lua api
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 4 Sep 2009 11:53:10 +0000 (15:53 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Fri, 4 Sep 2009 11:53:10 +0000 (15:53 +0400)
src/lua/lua_common.c
src/lua/lua_common.h
src/lua/lua_config.c
src/lua/lua_message.c
src/lua/lua_task.c

index 0480ea1b6ef466c344ab78ec7c3ed8fb5f9e90b4..b52e88df2c34d8b2168c9c846b08bc8256360751 100644 (file)
 
 lua_State *L = NULL;
 const luaL_reg null_reg[] = {
+       {"__tostring", lua_class_tostring},
     {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),
+       {"__tostring", lua_class_tostring},
     {NULL, NULL}
 };
 
@@ -51,24 +53,51 @@ void
 lua_newclass (lua_State *L, const char *classname, const struct luaL_reg *func) 
 {
        luaL_newmetatable (L, classname); /* mt */
-       /* create __index table to place methods */
-       lua_pushstring (L, "__index");    /* mt,"__index" */
-       lua_newtable (L);                 /* mt,"__index",it */
-       /* put class name into class metatable */
+       lua_pushstring(L, "__index");
+       lua_pushvalue(L, -2);  /* pushes the metatable */
+       lua_settable(L, -3);  /* metatable.__index = metatable */
+
        lua_pushstring (L, "class");      /* mt,"__index",it,"class" */
        lua_pushstring (L, classname);    /* mt,"__index",it,"class",classname */
        lua_rawset (L, -3);               /* mt,"__index",it */
-       /* pass all methods that start with _ to the metatable, and all others
-        * to the index table */
-       for (; func->name; func++) {     /* mt,"__index",it */
-               lua_pushstring (L, func->name);
-               lua_pushcfunction (L, func->func);
-               lua_rawset (L, func->name[0] == '_' ? -5: -3);
+    
+       luaL_openlib(L, NULL, func, 0);
+}
+
+int 
+lua_class_tostring (lua_State *L) 
+{
+       char buf[32];
+
+       if (!lua_getmetatable (L, 1)) {
+               goto error;
+       }
+    lua_pushstring (L, "__index");
+    lua_gettable (L, -2);
+
+    if (!lua_istable (L, -1)) {
+                       goto error;
        }
-       lua_rawset (L, -3);               /* mt */
-       lua_pop (L, 1);
+    lua_pushstring (L, "class");
+    lua_gettable (L, -2);
+
+    if (!lua_isstring (L, -1)) {
+               goto error;
+       }
+
+    snprintf (buf, sizeof (buf), "%p", lua_touserdata (L, 1));
+
+    lua_pushfstring (L, "%s: %s", lua_tostring (L, -1), buf);
+
+    return 1;
+
+error:
+    lua_pushstring (L, "invalid object passed to 'lua_common.c:__tostring'");
+    lua_error (L);
+    return 1;
 }
 
+
 void 
 lua_setclass (lua_State *L, const char *classname, int objidx) 
 {
@@ -92,37 +121,37 @@ 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);
+    msg = luaL_checkstring (L, 1);
     msg_err (msg);
     return 1;
 }
 
 static int
-lua_logger__warn (lua_State *L)
+lua_logger_warn (lua_State *L)
 {
     const char *msg;
-    msg = luaL_checkstring (L, 2);
+    msg = luaL_checkstring (L, 1);
     msg_warn (msg);
     return 1;
 }
 
 static int
-lua_logger__info (lua_State *L)
+lua_logger_info (lua_State *L)
 {
     const char *msg;
-    msg = luaL_checkstring (L, 2);
+    msg = luaL_checkstring (L, 1);
     msg_info (msg);
     return 1;
 }
 
 static int
-lua_logger__debug (lua_State *L)
+lua_logger_debug (lua_State *L)
 {
     const char *msg;
-    msg = luaL_checkstring (L, 2);
+    msg = luaL_checkstring (L, 1);
     msg_debug (msg);
     return 1;
 }
@@ -146,8 +175,7 @@ int
 luaopen_logger (lua_State *L)
 {
 
-    lua_newclass (L, "rspamd{logger}", loggerlib_m);
-       luaL_openlib (L, NULL, null_reg, 0);
+       luaL_openlib (L, "rspamd_logger", loggerlib_m, 0);
 
     return 1;
 }
index 886960ab3ca5405ae7fa55dbf38cbb4ab5c9aace..ff475c0a174fb9381628c1255d2bfbf3812a19d3 100644 (file)
@@ -17,6 +17,7 @@ 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);
+int lua_class_tostring (lua_State *L);
 int luaopen_message (lua_State *L);
 int luaopen_task (lua_State *L);
 int luaopen_config (lua_State *L);
index 0879cf835d61fa825d617fb78ef42d96f5727412..edd2bc6eafc5b7e0efedea7e3706561934b312a7 100644 (file)
@@ -34,6 +34,7 @@ static const struct luaL_reg configlib_m[] = {
     LUA_INTERFACE_DEF(config, get_module_opt),
     LUA_INTERFACE_DEF(config, get_metric),
     LUA_INTERFACE_DEF(config, get_all_opt),
+       {"__tostring", lua_class_tostring},
     {NULL, NULL}
 };
 
@@ -42,6 +43,7 @@ LUA_FUNCTION_DEF(metric, register_symbol);
 
 static const struct luaL_reg metriclib_m[] = {
        LUA_INTERFACE_DEF(metric, register_symbol),
+       {"__tostring", lua_class_tostring},
        {NULL, NULL}
 };
 
@@ -177,7 +179,8 @@ lua_metric_register_symbol (lua_State *L)
                callback = luaL_checkstring (L, 4);
                if (name) {
                        cd = g_malloc (sizeof (struct lua_callback_data));
-                       cd->name = g_strdup (name);
+                       cd->name = g_strdup (callback);
+            cd->L = L;
                        register_symbol (&metric->cache, name, weight, lua_metric_symbol_callback, cd);
                }
        }
@@ -188,7 +191,7 @@ int
 luaopen_config (lua_State *L)
 {
     lua_newclass (L, "rspamd{config}", configlib_m);
-       luaL_openlib (L, NULL, null_reg, 0);
+       luaL_openlib (L, "rspamd_config", null_reg, 0);
 
     return 1;
 }
@@ -196,8 +199,8 @@ luaopen_config (lua_State *L)
 int
 luaopen_metric (lua_State *L)
 {
-    lua_newclass (L, "rspamd{metric}", configlib_m);
-       luaL_openlib (L, NULL, null_reg, 0);
+    lua_newclass (L, "rspamd{metric}", metriclib_m);
+       luaL_openlib (L, "rspamd_metric", null_reg, 0);
 
     return 1;
 }
index a99fa070c1d5c69ed76e8685a1795fb1b5ca57f2..bef809230ebade524133d8884febb147975bed97 100644 (file)
@@ -79,6 +79,7 @@ static const struct luaL_reg msglib_m[] = {
        LUA_INTERFACE_DEF(message, set_reply_to),
        LUA_INTERFACE_DEF(message, get_header),
        LUA_INTERFACE_DEF(message, set_header),
+       {"__tostring", lua_class_tostring},
        {NULL, NULL}
 };
 
@@ -167,7 +168,7 @@ int
 luaopen_message (lua_State *L)
 {
        lua_newclass (L, "rspamd{message}", msglib_m);
-       luaL_openlib (L, NULL, null_reg, 0);
+       luaL_openlib (L, "rspamd_message", null_reg, 0);
     
        return 1;
 }
index e8957214479e7851d5ac502ede8d0f2809da406f..6a6ce5bde99c79dd9084a521d46ad5fe9e8e398b 100644 (file)
@@ -37,6 +37,7 @@ static const struct luaL_reg tasklib_m[] = {
        LUA_INTERFACE_DEF(task, insert_result),
        LUA_INTERFACE_DEF(task, get_urls),
        LUA_INTERFACE_DEF(task, get_text_parts),
+       {"__tostring", lua_class_tostring},
        {NULL, NULL}
 };
 
@@ -51,6 +52,7 @@ static const struct luaL_reg textpartlib_m[] = {
        LUA_INTERFACE_DEF(textpart, is_empty),
        LUA_INTERFACE_DEF(textpart, is_html),
        LUA_INTERFACE_DEF(textpart, get_fuzzy),
+       {"__tostring", lua_class_tostring},
        {NULL, NULL}
 };
 
@@ -106,15 +108,18 @@ lua_task_insert_result (lua_State *L)
 static int 
 lua_task_get_urls (lua_State *L)
 {
+    int i = 1;
        struct worker_task *task = lua_check_task (L);
        GList *cur;
        struct uri *url;
 
        if (task != NULL) {
+        lua_newtable (L);
                cur = g_list_first (task->urls);
                while (cur) {
                        url = cur->data;
                        lua_pushstring (L, struri (url));
+            lua_rawseti(L, -2, i++);
                        cur = g_list_next (cur);
                }
        }
@@ -125,20 +130,24 @@ lua_task_get_urls (lua_State *L)
 static int
 lua_task_get_text_parts (lua_State *L)
 {
-
+    int i = 1;
        struct worker_task *task = lua_check_task (L);
        GList *cur;
        struct mime_text_part *part, **ppart;
 
        if (task != NULL) {
+        lua_newtable (L);
                cur = task->text_parts;
                while (cur) {
                        part = cur->data;
                        ppart = lua_newuserdata (L, sizeof (struct mime_text_part *));
-                       lua_setclass (L, "rspamd{textpart}", -1);
                        *ppart = part;
+                       lua_setclass (L, "rspamd{textpart}", -1);
+            /* Make it array */
+            lua_rawseti(L, -2, i++);
                        cur = g_list_next (cur);
                }
+        return 1;
        }
        lua_pushnil (L);
        return 1;
@@ -210,7 +219,7 @@ int
 luaopen_task (lua_State *L)
 {
        lua_newclass (L, "rspamd{task}", tasklib_m);
-       luaL_openlib (L, NULL, null_reg, 0);
+       luaL_openlib (L, "rspamd_task", null_reg, 0);
     
        return 1;
 }
@@ -219,7 +228,7 @@ int
 luaopen_textpart (lua_State *L)
 {
        lua_newclass (L, "rspamd{textpart}", textpartlib_m);
-       luaL_openlib (L, NULL, null_reg, 0);
+       luaL_openlib (L, "rspamd_textpart", null_reg, 0);
     
        return 1;
 }