xref: /netbsd-src/external/mit/lua/dist/src/linit.c (revision 80d9064ac03cbb6a4174695f0d5b237c8766d3d0)
1 /*	$NetBSD: linit.c,v 1.3 2014/07/19 18:38:34 lneto Exp $	*/
2 
3 /*
4 ** $Id: linit.c,v 1.3 2014/07/19 18:38:34 lneto Exp $
5 ** Initialization of libraries for lua.c and other clients
6 ** See Copyright Notice in lua.h
7 */
8 
9 
10 /*
11 ** If you embed Lua in your program and need to open the standard
12 ** libraries, call luaL_openlibs in your program. If you need a
13 ** different set of libraries, copy this file to your project and edit
14 ** it to suit your needs.
15 */
16 
17 
18 #define linit_c
19 #define LUA_LIB
20 
21 #include "lua.h"
22 
23 #include "lualib.h"
24 #include "lauxlib.h"
25 
26 
27 /*
28 ** these libs are loaded by lua.c and are readily available to any Lua
29 ** program
30 */
31 static const luaL_Reg loadedlibs[] = {
32   {"_G", luaopen_base},
33 #ifndef _KERNEL
34   {LUA_LOADLIBNAME, luaopen_package},
35 #endif
36   {LUA_COLIBNAME, luaopen_coroutine},
37   {LUA_TABLIBNAME, luaopen_table},
38 #ifndef _KERNEL
39   {LUA_IOLIBNAME, luaopen_io},
40   {LUA_OSLIBNAME, luaopen_os},
41 #endif
42   {LUA_STRLIBNAME, luaopen_string},
43 #ifndef _KERNEL
44   {LUA_MATHLIBNAME, luaopen_math},
45 #endif
46   {LUA_DBLIBNAME, luaopen_debug},
47   {LUA_UTF8LIBNAME, luaopen_utf8},
48 #if defined(LUA_COMPAT_BITLIB)
49   {LUA_BITLIBNAME, luaopen_bit32},
50 #endif
51   {NULL, NULL}
52 };
53 
54 
55 /*
56 ** these libs are preloaded and must be required before used
57 */
58 static const luaL_Reg preloadedlibs[] = {
59   {NULL, NULL}
60 };
61 
62 
63 LUALIB_API void luaL_openlibs (lua_State *L) {
64   const luaL_Reg *lib;
65   /* call open functions from 'loadedlibs' and set results to global table */
66   for (lib = loadedlibs; lib->func; lib++) {
67     luaL_requiref(L, lib->name, lib->func, 1);
68     lua_pop(L, 1);  /* remove lib */
69   }
70   /* add open functions from 'preloadedlibs' into 'package.preload' table */
71   luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
72   for (lib = preloadedlibs; lib->func; lib++) {
73     lua_pushcfunction(L, lib->func);
74     lua_setfield(L, -2, lib->name);
75   }
76   lua_pop(L, 1);  /* remove _PRELOAD table */
77 }
78 
79