From d3b65df599f05d7d965b4c0b22d4db7a459c90bd Mon Sep 17 00:00:00 2001 From: lionsoul Date: Wed, 10 Oct 2018 11:54:26 +0800 Subject: [PATCH] Optimize the lua_c module --- binding/lua_c/lua_ip2region.c | 19 +++++++++++++++---- binding/lua_c/testSearcher.lua | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/binding/lua_c/lua_ip2region.c b/binding/lua_c/lua_ip2region.c index 4c87ef2..c314494 100644 --- a/binding/lua_c/lua_ip2region.c +++ b/binding/lua_c/lua_ip2region.c @@ -11,8 +11,7 @@ #include #include "../c/ip2region.h" -#define L_METATABLE_NAME "Ip2region" - +#define L_METATABLE_NAME "Ip2region_MT" /** create a new ip2region object with a specified dbFile */ static int lua_ip2region_new(lua_State *L) @@ -186,8 +185,8 @@ static int lua_ip2region_tostring(lua_State *L) /** module method array */ static const struct luaL_Reg ip2region_methods[] = { - { "new", lua_ip2region_new }, - { "ip2long", lua_ip2long }, + // { "new", lua_ip2region_new }, + // { "ip2long", lua_ip2long }, { "memorySearch", lua_ip2region_memory_search }, { "binarySearch", lua_ip2region_binary_search }, { "btreeSearch", lua_ip2region_btree_search }, @@ -197,6 +196,13 @@ static const struct luaL_Reg ip2region_methods[] = { { NULL, NULL }, }; +/** module function array */ +static const struct luaL_Reg ip2region_functions[] = { + { "new", lua_ip2region_new }, + { "ip2long", lua_ip2long }, + { NULL, NULL } +}; + /** module open function interface */ int luaopen_Ip2region(lua_State *L) { @@ -216,6 +222,11 @@ int luaopen_Ip2region(lua_State *L) * access via object:func in lua block */ luaL_setfuncs(L, ip2region_methods, 0); + luaL_setfuncs(L, ip2region_functions, 0); + + /* Finally register the object.func functions + * into the table witch at the top of the stack */ + // luaL_newlib(L, ip2region_functions); return 1; } diff --git a/binding/lua_c/testSearcher.lua b/binding/lua_c/testSearcher.lua index 178bfc0..cd13198 100644 --- a/binding/lua_c/testSearcher.lua +++ b/binding/lua_c/testSearcher.lua @@ -58,7 +58,7 @@ while ( true ) do break; elseif ( line == "quit" ) then break; - elseif ( ip2region.ip2long(line) == nil ) then + elseif ( Ip2region.ip2long(line) == nil ) then print("Invalid ip address=", line); else local data;