7
votes

I'm using Lua as scripting language in my (C++) game. In one call (from lua to c++) I check what type is on the top of the stack:

if(lua_type(L, (0)) == LUA_TSTRING)

But sometimes lua_type(L, (0)) returns 9.

I can't seem to find any references to this (return values should be between -1 and 8 or LUA_TNONE, LUA_TNIL, ... LUA_TTHREAD).

What's happening?

1
Ive found the following in lobject.h: /* ** Extra tags for non-values */ #define LUA_TPROTO LUA_NUMTAGS #define LUA_TUPVAL (LUA_NUMTAGS+1) #define LUA_TDEADKEY (LUA_NUMTAGS+2) Where LUA_NUMTAGS is 9Phil Williams

1 Answers

5
votes

The top of the stack is at index -1, not 0.

0 can never be used as an index for accessing the stack:

(Note that 0 is never an acceptable index.)

in §4.3 – Valid and Acceptable Indices in the reference manual.

The C API in Lua does not hold the programmer's hand:

As in most C libraries, the Lua API functions do not check their arguments for validity or consistency. However, you can change this behavior by compiling Lua with the macro LUA_USE_APICHECK defined. [§4]