lua-users home
lua-l archive

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


>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])));
}