lua-users home
lua-l archive

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


> hi.i have a question.let's say i register a c  function in lua called f
().
> and i implement another function show(). when i call  show(f),
> ofcourse, on the top of the stack i will have a value.if i apply
>  lua_cfunction to that value it will return true(obvios, because it's
> all about a  C function). Now , how can I convert this function name into
a C string

It is not a function name. Functions in Lua are objects; they do not have
names. Lua does not have the concept of a name for an object. (What is the
name of the number 3.14159, for example?)

However, there is nothing stopping you from creating your own naming
system. I find that quite useful for debugging, myself. The basic idea is
to use the registry to map cclosures onto names (this is ok, AFAIK; luaL
uses string keys, numeric keys and table keys but I don't believe it yet
uses function keys). If you consistently use the luaL_openlib interface to
register C functions (which is certainly the easiest), then you can simply
create your own version, adding a couple of lines at the end of the second
for loop:

...
for (; l->name; l++) {
  int i;
  lua_pushstring(L, l->name);
  for (i=0; i<nup; i++)  /* copy upvalues to the top */
    lua_pushvalue(L, -(nup+1));
  lua_pushcclosure(L, l->func, nup);
  /* Insert here: */
  lua_pushvalue(L, -1); /* copy of closure */
  lua_pushvalue(L, -3); /* copy of "name" */
  lua_settable(L, LUA_REGISTRYINDEX); /* put it in the registry */
  /* End of insertion */
  lua_settable(L, -(nup+3));
}

The registry is not weak-keyed, so this will prevent your cclosures from
being garbage collected. On the other hand, this might not be an issue for
your application. If it were, you would have to use your own table, which
complicates the code slightly.

If you are also using other interfaces, such as lua_register,
lua_pushcfunction, lua_pushcclosure, then you would have to write similar
equivalents of those. You might not want to name all cclosures,
particularly given the garbage collection issue mentioned above.

Once you've done this you can get the "name" of a cclosure very easily.
Suppose the cclosure is on the top of the stack, and you will not need it
again (if this is not the case, you just need to copy the value before
doing this.)

All you need is a couple of lines:

lua_gettable(L, LUA_REGISTRYINDEX);  /* Try to find the name */
if (lua_tostring(L, -1) == NULL) { /* Make sure it worked */
  /* whatever was registered was not a string */
  lua_pop(L, 1);  /* get rid of it */
  lua_pushliteral(L, "unknown"); /* maybe "" or "?" ? */
}

....

If you make the modification indicated in lauxlib.c itself, then you will
succeed in assigning "names" to most (but not quite all) Lua built-in
functions. The "names" will not be fully qualified (i.e. they won't tell
you what library the function is in) but this is still useful for
debugging.

The Lua debugging interface does try to work out what a "plausible name"
for a cclosure might be, based on where the cclosure is stored, but there
are many cases where it doesn't manage to provide useful information. So
the above can be more useful.

Hope this helps.

Rici