liValue* li_value_copy(liValue* val) { liValue *n; if (NULL == val) return NULL; switch (val->type) { case LI_VALUE_NONE: return li_value_new_none(); case LI_VALUE_BOOLEAN: return li_value_new_bool(val->data.boolean); case LI_VALUE_NUMBER: return li_value_new_number(val->data.number); case LI_VALUE_STRING: return li_value_new_string(g_string_new_len(GSTR_LEN(val->data.string))); /* list: we have to copy every value in the list! */ case LI_VALUE_LIST: n = li_value_new_list(); g_array_set_size(n->data.list, val->data.list->len); for (guint i = 0; i < val->data.list->len; i++) { g_array_index(n->data.list, liValue*, i) = li_value_copy(g_array_index(val->data.list, liValue*, i)); } return n; /* hash: iterate over hashtable, clone each value */ case LI_VALUE_HASH: n = li_value_new_hash(); { GHashTableIter iter; gpointer k, v; g_hash_table_iter_init(&iter, val->data.hash); while (g_hash_table_iter_next(&iter, &k, &v)) g_hash_table_insert(n->data.hash, g_string_new_len(GSTR_LEN((GString*)k)), li_value_copy((liValue*)v)); } return n; case LI_VALUE_ACTION: li_action_acquire(val->data.val_action.action); n = li_value_new_action(val->data.val_action.srv, val->data.val_action.action); return n; case LI_VALUE_CONDITION: li_condition_acquire(val->data.val_cond.cond); n = li_value_new_condition(val->data.val_cond.srv, val->data.val_cond.cond); return n; } return NULL; }
liValue* li_common_value_copy_(liValue* val) { liValue *n; if (NULL == val) return NULL; switch (val->type) { case LI_VALUE_NONE: return li_value_new_none(); case LI_VALUE_BOOLEAN: return li_value_new_bool(val->data.boolean); case LI_VALUE_NUMBER: return li_value_new_number(val->data.number); case LI_VALUE_STRING: return li_value_new_string(g_string_new_len(GSTR_LEN(val->data.string))); /* list: we have to copy every value in the list! */ case LI_VALUE_LIST: n = li_value_new_list(); g_ptr_array_set_size(n->data.list, val->data.list->len); for (guint i = 0; i < val->data.list->len; i++) { g_ptr_array_index(n->data.list, i) = li_value_copy(g_ptr_array_index(val->data.list, i)); } return n; } return NULL; }
liValue* li_value_from_lua(liServer *srv, lua_State *L) { liValue *val; switch (lua_type(L, -1)) { case LUA_TNIL: lua_pop(L, 1); return NULL; case LUA_TBOOLEAN: val = li_value_new_bool(lua_toboolean(L, -1)); lua_pop(L, 1); return val; case LUA_TNUMBER: val = li_value_new_number(lua_tonumber(L, -1)); lua_pop(L, 1); return val; case LUA_TSTRING: val = li_value_new_string(li_lua_togstring(L, -1)); lua_pop(L, 1); return val; case LUA_TTABLE: val = li_value_from_lua_table(srv, L, -1); lua_pop(L, 1); return val; case LUA_TUSERDATA: { /* check for action */ liAction *a = li_lua_get_action(L, -1); if (a) { 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) { 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); } case LUA_TLIGHTUSERDATA: case LUA_TTHREAD: case LUA_TNONE: default: ERROR(srv, "Unexpected lua type: %s (%i)", lua_typename(L, lua_type(L, -1)), lua_type(L, -1)); lua_pop(L, 1); return NULL; } }