Optimize the lua_c module

This commit is contained in:
lionsoul 2018-10-10 11:54:26 +08:00
parent 253dc12917
commit 9c37b5be7f
2 changed files with 16 additions and 5 deletions

View File

@ -11,8 +11,7 @@
#include <lauxlib.h> #include <lauxlib.h>
#include "../c/ip2region.h" #include "../c/ip2region.h"
#define L_METATABLE_NAME "Ip2region" #define L_METATABLE_NAME "Ip2region_MT"
/** create a new ip2region object with a specified dbFile */ /** create a new ip2region object with a specified dbFile */
static int lua_ip2region_new(lua_State *L) static int lua_ip2region_new(lua_State *L)
@ -186,8 +185,8 @@ static int lua_ip2region_tostring(lua_State *L)
/** module method array */ /** module method array */
static const struct luaL_Reg ip2region_methods[] = { static const struct luaL_Reg ip2region_methods[] = {
{ "new", lua_ip2region_new }, // { "new", lua_ip2region_new },
{ "ip2long", lua_ip2long }, // { "ip2long", lua_ip2long },
{ "memorySearch", lua_ip2region_memory_search }, { "memorySearch", lua_ip2region_memory_search },
{ "binarySearch", lua_ip2region_binary_search }, { "binarySearch", lua_ip2region_binary_search },
{ "btreeSearch", lua_ip2region_btree_search }, { "btreeSearch", lua_ip2region_btree_search },
@ -197,6 +196,13 @@ static const struct luaL_Reg ip2region_methods[] = {
{ NULL, NULL }, { 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 */ /** module open function interface */
int luaopen_Ip2region(lua_State *L) int luaopen_Ip2region(lua_State *L)
{ {
@ -216,6 +222,11 @@ int luaopen_Ip2region(lua_State *L)
* access via object:func in lua block * access via object:func in lua block
*/ */
luaL_setfuncs(L, ip2region_methods, 0); 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; return 1;
} }

View File

@ -58,7 +58,7 @@ while ( true ) do
break; break;
elseif ( line == "quit" ) then elseif ( line == "quit" ) then
break; break;
elseif ( ip2region.ip2long(line) == nil ) then elseif ( Ip2region.ip2long(line) == nil ) then
print("Invalid ip address=", line); print("Invalid ip address=", line);
else else
local data; local data;