lua-users home
lua-l archive

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

On Tuesday 30, liam mail wrote:
> "Most query functions accept as indices any value inside the available
> stack space ..." [1]
> Would it be correct to say, "all functions which take a stack index
> require that it is inside the available stack space",


> or are there
> exceptions to this?

If there are exceptions, they should be noted in the manual.

> I ask because functions such luaL_opt*[2] and
> luaL_check* [3] do not mention they require acceptable indicies and
> ones which are not acceptable result in undefined behaviour, whilst
> nearly all other functions that take indicies are noted they have a
> precondition of an acceptable index.

I would say that all C API functions need an acceptable index.  I have already 
checked the luaL_check* and luaL_opt* function and they still require an 
acceptable index.

> For example
> if lua_gettop equals 0
>     luaL_checkany(L,-1) is undefined.

You should never use -1, unless you pushed a value onto the stack.

> or when stackspace equals X
>     luaL_checkany(L,X+1) is undefined.

The manual says that Lua ensures at least LUA_MINSTACK (defined as 20) stack 
positions are available.  So it is safe to use position positions 
1-LUA_MINSTACK as acceptable indices.  If your C function wants to use higher 
indices, then it needs to call lua_checkstack() to grow the stack.

> [1]
> [2]
> [3]
> Liam

Robert G. Jakabosky