3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h
20 /* ------------------------------------------------------------------------ */
22 /* Error codes for ll_loadfunc. */
23 #define PACKAGE_ERR_LIB 1
24 #define PACKAGE_ERR_FUNC 2
26 /* Redefined in platform specific part. */
27 #define PACKAGE_LIB_FAIL "open"
28 #define setprogdir(L) ((void)0)
34 static void ll_unloadlib(void *lib
)
39 static void *ll_load(lua_State
*L
, const char *path
)
41 void *lib
= dlopen(path
, RTLD_NOW
);
42 if (lib
== NULL
) lua_pushstring(L
, dlerror());
46 static lua_CFunction
ll_sym(lua_State
*L
, void *lib
, const char *sym
)
48 lua_CFunction f
= (lua_CFunction
)dlsym(lib
, sym
);
49 if (f
== NULL
) lua_pushstring(L
, dlerror());
53 #elif LJ_TARGET_WINDOWS
55 #define WIN32_LEAN_AND_MEAN
60 static void setprogdir(lua_State
*L
)
62 char buff
[MAX_PATH
+ 1];
64 DWORD nsize
= sizeof(buff
);
65 DWORD n
= GetModuleFileNameA(NULL
, buff
, nsize
);
66 if (n
== 0 || n
== nsize
|| (lb
= strrchr(buff
, '\\')) == NULL
) {
67 luaL_error(L
, "unable to get ModuleFileName");
70 luaL_gsub(L
, lua_tostring(L
, -1), LUA_EXECDIR
, buff
);
71 lua_remove(L
, -2); /* remove original string */
75 static void pusherror(lua_State
*L
)
77 DWORD error
= GetLastError();
79 if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM
,
80 NULL
, error
, 0, buffer
, sizeof(buffer
), NULL
))
81 lua_pushstring(L
, buffer
);
83 lua_pushfstring(L
, "system error %d\n", error
);
86 static void ll_unloadlib(void *lib
)
88 FreeLibrary((HINSTANCE
)lib
);
91 static void *ll_load(lua_State
*L
, const char *path
)
93 HINSTANCE lib
= LoadLibraryA(path
);
94 if (lib
== NULL
) pusherror(L
);
98 static lua_CFunction
ll_sym(lua_State
*L
, void *lib
, const char *sym
)
100 lua_CFunction f
= (lua_CFunction
)GetProcAddress((HINSTANCE
)lib
, sym
);
101 if (f
== NULL
) pusherror(L
);
107 #undef PACKAGE_LIB_FAIL
108 #define PACKAGE_LIB_FAIL "absent"
110 #define DLMSG "dynamic libraries not enabled; no support for target OS"
112 static void ll_unloadlib(void *lib
)
117 static void *ll_load(lua_State
*L
, const char *path
)
120 lua_pushliteral(L
, DLMSG
);
124 static lua_CFunction
ll_sym(lua_State
*L
, void *lib
, const char *sym
)
126 (void)lib
; (void)sym
;
127 lua_pushliteral(L
, DLMSG
);
132 /* ------------------------------------------------------------------------ */
134 static void **ll_register(lua_State
*L
, const char *path
)
137 lua_pushfstring(L
, "LOADLIB: %s", path
);
138 lua_gettable(L
, LUA_REGISTRYINDEX
); /* check library in registry? */
139 if (!lua_isnil(L
, -1)) { /* is there an entry? */
140 plib
= (void **)lua_touserdata(L
, -1);
141 } else { /* no entry yet; create one */
143 plib
= (void **)lua_newuserdata(L
, sizeof(void *));
145 luaL_getmetatable(L
, "_LOADLIB");
146 lua_setmetatable(L
, -2);
147 lua_pushfstring(L
, "LOADLIB: %s", path
);
148 lua_pushvalue(L
, -2);
149 lua_settable(L
, LUA_REGISTRYINDEX
);
154 static int ll_loadfunc(lua_State
*L
, const char *path
, const char *sym
)
156 void **reg
= ll_register(L
, path
);
157 if (*reg
== NULL
) *reg
= ll_load(L
, path
);
159 return PACKAGE_ERR_LIB
; /* unable to load library */
161 lua_CFunction f
= ll_sym(L
, *reg
, sym
);
163 return PACKAGE_ERR_FUNC
; /* unable to find function */
164 lua_pushcfunction(L
, f
);
165 return 0; /* return function */
169 static int lj_cf_package_loadlib(lua_State
*L
)
171 const char *path
= luaL_checkstring(L
, 1);
172 const char *init
= luaL_checkstring(L
, 2);
173 int st
= ll_loadfunc(L
, path
, init
);
174 if (st
== 0) { /* no errors? */
175 return 1; /* return the loaded function */
176 } else { /* error; error message is on stack top */
179 lua_pushstring(L
, (st
== PACKAGE_ERR_LIB
) ? PACKAGE_LIB_FAIL
: "init");
180 return 3; /* return nil, error message, and where */
184 static int lj_cf_package_unloadlib(lua_State
*L
)
186 void **lib
= (void **)luaL_checkudata(L
, 1, "_LOADLIB");
187 if (*lib
) ll_unloadlib(*lib
);
188 *lib
= NULL
; /* mark library as closed */
192 /* ------------------------------------------------------------------------ */
194 static int readable(const char *filename
)
196 FILE *f
= fopen(filename
, "r"); /* try to open file */
197 if (f
== NULL
) return 0; /* open failed */
202 static const char *pushnexttemplate(lua_State
*L
, const char *path
)
205 while (*path
== *LUA_PATHSEP
) path
++; /* skip separators */
206 if (*path
== '\0') return NULL
; /* no more templates */
207 l
= strchr(path
, *LUA_PATHSEP
); /* find next separator */
208 if (l
== NULL
) l
= path
+ strlen(path
);
209 lua_pushlstring(L
, path
, (size_t)(l
- path
)); /* template */
213 static const char *searchpath (lua_State
*L
, const char *name
,
216 name
= luaL_gsub(L
, name
, ".", LUA_DIRSEP
);
217 lua_pushliteral(L
, ""); /* error accumulator */
218 while ((path
= pushnexttemplate(L
, path
)) != NULL
) {
219 const char *filename
= luaL_gsub(L
, lua_tostring(L
, -1),
220 LUA_PATH_MARK
, name
);
221 lua_remove(L
, -2); /* remove path template */
222 if (readable(filename
)) /* does file exist and is readable? */
223 return filename
; /* return that file name */
224 lua_pushfstring(L
, "\n\tno file " LUA_QS
, filename
);
225 lua_remove(L
, -2); /* remove file name */
226 lua_concat(L
, 2); /* add entry to possible error message */
228 return NULL
; /* not found */
231 static int lj_cf_package_searchpath(lua_State
*L
)
233 const char *f
= searchpath(L
, luaL_checkstring(L
, 1), luaL_checkstring(L
, 2));
236 } else { /* error message is on top of the stack */
239 return 2; /* return nil + error message */
243 static const char *findfile(lua_State
*L
, const char *name
,
247 lua_getfield(L
, LUA_ENVIRONINDEX
, pname
);
248 path
= lua_tostring(L
, -1);
250 luaL_error(L
, LUA_QL("package.%s") " must be a string", pname
);
251 return searchpath(L
, name
, path
);
254 static void loaderror(lua_State
*L
, const char *filename
)
256 luaL_error(L
, "error loading module " LUA_QS
" from file " LUA_QS
":\n\t%s",
257 lua_tostring(L
, 1), filename
, lua_tostring(L
, -1));
260 static int lj_cf_package_loader_lua(lua_State
*L
)
262 const char *filename
;
263 const char *name
= luaL_checkstring(L
, 1);
264 filename
= findfile(L
, name
, "path");
265 if (filename
== NULL
) return 1; /* library not found in this path */
266 if (luaL_loadfile(L
, filename
) != 0)
267 loaderror(L
, filename
);
268 return 1; /* library loaded successfully */
271 static const char *mkfuncname(lua_State
*L
, const char *modname
)
273 const char *funcname
;
274 const char *mark
= strchr(modname
, *LUA_IGMARK
);
275 if (mark
) modname
= mark
+ 1;
276 funcname
= luaL_gsub(L
, modname
, ".", "_");
277 funcname
= lua_pushfstring(L
, "luaopen_%s", funcname
);
278 lua_remove(L
, -2); /* remove 'gsub' result */
282 static int lj_cf_package_loader_c(lua_State
*L
)
284 const char *funcname
;
285 const char *name
= luaL_checkstring(L
, 1);
286 const char *filename
= findfile(L
, name
, "cpath");
287 if (filename
== NULL
) return 1; /* library not found in this path */
288 funcname
= mkfuncname(L
, name
);
289 if (ll_loadfunc(L
, filename
, funcname
) != 0)
290 loaderror(L
, filename
);
291 return 1; /* library loaded successfully */
294 static int lj_cf_package_loader_croot(lua_State
*L
)
296 const char *funcname
;
297 const char *filename
;
298 const char *name
= luaL_checkstring(L
, 1);
299 const char *p
= strchr(name
, '.');
301 if (p
== NULL
) return 0; /* is root */
302 lua_pushlstring(L
, name
, (size_t)(p
- name
));
303 filename
= findfile(L
, lua_tostring(L
, -1), "cpath");
304 if (filename
== NULL
) return 1; /* root not found */
305 funcname
= mkfuncname(L
, name
);
306 if ((st
= ll_loadfunc(L
, filename
, funcname
)) != 0) {
307 if (st
!= PACKAGE_ERR_FUNC
) loaderror(L
, filename
); /* real error */
308 lua_pushfstring(L
, "\n\tno module " LUA_QS
" in file " LUA_QS
,
310 return 1; /* function not found */
315 static int lj_cf_package_loader_preload(lua_State
*L
)
317 const char *name
= luaL_checkstring(L
, 1);
318 lua_getfield(L
, LUA_ENVIRONINDEX
, "preload");
319 if (!lua_istable(L
, -1))
320 luaL_error(L
, LUA_QL("package.preload") " must be a table");
321 lua_getfield(L
, -1, name
);
322 if (lua_isnil(L
, -1)) /* not found? */
323 lua_pushfstring(L
, "\n\tno field package.preload['%s']", name
);
327 /* ------------------------------------------------------------------------ */
329 static const int sentinel_
= 0;
330 #define sentinel ((void *)&sentinel_)
332 static int lj_cf_package_require(lua_State
*L
)
334 const char *name
= luaL_checkstring(L
, 1);
336 lua_settop(L
, 1); /* _LOADED table will be at index 2 */
337 lua_getfield(L
, LUA_REGISTRYINDEX
, "_LOADED");
338 lua_getfield(L
, 2, name
);
339 if (lua_toboolean(L
, -1)) { /* is it there? */
340 if (lua_touserdata(L
, -1) == sentinel
) /* check loops */
341 luaL_error(L
, "loop or previous error loading module " LUA_QS
, name
);
342 return 1; /* package is already loaded */
344 /* else must load it; iterate over available loaders */
345 lua_getfield(L
, LUA_ENVIRONINDEX
, "loaders");
346 if (!lua_istable(L
, -1))
347 luaL_error(L
, LUA_QL("package.loaders") " must be a table");
348 lua_pushliteral(L
, ""); /* error message accumulator */
350 lua_rawgeti(L
, -2, i
); /* get a loader */
351 if (lua_isnil(L
, -1))
352 luaL_error(L
, "module " LUA_QS
" not found:%s",
353 name
, lua_tostring(L
, -2));
354 lua_pushstring(L
, name
);
355 lua_call(L
, 1, 1); /* call it */
356 if (lua_isfunction(L
, -1)) /* did it find module? */
357 break; /* module loaded successfully */
358 else if (lua_isstring(L
, -1)) /* loader returned error message? */
359 lua_concat(L
, 2); /* accumulate it */
363 lua_pushlightuserdata(L
, sentinel
);
364 lua_setfield(L
, 2, name
); /* _LOADED[name] = sentinel */
365 lua_pushstring(L
, name
); /* pass name as argument to module */
366 lua_call(L
, 1, 1); /* run loaded module */
367 if (!lua_isnil(L
, -1)) /* non-nil return? */
368 lua_setfield(L
, 2, name
); /* _LOADED[name] = returned value */
369 lua_getfield(L
, 2, name
);
370 if (lua_touserdata(L
, -1) == sentinel
) { /* module did not set a value? */
371 lua_pushboolean(L
, 1); /* use true as result */
372 lua_pushvalue(L
, -1); /* extra copy to be returned */
373 lua_setfield(L
, 2, name
); /* _LOADED[name] = true */
379 /* ------------------------------------------------------------------------ */
381 static void setfenv(lua_State
*L
)
384 if (lua_getstack(L
, 1, &ar
) == 0 ||
385 lua_getinfo(L
, "f", &ar
) == 0 || /* get calling function */
386 lua_iscfunction(L
, -1))
387 luaL_error(L
, LUA_QL("module") " not called from a Lua function");
388 lua_pushvalue(L
, -2);
393 static void dooptions(lua_State
*L
, int n
)
396 for (i
= 2; i
<= n
; i
++) {
397 lua_pushvalue(L
, i
); /* get option (a function) */
398 lua_pushvalue(L
, -2); /* module */
403 static void modinit(lua_State
*L
, const char *modname
)
406 lua_pushvalue(L
, -1);
407 lua_setfield(L
, -2, "_M"); /* module._M = module */
408 lua_pushstring(L
, modname
);
409 lua_setfield(L
, -2, "_NAME");
410 dot
= strrchr(modname
, '.'); /* look for last dot in module name */
411 if (dot
== NULL
) dot
= modname
; else dot
++;
412 /* set _PACKAGE as package name (full module name minus last part) */
413 lua_pushlstring(L
, modname
, (size_t)(dot
- modname
));
414 lua_setfield(L
, -2, "_PACKAGE");
417 static int lj_cf_package_module(lua_State
*L
)
419 const char *modname
= luaL_checkstring(L
, 1);
420 int loaded
= lua_gettop(L
) + 1; /* index of _LOADED table */
421 lua_getfield(L
, LUA_REGISTRYINDEX
, "_LOADED");
422 lua_getfield(L
, loaded
, modname
); /* get _LOADED[modname] */
423 if (!lua_istable(L
, -1)) { /* not found? */
424 lua_pop(L
, 1); /* remove previous result */
425 /* try global variable (and create one if it does not exist) */
426 if (luaL_findtable(L
, LUA_GLOBALSINDEX
, modname
, 1) != NULL
)
427 lj_err_callerv(L
, LJ_ERR_BADMODN
, modname
);
428 lua_pushvalue(L
, -1);
429 lua_setfield(L
, loaded
, modname
); /* _LOADED[modname] = new table */
431 /* check whether table already has a _NAME field */
432 lua_getfield(L
, -1, "_NAME");
433 if (!lua_isnil(L
, -1)) { /* is table an initialized module? */
435 } else { /* no; initialize it */
439 lua_pushvalue(L
, -1);
441 dooptions(L
, loaded
- 1);
445 static int lj_cf_package_seeall(lua_State
*L
)
447 luaL_checktype(L
, 1, LUA_TTABLE
);
448 if (!lua_getmetatable(L
, 1)) {
449 lua_createtable(L
, 0, 1); /* create new metatable */
450 lua_pushvalue(L
, -1);
451 lua_setmetatable(L
, 1);
453 lua_pushvalue(L
, LUA_GLOBALSINDEX
);
454 lua_setfield(L
, -2, "__index"); /* mt.__index = _G */
458 /* ------------------------------------------------------------------------ */
462 static void setpath(lua_State
*L
, const char *fieldname
, const char *envname
,
465 const char *path
= getenv(envname
);
467 lua_pushstring(L
, def
);
469 path
= luaL_gsub(L
, path
, LUA_PATHSEP LUA_PATHSEP
,
470 LUA_PATHSEP AUXMARK LUA_PATHSEP
);
471 luaL_gsub(L
, path
, AUXMARK
, def
);
475 lua_setfield(L
, -2, fieldname
);
478 static const luaL_Reg package_lib
[] = {
479 { "loadlib", lj_cf_package_loadlib
},
480 { "searchpath", lj_cf_package_searchpath
},
481 { "seeall", lj_cf_package_seeall
},
485 static const luaL_Reg package_global
[] = {
486 { "module", lj_cf_package_module
},
487 { "require", lj_cf_package_require
},
491 static const lua_CFunction package_loaders
[] =
493 lj_cf_package_loader_preload
,
494 lj_cf_package_loader_lua
,
495 lj_cf_package_loader_c
,
496 lj_cf_package_loader_croot
,
500 LUALIB_API
int luaopen_package(lua_State
*L
)
503 luaL_newmetatable(L
, "_LOADLIB");
504 lj_lib_pushcf(L
, lj_cf_package_unloadlib
, 1);
505 lua_setfield(L
, -2, "__gc");
506 luaL_register(L
, LUA_LOADLIBNAME
, package_lib
);
507 lua_pushvalue(L
, -1);
508 lua_replace(L
, LUA_ENVIRONINDEX
);
509 lua_createtable(L
, sizeof(package_loaders
)/sizeof(package_loaders
[0])-1, 0);
510 for (i
= 0; package_loaders
[i
] != NULL
; i
++) {
511 lj_lib_pushcf(L
, package_loaders
[i
], 1);
512 lua_rawseti(L
, -2, i
+1);
514 lua_setfield(L
, -2, "loaders");
515 setpath(L
, "path", LUA_PATH
, LUA_PATH_DEFAULT
);
516 setpath(L
, "cpath", LUA_CPATH
, LUA_CPATH_DEFAULT
);
517 lua_pushliteral(L
, LUA_PATH_CONFIG
);
518 lua_setfield(L
, -2, "config");
519 luaL_findtable(L
, LUA_REGISTRYINDEX
, "_LOADED", 16);
520 lua_setfield(L
, -2, "loaded");
521 luaL_findtable(L
, LUA_REGISTRYINDEX
, "_PRELOAD", 4);
522 lua_setfield(L
, -2, "preload");
523 lua_pushvalue(L
, LUA_GLOBALSINDEX
);
524 luaL_register(L
, NULL
, package_global
);