linit.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. (Must be listed in the same ORDER of their
  15. ** respective constants LUA_<libname>K.)
  16. */
  17. static const luaL_Reg stdlibs[] = {
  18. {LUA_GNAME, luaopen_base},
  19. {LUA_LOADLIBNAME, luaopen_package},
  20. {LUA_COLIBNAME, luaopen_coroutine},
  21. {LUA_DBLIBNAME, luaopen_debug},
  22. {LUA_IOLIBNAME, luaopen_io},
  23. {LUA_MATHLIBNAME, luaopen_math},
  24. {LUA_OSLIBNAME, luaopen_os},
  25. {LUA_STRLIBNAME, luaopen_string},
  26. {LUA_TABLIBNAME, luaopen_table},
  27. {LUA_UTF8LIBNAME, luaopen_utf8},
  28. {NULL, NULL}
  29. };
  30. /*
  31. ** require and preload selected standard libraries
  32. */
  33. LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
  34. int mask;
  35. const luaL_Reg *lib;
  36. luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
  37. for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
  38. if (load & 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 if (preload & mask) { /* selected? */
  43. lua_pushcfunction(L, lib->func);
  44. lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
  45. }
  46. }
  47. lua_assert((mask >> 1) == LUA_UTF8LIBK);
  48. lua_pop(L, 1); /* remove PRELOAD table */
  49. }