linit.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. ** $Id: linit.c,v 1.19 2009/07/01 16:16:40 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_TABLIBNAME, luaopen_table},
  25. {LUA_IOLIBNAME, luaopen_io},
  26. {LUA_OSLIBNAME, luaopen_os},
  27. {LUA_STRLIBNAME, luaopen_string},
  28. {LUA_BITLIBNAME, luaopen_bit},
  29. {LUA_MATHLIBNAME, luaopen_math},
  30. {NULL, NULL}
  31. };
  32. /*
  33. ** these libs are preloaded and must be required before used
  34. */
  35. static const luaL_Reg preloadedlibs[] = {
  36. {LUA_DBLIBNAME, luaopen_debug},
  37. {NULL, NULL}
  38. };
  39. LUALIB_API void luaL_openlibs (lua_State *L) {
  40. const luaL_Reg *lib;
  41. /* call open functions from 'loadedlibs' */
  42. for (lib = loadedlibs; lib->func; lib++) {
  43. lua_pushcfunction(L, lib->func);
  44. lua_pushstring(L, lib->name);
  45. lua_call(L, 1, 0);
  46. }
  47. /* add open functions from 'preloadedlibs' into 'package.preload' table */
  48. luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", 0);
  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 package.preload table */
  54. #ifdef LUA_COMPAT_DEBUGLIB
  55. lua_getglobal(L, "require");
  56. lua_pushliteral(L, LUA_DBLIBNAME);
  57. lua_call(L, 1, 0); /* call 'require"debug"' */
  58. #endif
  59. }