lua-users home
lua-l archive

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


Thank you for having a look.
I will learn and do a module.

For the moment, i did another test, simpler.
I got the same behaviour than before: segfault (or bus error) if "lua_step" call the inside "step" function. If i rename "step" to "tep", no problems with Lua.

That's strange.. i will try with a module, asap.


// build exe: gcc simple.c -o simple.elf -DBUILD_EXE -Wall -W -llua
// build lib:  gcc simple.c -o simple.so -Wall -W -fPIC -shared

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <lua.h> //DON'T link with -llua if shared lib.

void init()
{
  srandom(time(NULL));
}


int step()
{
  return random()%6+1;
}


int lua_step(lua_State *L)
{
  static int is_init=0;

  if (!is_init) {
    init();
    is_init=1;
  }
  int res = step();
  lua_pushinteger (L,res);

  return 1;
}

#ifdef BUILD_EXE
int main(void)
{ int i,res;
  init();
  for (i=0; i<5; i++) {
    res = step();
    fprintf(stderr,"dice: %d\n",res);
  }
  return 0;
}
#endif


====
and the lua script:
#!/usr/bin/env lua

lua_step = package.loadlib ("./simple.so", "lua_step")

for i=1,5 do
  print ("lua dice: ", lua_step() )
end