[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Acceptable indicies
- From: "Robert G. Jakabosky" <bobby@...>
- Date: Tue, 30 Aug 2011 04:51:56 -0700
On Tuesday 30, liam mail wrote:
> "Most query functions accept as indices any value inside the available
> stack space ..." 
> 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* and
> luaL_check*  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
> 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.
>  http://www.lua.org/manual/5.1/manual.html#3.2
>  http://www.lua.org/manual/5.1/manual.html#luaL_optint
>  http://www.lua.org/manual/5.1/manual.html#luaL_checkany
Robert G. Jakabosky