2
0

Compare commits

..

No commits in common. "60e2ada142ff35e2524119b1e4e253a6fd6d8402" and "87290a137bde74bd00dbf6f4ab06f6535304de63" have entirely different histories.

18 changed files with 102 additions and 178 deletions

View File

@ -125,8 +125,6 @@ LI_API GString* li_lua_print_get_string(lua_State *L, int from, int to);
*/ */
LI_API int li_lua_ghashtable_gstring_pairs(lua_State *L, GHashTable *ht); LI_API int li_lua_ghashtable_gstring_pairs(lua_State *L, GHashTable *ht);
LI_API void li_lua_setfenv(lua_State *L, int funcindex); /* -1 */
/* internal: subrequests (vrequest metamethod) */ /* internal: subrequests (vrequest metamethod) */
LI_API int li_lua_vrequest_subrequest(lua_State *L); LI_API int li_lua_vrequest_subrequest(lua_State *L);
@ -158,24 +156,4 @@ INLINE int li_lua_new_protected_metatable(lua_State *L, const char *tname) {
return r; return r;
} }
INLINE void li_lua_setfuncs(lua_State *L, const luaL_Reg *l) {
#if LUA_VERSION_NUM == 501
luaL_register(L, NULL, l);
#else
luaL_setfuncs(L, l, 0);
#endif
}
INLINE int li_lua_equal(lua_State *L, int index1, int index2) {
#if LUA_VERSION_NUM == 501
return lua_equal(L, index1, index2);
#else
return lua_compare(L, index1, index2, LUA_OPEQ);
#endif
}
#if LUA_VERSION_NUM == 501
#define lua_rawlen(L, index) lua_objlen(L, index)
#endif
#endif #endif

View File

