lua-users home
lua-l archive

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


On Friday 26, liam mail wrote:
> On 26 August 2011 10:21, Josh Simmons <simmons.44@gmail.com> wrote:
> > On Fri, Aug 26, 2011 at 7:05 PM, liam mail <liam.list@googlemail.com> 
wrote:
> >> On 26 August 2011 09:50, Josh Simmons <simmons.44@gmail.com> wrote:
> >>> On Fri, Aug 26, 2011 at 5:58 PM, liam mail <liam.list@googlemail.com> 
wrote:
> >>>> I am just looking for some clarification when using the macro
> >>>> LUA_USE_APICHECK, is it the case that indicators in the manual are not
> >>>> respected for functions when using this macro? I ask because I decided
> >>>> to turn the option on and run unit tests on OOLua to find that an
> >>>> assert is being triggered for the function lua_touserdata (indirectly
> >>>> via index2addr) with it, yet the manual states "'-' means the function
> >>>> never throws any error". The situation under test was trying to pull a
> >>>> user data of an empty stack which as I understood it would return NULL
> >>>> and does without the macro, this situation is handled gracefully in
> >>>> the test but means I can not use the macro as is. Firstly because an
> >>>> assert (from assert.h) in unit tests is bad and also it would require
> >>>> different tests for release and debug builds.
> >>>> 
> >>>> Thanks
> >>>> Liam
> >>> 
> >>> I suggest checking the stack height before trying to remove things from
> >>> it.
> >> 
> >> There is no need to check the stack depth as it will return NULL as it
> >> should when the macro is not used.
> >> 
> >>> Errors in this instance refer to Lua errors, not any kind of C
> >>> assertions or error handling.
> >> 
> >> Yet it does say any error, which is why I ask the question.
> >> 
> >> Liam
> > 
> > Just because it returns what you think it should does not mean it's a
> > valid API usage. Popping off an empty stack is using an invalid stack
> > index (http://www.lua.org/manual/5.1/manual.html#3.2) and is therefore
> > not sane API usage.
> 
> It returns what the manual says
> "If the value at the given acceptable index is a full userdata,
> returns its block address. If the value is a light userdata, returns
> its pointer. Otherwise, returns NULL"
> The index is neither an acceptable index, full or a light userdata
> therefore it must return NULL.

The manual doesn't say that non-acceptable indices should return NULL.

If you are calling lua_touserdata() on an optional parameter, then use a 
positive index.  From what I can see index2addr() will only assert in the case 
of an empty stack if you are using a negative index.

A good rule of thumb is to use positive indices for parameters and negative 
indices for values the C function pushes onto the stack.

> > Error when used in the reference manual refers to Lua Errors
> > (http://www.lua.org/manual/5.1/manual.html#2.7) specifically, not
> > general failure.
> 
> Again sorry to point this out but "any error".

The manual is talking about "thrown" errors that can be caught by pcall, not 
assertions that will abort the whole process.

-- 
Robert G. Jakabosky