Gets function name from the stack

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Gets function name from the stack

Nicolas ANTONIAZZI
Hi,

I'm trying to integrate LUA script engine in my program for the first time. I would like to create a "generic" way to access C code from LUA. To do this, I have a global C array that contains all informations that I need (list of function names, function pointers, function parameters type).

I have registered all those functions to a lua_Cfunction callback :

static int generic_func(lua_State *L)
{
  // gets arguments count
  int arg_count = lua_gettop(L);

  // gets called function name
  char func_name[255];
  // --- TODO : --- GET THE FUNCTION NAME ?? -- MY PROBLEM IS HERE ---

  // Debug
  printf("args count = %d - function name = %s\n", arg_count, func_name);
  return 0;
}

---------------------
Now, I would like to register exported function to this generic function.
lua_pushcfunction(L, generic_func);
lua_setglobal(L, "my_func1");

lua_pushcfunction(L, generic_func);
lua_setglobal(L, "my_func2");

lua_pushcfunction(L, generic_func);
lua_setglobal(L, "my_func3");

--------------------

Do you have an idea about how i could get the name of the function called by LUA ?
for instance, if I do : my_func2("test"); from lua,
I would like to receive : args count = 1 - function name = my_func2
I know how to get the arguments count, but I did not find a way to get the function name.
Is it possible ?

Thanks a lot,

Nicolas.
Reply | Threaded
Open this post in threaded view
|

Re: Gets function name from the stack

Luiz Henrique de Figueiredo
> lua_pushcfunction(L, generic_func);
> lua_setglobal(L, "my_func2");
...
> for instance, if I do : my_func2("test"); from lua,
> I would like to receive : args count = 1 - function name = my_func2

Use the name as an upvalue to generic_func:
	lua_pushliteral(L, "my_func2");
	lua_pushcclosure(L, generic_func, 1);
	lua_setglobal(L, "my_func2");

Reply | Threaded
Open this post in threaded view
|

Re: Gets function name from the stack

Patrick Donnelly-3
If you want the name the function was called with, here's how Lua gets
it (and how you get those really nice argument error messages in C):

/* UNTESTED */

  lua_Debug ar;
  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
    ar.name = "?"; /* Some default ? */
  else
  {
    lua_getinfo(L, "n", &ar);
    if (ar.name == NULL)
      ar.name = "?"; /* <-- PUT DEFAULT HERE */
  }

I think this is slow though? So perhaps you may want to avoid it
unless it is really necessary...

Hope that helps,

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant

Reply | Threaded
Open this post in threaded view
|

Re: Gets function name from the stack

Nicolas ANTONIAZZI
Thanks a lot for your answers!!! It helps me a lot.

Nicolas


On Fri, Mar 28, 2008 at 2:48 AM, Patrick Donnelly <[hidden email]> wrote:
If you want the name the function was called with, here's how Lua gets
it (and how you get those really nice argument error messages in C):

/* UNTESTED */

 lua_Debug ar;
 if (!lua_getstack(L, 0, &ar))  /* no stack frame? */
   ar.name = "?"; /* Some default ? */
 else
 {
   lua_getinfo(L, "n", &ar);
   if (ar.name == NULL)
     ar.name = "?"; /* <-- PUT DEFAULT HERE */
 }

I think this is slow though? So perhaps you may want to avoid it
unless it is really necessary...

Hope that helps,

--
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant

Reply | Threaded
Open this post in threaded view
|

Re: Gets function name from the stack

Erik Hougaard
Nicolas ANTONIAZZI wrote:
Thanks a lot for your answers!!! It helps me a lot.

Nicolas
I would say Luiz's use of lua_pushcclosure is much better.

/Erik

Reply | Threaded
Open this post in threaded view
|

Re: Gets function name from the stack

Patrick Donnelly-3
On Fri, Mar 28, 2008 at 6:38 AM, Erik Hougaard <[hidden email]> wrote:
> Nicolas ANTONIAZZI wrote:
>  > Thanks a lot for your answers!!! It helps me a lot.
>  >
>  > Nicolas
>  I would say Luiz's use of lua_pushcclosure is much better.
>
>  /Erik

Naturally, but lhf's solution provides a static name; however, I got
the impression he wanted the name the function was called with.

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant

Reply | Threaded
Open this post in threaded view
|

[ANN] Lua server on the Linksys NSLU2 and the Acme Fox Board

wini
In reply to this post by Erik Hougaard
We have ported our Lua powered web server to the two following embedded systems:

The Linksys NSLU2 and the Acme Fox Board.

The NSLU2 (Network Storage Link for USB 2.0 Disk Drives) is a device made by Linksys. It makes USB Flash memory and hard disks accessible over a network (NAS).

The FOX Board runs the Linux operating system on an ETRAX 100LX microprocessor.

We are very pleased with the speed of the Lua scripting language. We ran a few stress tests on the 266Mhz ARM powered NSLU2 and the NSLU2 delivered 22Mbyte per second data from a mix of dynamically generated content and static content.

See the two following links if you like to know more:
http://barracudadrive.net/blog/2008/03/BarracudaDrive-for-Linksys-NSLU2
http://barracudadrive.net/blog/2008/03/BarracudaDrive-for-the-FOX-Board-LX

-Wilfred




Reply | Threaded
Open this post in threaded view
|

Re: [ANN] Lua server on the Linksys NSLU2 and the Acme Fox Board

Roberto Ierusalimschy
> We have ported our Lua powered web server to the two following embedded 
> systems:
>
> The Linksys NSLU2 and the Acme Fox Board.

I have seen some news about that:

  * http://www.embedded-computing.com/news/db/?10916
  * http://www.linuxdevices.com/news/NS9801882978.html

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: [ANN] Lua server on the Linksys NSLU2 and the Acme Fox Board

Fabio Mascarenhas
And speaking of the NSLU2, Lablua's web site
(http://www.lua.inf.puc-rio.br) has been running on an NSLU2 for some
time (with a Xavante proxied through Apache, not Barracuda). There is
a Debian distro for the NSLU2, this is a very easy way of running Lua
on this device.

--
Fabio Mascarenhas

On Fri, Mar 28, 2008 at 6:20 PM, Roberto Ierusalimschy
<[hidden email]> wrote:
> > We have ported our Lua powered web server to the two following embedded
>  > systems:
>  >
>  > The Linksys NSLU2 and the Acme Fox Board.
>
>  I have seen some news about that:
>
>   * http://www.embedded-computing.com/news/db/?10916
>   * http://www.linuxdevices.com/news/NS9801882978.html
>
>  -- Roberto
>

Reply | Threaded
Open this post in threaded view
|

Re: [ANN] Lua server on the Linksys NSLU2 and the Acme Fox Board

wini
In reply to this post by wini

Did you apply the LNUM patch on the Lua you are using, may I ask?


The NSLU2 build is using integers, but is not directly using the LNUM patch.

The NSLU2 is a very good platform for anyone that would like to try Lua on a small device.

After installing the Unslung firmware, a native gcc compiler can easily be installed on the NSLU2.

I purchased the NSLU2 online for $50.
I have a 2Gbyte external USB pen drive for storing Unslung and all the packages I have installed, including gcc.

-W