[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Newbie questions regarding luasocket, tables
- From: mpb <mpb.spam@...>
- Date: Mon, 7 May 2007 12:58:56 -0700
Hi,
I've been using Lua for a couple days now. Lua is fun! I have three questions.
Question 1:
Is there a good reason why socket.select requires array inputs and
fails to work with inputs that are associative tables (for example:
recvt[sock] = sock) ?
According to the documentation, socket.select returns associative
tables "to simplify the test if a specific socket has changed status".
As I often want to add (or remove) a single file description to (or
from) recvt/sendt between calls to select, I find it very useful to
store recvt and sendt in associative table.
I have a tiny patch that "fixes" socket.select so it works with both
arrays and associative tables. The patch is pasted at the bottom of
this email. (I would have attached it, but this list has a no-MIME
policy.)
Question 2:
Does the C implementation of tables really consist of 2 "behind the
scenes" implementations - one for arrays and the other for associative
tables? It feels like this based on how Lua and the libraries are
designed.
Question 3:
It seems like there is no quick way to determine the number key-value
pairs in an associative table (short of iterating through and counting
them all). If I am not missing something, why is there no quick way
to determine the number of key-value pairs in an associative table?
Perhaps the C implementation of Lua counts the number of pairs as they
are added and removed, but this count is not exposed in Lua?
Or does the C implementation not even count the number of pairs in a
table? If not, why not? This would seem to be a very useful number
to have quick access to.
Thanks!
-mpb
--------
--- luasocket-2.0.1.orig/src/select.c 2006-04-26 20:23:21.000000000 -0700
+++ luasocket-2.0.1/src/select.c 2007-05-06 11:57:24.000000000 -0700
@@ -114,17 +114,11 @@
static t_socket collect_fd(lua_State *L, int tab, t_socket max_fd,
int itab, fd_set *set) {
- int i = 1;
if (lua_isnil(L, tab))
return max_fd;
- while (1) {
+ lua_pushnil(L);
+ while (lua_next(L, tab) != 0) {
t_socket fd;
- lua_pushnumber(L, i);
- lua_gettable(L, tab);
- if (lua_isnil(L, -1)) {
- lua_pop(L, 1);
- break;
- }
fd = getfd(L);
if (fd != SOCKET_INVALID) {
FD_SET(fd, set);
@@ -135,7 +129,6 @@
lua_settable(L, itab);
}
lua_pop(L, 1);
- i = i + 1;
}
return max_fd;
}