Hi Folks,
I'ld like to extend Lua (with a new module), with an existing C codes.
I'm using Lua 5.2 on Linux.
Here is how I try:
==%==
luamodule.c:
-------------------
#include "lua.h"
#include "luaconf.h"
#include "lauxlib.h"
#include "example1.h" // contains variable and function declarations
static int _wrap_get_str (lua_State *L) {
const char *s = luaL_checkstring(L, 1);
char retstr[1024];
get_str(s, retstr);
printf("retstr: '%s'\n", retstr);
return 0;
}
void luaopen_example2(lua_State *L) {
static const struct luaL_Reg example2[] = {
{ "get_str", _wrap_get_str },
{ NULL, NULL }
};
luaL_newlib(L, example2);
lua_setglobal(L, "example2");
}
==%==
Compiling with this statement:
gcc -g -Wall -shared -fPIC example1.c luamodule.c -o example2.so
-I/usr/include/lua5.2 -llua5.2 -lm -ldl
My Lua script:
==%==
#!/usr/bin/lua
example2 = require("example2")
==%==
and then the example2 will be a "true" (boolean) evaulated variable,
not a table. So, when I calling example2.get_str("foo"), then I get an
error message:
attempt to index global 'example2' (a boolean value)
What's wrong with my code?
Thanks,
a.