aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c187
1 files changed, 179 insertions, 8 deletions
diff --git a/loadlib.c b/loadlib.c
index ae5b8ca7..dabeaeba 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.8 2004/10/18 18:07:31 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.9 2004/11/11 15:42:57 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5* 5*
@@ -25,6 +25,10 @@
25* loaders on top of which loadlib could be implemented. 25* loaders on top of which loadlib could be implemented.
26*/ 26*/
27 27
28
29#include <string.h>
30
31
28#define loadlib_c 32#define loadlib_c
29#define LUA_LIB 33#define LUA_LIB
30 34
@@ -224,13 +228,180 @@ static int loadlib1 (lua_State *L) {
224 } 228 }
225} 229}
226 230
227LUALIB_API int luaopen_loadlib (lua_State *L) 231
228{ 232
229 luaL_newmetatable(L, "_LOADLIB"); 233/*
230 lua_pushcfunction(L, gctm); 234** {======================================================
231 lua_setfield(L, -2, "__gc"); 235** `require' and `module' functions
232 lua_register(L,"loadlib",loadlib1); 236** =======================================================
233 return 0; 237*/
238
239
240static const char *loadLua (lua_State *L, const char *fname, const char *name) {
241 const char *path;
242 /* try first `LUA_PATH' for compatibility */
243 lua_getglobal(L, "LUA_PATH");
244 path = lua_tostring(L, -1);
245 if (!path) {
246 lua_pop(L, 1);
247 luaL_getfield(L, LUA_GLOBALSINDEX, "package.path");
248 path = lua_tostring(L, -1);
249 }
250 if (path == NULL)
251 luaL_error(L, "`package.path' must be a string");
252 fname = luaL_searchpath(L, fname, path);
253 if (fname == NULL) return path; /* library not found in this path */
254 if (luaL_loadfile(L, fname) != 0)
255 luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1));
256 return NULL; /* library loaded successfully */
257}
258
259
260static const char *loadC (lua_State *L, const char *fname, const char *name) {
261 const char *path;
262 const char *funcname;
263 luaL_getfield(L, LUA_GLOBALSINDEX, "package.cpath");
264 path = lua_tostring(L, -1);
265 if (path == NULL)
266 luaL_error(L, "global _CPATH must be a string");
267 fname = luaL_searchpath(L, fname, path);
268 if (fname == NULL) return path; /* library not found in this path */
269 funcname = luaL_gsub(L, name, ".", "");
270 funcname = lua_pushfstring(L, "%s%s", LUA_POF, funcname);
271 if (loadlib(L, fname, funcname) != 0)
272 luaL_error(L, "error loading package `%s' (%s)", name, lua_tostring(L, -1));
273 return NULL; /* library loaded successfully */
274}
275
276
277static int ll_require (lua_State *L) {
278 const char *name = luaL_checkstring(L, 1);
279 lua_settop(L, 1);
280 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
281 lua_getfield(L, 2, name);
282 if (lua_toboolean(L, -1)) /* is it there? */
283 return 1; /* package is already loaded; return its result */
284 /* else must load it; first mark it as loaded */
285 lua_pushboolean(L, 1);
286 lua_setfield(L, 2, name); /* _LOADED[name] = true */
287 /* check whether it is preloaded */
288 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
289 lua_getfield(L, -1, name);
290 if (lua_isnil(L, -1)) { /* no preload function for that module? */
291 const char *fname = luaL_gsub(L, name, ".", LUA_DIRSEP);
292 const char *cpath = loadC(L, fname, name); /* try a C module */
293 if (cpath) { /* not found? */
294 const char *path = loadLua(L, fname, name); /* try a Lua module */
295 if (path) { /* yet not found? */
296 lua_pushnil(L);
297 lua_setfield(L, 2, name); /* unmark _LOADED[name] */
298 return luaL_error(L, "package `%s' not found\n"
299 " cpath: %s\n path: %s",
300 name, cpath, path);
301 }
302 }
303 }
304 lua_pushvalue(L, 1); /* pass name as argument to module */
305 lua_call(L, 1, 1); /* run loaded module */
306 if (!lua_isnil(L, -1)) /* non-nil return? */
307 lua_setfield(L, 2, name); /* update _LOADED[name] with returned value */
308 lua_getfield(L, 2, name); /* return _LOADED[name] */
309 return 1;
310}
311
312
313static void setfenv (lua_State *L) {
314 lua_Debug ar;
315 lua_getstack(L, 1, &ar);
316 lua_getinfo(L, "f", &ar);
317 lua_pushvalue(L, -2);
318 lua_setfenv(L, -2);
319}
320
321
322static int ll_module (lua_State *L) {
323 const char *modname = luaL_checkstring(L, 1);
324 const char *dot;
325 lua_settop(L, 1);
326 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
327 /* try to find given table */
328 luaL_getfield(L, LUA_GLOBALSINDEX, modname);
329 if (lua_isnil(L, -1)) { /* not found? */
330 lua_pop(L, 1); /* remove previous result */
331 lua_newtable(L); /* create it */
332 /* register it with given name */
333 lua_pushvalue(L, -1);
334 luaL_setfield(L, LUA_GLOBALSINDEX, modname);
335 }
336 else if (!lua_istable(L, -1))
337 return luaL_error(L, "name conflict for module `%s'", modname);
338 /* check whether table already has a _NAME field */
339 lua_getfield(L, -1, "_NAME");
340 if (!lua_isnil(L, -1)) /* is table an initialized module? */
341 lua_pop(L, 1);
342 else { /* no; initialize it */
343 lua_pop(L, 1);
344 lua_newtable(L); /* create new metatable */
345 lua_pushvalue(L, LUA_GLOBALSINDEX);
346 lua_setfield(L, -2, "__index"); /* mt.__index = _G */
347 lua_setmetatable(L, -2);
348 lua_pushstring(L, modname);
349 lua_setfield(L, -2, "_NAME");
350 dot = strrchr(modname, '.'); /* look for last dot in module name */
351 if (dot == NULL) dot = modname;
352 else dot++;
353 /* set _PACKAGE as package name (full module name minus last part) */
354 lua_pushlstring(L, modname, dot - modname);
355 lua_setfield(L, -2, "_PACKAGE");
356 }
357 lua_pushvalue(L, -1);
358 lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */
359 setfenv(L);
360 return 0;
361}
362
363
364/* }====================================================== */
365
366
367static const luaL_reg ll_funcs[] = {
368 {"loadlib", loadlib1},
369 {"require", ll_require},
370 {"module", ll_module},
371 {NULL, NULL}
372};
373
374
375
376LUALIB_API int luaopen_loadlib (lua_State *L) {
377 const char *path;
378 /* create new type _LOADLIB */
379 luaL_newmetatable(L, "_LOADLIB");
380 lua_pushcfunction(L, gctm);
381 lua_setfield(L, -2, "__gc");
382 /* create `package' table */
383 lua_newtable(L);
384 lua_pushvalue(L, -1);
385 lua_setglobal(L, "package");
386 /* set field `path' */
387 path = getenv(LUA_PATH);
388 if (path == NULL) path = LUA_PATH_DEFAULT;
389 lua_pushstring(L, path);
390 lua_setfield(L, -2, "path");
391 /* set field `cpath' */
392 path = getenv(LUA_CPATH);
393 if (path == NULL) path = LUA_CPATH_DEFAULT;
394 lua_pushstring(L, path);
395 lua_setfield(L, -2, "cpath");
396 /* set field `loaded' */
397 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
398 lua_setfield(L, -2, "loaded");
399 /* set field `preload' */
400 lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD");
401 lua_setfield(L, -2, "preload");
402 lua_pushvalue(L, LUA_GLOBALSINDEX);
403 luaL_openlib(L, NULL, ll_funcs, 0); /* open lib into global table */
404 return 1;
234} 405}
235 406
236/* 407/*