xref: /minix/external/mit/lua/dist/src/linit.c (revision 0a6a1f1d)
1 /*	$NetBSD: linit.c,v 1.4 2015/02/02 14:03:05 lneto Exp $	*/
2 
3 /*
4 ** Id: linit.c,v 1.38 2015/01/05 13:48:33 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, "_PRELOAD");
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
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
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
57   {LUA_STRLIBNAME, luaopen_string},
58 #ifndef _KERNEL
59   {LUA_MATHLIBNAME, luaopen_math},
60 #endif
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 
luaL_openlibs(lua_State * L)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