lua-users home
lua-l archive

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


Hi Everyone,

I was embedding Lua into an RTOS and needed to add my own user libraries. I decided to modify linit.c to allow me to hook in another set of libraries. My question is, what would be the best way to add your own libraries as I can not dynamically load the libraries. I have attached my modified version of linit.c in hopes you can enlighten me on a better method.

Thanks
--Keith
/*
** $Id: linit.c,v 1.13 2005/08/26 17:36:32 roberto Exp $
** Initialization of libraries for lua.c
** See Copyright Notice in lua.h
*/


#define linit_c
#define LUA_LIB

#include "lua.h"

#include "lualib.h"
#include "lauxlib.h"

const luaL_Reg * userLibHook;

static const luaL_Reg lualibs[] = {
  {"", luaopen_base},
  {LUA_TABLIBNAME, luaopen_table},
  {LUA_IOLIBNAME, luaopen_io},
  {LUA_OSLIBNAME, luaopen_os},
  {LUA_STRLIBNAME, luaopen_string},
  {LUA_MATHLIBNAME, luaopen_math},
  {LUA_DBLIBNAME, luaopen_debug},
  {LUA_LOADLIBNAME, luaopen_package},
  {NULL, NULL}
};

static void doLibs(lua_State *L, const char * msg, const luaL_Reg * lib) {
  printf("-- %s Modules:", msg);
  for (; lib && lib->func; lib++) {
    lua_pushcfunction(L, lib->func);
	lua_pushstring(L, lib->name);
	lua_call(L, 1, 0);
	if ( (lib->name == NULL) || (lib->name[0] == '\0') )
	  printf("(base) ");
	else
	  printf("%s ",lib->name);
  }
  printf("\n");
}

LUALIB_API void luaL_openlibs (lua_State *L) {
  doLibs(L, "Lua ", lualibs);
  doLibs(L, "User", userLibHook);
  printf("\n");
}