lua-users home
lua-l archive

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




On Tue, Aug 30, 2011 at 7:23 PM, Josh Simmons <simmons.44@gmail.com> wrote:
On Wed, Aug 31, 2011 at 4:22 AM, oliver <oliver.schoenborn@gmail.com> wrote:
>
>
> On Tue, Aug 30, 2011 at 11:39 AM, Robert Raschke <rtrlists@googlemail.com>
> wrote:
>>
>> On Tue, Aug 30, 2011 at 4:22 PM, oliver <oliver.schoenborn@gmail.com>
>> wrote:
>>>
>>> On Tue, Aug 30, 2011 at 10:58 AM, Robert Raschke
>>> <rtrlists@googlemail.com> wrote:
>>>>
>>>> On Tue, Aug 30, 2011 at 3:52 PM, Robert Raschke
>>>> <rtrlists@googlemail.com> wrote:
>>>>>
>>>>> On Tue, Aug 30, 2011 at 3:22 PM, oliver <oliver.schoenborn@gmail.com>
>>>>> wrote:
>>>>>>>
>>>>>>> Would it be correct to say, "all functions which take a stack index
>>>>>>> require that it is inside the available stack space",
>>>>>>> ...
>>>>>>> or when stackspace equals X
>>>>>>>    luaL_checkany(L,X+1) is undefined.
>>>>>>
>>>>>> BTW, is there a way of knowing "available stack space"? Alternately a
>>>>>> "bool lua_isacceptable(L, index)" would be a nice function to have.
>>>>>> Oliver
>>>>>
>>>>> int lua_checkstack(lua_State *L, int extra);
>>>>>
>>>>> http://www.lua.org/manual/5.1/manual.html#lua_checkstack
>>>>>
>>>>
>>>> Actually, on reflection that's not exactly what you're asking for. But
>>>> lua_gettop (http://www.lua.org/manual/5.1/manual.html#lua_gettop) gets you
>>>> the size of the stack. So these two give you what you need, or not?
>>>>
>>>> Robby
>>>>
>>> I was under the impression that the stack has a certain size (say, 20,
>>> defined when the lua lib is compiled), and that gettop just returns the
>>> index of the top-most element on that stack. So if top=10, an index is still
>>> acceptable if it refers to 11, but not if it refers to 21. Is this defined
>>> rigorously anywhere?
>>> Oliver
>>
>> I'm might be misunderstanding what you are trying to do. If you want to
>> know if you can push more elements on to the stack then you use
>> lua_checkstack(). If you want to know how many elements currently reside on
>> the stack, you use lua_gettop(). I fail to see why you might want to know
>> how many currently unused, but allocated, stack spaces lie above the current
>> top element within it.
>>
>> Robby
>>
>
> Because as Liam mentioned in another post in this thread, acceptable index
> is
> (index < 0 && abs(index) <= top) ||
>     (index > 0 && index <= stackspace)
> (which I had seen in several other places). The index is known, top is from
> lua_gettop, but what is stackspace? I can't just assume it is 20 since it
> might have been changed by whoever built the lua library.
> Oliver
>

This is all a bit meta because there's no place you actually need to
validate an index without actually having to use it, i.e. there's no
case I'm aware of that you'd be using a positive stack index without
already knowing the height of the stack or having called checkstack.

So why isn't there a lua_isacceptable function? Because there's no
case you actually need to use one (that I can think of, anyway).

Checkstack is only useful when you push stuff onto the stack. But how do you validate, say, an index you give to a C function that queries the Lua stack? For instance: 

int someFunc(int stackIndex) {
    return lua_tonumber(L, stackIndex); 
}

The lua_tonumber does not check that index is acceptable; if it is not, it can return garbage (0, NaN, whatever) and the caller wouldn't know that app state is now undefined. 

The only solution is to assert that stackIndex != 0 and abs(stackIndex) <= top, or?

Oliver