linit.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /*
  14. ** Standard Libraries
  15. */
  16. static const luaL_Reg stdlibs[] = {
  17. {LUA_GNAME, luaopen_base},
  18. {LUA_LOADLIBNAME, luaopen_package},
  19. {LUA_COLIBNAME, luaopen_coroutine},
  20. {LUA_DBLIBNAME, luaopen_debug},
  21. {LUA_IOLIBNAME, luaopen_io},
  22. {LUA_MATHLIBNAME, luaopen_math},
  23. {LUA_OSLIBNAME, luaopen_os},
  24. {LUA_STRLIBNAME, luaopen_string},
  25. {LUA_TABLIBNAME, luaopen_table},
  26. {LUA_UTF8LIBNAME, luaopen_utf8},
  27. {NULL, NULL}
  28. };
  29. /*
  30. ** require selected standard libraries and add the others to the
  31. ** preload table.
  32. */
  33. LUALIB_API void luaL_openselectedlibs (lua_State *L, int what) {
  34. int mask = 1;
  35. const luaL_Reg *lib;
  36. luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
  37. for (lib = stdlibs; lib->func; (lib++, mask <<= 1)) {
  38. if (what & mask) { /* selected? */
  39. luaL_requiref(L, lib->name, lib->func, 1); /* require library */
  40. lua_pop(L, 1); /* remove result from the stack */
  41. }
  42. else { /* add library to PRELOAD table */
  43. lua_pushcfunction(L, lib->func);
  44. lua_setfield(L, -2, lib->name);
  45. }
  46. }
  47. lua_assert((mask >> 1) == LUA_UTF8LIBK);
  48. lua_pop(L, 1); // remove PRELOAD table
  49. }