lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've been trying to use the Lua debug library to tell me what the
current function name is. But I keep getting a blank/empty value. I
had done this before with Lua 5.0, so I have created a simple test
case. It seems that I do get the function name under Lua 5.0.3, but
not under Lua 5.1.2.

Adapting some startup code from PiL, I have the following C code:

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

double do_f(double x, double y, lua_State* L) {
	double z;
	/* push functions and arguments */
	lua_getglobal(L, "f");  /* function to be called */
	lua_pushnumber(L, x);   /* push 1st argument */
	lua_pushnumber(L, y);   /* push 2nd argument */

	/* do the call (2 arguments, 1 result) */
	if (lua_pcall(L, 2, 1, 0) != 0)
	{
		printf("error running function `f': %s",
			  lua_tostring(L, -1));
	}

	/* retrieve result */
	if (!lua_isnumber(L, -1))
	{
		printf("function `f' must return a number");
	}
	z = lua_tonumber(L, -1);
	lua_pop(L, 1);  /* pop returned value */
	return z;
}

int main(int argc, char* argv[])
{
	int error;
	lua_State *L = lua_open();   /* opens Lua */

#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
	printf("Using Lua 5.1\n");
	luaL_openlibs(L);             /* opens the basic library */
#else
	printf("Using Lua 5.0\n");
	luaopen_base(L);             /* opens the basic library */
	luaopen_table(L);            /* opens the table library */
	luaopen_io(L);               /* opens the I/O library */
	luaopen_string(L);           /* opens the string lib. */
	luaopen_math(L);             /* opens the math lib. */
	luaopen_debug(L);
#endif

	error = luaL_loadfile(L, "/Users/ewing/TEMP/luadebug/luadebug.lua") ||
	lua_pcall(L, 0, 0, 0);
	if (error) {
		fprintf(stderr, "%s", lua_tostring(L, -1));
		lua_pop(L, 1);  /* pop error message from the stack */
	}

	double my_f = do_f(2, 3, L);
	lua_close(L);
	return 0;
}

And I have the following Lua script (luadebug.lua):

function f (x, y)
	print("in f:", x, y)

	for level=1, 100000 do
		print("level=", level)
		local info = debug.getinfo(level, "nSl")
		if not info then break end
	    for key, value in pairs(info) do
			print("key:", key, "value:", value)
		end
	end

	return (x^2 * math.sin(y))/(1 - x)
end


The output is:

Using Lua 5.0
in f:	2	3
level=	1
key:	short_src	value:	/Users/ewing/TEMP/luadebug/luadebug.lua
key:	source	value:	@/Users/ewing/TEMP/luadebug/luadebug.lua
key:	what	value:	Lua
key:	currentline	value:	6
key:	namewhat	value:	global
key:	linedefined	value:	1
key:	name	value:	f
level=	2

Using Lua 5.1
in f:	2	3
level=	1
key:	lastlinedefined	value:	14
key:	source	value:	@/Users/ewing/TEMP/luadebug/luadebug.lua
key:	what	value:	Lua
key:	currentline	value:	6
key:	namewhat	value:	
key:	linedefined	value:	1
key:	short_src	value:	/Users/ewing/TEMP/luadebug/luadebug.lua
level=	2


Notice that for Lua 5.0, I get the function 'name' as 'f', and the
'namewhat' as 'global'. But in 5.1, 'namewhat' has no value, and
'name' doesn't even appear as a key.

I have also tried getting at this information directly from the C API,
but the results are the same.

Am I doing something wrong? Or is this some kind of bug?

Thanks,
Eric