lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


André de Leiradella wrote:
> c:keyDown(rfb.ALT, rfb.TAB)
> 2 3 number
> 3 3 no value

Well, a stripped down test does not show this error (but shows another
not-so-obvious flaw):

static int foo(lua_State *L)
  int i, t;

  /* ignoring the first argument here. */
  top = lua_gettop(L);
  for (i=2; i<=top; i++) {
    printf("%d %d %s\n", i, top, lua_typename(L, lua_type(L, i)));
    if (lua_isnumber(L, i)) {
      printf("number %f\n", lua_tonumber(L, i));
    } else if (lua_isstring(L, i)) {
      printf("string %s\n", lua_tostring(L, i));
    } else {
  return 0;

* foo(nil, 1, 2)
2 3 number
number 1.000000
3 3 number
number 2.000000

* foo(nil, "bar", 1, "1")
2 4 string
string bar
3 4 number
number 1.000000
4 4 string         <---!
number 1.000000    <---!

As you can see lua_isnumber() is not the call you want because it promotes
the argument to a number if possible. Better use switch(lua_type(L, i)).

> I know I can work around this problem, but the question is: why it's not
> working? Could someone enlight me? My build and test environment is
> cygwin 5.1, gcc 3.3.3, lua-5.0.2 with an unmodified config file and a
> Lua interpreter modified only to open the rfb library.

Umm, the stripped down test is working. So the only explanation is that
something else is modifying the Lua stack between the first and the second
iteration of the loop. Maybe one of your callback functions?

Print out the value of lua_gettop() in strategic places. Check for correct
absolute vs. relative indexes. Check if the callbacks push/pop the correct
number of arguments to/from the stack and if they keep it at the same level.