linit.c 1.7 KB

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