lua-users home
lua-l archive

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


2011/7/4 Rebel Neurofog <rebelneurofog@gmail.com>:
> I use lua_pushcclosure () to push C function with user data attached to it.
> Currently I check it as an ordinary C function argument.
> My rational mind told me that I don't have to, but my paranoia have a
> different point of view.
>
> So the question is (I feel pretty stupid asking it):
> may I always trust in upvalues or there are some states (upvalues
> aren't collected, right?)
> when I may get NULL from lua_touserdata () instead?

The upvalue will not be collected. However it may be replaced with
debug.setupvalue. On one hand you can protect your code by adding a
check, so that debug.setupvalue make your function unusable. On the
other hand if debug.setupvalue is available to the user, he may have
valid reason to use it on your function, and it may be nicer to avoid
checking for your upvalues.

I'd choose the second option, let the user put anything he wants in
the upvalues, and just use the value as usual, letting regular errors
(like indexing a nil value) propagate. In your lua_touserdata case,
just check the return value for NULL, and eventually throw an error
(or implement a fallback behavior if one is possible).