|
Am 03.12.2013 02:48 schröbte Andrew Starks:
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?
static int iter( lua_State* L ) {
/* increment and push value to stack */
lua_pushinteger( L, lua_tointeger( L, lua_upvalueindex( 1 ) )+1 );
/* replace upvalue with incremented value at stack top */
lua_replace( L, lua_upvalueindex( 1 ) );
/* lua_replace popped the updated value, so push it again to
* return it */
lua_pushvalue( L, lua_upvalueindex( 1 ) );
return 1;
}
On the Lua side:
> print( iter() )
1
> print( iter() )
2
> print( iter() )
3
I hope that's what you wanted ...
-Andrew
Philipp
lua_replace
[-1, +0, –]
void lua_replace (lua_State *L, int index);
Moves the top element into the given valid index without shifting any element (therefore replacing the value at the given index), and then pops the top element.
Am I the only one that reads that incorrectly?