@ -41,7 +41,7 @@ liAction* li_lua_get_action(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_ACTION); luaL_getmetatable(L, LUA_ACTION);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -68,7 +68,6 @@ int li_lua_push_action(liServer *srv, lua_State *L, liAction *a) {
typedef struct lua_action_param lua_action_param; typedef struct lua_action_param lua_action_param;
struct lua_action_param { struct lua_action_param {
int func_ref; int func_ref;
int env_ref;
liLuaState *LL; liLuaState *LL;
}; };
@ -87,26 +86,27 @@ static liHandlerResult lua_action_func(liVRequest *vr, gpointer param, gpointer
li_lua_lock(par->LL); li_lua_lock(par->LL);
/* set _G in environment to request specific table */ lua_rawgeti(L, LUA_REGISTRYINDEX, par->func_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, par->env_ref); /* +1 */ lua_pushvalue(L, -1);
lua_getfenv(L, -1);
if (!ctx) { if (!ctx) {
*context = ctx = g_slice_new0(lua_action_ctx); *context = ctx = g_slice_new0(lua_action_ctx);
lua_newtable(L); /* +1 */ lua_newtable(L);
lua_pushvalue(L, -1); /* +1 */ lua_pushvalue(L, -1);
ctx->g_ref = luaL_ref(L, LUA_REGISTRYINDEX); /* -1 */ ctx->g_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else { } else {
lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->g_ref); /* +1 */ lua_rawgeti(L, LUA_REGISTRYINDEX, ctx->g_ref);
} }
lua_setfield(L, -2, "_G"); /* -1 (pops request context table) */ lua_setfield(L, -2, "_G");
lua_pop(L, 1); /* -1 (pop env) */ lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, par->func_ref); /* +1 */ li_lua_push_vrequest(L, vr);
li_lua_push_vrequest(L, vr); /* +1 */
errfunc = li_lua_push_traceback(L, 1); /* +1, but inserted before `1` args */ errfunc = li_lua_push_traceback(L, 1);
if (lua_pcall(L, 1, 1, errfunc)) { if (lua_pcall(L, 1, 1, errfunc)) {
ERROR(srv, "lua_pcall(): %s", lua_tostring(L, -1)); ERROR(srv, "lua_pcall(): %s", lua_tostring(L, -1));
lua_pop(L, 1); /* -1 (pop error) */ lua_pop(L, 1);
res = LI_HANDLER_ERROR; res = LI_HANDLER_ERROR;
} else { } else {
if (!lua_isnil(L, -1)) { if (!lua_isnil(L, -1)) {
@ -122,15 +122,14 @@ static liHandlerResult lua_action_func(liVRequest *vr, gpointer param, gpointer
res = LI_HANDLER_ERROR; res = LI_HANDLER_ERROR;
} }
} }
lua_pop(L, 1); /* -1 (pop result) */ lua_pop(L, 1);
} }
lua_remove(L, errfunc); /* -1 (should be at the top of the stack) */ lua_remove(L, errfunc);
/* clear _G */ lua_getfenv(L, -1);
lua_rawgeti(L, LUA_REGISTRYINDEX, par->env_ref); /* +1 */ lua_pushnil(L);
lua_pushnil(L); /* +1 */ lua_setfield(L, -2, "_G");
lua_setfield(L, -2, "_G"); /* -1 (pops value nil) */ lua_pop(L, 2);
lua_pop(L, 1); /* -1 (pop env) */
li_lua_unlock(par->LL); li_lua_unlock(par->LL);
@ -162,7 +161,6 @@ static void lua_action_free(liServer *srv, gpointer param) {
L = par->LL->L; L = par->LL->L;
li_lua_lock(par->LL); li_lua_lock(par->LL);
luaL_unref(L, LUA_REGISTRYINDEX, par->env_ref);
luaL_unref(L, LUA_REGISTRYINDEX, par->func_ref); luaL_unref(L, LUA_REGISTRYINDEX, par->func_ref);
lua_gc(L, LUA_GCCOLLECT, 0); lua_gc(L, LUA_GCCOLLECT, 0);
li_lua_unlock(par->LL); li_lua_unlock(par->LL);
@ -195,11 +193,7 @@ liAction* li_lua_make_action(lua_State *L, int ndx) {
li_lua_push_action_table(wrk->srv, wrk, L); /* +1 */ li_lua_push_action_table(wrk->srv, wrk, L); /* +1 */
lua_setfield(L, -2, "action"); /* -1 */ lua_setfield(L, -2, "action"); /* -1 */
} }
/* remember environment */ lua_setfenv(L, -2); /* -1 */
lua_pushvalue(L, -1); /* +1 */
par->env_ref = luaL_ref(L, LUA_REGISTRYINDEX); /* -1 */
/* set environment for function */
li_lua_setfenv(L, -2); /* -1 */
lua_pop(L, 1); /* -1 */ lua_pop(L, 1); /* -1 */
return li_action_new_function(lua_action_func, lua_action_cleanup, lua_action_free, par); return li_action_new_function(lua_action_func, lua_action_cleanup, lua_action_free, par);

View File

