I have the following problem using the native Lua <-> C bridge in the Corona Simulator under OS X. I try to push a C closure onto the stack, everything works fine, but when I try to retrieve the indices of upvalues within the function, I get some crazy values and thus can not access the upvalues.
static int get_closure (lua_State *L)
{
lua_pushnumber(L, 0);
lua_pushcclosure(L, &closure, 1);
return 1;
}
static int closure (lua_State *L)
{
int index = lua_upvalueindex(1); // The index here is something like: -6001001
int upvalue = luaL_checkint(L, index); // ERROR: bad argument
<othe code which is never called>
}
The same code works well in Lua 5.1 and 5.2. Creation of closure and calling the closure function works well in Corona Simulator.
static int closure (lua\_State \*L) { int index = lua\_tointeger( L, lua\_upvalueindex(1) ); // Change int upvalue = luaL\_checkint(L, index); \<other code which is never called\> }
Guys, thanks a lot for the help, it works now like a charm using the combination of lua_tointeger and luaL_checkint. I was using the “App” project template but for testing reasons I’m using local Lua 5.2 interpreter a lot and my first code snippet works well in the interpreter but not in the Simulator as regarding the header file there is Lua 5.1.x in the Simulator. May be it was the problem.
static int closure (lua\_State \*L) { int index = lua\_tointeger( L, lua\_upvalueindex(1) ); // Change int upvalue = luaL\_checkint(L, index); \<other code which is never called\> }
Guys, thanks a lot for the help, it works now like a charm using the combination of lua_tointeger and luaL_checkint. I was using the “App” project template but for testing reasons I’m using local Lua 5.2 interpreter a lot and my first code snippet works well in the interpreter but not in the Simulator as regarding the header file there is Lua 5.1.x in the Simulator. May be it was the problem.