xref: /freebsd/contrib/lua/src/linit.c (revision 0495ed398c4f64013bab2327eb13a303e1f90c13)
18e3e3a7aSWarner Losh /*
2*0495ed39SKyle Evans ** $Id: linit.c $
38e3e3a7aSWarner Losh ** Initialization of libraries for lua.c and other clients
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh 
88e3e3a7aSWarner Losh #define linit_c
98e3e3a7aSWarner Losh #define LUA_LIB
108e3e3a7aSWarner Losh 
118e3e3a7aSWarner Losh /*
128e3e3a7aSWarner Losh ** If you embed Lua in your program and need to open the standard
138e3e3a7aSWarner Losh ** libraries, call luaL_openlibs in your program. If you need a
148e3e3a7aSWarner Losh ** different set of libraries, copy this file to your project and edit
158e3e3a7aSWarner Losh ** it to suit your needs.
168e3e3a7aSWarner Losh **
178e3e3a7aSWarner Losh ** You can also *preload* libraries, so that a later 'require' can
188e3e3a7aSWarner Losh ** open the library, which is already linked to the application.
198e3e3a7aSWarner Losh ** For that, do the following code:
208e3e3a7aSWarner Losh **
218e3e3a7aSWarner Losh **  luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
228e3e3a7aSWarner Losh **  lua_pushcfunction(L, luaopen_modname);
238e3e3a7aSWarner Losh **  lua_setfield(L, -2, modname);
248e3e3a7aSWarner Losh **  lua_pop(L, 1);  // remove PRELOAD table
258e3e3a7aSWarner Losh */
268e3e3a7aSWarner Losh 
278e3e3a7aSWarner Losh #include "lprefix.h"
288e3e3a7aSWarner Losh 
298e3e3a7aSWarner Losh 
308e3e3a7aSWarner Losh #include <stddef.h>
318e3e3a7aSWarner Losh 
328e3e3a7aSWarner Losh #include "lua.h"
338e3e3a7aSWarner Losh 
348e3e3a7aSWarner Losh #include "lualib.h"
358e3e3a7aSWarner Losh #include "lauxlib.h"
368e3e3a7aSWarner Losh 
378e3e3a7aSWarner Losh 
388e3e3a7aSWarner Losh /*
398e3e3a7aSWarner Losh ** these libs are loaded by lua.c and are readily available to any Lua
408e3e3a7aSWarner Losh ** program
418e3e3a7aSWarner Losh */
428e3e3a7aSWarner Losh static const luaL_Reg loadedlibs[] = {
43*0495ed39SKyle Evans   {LUA_GNAME, luaopen_base},
448e3e3a7aSWarner Losh   {LUA_LOADLIBNAME, luaopen_package},
458e3e3a7aSWarner Losh   {LUA_COLIBNAME, luaopen_coroutine},
468e3e3a7aSWarner Losh   {LUA_TABLIBNAME, luaopen_table},
478e3e3a7aSWarner Losh   {LUA_IOLIBNAME, luaopen_io},
488e3e3a7aSWarner Losh   {LUA_OSLIBNAME, luaopen_os},
498e3e3a7aSWarner Losh   {LUA_STRLIBNAME, luaopen_string},
508e3e3a7aSWarner Losh   {LUA_MATHLIBNAME, luaopen_math},
518e3e3a7aSWarner Losh   {LUA_UTF8LIBNAME, luaopen_utf8},
528e3e3a7aSWarner Losh   {LUA_DBLIBNAME, luaopen_debug},
538e3e3a7aSWarner Losh   {NULL, NULL}
548e3e3a7aSWarner Losh };
558e3e3a7aSWarner Losh 
568e3e3a7aSWarner Losh 
luaL_openlibs(lua_State * L)578e3e3a7aSWarner Losh LUALIB_API void luaL_openlibs (lua_State *L) {
588e3e3a7aSWarner Losh   const luaL_Reg *lib;
598e3e3a7aSWarner Losh   /* "require" functions from 'loadedlibs' and set results to global table */
608e3e3a7aSWarner Losh   for (lib = loadedlibs; lib->func; lib++) {
618e3e3a7aSWarner Losh     luaL_requiref(L, lib->name, lib->func, 1);
628e3e3a7aSWarner Losh     lua_pop(L, 1);  /* remove lib */
638e3e3a7aSWarner Losh   }
648e3e3a7aSWarner Losh }
658e3e3a7aSWarner Losh 
66