• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 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 #ifndef LUA_NUMBER_INTEGRAL
39   {LUA_MATHLIBNAME, luaopen_math},
40 #endif
41   {LUA_DBLIBNAME, luaopen_debug},
42   {NULL, NULL}
43 };
44 
45 
46 /*
47 ** these libs are preloaded and must be required before used
48 */
49 static const luaL_Reg preloadedlibs[] = {
50   {NULL, NULL}
51 };
52 
53 
luaL_openlibs(lua_State * L)54 LUALIB_API void luaL_openlibs (lua_State *L) {
55   const luaL_Reg *lib;
56   /* call open functions from 'loadedlibs' and set results to global table */
57   for (lib = loadedlibs; lib->func; lib++) {
58     luaL_requiref(L, lib->name, lib->func, 1);
59     lua_pop(L, 1);  /* remove lib */
60   }
61   /* add open functions from 'preloadedlibs' into 'package.preload' table */
62   luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
63   for (lib = preloadedlibs; lib->func; lib++) {
64     lua_pushcfunction(L, lib->func);
65     lua_setfield(L, -2, lib->name);
66   }
67   lua_pop(L, 1);  /* remove _PRELOAD table */
68 }
69 
70