2
0

Compare commits

...

11 Commits

Author SHA1 Message Date
60e2ada142 [lua] use luaL_checkinteger instead of luaL_checkint (deprecated in lua5.3)
Change-Id: I17603746e49f0497e685d6413e1f65c6e78036ce
2024-12-27 22:08:26 +01:00
35ad3e0f6c [lua] use lua5.2+ lua_rawlen instead of lua_objlen
Change-Id: I9ad64c1632d08907a64fc02b5e54b62a2d6a5550
2024-12-27 22:08:26 +01:00
96f43af312 [lua] add li_lua_equal wrapper to use lua_compare on lua5.2 instead of lua_equal
Change-Id: I4f329c0ce37460fb131f471ba0fff248cb265c2d
2024-12-27 22:08:26 +01:00
c73bbae2a0 [lua] replace lua5.1 luaL_register with wrapper for lua5.2 luaL_setfuncs
Change-Id: I4435954924daf53547fe61723400949431c0508f
2024-12-27 22:08:26 +01:00
e88bb171f1 [lua] get rid of lua_getfenv when modifying env for request context _G for lua actions
Store environment created for lua function by ref for easy modification.

Change-Id: Ia265a38e40436362609f3981ef0370b4948e4c7e
2024-12-27 22:08:26 +01:00
0de5da97c8 [lua] provide li_lua_setfenv for lua5.2+
Change-Id: I1b3a5e6e177d4c02b36a6c88c917163708153f8a
2024-12-27 22:08:22 +01:00
333aecdd79 [lua] make remaining LUA_GLOBALSINDEX usages to manage global environment lua5.2+ compatible
Change-Id: I22dd572caedc5d6833a94efa1e085caf55566a29
2024-12-27 20:45:18 +01:00
8aeb14ab3d [mod_lua] LUA_GLOBALSINDEX should be the default environment of a new function, no need to set it
Change-Id: Ifa95d55b24461f9d81078216a232920f9b8f28f6
2024-12-27 20:02:32 +01:00
e93c264981 [lua] get rid of LUA_GLOBALSINDEX in lua_settop_in_dicts
Change-Id: I5ba10af08a9023ffcdb24ecfbad64e23bde20a57
2024-12-27 19:47:46 +01:00
6c9508cf8f [lua] use protected call to "tostring" in li_lua_print_get_string
Change-Id: If731428ef391305b78981a8eed2084e2504bb492
2024-12-27 19:41:24 +01:00
6e4a9f751b [lua] use lua_{get,set}global instead of lua_{get,set}field with LUA_GLOBALSINDEX
Change-Id: Ib38c74605e7b6818d31bfef064f3ba1da6545f49
2024-12-27 19:40:06 +01:00
18 changed files with 178 additions and 102 deletions

View File

@ -125,6 +125,8 @@ 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 void li_lua_setfenv(lua_State *L, int funcindex); /* -1 */
/* internal: subrequests (vrequest metamethod) */
LI_API int li_lua_vrequest_subrequest(lua_State *L);
@ -156,4 +158,24 @@ INLINE int li_lua_new_protected_metatable(lua_State *L, const char *tname) {
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

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

View File

@ -28,20 +28,28 @@
/* 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) {
int ndx, curtable;
int ndx, curtable = 0;
gchar** segments;
size_t i;
ndx = lua_gettop(L);
segments = g_strsplit(path, ".", 10);
LI_FORCE_ASSERT(segments[0]);
for (i = 0, curtable = LUA_GLOBALSINDEX; segments[i+1]; i++) {
lua_getfield(L, curtable, segments[i]);
for (i = 0; segments[i+1]; i++) {
if (curtable == 0) {
lua_getglobal(L, segments[i]);
} else {
lua_getfield(L, curtable, segments[i]);
}
if (lua_isnil(L, -1) || !lua_istable(L, -1)) {
lua_pop(L, 1); /* pop nil */
lua_newtable(L);
lua_pushvalue(L, -1); /* save table in field */
lua_setfield(L, curtable, segments[i]);
if (curtable == 0) {
lua_setglobal(L, segments[i]);
} else {
lua_setfield(L, curtable, segments[i]);
}
}
curtable = lua_gettop(L);
}
@ -57,7 +65,7 @@ liCondition* li_lua_get_condition(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_CONDITION);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
return NULL;
}
@ -69,7 +77,7 @@ static liConditionLValue* lua_get_condition_lvalue(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return NULL;
if (!lua_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_COND_LVALUE);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
return NULL;
}
@ -81,7 +89,7 @@ static liCondLValue lua_get_cond_lvalue_t(lua_State *L, int ndx) {
if (!lua_isuserdata(L, ndx)) return -1;
if (!lua_getmetatable(L, ndx)) return -1;
luaL_getmetatable(L, LUA_COND_LVALUE_T);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
return -1;
}

