linit.c 1.7 KB

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