Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ called from Lua, lua_type(L,0) returns 9 which isn't documented

Tags:

c++

lua

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?

like image 269
Valmond Avatar asked May 18 '15 11:05

Valmond


People also ask

How do I call a Lua function from c?

So, the complete round trip is: Start the lua interpreter Register the C functions you want lua to use Call a lua function from within C, either by a call to luaL_dofile, or by calling a lua function directly The lua function that you called from C can access the C function

What is data type in Lua?

Lua - Data Types. Lua is a dynamically typed language, so the variables don't have types, only the values have types. Values can be stored in variables, passed as parameters and returned as results.

How to store values in Lua programming?

Values can be stored in variables, passed as parameters and returned as results. In Lua, though we don't have variable data types, but we have types for the values. The list of data types for values are given below.

How to get the input parameter of a Lua function?

C/C++ functions that are called from lua need to take as an input parameter the lua state. The parameters can be examined on the call stack using the functions lua_tointeger, lua_tonumber, etc (described above). The first parameter is at index 1, the second parameter is at index 2, and so on.


1 Answers

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]

like image 128
lhf Avatar answered Sep 29 '22 09:09

lhf