我在我的 (C++) 游戏中使用 Lua 作为脚本语言。 在一次调用中(从 lua 到 c++)我检查堆栈顶部的类型:
if(lua_type(L, (0)) == LUA_TSTRING)
但有时 lua_type(L, (0)) 返回 9。
我似乎找不到对此的任何引用(返回值应介于 -1 和 8 之间或 LUA_TNONE、LUA_TNIL、... LUA_TTHREAD)。
发生了什么事?
最佳答案
栈顶索引为 -1,而不是 0。
0 永远不能用作访问堆栈的索引:
(Note that 0 is never an acceptable index.)
在§4.3 – Valid and Acceptable Indices在引用手册中。
Lua 中的 C API 并没有牵着程序员的手:
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]
关于c++ - 在从 Lua 调用的 C++ 中,lua_type(L,0) 返回未记录的 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30301447/