linit.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. ** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $
  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_bit32},
  30. {LUA_MATHLIBNAME, luaopen_math},
  31. {LUA_DBLIBNAME, luaopen_debug},
  32. {NULL, NULL}
  33. };
  34. /*
  35. ** these libs are preloaded and must be required before used
  36. */
  37. static const luaL_Reg preloadedlibs[] = {
  38. {NULL, NULL}
  39. };
  40. LUALIB_API void luaL_openlibs (lua_State *L) {
  41. const luaL_Reg *lib;
  42. /* call open functions from 'loadedlibs' and set results to global table */
  43. for (lib = loadedlibs; lib->func; lib++) {
  44. luaL_requiref(L, lib->name, lib->func, 1);
  45. lua_pop(L, 1); /* remove lib */
  46. }
  47. /* add open functions from 'preloadedlibs' into 'package.preload' table */
  48. luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
  49. for (lib = preloadedlibs; lib->func; lib++) {
  50. lua_pushcfunction(L, lib->func);
  51. lua_setfield(L, -2, lib->name);
  52. }
  53. lua_pop(L, 1); /* remove _PRELOAD table */
  54. }