linit.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. ** $Id: linit.c,v 1.18 2009/05/01 13:46:35 roberto Exp roberto $
  3. ** Initialization of libraries for lua.c
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define linit_c
  7. #define LUA_LIB
  8. #include "lua.h"
  9. #include "lualib.h"
  10. #include "lauxlib.h"
  11. /*
  12. ** these libs are loaded by lua.c and are readily available to any program
  13. */
  14. static const luaL_Reg loadedlibs[] = {
  15. {"_G", luaopen_base},
  16. {LUA_LOADLIBNAME, luaopen_package},
  17. {LUA_TABLIBNAME, luaopen_table},
  18. {LUA_IOLIBNAME, luaopen_io},
  19. {LUA_OSLIBNAME, luaopen_os},
  20. {LUA_STRLIBNAME, luaopen_string},
  21. {LUA_BITLIBNAME, luaopen_bit},
  22. {LUA_MATHLIBNAME, luaopen_math},
  23. {NULL, NULL}
  24. };
  25. /*
  26. ** these libs are preloaded and must be required before used
  27. */
  28. static const luaL_Reg preloadedlibs[] = {
  29. {LUA_DBLIBNAME, luaopen_debug},
  30. {NULL, NULL}
  31. };
  32. LUALIB_API void luaL_openlibs (lua_State *L) {
  33. /* call open functions from 'loadedlibs' */
  34. const luaL_Reg *lib = loadedlibs;
  35. for (; lib->func; lib++) {
  36. lua_pushcfunction(L, lib->func);
  37. lua_pushstring(L, lib->name);
  38. lua_call(L, 1, 0);
  39. }
  40. /* add open functions from 'preloadedlibs' into 'package.preload' table */
  41. lib = preloadedlibs;
  42. luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", 0);
  43. for (; lib->func; lib++) {
  44. lua_pushcfunction(L, lib->func);
  45. lua_setfield(L, -2, lib->name);
  46. }
  47. lua_pop(L, 1); /* remove package.preload table */
  48. #ifdef LUA_COMPAT_DEBUGLIB
  49. lua_getglobal(L, "require");
  50. lua_pushliteral(L, LUA_DBLIBNAME);
  51. lua_call(L, 1, 0); /* call 'require"debug"' */
  52. #endif
  53. }