2
0

linit.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. ** $Id: linit.c $
  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. #include <stddef.h>
  10. #include "lua.h"
  11. #include "lualib.h"
  12. #include "lauxlib.h"
  13. #include "llimits.h"
  14. /*
  15. ** Standard Libraries. (Must be listed in the same ORDER of their
  16. ** respective constants LUA_<libname>K.)
  17. */
  18. static const luaL_Reg stdlibs[] = {
  19. {LUA_GNAME, luaopen_base},
  20. {LUA_LOADLIBNAME, luaopen_package},
  21. {LUA_COLIBNAME, luaopen_coroutine},
  22. {LUA_DBLIBNAME, luaopen_debug},
  23. {LUA_IOLIBNAME, luaopen_io},
  24. {LUA_MATHLIBNAME, luaopen_math},
  25. {LUA_OSLIBNAME, luaopen_os},
  26. {LUA_STRLIBNAME, luaopen_string},
  27. {LUA_TABLIBNAME, luaopen_table},
  28. {LUA_UTF8LIBNAME, luaopen_utf8},
  29. {NULL, NULL}
  30. };
  31. /*
  32. ** require and preload selected standard libraries
  33. */
  34. LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
  35. int mask;
  36. const luaL_Reg *lib;
  37. luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
  38. for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
  39. if (load & mask) { /* selected? */
  40. luaL_requiref(L, lib->name, lib->func, 1); /* require library */
  41. lua_pop(L, 1); /* remove result from the stack */
  42. }
  43. else if (preload & mask) { /* selected? */
  44. lua_pushcfunction(L, lib->func);
  45. lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
  46. }
  47. }
  48. lua_assert((mask >> 1) == LUA_UTF8LIBK);
  49. lua_pop(L, 1); /* remove PRELOAD table */
  50. }