Lua native C API in Corona Simulator (OS X)

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. 

Nothing obvious sticks out. I assume you are running this all on the same thread?

Also, did you use the “App” project template for Mac that’s in CoronaEnterprise?

Does this help?

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.

Glad to hear it. This site should help you also: http://pgl.yoyo.org/luai/i/_

It contains a lot of valuable information regarding the Lua C Api.

Nothing obvious sticks out. I assume you are running this all on the same thread?

Also, did you use the “App” project template for Mac that’s in CoronaEnterprise?

Does this help?

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.

Glad to hear it. This site should help you also: http://pgl.yoyo.org/luai/i/_

It contains a lot of valuable information regarding the Lua C Api.