linit.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. ** $Id: linit.c,v 1.35 2014/11/02 19:19:04 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_UTF8LIBNAME, luaopen_utf8},
  32. #if !defined(LUA_NODEBUGLIB)
  33. {LUA_DBLIBNAME, luaopen_debug},
  34. #endif
  35. #if defined(LUA_COMPAT_BITLIB)
  36. {LUA_BITLIBNAME, luaopen_bit32},
  37. #endif
  38. {NULL, NULL}
  39. };
  40. /*
  41. ** these libs are preloaded and must be required before used
  42. */
  43. static const luaL_Reg preloadedlibs[] = {
  44. #if defined(LUA_NODEBUGLIB)
  45. {LUA_DBLIBNAME, luaopen_debug},
  46. #endif
  47. {NULL, NULL}
  48. };
  49. LUALIB_API void luaL_openlibs (lua_State *L) {
  50. const luaL_Reg *lib;
  51. /* call open functions from 'loadedlibs' and set results to global table */
  52. for (lib = loadedlibs; lib->func; lib++) {
  53. luaL_requiref(L, lib->name, lib->func, 1);
  54. lua_pop(L, 1); /* remove lib */
  55. }
  56. /* add open functions from 'preloadedlibs' into 'package.preload' table */
  57. luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
  58. for (lib = preloadedlibs; lib->func; lib++) {
  59. lua_pushcfunction(L, lib->func);
  60. lua_setfield(L, -2, lib->name);
  61. }
  62. lua_pop(L, 1); /* remove _PRELOAD table */
  63. }