Ejemplo n.º 1
0
int li_lua_push_value(lua_State *L, liValue *value) {
	if (NULL == value) {
		lua_pushnil(L);
		return 1;
	}

	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);
	} break;
	case LI_VALUE_HASH: {
		GHashTableIter it;
		gpointer pkey, pvalue;
		lua_newtable(L);

		g_hash_table_iter_init(&it, value->data.hash);
		while (g_hash_table_iter_next(&it, &pkey, &pvalue)) {
			GString *key = pkey;
			liValue *subval = pvalue;
			lua_pushlstring(L, GSTR_LEN(key));
			li_lua_push_value(L, subval);
			lua_rawset(L, -3);
		}
	} 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;
}
Ejemplo n.º 2
0
int li_lua_push_value(lua_State *L, liValue *value) {
	if (NULL == value) {
		lua_pushnil(L);
		return 1;
	}

	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: {
		GArray *list = value->data.list;
		guint i;
		lua_newtable(L);
		for (i = 0; i < list->len; i++) {
			liValue *subval = g_array_index(list, liValue*, i);
			li_lua_push_value(L, subval);
			lua_rawseti(L, -2, i);
		}
	} break;
	case LI_VALUE_HASH: {
		GHashTableIter it;
		gpointer pkey, pvalue;
		lua_newtable(L);

		g_hash_table_iter_init(&it, value->data.hash);
		while (g_hash_table_iter_next(&it, &pkey, &pvalue)) {
			GString *key = pkey;
			liValue *subval = pvalue;
			lua_pushlstring(L, GSTR_LEN(key));
			li_lua_push_value(L, subval);
			lua_rawset(L, -3);
		}
	} 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;
}