Function lua_register

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

Function lua_register

christinesherbrooke
Hi,


      I have a dll (in C language) so I use the function

                  lua_register(L,fnLua,fnC)

to use function C in LUA .If I understand well, the function C must 
have ONLY ONE argument : L so the corresponding function in lua have 
also one argument.

       I would like to know if it is possible to register a function 
C with TWO or more arguments.

        Thank you very much.

        Have a nice day.

                      Christine ROLLAND.



Reply | Threaded
Open this post in threaded view
|

Re: Function lua_register

Alex Sandro Queiroz e Silva
Hallo,

On Fri, 11 Jan 2002, christinesherbrooke wrote:

>
>        I would like to know if it is possible to register a function
> C with TWO or more arguments.
>

	Every C funtion to be registered in Lua must have only one
argument because when in Lua functions have any number of arguments. To
obtain those one must use the stack referenced by the lua_State passed to
the C function.

  --Alex	[hidden email]		Lab. de Computação Gráfica/UFC
+----------------------------------------------------------------------------+
|"Minha força vem da solidão. Não tenho medo das chuvas tempestuosas nem das |
| grandes ventanias soltas, pois eu também sou o escuro da noite."	     |
|	- Clarice Lispector						     |
+----------------------------------------------------------------------------+


Reply | Threaded
Open this post in threaded view
|

Re: Function lua_register

Reuben Thomas
> >        I would like to know if it is possible to register a function
> > C with TWO or more arguments.
> >
>
> 	Every C funtion to be registered in Lua must have only one
> argument because when in Lua functions have any number of arguments.

This is misleading. The single argument passed to a C function is the
lua_State pointer, and the reason for having this argument is so that you
know which Lua state you are referring to (because the Lua API calls all
take a state parameter).

> To obtain those one must use the stack referenced by the lua_State
> passed to the C function.

Exactly. The standard libraries are a good source of examples.

-- 
http://sc3d.org/rrt/ | competent, a.  underpromoted


Reply | Threaded
Open this post in threaded view
|

RE: Function lua_register

Peter Prade
In reply to this post by christinesherbrooke
Hi!

you might want to take a look at tolua
(http://www.tecgraf.puc-rio.br/~celes/tolua/)
or one of the other code wrappers on the page http://www.lua.org/addons.html

what tolua does, it creates a wrapper function that does all the dirty work
for you ;)

if you have a C function, let's say:

	int calculate(int a, int b);

then tolua creates a wrapper function for it so that you can call it in lua
with two number arguments:

	print(calculate(1,2))

the hard way is to get the arguments from the stack by hand. not good for
readability and you have all kinds of possible pitfalls (like the trailing
nil problem with a lot of the standard library functions etc.)

Regards,
Peter

