1 /* 2 ** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $ 3 ** Initialization of libraries for lua.c and other clients 4 ** See Copyright Notice in lua.h 5 */ 6 7 8 #define linit_c 9 #define LUA_LIB 10 11 /* 12 ** If you embed Lua in your program and need to open the standard 13 ** libraries, call luaL_openlibs in your program. If you need a 14 ** different set of libraries, copy this file to your project and edit 15 ** it to suit your needs. 16 ** 17 ** You can also *preload* libraries, so that a later 'require' can 18 ** open the library, which is already linked to the application. 19 ** For that, do the following code: 20 ** 21 ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 22 ** lua_pushcfunction(L, luaopen_modname); 23 ** lua_setfield(L, -2, modname); 24 ** lua_pop(L, 1); // remove PRELOAD table 25 */ 26 27 #include "lprefix.h" 28 29 #include <stddef.h> 30 #include <stdlib.h> 31 32 #include "lua.h" 33 34 #include "lualib.h" 35 #include "lauxlib.h" 36 37 #include "bootstrap.h" 38 39 /* 40 ** these libs are loaded by lua.c and are readily available to any Lua 41 ** program 42 */ 43 static const luaL_Reg loadedlibs[] = { 44 {"_G", luaopen_base}, 45 {LUA_LOADLIBNAME, luaopen_package}, 46 {LUA_COLIBNAME, luaopen_coroutine}, 47 {LUA_TABLIBNAME, luaopen_table}, 48 {LUA_IOLIBNAME, luaopen_io}, 49 {LUA_OSLIBNAME, luaopen_os}, 50 {LUA_STRLIBNAME, luaopen_string}, 51 {LUA_MATHLIBNAME, luaopen_math}, 52 {LUA_UTF8LIBNAME, luaopen_utf8}, 53 {LUA_DBLIBNAME, luaopen_debug}, 54 #if defined(LUA_COMPAT_BITLIB) 55 {LUA_BITLIBNAME, luaopen_bit32}, 56 #endif 57 {NULL, NULL} 58 }; 59 60 #ifdef BOOTSTRAPPING 61 static void __attribute__((constructor)) flua_init_env(void) { 62 /* 63 * This happens in the middle of luaopen_package(). We could move it into 64 * flua_setup_mods(), but it seems better to avoid its timing being so 65 * important that it would break some of our bootstrap modules if someone 66 * were to reorder things. 67 */ 68 if (getenv("LUA_PATH") == NULL) 69 setenv("LUA_PATH", BOOTSTRAP_FLUA_PATH, 1); 70 } 71 72 static void flua_setup_mods (lua_State *L) { 73 const luaL_Reg **flib; 74 75 SET_FOREACH(flib, FLUA_MODULE_SETNAME) { 76 luaL_requiref(L, (*flib)->name, (*flib)->func, 1); 77 lua_pop(L, 1); /* remove lib */ 78 } 79 }; 80 #endif 81 82 LUALIB_API void luaL_openlibs (lua_State *L) { 83 const luaL_Reg *lib; 84 /* "require" functions from 'loadedlibs' and set results to global table */ 85 for (lib = loadedlibs; lib->func; lib++) { 86 luaL_requiref(L, lib->name, lib->func, 1); 87 lua_pop(L, 1); /* remove lib */ 88 } 89 #ifdef BOOTSTRAPPING 90 flua_setup_mods(L); 91 #endif 92 } 93