diff --git a/lua-foo.c b/lua-foo.c index 7457454..c26ac8b 100644 --- a/lua-foo.c +++ b/lua-foo.c @@ -37,12 +37,22 @@ static int l_mysin(lua_State *L) { int main(void) { /* Set up Lua. */ lua_State *L; - L = lua_open(); + L = luaL_newstate(); - luaopen_base(L); - luaopen_string(L); - /* Alternatively: - luaL_openlibs(L); */ + /* Load necessary libs. */ + luaL_Reg lualibs[] = { + { "base", luaopen_base }, + { "string", luaopen_string }, + { NULL, NULL} + }; + for(luaL_Reg *lib = lualibs; lib->func; lib++) { + printf("Loading %s\n", lib->name); + lib->func(L); + lua_setglobal(L, lib->name); + lua_settop(L, 0); + } + /* Alternatively, we could have called LuaL_openlibs(L) but that would have + loaded *all* libs. */ /* Register 'mysin'. */ lua_pushcfunction(L, l_mysin);