aboutsummaryrefslogtreecommitdiff
path: root/linit.c
diff options
context:
space:
mode:
Diffstat (limited to 'linit.c')
-rw-r--r--linit.c33
1 files changed, 11 insertions, 22 deletions
diff --git a/linit.c b/linit.c
index 304c8093..ed105956 100644
--- a/linit.c
+++ b/linit.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: linit.c,v 1.35 2014/11/02 19:19:04 roberto Exp roberto $ 2** $Id: linit.c,v 1.36 2014/12/06 20:42:58 roberto Exp roberto $
3** Initialization of libraries for lua.c and other clients 3** Initialization of libraries for lua.c and other clients
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -16,6 +16,15 @@
16** libraries, call luaL_openlibs in your program. If you need a 16** libraries, call luaL_openlibs in your program. If you need a
17** different set of libraries, copy this file to your project and edit 17** different set of libraries, copy this file to your project and edit
18** it to suit your needs. 18** it to suit your needs.
19**
20** You can also *preload* libraries, so that a later 'require' can
21** open the library, which is already linked to the application.
22** For that, do the following code:
23**
24** luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
25** lua_pushcfunction(L, luaopen_modname);
26** lua_setfield(L, -2, modname);
27** lua_pop(L, 1); // remove _PRELOAD table
19*/ 28*/
20 29
21#include "lua.h" 30#include "lua.h"
@@ -38,9 +47,7 @@ static const luaL_Reg loadedlibs[] = {
38 {LUA_STRLIBNAME, luaopen_string}, 47 {LUA_STRLIBNAME, luaopen_string},
39 {LUA_MATHLIBNAME, luaopen_math}, 48 {LUA_MATHLIBNAME, luaopen_math},
40 {LUA_UTF8LIBNAME, luaopen_utf8}, 49 {LUA_UTF8LIBNAME, luaopen_utf8},
41#if !defined(LUA_NODEBUGLIB)
42 {LUA_DBLIBNAME, luaopen_debug}, 50 {LUA_DBLIBNAME, luaopen_debug},
43#endif
44#if defined(LUA_COMPAT_BITLIB) 51#if defined(LUA_COMPAT_BITLIB)
45 {LUA_BITLIBNAME, luaopen_bit32}, 52 {LUA_BITLIBNAME, luaopen_bit32},
46#endif 53#endif
@@ -48,30 +55,12 @@ static const luaL_Reg loadedlibs[] = {
48}; 55};
49 56
50 57
51/*
52** these libs are preloaded and must be required before used
53*/
54static const luaL_Reg preloadedlibs[] = {
55#if defined(LUA_NODEBUGLIB)
56 {LUA_DBLIBNAME, luaopen_debug},
57#endif
58 {NULL, NULL}
59};
60
61
62LUALIB_API void luaL_openlibs (lua_State *L) { 58LUALIB_API void luaL_openlibs (lua_State *L) {
63 const luaL_Reg *lib; 59 const luaL_Reg *lib;
64 /* call open functions from 'loadedlibs' and set results to global table */ 60 /* "require" functions from 'loadedlibs' and set results to global table */
65 for (lib = loadedlibs; lib->func; lib++) { 61 for (lib = loadedlibs; lib->func; lib++) {
66 luaL_requiref(L, lib->name, lib->func, 1); 62 luaL_requiref(L, lib->name, lib->func, 1);
67 lua_pop(L, 1); /* remove lib */ 63 lua_pop(L, 1); /* remove lib */
68 } 64 }
69 /* add open functions from 'preloadedlibs' into 'package.preload' table */
70 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
71 for (lib = preloadedlibs; lib->func; lib++) {
72 lua_pushcfunction(L, lib->func);
73 lua_setfield(L, -2, lib->name);
74 }
75 lua_pop(L, 1); /* remove _PRELOAD table */
76} 65}
77 66