lua-users home
lua-l archive

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

> Suppose I'd like to keep references to a bunch of lua_Objects that  
> were passed to my C function by Lua. The reference manual (no pun  
> intended) says that lua_ref() will obtain a reference to the  
> lua_Object that's on top of the stack, but does this apply for the  
> "getting arguments" situation? In other words, is this the right way  
> to obtain references to passed arguments (for later use), or am I way  
> off? In the likely event that I'm "way off" -- how is this done  
> correctly?
> void my_Lua_callable_function()
> {
>   int ref_to_param_1 = lua_ref(1);
>   lua_pop();
>   int ref_to_param_2 = lua_ref(1);
>   lua_pop();
>   // etc ...
> }

This way is fine. If you need to use these parameters in the same function
after building their references, then you may prefer to copy thm to the
stack top (with lua_pushobject) and then call lua_ref. But if all you want
is to build the references, your code is quite Ok. (Except that the
signature of your function should be int foo (lua_State *L), and you
should pass this 'L' when calling lua_ref...)

-- Roberto