[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Calling Lua from C
- From: Chris <coderight@...>
- Date: Sat, 8 Oct 2005 19:43:47 -0400
Hi, I'm fairly new to Lua and this issue is driving me insane.
I'm trying to call table.getn() from C which seems to work on the Lua
side but not inside the C code (see below). Here is the code in
question:
First the C function:
static int TestCall(lua_State* L)
{
lua_pushstring(L, "table");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_pushstring(L, "getn");
lua_gettable(L, -2);
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
lua_remove(L, -2);
printf("c_num: %d, %d\n", lua_isnumber(L, -1), lua_tonumber(L, -1));
return 1;
}
And the Lua code:
sometable = {}
sometable[1] = 1;
sometable[2] = 2;
print(TestCall(sometable))
Now what this prints out when run is:
c_num: 1, 5
2
Meaning the stack value for the number of elements is 5 on the C side
but magically turns into the correct value of 2 back in Lua. WTF?
Thanks for any help...