> -----Original Message-----
> From: [hidden email]
> [[hidden email] Behalf Of christinesherbrooke
> Sent: Friday, January 11, 2002 3:26 PM
> To: Multiple recipients of list
> Subject: Function lua_register
>
>
> Hi,
>
>
>       I have a dll (in C language) so I use the function
>
>                   lua_register(L,fnLua,fnC)
>
> to use function C in LUA .If I understand well, the function C must
> have ONLY ONE argument : L so the corresponding function in lua have
> also one argument.
>
>        I would like to know if it is possible to register a function
> C with TWO or more arguments.
>
>         Thank you very much.
>
>         Have a nice day.
>
>                       Christine ROLLAND.
>
>


Reply | Threaded
Open this post in threaded view
|

glua (was: Function lua_register)

Enrico Colombini
>you might want to take a look at tolua
>(http://www.tecgraf.puc-rio.br/~celes/tolua/)
>or one of the other code wrappers on the page http://www.lua.org/addons.html

By the way... after using tolua for a while, I decided that I needed
something simpler but more suited to low-level hand-crafted code (e.g.
functions accepting a variable number of arguments), so I wrote yet another
"code wrapper".
It is called "glua" (Lua-C glue), it is minimalist but very efficient and
it has error checking and meaningful error messages like tolua (usertypes
included). A typical C definition looks like this:

  /*----- A minimal function: compute a bitwise AND -----*/

  /* Lua: c = Bitand(a, b)  [a,b,c: integers] */

  glua_function(Bitand)
    int a = glua_getInteger(1);  /* get arg #1 */
    int b = glua_getInteger(2);  /* get arg #2 */
    glua_getNone(3);             /* error if extra args */
    glua_pushNumber(a & b);      /* compute and return 1 numeric result */
    return 1;
  }

(the missing open brace is intentional -- macros at work)
and, later in the file:

  /*----- List of functions visible from Lua -----*/

  static glua_fitem flist[] = {
    glua_fn(Bitand)
    /* ... */
  };

  /*----- Initialization -----*/

  void gluatest_open (lua_State* lua)
  {
    glua_regFunctions(flist);               /* functions */
    /* numeric constants can be defined too */
    /* usertags are defined and registered here */
  }

I wrote glua a few months ago to interface with low-level graphics, than I
had a work overload and never got around to write a documentation.
If somebody is interested, I can try to find time to make that last step.

  Enrico


Reply | Threaded
Open this post in threaded view
|

Re: glua (was: Function lua_register)

Daniel Vollmer-2
interest!

----- Original Message -----
From: Enrico Colombini <[hidden email]>
To: Multiple recipients of list <[hidden email]>
Sent: Friday, January 11, 2002 6:45 PM
Subject: glua (was: Function lua_register)


> >you might want to take a look at tolua
> >(http://www.tecgraf.puc-rio.br/~celes/tolua/)
> >or one of the other code wrappers on the page
http://www.lua.org/addons.html
>
> By the way... after using tolua for a while, I decided that I needed
> something simpler but more suited to low-level hand-crafted code (e.g.
> functions accepting a variable number of arguments), so I wrote yet
another
> "code wrapper".
> It is called "glua" (Lua-C glue), it is minimalist but very efficient and
> it has error checking and meaningful error messages like tolua (usertypes
> included). A typical C definition looks like this:
>
>   /*----- A minimal function: compute a bitwise AND -----*/
>
>   /* Lua: c = Bitand(a, b)  [a,b,c: integers] */
>
>   glua_function(Bitand)
>     int a = glua_getInteger(1);  /* get arg #1 */
>     int b = glua_getInteger(2);  /* get arg #2 */
>     glua_getNone(3);             /* error if extra args */
>     glua_pushNumber(a & b);      /* compute and return 1 numeric result */
>     return 1;
>   }
>
> (the missing open brace is intentional -- macros at work)
> and, later in the file:
>
>   /*----- List of functions visible from Lua -----*/
>
>   static glua_fitem flist[] = {
>     glua_fn(Bitand)
>     /* ... */
>   };
>
>   /*----- Initialization -----*/
>
>   void gluatest_open (lua_State* lua)
>   {
>     glua_regFunctions(flist);               /* functions */
>     /* numeric constants can be defined too */
>     /* usertags are defined and registered here */
>   }
>
> I wrote glua a few months ago to interface with low-level graphics, than I
> had a work overload and never got around to write a documentation.
> If somebody is interested, I can try to find time to make that last step.
>
>   Enrico
>
>


Reply | Threaded
Open this post in threaded view
|

Re: glua (was: Function lua_register)

Jilani Khaldi-2
In reply to this post by Enrico Colombini
> I wrote glua a few months ago to interface with low-level graphics, than I
> had a work overload and never got around to write a documentation.
> If somebody is interested, I can try to find time to make that last step.
Bravo Enrico! Let's have this interface with the documentation. Anche in
italiano adrebbe bene; almeno per me. Grazie e Ciao!

--
Jilani Khaldi
http://space.tin.it/scuola/jkhaldi


Reply | Threaded
Open this post in threaded view
|

Re: glua (was: Function lua_register)

Enrico Colombini
>Let's have this interface with the documentation. Anche in
>italiano adrebbe bene; almeno per me. Grazie e Ciao!

:-)
I hope to find a 'time slot' soon, and... I think I'll stick to English
only, or I'll never get it done.

  Enrico