@ -264,7 +264,7 @@ static const luaL_Reg chunkqueue_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_chunkqueue_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_chunkqueue_mt(lua_State *L) {
li_lua_setfuncs(L, chunkqueue_mt); luaL_register(L, NULL, chunkqueue_mt);
} }
static void lua_push_chunkqueue_metatable(lua_State *L) { static void lua_push_chunkqueue_metatable(lua_State *L) {
@ -285,7 +285,7 @@ liChunk* li_lua_get_chunk(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_CHUNK); luaL_getmetatable(L, LUA_CHUNK);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -313,7 +313,7 @@ liChunkQueue* li_lua_get_chunkqueue(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_CHUNKQUEUE); luaL_getmetatable(L, LUA_CHUNKQUEUE);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -28,29 +28,21 @@
/* save the top of the stack in dicts described by a '.' separated path */ /* save the top of the stack in dicts described by a '.' separated path */
static void lua_settop_in_dicts(lua_State *L, const gchar *path) { static void lua_settop_in_dicts(lua_State *L, const gchar *path) {
int ndx, curtable = 0; int ndx, curtable;
gchar** segments; gchar** segments;
size_t i; size_t i;
ndx = lua_gettop(L); ndx = lua_gettop(L);
segments = g_strsplit(path, ".", 10); segments = g_strsplit(path, ".", 10);
LI_FORCE_ASSERT(segments[0]); LI_FORCE_ASSERT(segments[0]);
for (i = 0; segments[i+1]; i++) { for (i = 0, curtable = LUA_GLOBALSINDEX; segments[i+1]; i++) {
if (curtable == 0) {
lua_getglobal(L, segments[i]);
} else {
lua_getfield(L, curtable, segments[i]); lua_getfield(L, curtable, segments[i]);
}
if (lua_isnil(L, -1) || !lua_istable(L, -1)) { if (lua_isnil(L, -1) || !lua_istable(L, -1)) {
lua_pop(L, 1); /* pop nil */ lua_pop(L, 1); /* pop nil */
lua_newtable(L); lua_newtable(L);
lua_pushvalue(L, -1); /* save table in field */ lua_pushvalue(L, -1); /* save table in field */
if (curtable == 0) {
lua_setglobal(L, segments[i]);
} else {
lua_setfield(L, curtable, segments[i]); lua_setfield(L, curtable, segments[i]);
} }
}
curtable = lua_gettop(L); curtable = lua_gettop(L);
} }
lua_pushvalue(L, ndx); lua_pushvalue(L, ndx);
@ -65,7 +57,7 @@ liCondition* li_lua_get_condition(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_CONDITION); luaL_getmetatable(L, LUA_CONDITION);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -77,7 +69,7 @@ static liConditionLValue* lua_get_condition_lvalue(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_COND_LVALUE); luaL_getmetatable(L, LUA_COND_LVALUE);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -89,7 +81,7 @@ static liCondLValue lua_get_cond_lvalue_t(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return -1; if (!lua_isuserdata(L, ndx)) return -1;
if (!lua_getmetatable(L, ndx)) return -1; if (!lua_getmetatable(L, ndx)) return -1;
luaL_getmetatable(L, LUA_COND_LVALUE_T); luaL_getmetatable(L, LUA_COND_LVALUE_T);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return -1; return -1;
} }

View File

@ -173,11 +173,11 @@ gboolean li_config_lua_load(liLuaState *LL, liServer *srv, liWorker *wrk, const
if (allow_setup) { if (allow_setup) {
LI_FORCE_ASSERT(wrk == srv->main_worker); LI_FORCE_ASSERT(wrk == srv->main_worker);
li_lua_push_setup_table(srv, wrk, L); li_lua_push_setup_table(srv, wrk, L);
lua_setglobal(L, "setup"); lua_setfield(L, LUA_GLOBALSINDEX, "setup");
} }
li_lua_push_action_table(srv, wrk, L); li_lua_push_action_table(srv, wrk, L);
lua_setglobal(L, "action"); lua_setfield(L, LUA_GLOBALSINDEX, "action");
li_lua_push_lvalues_dict(srv, L); li_lua_push_lvalues_dict(srv, L);
@ -204,7 +204,7 @@ gboolean li_config_lua_load(liLuaState *LL, liServer *srv, liWorker *wrk, const
} }
lua_remove(L, errfunc); lua_remove(L, errfunc);
lua_getglobal(L, "actions"); lua_getfield(L, LUA_GLOBALSINDEX, "actions");
*pact = li_lua_get_action_ref(L, -1); *pact = li_lua_get_action_ref(L, -1);
lua_pop(L, 1); lua_pop(L, 1);

View File

@ -29,7 +29,7 @@ int li_lua_fixindex(lua_State *L, int ndx) {
static int traceback (lua_State *L) { static int traceback (lua_State *L) {
if (!lua_isstring(L, 1)) /* 'message' not a string? */ if (!lua_isstring(L, 1)) /* 'message' not a string? */
return 1; /* keep it intact */ return 1; /* keep it intact */
lua_getglobal(L, "debug"); lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(L, -1)) { if (!lua_istable(L, -1)) {
lua_pop(L, 1); lua_pop(L, 1);
return 1; return 1;
@ -96,62 +96,32 @@ int li_lua_metatable_index(lua_State *L) {
return 0; return 0;
} }
static void li_lua_push_globals(lua_State *L) { /* +1 */
#if LUA_VERSION_NUM == 501
lua_pushvalue(L, LUA_GLOBALSINDEX); /* +1 */
#else
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); /* +1 */
#endif
}
static void li_lua_set_globals(lua_State *L) { /* -1 */
#if LUA_VERSION_NUM == 501
lua_replace(L, LUA_GLOBALSINDEX); /* -1 */
#else
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); /* -1 */
#endif
}
static void li_lua_store_globals(lua_State *L) { static void li_lua_store_globals(lua_State *L) {
/* backup global table reference */ /* backup global table reference */
li_lua_push_globals(L); /* +1 */ lua_pushvalue(L, LUA_GLOBALSINDEX); /* +1 */
lua_setfield(L, LUA_REGISTRYINDEX, LI_LUA_REGISTRY_GLOBALS); /* -1 */ lua_setfield(L, LUA_REGISTRYINDEX, LI_LUA_REGISTRY_GLOBALS); /* -1 */
} }
void li_lua_restore_globals(lua_State *L) {
lua_getfield(L, LUA_REGISTRYINDEX, LI_LUA_REGISTRY_GLOBALS); /* +1 */
li_lua_set_globals(L); /* -1 */
}
void li_lua_new_globals(lua_State *L) {
lua_newtable(L); /* +1 */
/* metatable for new global env, link old globals as readonly */
lua_newtable(L); /* +1 */
/* TODO: protect metatable? */
li_lua_push_globals(L); /* +1 */
lua_setfield(L, -2, "__index"); /* -1 */
lua_setmetatable(L, -2); /* -1 */
li_lua_set_globals(L); /* -1 */
}
GString* li_lua_print_get_string(lua_State *L, int from, int to) { GString* li_lua_print_get_string(lua_State *L, int from, int to) {
int i, n = lua_gettop(L); int i, n = lua_gettop(L);
GString *buf = g_string_sized_new(0); GString *buf = g_string_sized_new(0);
lua_getglobal(L, "tostring"); lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
for (i = from; i <= to; i++) { for (i = from; i <= to; i++) {
const char *s; const char *s;
size_t len; size_t len;
lua_pushvalue(L, n+1); lua_pushvalue(L, n+1);
lua_pushvalue(L, i); lua_pushvalue(L, i);
if (0 != lua_pcall(L, 1, 1, 0)) goto failed; lua_call(L, 1, 1);
s = lua_tolstring(L, -1, &len); s = lua_tolstring(L, -1, &len);
lua_pop(L, 1); lua_pop(L, 1);
if (NULL == s) goto failed; if (NULL == s) {
g_string_free(buf, TRUE);
lua_pushliteral(L, "lua_print_get_string: Couldn't convert parameter to string");
lua_error(L);
}
if (0 == len) continue; if (0 == len) continue;
if (buf->len > 0) { if (buf->len > 0) {
g_string_append_c(buf, ' '); g_string_append_c(buf, ' ');
@ -162,12 +132,6 @@ GString* li_lua_print_get_string(lua_State *L, int from, int to) {
} }
lua_pop(L, 1); lua_pop(L, 1);
return buf; return buf;
failed:
g_string_free(buf, TRUE);
lua_pushliteral(L, "lua_print_get_string: Couldn't convert parameter to string");
lua_error(L);
return NULL; /* should be unreachable */
} }
static int li_lua_error(lua_State *L) { static int li_lua_error(lua_State *L) {
@ -405,7 +369,7 @@ void li_lua_init2(liLuaState *LL, liServer *srv, liWorker *wrk) {
lua_pushlightuserdata(L, wrk); lua_pushlightuserdata(L, wrk);
lua_pushcclosure(L, li_lua_error, 2); lua_pushcclosure(L, li_lua_error, 2);
lua_pushvalue(L, -1); /* overwrite global print too */ lua_pushvalue(L, -1); /* overwrite global print too */
lua_setglobal(L, "print"); lua_setfield(L, LUA_GLOBALSINDEX, "print");
lua_pushvalue(L, -1); /* lighty.error alias */ lua_pushvalue(L, -1); /* lighty.error alias */
lua_setfield(L, -3, "error"); lua_setfield(L, -3, "error");
lua_setfield(L, -2, "print"); lua_setfield(L, -2, "print");
@ -447,13 +411,32 @@ void li_lua_init2(liLuaState *LL, liServer *srv, liWorker *wrk) {
/* associate metatable(lighty) */ /* associate metatable(lighty) */
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
/* store "lighty" object in globals */ /* store "lighty" object in globals */
lua_setglobal(L, "lighty"); lua_setfield(L, LUA_GLOBALSINDEX, "lighty");
li_lua_store_globals(L); li_lua_store_globals(L);
li_plugins_init_lua(LL, srv, wrk); li_plugins_init_lua(LL, srv, wrk);
} }
void li_lua_restore_globals(lua_State *L) {
lua_getfield(L, LUA_REGISTRYINDEX, LI_LUA_REGISTRY_GLOBALS); /* +1 */
lua_replace(L, LUA_GLOBALSINDEX); /* -1 */
}
void li_lua_new_globals(lua_State *L) {
lua_newtable(L); /* +1 */
/* metatable for new global env, link old globals as readonly */
lua_newtable(L); /* +1 */
/* TODO: protect metatable? */
lua_pushvalue(L, LUA_GLOBALSINDEX); /* +1 */
lua_setfield(L, -2, "__index"); /* -1 */
lua_setmetatable(L, -2); /* -1 */
lua_replace(L, LUA_GLOBALSINDEX); /* -1 */
}
static int ghashtable_gstring_next(lua_State *L) { static int ghashtable_gstring_next(lua_State *L) {
GHashTableIter *it = lua_touserdata(L, lua_upvalueindex(1)); GHashTableIter *it = lua_touserdata(L, lua_upvalueindex(1));
gpointer pkey = NULL, pvalue = NULL; gpointer pkey = NULL, pvalue = NULL;
@ -476,29 +459,3 @@ int li_lua_ghashtable_gstring_pairs(lua_State *L, GHashTable *ht) {
lua_pushnil(L); lua_pushnil(L); /* +2 */ lua_pushnil(L); lua_pushnil(L); /* +2 */
return 3; return 3;
} }
void li_lua_setfenv(lua_State *L, int funcindex) /* -1 */ {
/* set _ENV upvalue of lua function */
#if LUA_VERSION_NUM == 501
lua_setfenv(L, funcindex); /* -1 */
#else
int n;
lua_pushvalue(L, funcindex); /* +1, for consistent index */
for (n = 0; n < 10; ++n) {
const char *name = lua_getupvalue(L, -1, n); /* +1 unless name == NULL */
if (!name) break; /* last upvalue or a C function (empty upvalue names) */
lua_pop(L, 1); /* -1, drop upvalue value */
if (!name[0]) break; /* C function (empty upvalue names) */
if (0 == strcmp(name, "_ENV")) {
lua_pushvalue(L, -2);
if (NULL == lua_setupvalue(L, -2, n)) {
/* shouldn't happen - upvalue index `n` was valid above */
lua_pop(L, 1); /* -1 */
}
break;
}
}
lua_pop(L, 2); /* -2: copy of function and env */
#endif
}

View File

@ -111,7 +111,7 @@ static const luaL_Reg environment_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_env_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_env_mt(lua_State *L) {
li_lua_setfuncs(L, environment_mt); luaL_register(L, NULL, environment_mt);
} }
static void lua_push_environment_metatable(lua_State *L) { static void lua_push_environment_metatable(lua_State *L) {
@ -129,7 +129,7 @@ liEnvironment* li_lua_get_environment(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_ENVIRONMENT); luaL_getmetatable(L, LUA_ENVIRONMENT);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -109,7 +109,7 @@ static const luaL_Reg filter_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_filter_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_filter_mt(lua_State *L) {
li_lua_setfuncs(L, filter_mt); luaL_register(L, NULL, filter_mt);
} }
static void lua_push_filter_metatable(lua_State *L) { static void lua_push_filter_metatable(lua_State *L) {
@ -127,7 +127,7 @@ liFilter* li_lua_get_filter(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_FILTER); luaL_getmetatable(L, LUA_FILTER);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -199,7 +199,7 @@ static const luaL_Reg http_headers_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_http_headers_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_http_headers_mt(lua_State *L) {
li_lua_setfuncs(L, http_headers_mt); luaL_register(L, NULL, http_headers_mt);
} }
static void lua_push_http_headers_metatable(lua_State *L) { static void lua_push_http_headers_metatable(lua_State *L) {
@ -217,7 +217,7 @@ liHttpHeaders* li_lua_get_http_headers(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_HTTPHEADERS); luaL_getmetatable(L, LUA_HTTPHEADERS);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -125,7 +125,7 @@ static const luaL_Reg physical_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_physical_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_physical_mt(lua_State *L) {
li_lua_setfuncs(L, physical_mt); luaL_register(L, NULL, physical_mt);
} }
static void lua_push_physical_metatable(lua_State *L) { static void lua_push_physical_metatable(lua_State *L) {
@ -143,7 +143,7 @@ liPhysical* li_lua_get_physical(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_PHYSICAL); luaL_getmetatable(L, LUA_PHYSICAL);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -141,7 +141,7 @@ static const luaL_Reg request_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_request_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_request_mt(lua_State *L) {
li_lua_setfuncs(L, request_mt); luaL_register(L, NULL, request_mt);
} }
static void lua_push_request_metatable(lua_State *L) { static void lua_push_request_metatable(lua_State *L) {
@ -282,7 +282,7 @@ static const luaL_Reg requesturi_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_requesturi_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_requesturi_mt(lua_State *L) {
li_lua_setfuncs(L, requesturi_mt); luaL_register(L, NULL, requesturi_mt);
} }
static void lua_push_requesturi_metatable(lua_State *L) { static void lua_push_requesturi_metatable(lua_State *L) {
@ -303,7 +303,7 @@ liRequest* li_lua_get_request(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_REQUEST); luaL_getmetatable(L, LUA_REQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -331,7 +331,7 @@ liRequestUri* li_lua_get_requesturi(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_REQUESTURI); luaL_getmetatable(L, LUA_REQUESTURI);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -16,7 +16,7 @@ static int lua_response_attr_read_status(liResponse *resp, lua_State *L) {
} }
static int lua_response_attr_write_status(liResponse *resp, lua_State *L) { static int lua_response_attr_write_status(liResponse *resp, lua_State *L) {
int status = (int) luaL_checkinteger(L, 3); int status = luaL_checkint(L, 3);
if (status < 200 || status > 999) { if (status < 200 || status > 999) {
lua_pushliteral(L, "Invalid http response status: "); lua_pushliteral(L, "Invalid http response status: ");
lua_pushinteger(L, status); lua_pushinteger(L, status);
@ -120,7 +120,7 @@ static const luaL_Reg response_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_response_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_response_mt(lua_State *L) {
li_lua_setfuncs(L, response_mt); luaL_register(L, NULL, response_mt);
} }
static void lua_push_response_metatable(lua_State *L) { static void lua_push_response_metatable(lua_State *L) {
@ -138,7 +138,7 @@ liResponse* li_lua_get_response(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_RESPONSE); luaL_getmetatable(L, LUA_RESPONSE);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -8,7 +8,7 @@ struct stat* li_lua_get_stat(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_STAT); luaL_getmetatable(L, LUA_STAT);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -203,7 +203,7 @@ static const luaL_Reg stat_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_stat_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_stat_mt(lua_State *L) {
li_lua_setfuncs(L, stat_mt); luaL_register(L, NULL, stat_mt);
} }
static void lua_push_stat_metatable(lua_State *L) { static void lua_push_stat_metatable(lua_State *L) {

View File

@ -154,7 +154,7 @@ static const luaL_Reg subrequest_mt[] = {
static HEDLEY_NEVER_INLINE void init_subrequest_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_subrequest_mt(lua_State *L) {
li_lua_setfuncs(L, subrequest_mt); luaL_register(L, NULL, subrequest_mt);
} }
static void lua_push_subrequest_metatable(lua_State *L) { static void lua_push_subrequest_metatable(lua_State *L) {
@ -172,7 +172,7 @@ static liSubrequest* li_lua_get_subrequest(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_SUBREQUEST); luaL_getmetatable(L, LUA_SUBREQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -26,18 +26,18 @@ static int lua_kvlist_index(lua_State *L) {
if (LUA_TTABLE != lua_type(L, 1)) goto fail; if (LUA_TTABLE != lua_type(L, 1)) goto fail;
len = lua_rawlen(L, 1); len = lua_objlen(L, 1);
for (i = len; i >= 1; lua_pop(L, 1), --i) { for (i = len; i >= 1; lua_pop(L, 1), --i) {
lua_rawgeti(L, 1, i); lua_rawgeti(L, 1, i);
if (LUA_TTABLE != lua_type(L, -1)) continue; if (LUA_TTABLE != lua_type(L, -1)) continue;
if (2 != lua_rawlen(L, -1)) continue; if (2 != lua_objlen(L, -1)) continue;
lua_rawgeti(L, -1, 1); lua_rawgeti(L, -1, 1);
switch (lua_type(L, -1)) { switch (lua_type(L, -1)) {
case LUA_TSTRING: case LUA_TSTRING:
if (nil_key) break; if (nil_key) break;
if (!li_lua_equal(L, -1, 2)) break; if (!lua_equal(L, -1, 2)) break;
lua_rawgeti(L, -2, 2); lua_rawgeti(L, -2, 2);
return 1; return 1;
case LUA_TNIL: case LUA_TNIL:

View File

@ -338,7 +338,7 @@ static const luaL_Reg vrequest_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_vrequest_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_vrequest_mt(lua_State *L) {
li_lua_setfuncs(L, vrequest_mt); luaL_register(L, NULL, vrequest_mt);
} }
static void lua_push_vrequest_metatable(lua_State *L) { static void lua_push_vrequest_metatable(lua_State *L) {
@ -351,7 +351,7 @@ liVRequest* li_lua_get_vrequest(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_VREQUEST); luaL_getmetatable(L, LUA_VREQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -486,7 +486,7 @@ static const luaL_Reg coninfo_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_coninfo_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_coninfo_mt(lua_State *L) {
li_lua_setfuncs(L, coninfo_mt); luaL_register(L, NULL, coninfo_mt);
} }
static void lua_push_coninfo_metatable(lua_State *L) { static void lua_push_coninfo_metatable(lua_State *L) {
@ -507,7 +507,7 @@ liConInfo* li_lua_get_coninfo(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_CONINFO); luaL_getmetatable(L, LUA_CONINFO);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }

View File

@ -358,7 +358,7 @@ static luaPlugin* lua_plugin_create_data(liServer *srv, lua_State *L) {
lp->actions = g_array_new(TRUE, TRUE, sizeof(liPluginAction)); lp->actions = g_array_new(TRUE, TRUE, sizeof(liPluginAction));
lp->setups = g_array_new(TRUE, TRUE, sizeof(liPluginSetup)); lp->setups = g_array_new(TRUE, TRUE, sizeof(liPluginSetup));
lua_getglobal(L, "actions"); lua_getfield(L, LUA_GLOBALSINDEX, "actions");
if (lua_istable(L, -1)) { if (lua_istable(L, -1)) {
liPluginAction plug_action; liPluginAction plug_action;
int ndx; int ndx;
@ -384,7 +384,7 @@ static luaPlugin* lua_plugin_create_data(liServer *srv, lua_State *L) {
} }
lua_pop(L, 1); lua_pop(L, 1);
lua_getglobal(L, "setups"); lua_getfield(L, LUA_GLOBALSINDEX, "setups");
if (lua_istable(L, -1)) { if (lua_istable(L, -1)) {
liPluginSetup plug_setup; liPluginSetup plug_setup;
int ndx; int ndx;
@ -457,13 +457,16 @@ static gboolean lua_plugin_load(liServer *srv, liPlugin *p, GString *filename, l
} }
li_lua_push_setup_table(srv, srv->main_worker, L); li_lua_push_setup_table(srv, srv->main_worker, L);
lua_setglobal(L, "setup"); lua_setfield(L, LUA_GLOBALSINDEX, "setup");
li_lua_push_action_table(srv, srv->main_worker, L); li_lua_push_action_table(srv, srv->main_worker, L);
lua_setglobal(L, "action"); lua_setfield(L, LUA_GLOBALSINDEX, "action");
li_lua_push_lvalues_dict(srv, L); li_lua_push_lvalues_dict(srv, L);
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_setfenv(L, -2);
/* arguments for plugin: local filename, args = ... */ /* arguments for plugin: local filename, args = ... */
/* 1. filename */ /* 1. filename */
lua_pushlstring(L, GSTR_LEN(filename)); lua_pushlstring(L, GSTR_LEN(filename));

View File

@ -912,7 +912,7 @@ static const luaL_Reg mc_con_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_mc_con_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_mc_con_mt(lua_State *L) {
li_lua_setfuncs(L, mc_con_mt); luaL_register(L, NULL, mc_con_mt);
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
} }
@ -991,7 +991,7 @@ static const luaL_Reg mc_req_mt[] = {
}; };
static HEDLEY_NEVER_INLINE void init_mc_req_mt(lua_State *L) { static HEDLEY_NEVER_INLINE void init_mc_req_mt(lua_State *L) {
li_lua_setfuncs(L, mc_req_mt); luaL_register(L, NULL, mc_req_mt);
} }
static void lua_push_mc_req_metatable(lua_State *L) { static void lua_push_mc_req_metatable(lua_State *L) {
@ -1004,7 +1004,7 @@ static liMemcachedCon* li_lua_get_memcached_con(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_MEMCACHEDCON); luaL_getmetatable(L, LUA_MEMCACHEDCON);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -1040,7 +1040,7 @@ static mc_lua_request* li_lua_get_memcached_req(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL; if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL; if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_MEMCACHEDREQUEST); luaL_getmetatable(L, LUA_MEMCACHEDREQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) { if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
lua_pop(L, 2); lua_pop(L, 2);
return NULL; return NULL;
} }
@ -1133,7 +1133,7 @@ static void mod_memcached_lua_init(liLuaState *LL, liServer *srv, liWorker *wrk,
lua_pushcclosure(L, mc_lua_new, 1); lua_pushcclosure(L, mc_lua_new, 1);
lua_setfield(L, -2, "new"); lua_setfield(L, -2, "new");
lua_setglobal(L, "memcached"); lua_setfield(L, LUA_GLOBALSINDEX, "memcached");
li_lua_unlock(LL); li_lua_unlock(LL);
} }
} }