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 Sean Conner once stated:
> It was thus said that the Great oliver once stated:
> > 
> > Programmers make mistakes when using library functions/methods, utility or
> > not. For optimal productivity, a library should assert its preconditions, at
> > least in debug build. It is an error *not* to check. Since some of us have
> > to (for variety of reasons) use pre-built release-mode Lua DLL, where
> > preconditions are therefore not asserted, and given that the
> > "is-acceptable-index" formula is not trivial, and that this precondition is
> > so prevalent in Lua lib, there should at least be a Lua lib function that
> > allows to validate stack indices.
> 
>   I think you gave the solution in another email---lua_gettop()!  Here:
> 
> int mylua_isacceptable(lua_State *L,int idx)
> {
>   return abs(idx) <= lua_gettop(L);
> }
> 
>   -spc (Or am I missing something?)

  Okay, in re-reading the thread, I see I am missing something.  Oliver
wants to check that an index is acceptable and thus, feels the need to know
the physical size of the stack at that instance, instead of the current top
of the stack.

  But in thinking about it, the code above is what you really want.  If you
(as programmer) are trying to access stack entries you know nothing about
(outside of lua_gettop()) then you are doing something wrong, regardless of
the size of the stack (but I am willing to conceed that this issue has never
bitten me, probably because of all my years of programming in assembly,
where one is working with a raw, system controlled, stack).  

  -spc