Lua当前行号
问题描述:
Lua是否支持类似于C的__LINE__
宏(该宏返回当前代码行的编号)?我知道Lua有一个名为_G
的特殊内置变量,但是我看不到那里的行号...
Does Lua support something like C's __LINE__
macro, which returns the number of the current code line? I know Lua has a special built-in variable called _G
, but I don't see line number in there...
答
从Lua使用debug.getinfo,例如
From Lua using debug.getinfo, e.g.,
local line = debug.getinfo(1).currentline
从C使用lua_getinfo(这将返回lua代码中的行号)
From C using lua_getinfo (This will return the linenumber inside lua code)
lua_Debug ar;
lua_getstack(L, 1, &ar);
lua_getinfo(L, "nSl", &ar);
int line = ar.currentline