linit.c 1.7 KB

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