Lua 中的当前行号 [英] current line number in Lua
本文介绍了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
http://www.lua.org/manual/5.1/manual.html#lua_getinfo
http://www.lua.org/manual/5.1/manual.html#pdf-debug.getinfo
这篇关于Lua 中的当前行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文