View File

@ -173,11 +173,11 @@ gboolean li_config_lua_load(liLuaState *LL, liServer *srv, liWorker *wrk, const
if (allow_setup) {
LI_FORCE_ASSERT(wrk == srv->main_worker);
li_lua_push_setup_table(srv, wrk, L);
lua_setfield(L, LUA_GLOBALSINDEX, "setup");
lua_setglobal(L, "setup");
}
li_lua_push_action_table(srv, wrk, L);
lua_setfield(L, LUA_GLOBALSINDEX, "action");
lua_setglobal(L, "action");
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_getfield(L, LUA_GLOBALSINDEX, "actions");
lua_getglobal(L, "actions");
*pact = li_lua_get_action_ref(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) {
if (!lua_isstring(L, 1)) /* 'message' not a string? */
return 1; /* keep it intact */
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
lua_getglobal(L, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
@ -96,32 +96,62 @@ int li_lua_metatable_index(lua_State *L) {
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) {
/* backup global table reference */
lua_pushvalue(L, LUA_GLOBALSINDEX); /* +1 */
li_lua_push_globals(L); /* +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) {
int i, n = lua_gettop(L);
GString *buf = g_string_sized_new(0);
lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
lua_getglobal(L, "tostring");
for (i = from; i <= to; i++) {
const char *s;
size_t len;
lua_pushvalue(L, n+1);
lua_pushvalue(L, i);
lua_call(L, 1, 1);
if (0 != lua_pcall(L, 1, 1, 0)) goto failed;
s = lua_tolstring(L, -1, &len);
lua_pop(L, 1);
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 (NULL == s) goto failed;
if (0 == len) continue;
if (buf->len > 0) {
g_string_append_c(buf, ' ');
@ -132,6 +162,12 @@ GString* li_lua_print_get_string(lua_State *L, int from, int to) {
}
lua_pop(L, 1);
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) {
@ -369,7 +405,7 @@ void li_lua_init2(liLuaState *LL, liServer *srv, liWorker *wrk) {
lua_pushlightuserdata(L, wrk);
lua_pushcclosure(L, li_lua_error, 2);
lua_pushvalue(L, -1); /* overwrite global print too */
lua_setfield(L, LUA_GLOBALSINDEX, "print");
lua_setglobal(L, "print");
lua_pushvalue(L, -1); /* lighty.error alias */
lua_setfield(L, -3, "error");
lua_setfield(L, -2, "print");
@ -411,32 +447,13 @@ void li_lua_init2(liLuaState *LL, liServer *srv, liWorker *wrk) {
/* associate metatable(lighty) */
lua_setmetatable(L, -2);
/* store "lighty" object in globals */
lua_setfield(L, LUA_GLOBALSINDEX, "lighty");
lua_setglobal(L, "lighty");
li_lua_store_globals(L);
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) {
GHashTableIter *it = lua_touserdata(L, lua_upvalueindex(1));
gpointer pkey = NULL, pvalue = NULL;
@ -459,3 +476,29 @@ int li_lua_ghashtable_gstring_pairs(lua_State *L, GHashTable *ht) {
lua_pushnil(L); lua_pushnil(L); /* +2 */
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) {
luaL_register(L, NULL, environment_mt);
li_lua_setfuncs(L, environment_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_ENVIRONMENT);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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) {
luaL_register(L, NULL, filter_mt);
li_lua_setfuncs(L, filter_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_FILTER);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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) {
luaL_register(L, NULL, http_headers_mt);
li_lua_setfuncs(L, http_headers_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_HTTPHEADERS);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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) {
luaL_register(L, NULL, physical_mt);
li_lua_setfuncs(L, physical_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_PHYSICAL);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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) {
luaL_register(L, NULL, request_mt);
li_lua_setfuncs(L, request_mt);
}
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) {
luaL_register(L, NULL, requesturi_mt);
li_lua_setfuncs(L, requesturi_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_REQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_REQUESTURI);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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) {
int status = luaL_checkint(L, 3);
int status = (int) luaL_checkinteger(L, 3);
if (status < 200 || status > 999) {
lua_pushliteral(L, "Invalid http response 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) {
luaL_register(L, NULL, response_mt);
li_lua_setfuncs(L, response_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_RESPONSE);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_STAT);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
return NULL;
}
@ -203,7 +203,7 @@ static const luaL_Reg stat_mt[] = {
};
static HEDLEY_NEVER_INLINE void init_stat_mt(lua_State *L) {
luaL_register(L, NULL, stat_mt);
li_lua_setfuncs(L, stat_mt);
}
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) {
luaL_register(L, NULL, subrequest_mt);
li_lua_setfuncs(L, subrequest_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_SUBREQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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;
len = lua_objlen(L, 1);
len = lua_rawlen(L, 1);
for (i = len; i >= 1; lua_pop(L, 1), --i) {
lua_rawgeti(L, 1, i);
if (LUA_TTABLE != lua_type(L, -1)) continue;
if (2 != lua_objlen(L, -1)) continue;
if (2 != lua_rawlen(L, -1)) continue;
lua_rawgeti(L, -1, 1);
switch (lua_type(L, -1)) {
case LUA_TSTRING:
if (nil_key) break;
if (!lua_equal(L, -1, 2)) break;
if (!li_lua_equal(L, -1, 2)) break;
lua_rawgeti(L, -2, 2);
return 1;
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) {
luaL_register(L, NULL, vrequest_mt);
li_lua_setfuncs(L, vrequest_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_VREQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
return NULL;
}
@ -486,7 +486,7 @@ static const luaL_Reg coninfo_mt[] = {
};
static HEDLEY_NEVER_INLINE void init_coninfo_mt(lua_State *L) {
luaL_register(L, NULL, coninfo_mt);
li_lua_setfuncs(L, coninfo_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_CONINFO);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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->setups = g_array_new(TRUE, TRUE, sizeof(liPluginSetup));
lua_getfield(L, LUA_GLOBALSINDEX, "actions");
lua_getglobal(L, "actions");
if (lua_istable(L, -1)) {
liPluginAction plug_action;
int ndx;
@ -384,7 +384,7 @@ static luaPlugin* lua_plugin_create_data(liServer *srv, lua_State *L) {
}
lua_pop(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "setups");
lua_getglobal(L, "setups");
if (lua_istable(L, -1)) {
liPluginSetup plug_setup;
int ndx;
@ -457,16 +457,13 @@ static gboolean lua_plugin_load(liServer *srv, liPlugin *p, GString *filename, l
}
li_lua_push_setup_table(srv, srv->main_worker, L);
lua_setfield(L, LUA_GLOBALSINDEX, "setup");
lua_setglobal(L, "setup");
li_lua_push_action_table(srv, srv->main_worker, L);
lua_setfield(L, LUA_GLOBALSINDEX, "action");
lua_setglobal(L, "action");
li_lua_push_lvalues_dict(srv, L);
lua_pushvalue(L, LUA_GLOBALSINDEX);
lua_setfenv(L, -2);
/* arguments for plugin: local filename, args = ... */
/* 1. 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) {
luaL_register(L, NULL, mc_con_mt);
li_lua_setfuncs(L, mc_con_mt);
lua_pushvalue(L, -1);
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) {
luaL_register(L, NULL, mc_req_mt);
li_lua_setfuncs(L, mc_req_mt);
}
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_MEMCACHEDCON);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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_getmetatable(L, ndx)) return NULL;
luaL_getmetatable(L, LUA_MEMCACHEDREQUEST);
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !lua_equal(L, -1, -2)) {
if (lua_isnil(L, -1) || lua_isnil(L, -2) || !li_lua_equal(L, -1, -2)) {
lua_pop(L, 2);
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_setfield(L, -2, "new");
lua_setfield(L, LUA_GLOBALSINDEX, "memcached");
lua_setglobal(L, "memcached");
li_lua_unlock(LL);
}
}