2 ** $Id: loadlib.c,v 1.52.1.3 2008/08/06 13:29:28 roberto Exp $
3 ** Dynamic library loader for Lua
4 ** See Copyright Notice in lua.h
6 ** This module contains an implementation of loadlib for Unix systems
7 ** that have dlfcn, an implementation for Darwin (Mac OS X), an
8 ** implementation for Windows, and a stub for other systems.
26 #define setprogdir(L) ((void)0)
30 ** {======================================================
32 ** =======================================================
36 static int readable (const char *filename
) {
37 int f
= rb
->open(filename
, O_RDONLY
); /* try to open file */
38 if (f
< 0) return 0; /* open failed */
44 static const char *pushnexttemplate (lua_State
*L
, const char *path
) {
46 while (*path
== *LUA_PATHSEP
) path
++; /* skip separators */
47 if (*path
== '\0') return NULL
; /* no more templates */
48 l
= strchr(path
, *LUA_PATHSEP
); /* find next separator */
49 if (l
== NULL
) l
= path
+ strlen(path
);
50 lua_pushlstring(L
, path
, l
- path
); /* template */
55 static const char *findfile (lua_State
*L
, const char *name
,
57 const char *path
, *current_path
= get_current_path(L
, 2);
58 name
= luaL_gsub(L
, name
, ".", LUA_DIRSEP
);
59 lua_getfield(L
, LUA_ENVIRONINDEX
, pname
);
60 path
= lua_tostring(L
, -1);
62 luaL_error(L
, LUA_QL("package.%s") " must be a string", pname
);
63 lua_pushliteral(L
, ""); /* error accumulator */
64 while ((path
= pushnexttemplate(L
, path
)) != NULL
) {
66 filename
= luaL_gsub(L
, lua_tostring(L
, -1), LUA_PATH_MARK
, name
);
67 if(current_path
!= NULL
) filename
= luaL_gsub(L
, filename
, "$", current_path
);
68 lua_remove(L
, -2); /* remove path template */
69 if (readable(filename
)) /* does file exist and is readable? */
70 return filename
; /* return that file name */
71 lua_pushfstring(L
, "\n\tno file " LUA_QS
, filename
);
72 lua_remove(L
, -2); /* remove file name */
73 lua_concat(L
, 2); /* add entry to possible error message */
75 return NULL
; /* not found */
79 static void loaderror (lua_State
*L
, const char *filename
) {
80 luaL_error(L
, "error loading module " LUA_QS
" from file " LUA_QS
":\n\t%s",
81 lua_tostring(L
, 1), filename
, lua_tostring(L
, -1));
85 static int loader_Lua (lua_State
*L
) {
87 const char *name
= luaL_checkstring(L
, 1);
88 filename
= findfile(L
, name
, "path");
89 if (filename
== NULL
) return 1; /* library not found in this path */
90 if (luaL_loadfile(L
, filename
) != 0)
91 loaderror(L
, filename
);
92 return 1; /* library loaded successfully */
96 static int loader_preload (lua_State
*L
) {
97 const char *name
= luaL_checkstring(L
, 1);
98 lua_getfield(L
, LUA_ENVIRONINDEX
, "preload");
99 if (!lua_istable(L
, -1))
100 luaL_error(L
, LUA_QL("package.preload") " must be a table");
101 lua_getfield(L
, -1, name
);
102 if (lua_isnil(L
, -1)) /* not found? */
103 lua_pushfstring(L
, "\n\tno field package.preload['%s']", name
);
108 static const int sentinel_
= 0;
109 #define sentinel ((void *)&sentinel_)
112 static int ll_require (lua_State
*L
) {
113 const char *name
= luaL_checkstring(L
, 1);
115 lua_settop(L
, 1); /* _LOADED table will be at index 2 */
116 lua_getfield(L
, LUA_REGISTRYINDEX
, "_LOADED");
117 lua_getfield(L
, 2, name
);
118 if (lua_toboolean(L
, -1)) { /* is it there? */
119 if (lua_touserdata(L
, -1) == sentinel
) /* check loops */
120 luaL_error(L
, "loop or previous error loading module " LUA_QS
, name
);
121 return 1; /* package is already loaded */
123 /* else must load it; iterate over available loaders */
124 lua_getfield(L
, LUA_ENVIRONINDEX
, "loaders");
125 if (!lua_istable(L
, -1))
126 luaL_error(L
, LUA_QL("package.loaders") " must be a table");
127 lua_pushliteral(L
, ""); /* error message accumulator */
129 lua_rawgeti(L
, -2, i
); /* get a loader */
130 if (lua_isnil(L
, -1))
131 luaL_error(L
, "module " LUA_QS
" not found:%s",
132 name
, lua_tostring(L
, -2));
133 lua_pushstring(L
, name
);
134 lua_call(L
, 1, 1); /* call it */
135 if (lua_isfunction(L
, -1)) /* did it find module? */
136 break; /* module loaded successfully */
137 else if (lua_isstring(L
, -1)) /* loader returned error message? */
138 lua_concat(L
, 2); /* accumulate it */
142 lua_pushlightuserdata(L
, sentinel
);
143 lua_setfield(L
, 2, name
); /* _LOADED[name] = sentinel */
144 lua_pushstring(L
, name
); /* pass name as argument to module */
145 lua_call(L
, 1, 1); /* run loaded module */
146 if (!lua_isnil(L
, -1)) /* non-nil return? */
147 lua_setfield(L
, 2, name
); /* _LOADED[name] = returned value */
148 lua_getfield(L
, 2, name
);
149 if (lua_touserdata(L
, -1) == sentinel
) { /* module did not set a value? */
150 lua_pushboolean(L
, 1); /* use true as result */
151 lua_pushvalue(L
, -1); /* extra copy to be returned */
152 lua_setfield(L
, 2, name
); /* _LOADED[name] = true */
157 /* }====================================================== */
162 ** {======================================================
164 ** =======================================================
168 static void setfenv (lua_State
*L
) {
170 if (lua_getstack(L
, 1, &ar
) == 0 ||
171 lua_getinfo(L
, "f", &ar
) == 0 || /* get calling function */
172 lua_iscfunction(L
, -1))
173 luaL_error(L
, LUA_QL("module") " not called from a Lua function");
174 lua_pushvalue(L
, -2);
180 static void dooptions (lua_State
*L
, int n
) {
182 for (i
= 2; i
<= n
; i
++) {
183 lua_pushvalue(L
, i
); /* get option (a function) */
184 lua_pushvalue(L
, -2); /* module */
190 static void modinit (lua_State
*L
, const char *modname
) {
192 lua_pushvalue(L
, -1);
193 lua_setfield(L
, -2, "_M"); /* module._M = module */
194 lua_pushstring(L
, modname
);
195 lua_setfield(L
, -2, "_NAME");
196 dot
= rb
->strrchr(modname
, '.'); /* look for last dot in module name */
197 if (dot
== NULL
) dot
= modname
;
199 /* set _PACKAGE as package name (full module name minus last part) */
200 lua_pushlstring(L
, modname
, dot
- modname
);
201 lua_setfield(L
, -2, "_PACKAGE");
205 static int ll_module (lua_State
*L
) {
206 const char *modname
= luaL_checkstring(L
, 1);
207 int loaded
= lua_gettop(L
) + 1; /* index of _LOADED table */
208 lua_getfield(L
, LUA_REGISTRYINDEX
, "_LOADED");
209 lua_getfield(L
, loaded
, modname
); /* get _LOADED[modname] */
210 if (!lua_istable(L
, -1)) { /* not found? */
211 lua_pop(L
, 1); /* remove previous result */
212 /* try global variable (and create one if it does not exist) */
213 if (luaL_findtable(L
, LUA_GLOBALSINDEX
, modname
, 1) != NULL
)
214 return luaL_error(L
, "name conflict for module " LUA_QS
, modname
);
215 lua_pushvalue(L
, -1);
216 lua_setfield(L
, loaded
, modname
); /* _LOADED[modname] = new table */
218 /* check whether table already has a _NAME field */
219 lua_getfield(L
, -1, "_NAME");
220 if (!lua_isnil(L
, -1)) /* is table an initialized module? */
222 else { /* no; initialize it */
226 lua_pushvalue(L
, -1);
228 dooptions(L
, loaded
- 1);
233 static int ll_seeall (lua_State
*L
) {
234 luaL_checktype(L
, 1, LUA_TTABLE
);
235 if (!lua_getmetatable(L
, 1)) {
236 lua_createtable(L
, 0, 1); /* create new metatable */
237 lua_pushvalue(L
, -1);
238 lua_setmetatable(L
, 1);
240 lua_pushvalue(L
, LUA_GLOBALSINDEX
);
241 lua_setfield(L
, -2, "__index"); /* mt.__index = _G */
246 /* }====================================================== */
250 /* auxiliary mark (for internal use) */
253 static void setpath (lua_State
*L
, const char *fieldname
, const char *envname
,
256 lua_pushstring(L
, def
); /* use default */
258 lua_setfield(L
, -2, fieldname
);
262 static const luaL_Reg pk_funcs
[] = {
263 {"seeall", ll_seeall
},
268 static const luaL_Reg ll_funcs
[] = {
269 {"module", ll_module
},
270 {"require", ll_require
},
275 static const lua_CFunction loaders
[] =
276 {loader_preload
, loader_Lua
, NULL
};
279 LUALIB_API
int luaopen_package (lua_State
*L
) {
281 /* create new type _LOADLIB */
282 luaL_newmetatable(L
, "_LOADLIB");
283 /* create `package' table */
284 luaL_register(L
, LUA_LOADLIBNAME
, pk_funcs
);
285 #if defined(LUA_COMPAT_LOADLIB)
286 lua_getfield(L
, -1, "loadlib");
287 lua_setfield(L
, LUA_GLOBALSINDEX
, "loadlib");
289 lua_pushvalue(L
, -1);
290 lua_replace(L
, LUA_ENVIRONINDEX
);
291 /* create `loaders' table */
292 lua_createtable(L
, 0, sizeof(loaders
)/sizeof(loaders
[0]) - 1);
293 /* fill it with pre-defined loaders */
294 for (i
=0; loaders
[i
] != NULL
; i
++) {
295 lua_pushcfunction(L
, loaders
[i
]);
296 lua_rawseti(L
, -2, i
+1);
298 lua_setfield(L
, -2, "loaders"); /* put it in field `loaders' */
299 setpath(L
, "path", LUA_PATH
, LUA_PATH_DEFAULT
); /* set field `path' */
300 /* store config information */
301 lua_pushliteral(L
, LUA_DIRSEP
"\n" LUA_PATHSEP
"\n" LUA_PATH_MARK
"\n"
302 LUA_EXECDIR
"\n" LUA_IGMARK
);
303 lua_setfield(L
, -2, "config");
304 /* set field `loaded' */
305 luaL_findtable(L
, LUA_REGISTRYINDEX
, "_LOADED", 2);
306 lua_setfield(L
, -2, "loaded");
307 /* set field `preload' */
309 lua_setfield(L
, -2, "preload");
310 lua_pushvalue(L
, LUA_GLOBALSINDEX
);
311 luaL_register(L
, NULL
, ll_funcs
); /* open lib into global table */
313 return 1; /* return 'package' table */