Lua API: Optimize code

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
main
Jianhui Zhao 2018-07-18 20:57:22 +08:00
parent f03acc8b5c
commit 69d7e91fb6
2 changed files with 64 additions and 65 deletions

View File

@ -24,21 +24,25 @@
static void *uh_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
{
void *ret = lua_newuserdata(L, size);
void *obj = lua_newuserdata(L, size);
memset(ret, 0, size);
lua_createtable(L, 0, 2);
memset(obj, 0, size);
/* creare metatable */
lua_newtable(L);
/* metatable.__index = metatable */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, gc);
lua_setfield(L, -2, "__gc");
lua_pushvalue(L, -1);
lua_setmetatable(L, -3);
lua_pushvalue(L, -2);
luaI_openlib(L, NULL, reg, 1);
lua_pushvalue(L, -2);
return ret;
luaL_setfuncs(L, reg, 0);
lua_setmetatable(L, -2);
return obj;
}
static inline void add_all_var(struct uh_client *cl, lua_State *L)
@ -276,7 +280,7 @@ static const luaL_Reg uhttpd_fun[] = {
int luaopen_uhttpd(lua_State *L)
{
lua_createtable(L, 1, 0);
lua_newtable(L);
lua_setglobal(L, "__uh_action_cb");
lua_newtable(L);

View File

@ -26,14 +26,9 @@
/* Compatibility defines */
#if LUA_VERSION_NUM <= 501
#define lua_setuservalue(L, i) lua_setfenv((L), (i))
#define lua_getuservalue(L, i) lua_getfenv((L), (i))
/* NOTE: this only works if nups == 0! */
#define luaL_setfuncs(L, fns, nups) luaL_register((L), NULL, (fns))
#define lua_rawlen(L, i) lua_objlen((L), (i))
#endif
struct lua_uh_server {