Blocks/Closures in Lua

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

Re: Lua and C

Jon Kleiser-2
>Hi,
>        I'm new to lua and am evaluating it as a scripting language I can
>use with an application I am writing. I am wondering if anyone has any
>code that shows lua being used as a scripting language that drives a c
>backend. The examples that come with lua are all standalone lua. I have
>read the docs, but they are brief on the topic. Any help is appreciated.
>
>thanks,
>-rich

If you want to call C functions from Lua, you should create something
similar to "mylib.c" below. My "mylib.c" example contains three (Mac)
functions, more or less "made up". You should (like the Lua manual says ;)
look at iolib.c, mathlib.c, and strlib.c for more variations. It's quite
easy!

/Jon

------- 8< ----------------------------------------------- >8 -------

/*
** mylib.c
**
*/

char *rcs_mylib = "$Id: mylib.c,v 0.9 1998/01/06 07:24:00 jon Exp $";

#include <stdlib.h>
// #include <math.h>

#include "lualib.h"
#include "auxlib.h"
#include "lua.h"


static void my_DoOrDont (void)
{
  DoOrDont((Boolean)luaL_check_number(1));   // fake
}


static void my_CursorShape (void)
{
  CursorShape(luaL_check_number(1));   // fake
}


static void my_SysBeep (void)
{
  SysBeep(luaL_check_number(1));      // real Mac
}


static struct luaL_reg mylib[] = {
  {"DoOrDont",    my_DoOrDont},
  {"CursorShape", my_CursorShape},
  {"SysBeep",     my_SysBeep}
};

/*
** Open my library
*/
void mylib_open (void);   // prototype
void mylib_open (void)
{
  luaL_openlib(mylib, (sizeof(mylib)/sizeof(mylib[0])));
}


Reply | Threaded
Open this post in threaded view
|

Re: Lua references

Roberto Ierusalimschy
In reply to this post by Steve Dekorte-2
> All that makes sense to me and is exactly what I had expected but the
> following isn't true when I try it:
> 
> >get_ref(a_lua_ref) == get_ref(b_lua_ref)

The explanation is perfectly correct, but for this last bit. According to
the manual:

  "Values of type lua_Object have no meaning outside Lua; for instance,
   the comparison of two lua_Object's is undefined."

-- Roberto

12