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