linit.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. ** $Id: linit.c,v 1.25 2010/05/20 12:57:59 roberto Exp roberto $
  3. ** Initialization of libraries for lua.c and other clients
  4. ** See Copyright Notice in lua.h
  5. */
  6. /*
  7. ** If you embed Lua in your program and need to open the standard
  8. ** libraries, call luaL_openlibs in your program. If you need a
  9. ** different set of libraries, copy this file to your project and edit
  10. ** it to suit your needs.
  11. */
  12. #define linit_c
  13. #define LUA_LIB
  14. #include "lua.h"
  15. #include "lualib.h"
  16. #include "lauxlib.h"
  17. /*
  18. ** these libs are loaded by lua.c and are readily available to any Lua
  19. ** program
  20. */
  21. static const luaL_Reg loadedlibs[] = {
  22. {"_G", luaopen_base},
  23. {LUA_LOADLIBNAME, luaopen_package},
  24. {LUA_COLIBNAME, luaopen_coroutine},
  25. {LUA_TABLIBNAME, luaopen_table},
  26. {LUA_IOLIBNAME, luaopen_io},
  27. {LUA_OSLIBNAME, luaopen_os},
  28. {LUA_STRLIBNAME, luaopen_string},
  29. {LUA_BITLIBNAME, luaopen_bit},
  30. {LUA_MATHLIBNAME, luaopen_math},
  31. {NULL, NULL}
  32. };
  33. /*
  34. ** these libs are preloaded and must be required before used
  35. */
  36. static const luaL_Reg preloadedlibs[] = {
  37. {LUA_DBLIBNAME, luaopen_debug},
  38. {NULL, NULL}
  39. };
  40. LUALIB_API void luaL_openlibs (lua_State *L) {
  41. const luaL_Reg *lib;
  42. /* call open functions from 'loadedlibs' */
  43. for (lib = loadedlibs; lib->func; lib++) {
  44. lua_pushcfunction(L, lib->func);
  45. lua_pushstring(L, lib->name);
  46. lua_call(L, 1, 0);
  47. }
  48. /* add open functions from 'preloadedlibs' into 'package.preload' table */
  49. lua_pushglobaltable(L);
  50. luaL_findtable(L, 0, "package.preload", 0);
  51. for (lib = preloadedlibs; lib->func; lib++) {
  52. lua_pushcfunction(L, lib->func);
  53. lua_setfield(L, -2, lib->name);
  54. }
  55. lua_pop(L, 1); /* remove package.preload table */
  56. #if defined(LUA_COMPAT_DEBUGLIB)
  57. lua_getglobal(L, "require");
  58. lua_pushliteral(L, LUA_DBLIBNAME);
  59. lua_call(L, 1, 0); /* call 'require"debug"' */
  60. lua_pop(L, 1); /* remove global table */
  61. #endif
  62. }