lua-users home
lua-l archive

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


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;
> }
> 
> ```
> 
> My question is, how do you change the value of an upvalue when you are
> in the C closure that has the upvalue? You can get it, but how does
> one set it? I didn't see a pseudo index for it.
> 
> I'm sure I'm missing something really basic.

  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