2
0
Fork 0
lighttpd2/src/main/value_lua.c

251 lines
5.4 KiB
C
Raw Normal View History

2008-06-28 18:37:28 +00:00
#include <lighttpd/value_lua.h>
#include <lighttpd/condition_lua.h>
#include <lighttpd/actions_lua.h>
2009-12-21 17:19:12 +00:00
#include <lighttpd/core_lua.h>
2008-06-28 18:37:28 +00:00
#include <lauxlib.h>
#define LUA_KVLIST_VALUE "li KeyValue list (string, liValue*)"
static int lua_kvlist_index(lua_State *L) {
guint len, i;
gboolean nil_key;
switch (lua_type(L, 2)) {
case LUA_TNUMBER:
lua_rawget(L, 1);
return 1;
case LUA_TSTRING:
nil_key = FALSE;
break;
case LUA_TNIL:
nil_key = TRUE;
break;
default:
goto fail;
}
if (LUA_TTABLE != lua_type(L, 1)) goto fail;
len = lua_objlen(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;
lua_rawgeti(L, -1, 1);
switch (lua_type(L, -1)) {
case LUA_TSTRING:
if (nil_key) break;
if (!lua_equal(L, -1, 2)) break;
lua_rawgeti(L, -2, 2);
return 1;
case LUA_TNIL:
if (!nil_key) break;
lua_rawgeti(L, -2, 2);
return 1;
default:
break;
}
lua_pop(L, 1);
}
fail:
lua_pushnil(L);
return 1;
}
static void lua_push_kvlist_metatable(lua_State *L) {
if (luaL_newmetatable(L, LUA_KVLIST_VALUE)) {
lua_pushcclosure(L, lua_kvlist_index, 0);
lua_setfield(L, -2, "__index");
}
}
2009-07-09 20:17:24 +00:00
static liValue* li_value_from_lua_table(liServer *srv, lua_State *L, int ndx) {
liValue *val, *entry;
gboolean is_list = FALSE, is_hash = FALSE;
2008-06-28 18:37:28 +00:00
int ikey;
liValue *kv_key, *kv_pair;
val = li_value_new_list();
2008-06-28 18:37:28 +00:00
2009-12-21 17:19:12 +00:00
ndx = li_lua_fixindex(L, ndx);
2008-06-28 18:37:28 +00:00
lua_pushnil(L);
while (lua_next(L, ndx) != 0) {
switch (lua_type(L, -2)) {
case LUA_TNUMBER:
if (is_hash) goto mixerror;
is_list = TRUE;
2009-01-10 14:59:54 +00:00
ikey = lua_tointeger(L, -2) - 1;
2008-06-28 18:37:28 +00:00
if (ikey < 0) {
2009-01-10 14:59:54 +00:00
ERROR(srv, "Invalid key < 0: %i - skipping entry", ikey + 1);
2008-06-28 18:37:28 +00:00
lua_pop(L, 1);
continue;
}
entry = li_value_from_lua(srv, L);
if (NULL == entry) continue;
li_value_list_set(val, ikey, entry);
2008-06-28 18:37:28 +00:00
break;
case LUA_TSTRING:
if (is_list) goto mixerror;
is_hash = TRUE;
kv_key = li_value_new_string(li_lua_togstring(L, -2));
entry = li_value_from_lua(srv, L);
if (NULL == entry) {
li_value_free(kv_key);
2008-06-28 18:37:28 +00:00
continue;
}
kv_pair = li_value_new_list();
li_value_list_append(kv_pair, kv_key);
li_value_list_append(kv_pair, entry);
li_value_list_append(val, kv_pair);
2008-06-28 18:37:28 +00:00
break;
default:
2013-08-21 11:49:49 +00:00
ERROR(srv, "Unexpted key type in table: %s (%i) - skipping entry", lua_typename(L, lua_type(L, -1)), lua_type(L, -1));
2008-06-28 18:37:28 +00:00
lua_pop(L, 1);
break;
}
}
return val;
2008-06-28 18:37:28 +00:00
mixerror:
ERROR(srv, "%s", "Cannot mix list with hash; skipping remaining part of table");
2008-06-28 18:37:28 +00:00
lua_pop(L, 2);
return val;
2008-06-28 18:37:28 +00:00
}
2009-07-09 20:17:24 +00:00
liValue* li_value_from_lua(liServer *srv, lua_State *L) {
liValue *val;
2008-06-28 18:37:28 +00:00
switch (lua_type(L, -1)) {
case LUA_TNIL:
lua_pop(L, 1);
return NULL;
2008-06-28 18:37:28 +00:00
case LUA_TBOOLEAN:
2009-07-09 20:17:24 +00:00
val = li_value_new_bool(lua_toboolean(L, -1));
2008-06-28 18:37:28 +00:00
lua_pop(L, 1);
return val;
2008-06-28 18:37:28 +00:00
case LUA_TNUMBER:
2009-07-09 20:17:24 +00:00
val = li_value_new_number(lua_tonumber(L, -1));
2008-06-28 18:37:28 +00:00
lua_pop(L, 1);
return val;
2008-06-28 18:37:28 +00:00
case LUA_TSTRING:
2009-07-09 20:17:24 +00:00
val = li_value_new_string(li_lua_togstring(L, -1));
2008-06-28 18:37:28 +00:00
lua_pop(L, 1);
return val;
2008-06-28 18:37:28 +00:00
case LUA_TTABLE:
2009-07-09 20:17:24 +00:00
val = li_value_from_lua_table(srv, L, -1);
2008-06-28 18:37:28 +00:00
lua_pop(L, 1);
return val;
2008-06-28 18:37:28 +00:00
case LUA_TUSERDATA:
{ /* check for action */
liAction *a = li_lua_get_action(L, -1);
if (a) {
2009-07-09 20:17:24 +00:00
li_action_acquire(a);
lua_pop(L, 1);
return li_value_new_action(srv, a);
}
}
{ /* check for condition */
liCondition *c = li_lua_get_condition(L, -1);
if (c) {
2009-07-09 20:17:24 +00:00
li_condition_acquire(c);
lua_pop(L, 1);
return li_value_new_condition(srv, c);
}
}
ERROR(srv, "%s", "Unknown lua userdata");
lua_pop(L, 1);
return NULL;
case LUA_TFUNCTION: {
liAction *a = li_lua_make_action(L, -1);
lua_pop(L, 1);
return li_value_new_action(srv, a);
}
2008-06-28 18:37:28 +00:00
case LUA_TLIGHTUSERDATA:
case LUA_TTHREAD:
case LUA_TNONE:
default:
2013-08-21 11:49:49 +00:00
ERROR(srv, "Unexpected lua type: %s (%i)", lua_typename(L, lua_type(L, -1)), lua_type(L, -1));
2008-06-28 18:37:28 +00:00
lua_pop(L, 1);
return NULL;
}
}
2009-07-09 20:17:24 +00:00
GString* li_lua_togstring(lua_State *L, int ndx) {
2008-06-28 18:37:28 +00:00
const char *buf;
size_t len = 0;
GString *str = NULL;
if (lua_type(L, ndx) == LUA_TSTRING) {
buf = lua_tolstring(L, ndx, &len);
if (buf) str = g_string_new_len(buf, len);
} else {
lua_pushvalue(L, ndx);
buf = lua_tolstring(L, -1, &len);
if (buf) str = g_string_new_len(buf, len);
lua_pop(L, 1);
}
return str;
}
2009-12-21 17:19:12 +00:00
int li_lua_push_value(lua_State *L, liValue *value) {
if (NULL == value) {
lua_pushnil(L);
return 1;
}
2009-12-21 17:19:12 +00:00
switch (value->type) {
case LI_VALUE_NONE:
lua_pushnil(L);
break;
case LI_VALUE_BOOLEAN:
lua_pushboolean(L, value->data.boolean);
break;
case LI_VALUE_NUMBER:
lua_pushinteger(L, value->data.number);
break;
case LI_VALUE_STRING:
lua_pushlstring(L, GSTR_LEN(value->data.string));
break;
case LI_VALUE_LIST: {
lua_newtable(L);
LI_VALUE_FOREACH(entry, value)
li_lua_push_value(L, entry);
lua_rawseti(L, -2, _entry_i + 1);
LI_VALUE_END_FOREACH()
/* kvlist lookup for string/nil keys */
lua_push_kvlist_metatable(L);
lua_setmetatable(L, -2);
2009-12-21 17:19:12 +00:00
} break;
case LI_VALUE_ACTION:
li_action_acquire(value->data.val_action.action);
li_lua_push_action(value->data.val_action.srv, L, value->data.val_action.action);
break;
case LI_VALUE_CONDITION:
li_condition_acquire(value->data.val_cond.cond);
li_lua_push_condition(value->data.val_cond.srv, L, value->data.val_cond.cond);
break;
default: /* ignore error and push nil */
lua_pushnil(L);
break;
}
return 1;
}