linit.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. ** $Id: linit.c,v 1.17 2009/05/01 13:37:11 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_MATHLIBNAME, luaopen_math},
  22. {NULL, NULL}
  23. };
  24. /*
  25. ** these libs are preloaded and must be required before used
  26. */
  27. static const luaL_Reg preloadedlibs[] = {
  28. {LUA_DBLIBNAME, luaopen_debug},
  29. {NULL, NULL}
  30. };
  31. LUALIB_API void luaL_openlibs (lua_State *L) {
  32. /* call open functions from 'loadedlibs' */
  33. const luaL_Reg *lib = loadedlibs;
  34. for (; lib->func; lib++) {
  35. lua_pushcfunction(L, lib->func);
  36. lua_pushstring(L, lib->name);
  37. lua_call(L, 1, 0);
  38. }
  39. /* add open functions from 'preloadedlibs' into 'package.preload' table */
  40. lib = preloadedlibs;
  41. luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", 0);
  42. for (; lib->func; lib++) {
  43. lua_pushcfunction(L, lib->func);
  44. lua_setfield(L, -2, lib->name);
  45. }
  46. lua_pop(L, 1); /* remove package.preload table */
  47. #ifdef LUA_COMPAT_DEBUGLIB
  48. lua_getglobal(L, "require");
  49. lua_pushliteral(L, LUA_DBLIBNAME);
  50. lua_call(L, 1, 0); /* call 'require"debug"' */
  51. #endif
  52. }