linit.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /*
  9. ** If you embed Lua in your program and need to open the standard
  10. ** libraries, call luaL_openlibs in your program. If you need a
  11. ** different set of libraries, copy this file to your project and edit
  12. ** it to suit your needs.
  13. **
  14. ** You can also *preload* libraries, so that a later 'require' can
  15. ** open the library, which is already linked to the application.
  16. ** For that, do the following code:
  17. **
  18. ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
  19. ** lua_pushcfunction(L, luaopen_modname);
  20. ** lua_setfield(L, -2, modname);
  21. ** lua_pop(L, 1); // remove PRELOAD table
  22. */
  23. #include "lprefix.h"
  24. #include <stddef.h>
  25. #include "lua.h"
  26. #include "lualib.h"
  27. #include "lauxlib.h"
  28. /*
  29. ** these libs are loaded by lua.c and are readily available to any Lua
  30. ** program
  31. */
  32. static const luaL_Reg loadedlibs[] = {
  33. {LUA_GNAME, luaopen_base},
  34. {LUA_LOADLIBNAME, luaopen_package},
  35. {LUA_COLIBNAME, luaopen_coroutine},
  36. {LUA_TABLIBNAME, luaopen_table},
  37. {LUA_IOLIBNAME, luaopen_io},
  38. {LUA_OSLIBNAME, luaopen_os},
  39. {LUA_STRLIBNAME, luaopen_string},
  40. {LUA_MATHLIBNAME, luaopen_math},
  41. {LUA_UTF8LIBNAME, luaopen_utf8},
  42. {LUA_DBLIBNAME, luaopen_debug},
  43. {NULL, NULL}
  44. };
  45. LUALIB_API void luaL_openlibs (lua_State *L) {
  46. const luaL_Reg *lib;
  47. /* "require" functions from 'loadedlibs' and set results to global table */
  48. for (lib = loadedlibs; lib->func; lib++) {
  49. luaL_requiref(L, lib->name, lib->func, 1);
  50. lua_pop(L, 1); /* remove lib */
  51. }
  52. }