1 /* $NetBSD: linit.c,v 1.9 2018/08/04 17:30:01 alnsn Exp $ */ 2 3 /* 4 ** Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp 5 ** Initialization of libraries for lua.c and other clients 6 ** See Copyright Notice in lua.h 7 */ 8 9 10 #define linit_c 11 #define LUA_LIB 12 13 /* 14 ** If you embed Lua in your program and need to open the standard 15 ** libraries, call luaL_openlibs in your program. If you need a 16 ** different set of libraries, copy this file to your project and edit 17 ** it to suit your needs. 18 ** 19 ** You can also *preload* libraries, so that a later 'require' can 20 ** open the library, which is already linked to the application. 21 ** For that, do the following code: 22 ** 23 ** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); 24 ** lua_pushcfunction(L, luaopen_modname); 25 ** lua_setfield(L, -2, modname); 26 ** lua_pop(L, 1); // remove PRELOAD table 27 */ 28 29 #include "lprefix.h" 30 31 32 #ifndef _KERNEL 33 #include <stddef.h> 34 #endif /* _KERNEL */ 35 36 #include "lua.h" 37 38 #include "lualib.h" 39 #include "lauxlib.h" 40 41 42 /* 43 ** these libs are loaded by lua.c and are readily available to any Lua 44 ** program 45 */ 46 static const luaL_Reg loadedlibs[] = { 47 {"_G", luaopen_base}, 48 #ifndef _KERNEL 49 {LUA_LOADLIBNAME, luaopen_package}, 50 #endif /* _KERNEL */ 51 {LUA_COLIBNAME, luaopen_coroutine}, 52 {LUA_TABLIBNAME, luaopen_table}, 53 #ifndef _KERNEL 54 {LUA_IOLIBNAME, luaopen_io}, 55 {LUA_OSLIBNAME, luaopen_os}, 56 #endif /* _KERNEL */ 57 {LUA_STRLIBNAME, luaopen_string}, 58 #ifndef _KERNEL 59 {LUA_MATHLIBNAME, luaopen_math}, 60 #endif /* _KERNEL */ 61 {LUA_UTF8LIBNAME, luaopen_utf8}, 62 {LUA_DBLIBNAME, luaopen_debug}, 63 #if defined(LUA_COMPAT_BITLIB) 64 {LUA_BITLIBNAME, luaopen_bit32}, 65 #endif 66 {NULL, NULL} 67 }; 68 69 70 LUALIB_API void luaL_openlibs (lua_State *L) { 71 const luaL_Reg *lib; 72 /* "require" functions from 'loadedlibs' and set results to global table */ 73 for (lib = loadedlibs; lib->func; lib++) { 74 luaL_requiref(L, lib->name, lib->func, 1); 75 lua_pop(L, 1); /* remove lib */ 76 } 77 } 78 79