linit.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. ** $Id: linit.c,v 1.36 2014/12/06 20:42:58 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. ** You can also *preload* libraries, so that a later 'require' can
  16. ** open the library, which is already linked to the application.
  17. ** For that, do the following code:
  18. **
  19. ** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
  20. ** lua_pushcfunction(L, luaopen_modname);
  21. ** lua_setfield(L, -2, modname);
  22. ** lua_pop(L, 1); // remove _PRELOAD table
  23. */
  24. #include "lua.h"
  25. #include "lualib.h"
  26. #include "lauxlib.h"
  27. /*
  28. ** these libs are loaded by lua.c and are readily available to any Lua
  29. ** program
  30. */
  31. static const luaL_Reg loadedlibs[] = {
  32. {"_G", luaopen_base},
  33. {LUA_LOADLIBNAME, luaopen_package},
  34. {LUA_COLIBNAME, luaopen_coroutine},
  35. {LUA_TABLIBNAME, luaopen_table},
  36. {LUA_IOLIBNAME, luaopen_io},
  37. {LUA_OSLIBNAME, luaopen_os},
  38. {LUA_STRLIBNAME, luaopen_string},
  39. {LUA_MATHLIBNAME, luaopen_math},
  40. {LUA_UTF8LIBNAME, luaopen_utf8},
  41. {LUA_DBLIBNAME, luaopen_debug},
  42. #if defined(LUA_COMPAT_BITLIB)
  43. {LUA_BITLIBNAME, luaopen_bit32},
  44. #endif
  45. {NULL, NULL}
  46. };
  47. LUALIB_API void luaL_openlibs (lua_State *L) {
  48. const luaL_Reg *lib;
  49. /* "require" functions from 'loadedlibs' and set results to global table */
  50. for (lib = loadedlibs; lib->func; lib++) {
  51. luaL_requiref(L, lib->name, lib->func, 1);
  52. lua_pop(L, 1); /* remove lib */
  53. }
  54. }