lua-users home
lua-l archive

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


It was thus said that the Great Iurii Belobeev once stated:
> Hi,
> 
> I have this code:
> 
> lua_getglobal(L, "mytable");
> lua_getfield(L, -1, "myfunc");
> lua_pushboolean(L, 1);
> lua_call(L, 1, 0);
> 
> and I have this check inside myfunc:
> 
> luaL_checktype(L, 1, LUA_TFUNCTION);

  I find that odd, because the first parameter to the function is a boolean,
not a function.  

> I intentionally receive this error:
> bad argument #1 to '?' (function expected, got boolean)
> 
> I want to find a way to provide a name for my function and have it
> printed instead of '?'.

  It's much harder than you think.  What's the name of the following
functions?

	f = function(a) if not a then error "Bad parameter" end return 1 end
	g = f
	h = function(b) if b then error "Bad parameter" end return 2 end
	i = h
	x = { y = function() error "There is no name" end }

  Or the name of this function as the second parameter to table.sort()?

	table.sort(array,function(a,b) return a > b end)

> I look at luaL_argerror, and I see that it calls lua_getinfo() which
> fills lua_Debug ar.name (which is used as function name).
> So ar.name is NULL in my case (I checked).
> I believe it would take an unpredictable time for me to analyze the
> lua_getinfo code, so maybe somebody knows, how should I provide a
> function name?

  At best (assuming the code hasn't been stripped of debugging information)
you can get the filename and line numbers of the function, which is about
the best you can do.

  -spc