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