lua-users home
lua-l archive

Re: lua_getupvalue question

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


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

AltStyle によって変換されたページ (->オリジナル) /