lua-users home
lua-l archive

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




On Monday, December 2, 2013, Philipp Janda wrote:
Am 03.12.2013 02:00 schröbte Andrew Starks:
On Monday, December 2, 2013, Sean Conner wrote:

It was thus said that the Great Andrew Starks once stated:
Given the following:

```
int iter(lua_State *L){
   ///what to do here?
     lua_pushinteger(L, (lua_Integer) luaL_checkint(L,
lua_upvalueindex(1)) + 1); //I can get the upvalue...
     // how do I set it?
     //lua_getupvalue (lua_State *L, int funcindex, int n); //doesn't
seem like the correct function because iter is not on the stack, at
this point.
}

int factory(lua_State *L){
   lua_pushnumber(L, 0);
   lua_pushcclosure(L, iter, 1);
   return 1;
}

```

   You want to call ua_upvalueindex().  That will return an index you can
pass to other Lua functions that take an index.  It's described in section
3.4 of Lua 5.1 and section 4.4 of Lua 5.2.

   -spc


I messed up. It's setupvalue not get.

That may be true, but given your description you probably wanted `lua_replace(L, lua_upvalueindex(1))` anyway as Sean suggested (sort of) ...


-Andrew


Philipp




I get the upvalue. It's on the stack. It's value is 0. I want it so that the next time I enter the c function and retrieve the value at the same lua_upvalueindex address, it returns unto me the previous value, added to one. 

Please to illustrate?

-Andrew