1*572c4311Sfengbojiang /*
2*572c4311Sfengbojiang ** $Id: loadlib.c,v 1.52.1.4 2009/09/09 13:17:16 roberto Exp $
3*572c4311Sfengbojiang ** Dynamic library loader for Lua
4*572c4311Sfengbojiang ** See Copyright Notice in lua.h
5*572c4311Sfengbojiang **
6*572c4311Sfengbojiang ** This module contains an implementation of loadlib for Unix systems
7*572c4311Sfengbojiang ** that have dlfcn, an implementation for Darwin (Mac OS X), an
8*572c4311Sfengbojiang ** implementation for Windows, and a stub for other systems.
9*572c4311Sfengbojiang */
10*572c4311Sfengbojiang
11*572c4311Sfengbojiang
12*572c4311Sfengbojiang #include <stdlib.h>
13*572c4311Sfengbojiang #include <string.h>
14*572c4311Sfengbojiang
15*572c4311Sfengbojiang
16*572c4311Sfengbojiang #define loadlib_c
17*572c4311Sfengbojiang #define LUA_LIB
18*572c4311Sfengbojiang
19*572c4311Sfengbojiang #include "lua.h"
20*572c4311Sfengbojiang
21*572c4311Sfengbojiang #include "lauxlib.h"
22*572c4311Sfengbojiang #include "lualib.h"
23*572c4311Sfengbojiang
24*572c4311Sfengbojiang
25*572c4311Sfengbojiang /* prefix for open functions in C libraries */
26*572c4311Sfengbojiang #define LUA_POF "luaopen_"
27*572c4311Sfengbojiang
28*572c4311Sfengbojiang /* separator for open functions in C libraries */
29*572c4311Sfengbojiang #define LUA_OFSEP "_"
30*572c4311Sfengbojiang
31*572c4311Sfengbojiang
32*572c4311Sfengbojiang #define LIBPREFIX "LOADLIB: "
33*572c4311Sfengbojiang
34*572c4311Sfengbojiang #define POF LUA_POF
35*572c4311Sfengbojiang #define LIB_FAIL "open"
36*572c4311Sfengbojiang
37*572c4311Sfengbojiang
38*572c4311Sfengbojiang /* error codes for ll_loadfunc */
39*572c4311Sfengbojiang #define ERRLIB 1
40*572c4311Sfengbojiang #define ERRFUNC 2
41*572c4311Sfengbojiang
42*572c4311Sfengbojiang #define setprogdir(L) ((void)0)
43*572c4311Sfengbojiang
44*572c4311Sfengbojiang
45*572c4311Sfengbojiang static void ll_unloadlib (void *lib);
46*572c4311Sfengbojiang static void *ll_load (lua_State *L, const char *path);
47*572c4311Sfengbojiang static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
48*572c4311Sfengbojiang
49*572c4311Sfengbojiang
50*572c4311Sfengbojiang
51*572c4311Sfengbojiang #if defined(LUA_DL_DLOPEN)
52*572c4311Sfengbojiang /*
53*572c4311Sfengbojiang ** {========================================================================
54*572c4311Sfengbojiang ** This is an implementation of loadlib based on the dlfcn interface.
55*572c4311Sfengbojiang ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,
56*572c4311Sfengbojiang ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
57*572c4311Sfengbojiang ** as an emulation layer on top of native functions.
58*572c4311Sfengbojiang ** =========================================================================
59*572c4311Sfengbojiang */
60*572c4311Sfengbojiang
61*572c4311Sfengbojiang #include <dlfcn.h>
62*572c4311Sfengbojiang
ll_unloadlib(void * lib)63*572c4311Sfengbojiang static void ll_unloadlib (void *lib) {
64*572c4311Sfengbojiang dlclose(lib);
65*572c4311Sfengbojiang }
66*572c4311Sfengbojiang
67*572c4311Sfengbojiang
ll_load(lua_State * L,const char * path)68*572c4311Sfengbojiang static void *ll_load (lua_State *L, const char *path) {
69*572c4311Sfengbojiang void *lib = dlopen(path, RTLD_NOW);
70*572c4311Sfengbojiang if (lib == NULL) lua_pushstring(L, dlerror());
71*572c4311Sfengbojiang return lib;
72*572c4311Sfengbojiang }
73*572c4311Sfengbojiang
74*572c4311Sfengbojiang
ll_sym(lua_State * L,void * lib,const char * sym)75*572c4311Sfengbojiang static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
76*572c4311Sfengbojiang lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
77*572c4311Sfengbojiang if (f == NULL) lua_pushstring(L, dlerror());
78*572c4311Sfengbojiang return f;
79*572c4311Sfengbojiang }
80*572c4311Sfengbojiang
81*572c4311Sfengbojiang /* }====================================================== */
82*572c4311Sfengbojiang
83*572c4311Sfengbojiang
84*572c4311Sfengbojiang
85*572c4311Sfengbojiang #elif defined(LUA_DL_DLL)
86*572c4311Sfengbojiang /*
87*572c4311Sfengbojiang ** {======================================================================
88*572c4311Sfengbojiang ** This is an implementation of loadlib for Windows using native functions.
89*572c4311Sfengbojiang ** =======================================================================
90*572c4311Sfengbojiang */
91*572c4311Sfengbojiang
92*572c4311Sfengbojiang #include <windows.h>
93*572c4311Sfengbojiang
94*572c4311Sfengbojiang
95*572c4311Sfengbojiang #undef setprogdir
96*572c4311Sfengbojiang
setprogdir(lua_State * L)97*572c4311Sfengbojiang static void setprogdir (lua_State *L) {
98*572c4311Sfengbojiang char buff[MAX_PATH + 1];
99*572c4311Sfengbojiang char *lb;
100*572c4311Sfengbojiang DWORD nsize = sizeof(buff)/sizeof(char);
101*572c4311Sfengbojiang DWORD n = GetModuleFileNameA(NULL, buff, nsize);
102*572c4311Sfengbojiang if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
103*572c4311Sfengbojiang luaL_error(L, "unable to get ModuleFileName");
104*572c4311Sfengbojiang else {
105*572c4311Sfengbojiang *lb = '\0';
106*572c4311Sfengbojiang luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);
107*572c4311Sfengbojiang lua_remove(L, -2); /* remove original string */
108*572c4311Sfengbojiang }
109*572c4311Sfengbojiang }
110*572c4311Sfengbojiang
111*572c4311Sfengbojiang
pusherror(lua_State * L)112*572c4311Sfengbojiang static void pusherror (lua_State *L) {
113*572c4311Sfengbojiang int error = GetLastError();
114*572c4311Sfengbojiang char buffer[128];
115*572c4311Sfengbojiang if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
116*572c4311Sfengbojiang NULL, error, 0, buffer, sizeof(buffer), NULL))
117*572c4311Sfengbojiang lua_pushstring(L, buffer);
118*572c4311Sfengbojiang else
119*572c4311Sfengbojiang lua_pushfstring(L, "system error %d\n", error);
120*572c4311Sfengbojiang }
121*572c4311Sfengbojiang
ll_unloadlib(void * lib)122*572c4311Sfengbojiang static void ll_unloadlib (void *lib) {
123*572c4311Sfengbojiang FreeLibrary((HINSTANCE)lib);
124*572c4311Sfengbojiang }
125*572c4311Sfengbojiang
126*572c4311Sfengbojiang
ll_load(lua_State * L,const char * path)127*572c4311Sfengbojiang static void *ll_load (lua_State *L, const char *path) {
128*572c4311Sfengbojiang HINSTANCE lib = LoadLibraryA(path);
129*572c4311Sfengbojiang if (lib == NULL) pusherror(L);
130*572c4311Sfengbojiang return lib;
131*572c4311Sfengbojiang }
132*572c4311Sfengbojiang
133*572c4311Sfengbojiang
ll_sym(lua_State * L,void * lib,const char * sym)134*572c4311Sfengbojiang static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
135*572c4311Sfengbojiang lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);
136*572c4311Sfengbojiang if (f == NULL) pusherror(L);
137*572c4311Sfengbojiang return f;
138*572c4311Sfengbojiang }
139*572c4311Sfengbojiang
140*572c4311Sfengbojiang /* }====================================================== */
141*572c4311Sfengbojiang
142*572c4311Sfengbojiang
143*572c4311Sfengbojiang
144*572c4311Sfengbojiang #elif defined(LUA_DL_DYLD)
145*572c4311Sfengbojiang /*
146*572c4311Sfengbojiang ** {======================================================================
147*572c4311Sfengbojiang ** Native Mac OS X / Darwin Implementation
148*572c4311Sfengbojiang ** =======================================================================
149*572c4311Sfengbojiang */
150*572c4311Sfengbojiang
151*572c4311Sfengbojiang #include <mach-o/dyld.h>
152*572c4311Sfengbojiang
153*572c4311Sfengbojiang
154*572c4311Sfengbojiang /* Mac appends a `_' before C function names */
155*572c4311Sfengbojiang #undef POF
156*572c4311Sfengbojiang #define POF "_" LUA_POF
157*572c4311Sfengbojiang
158*572c4311Sfengbojiang
pusherror(lua_State * L)159*572c4311Sfengbojiang static void pusherror (lua_State *L) {
160*572c4311Sfengbojiang const char *err_str;
161*572c4311Sfengbojiang const char *err_file;
162*572c4311Sfengbojiang NSLinkEditErrors err;
163*572c4311Sfengbojiang int err_num;
164*572c4311Sfengbojiang NSLinkEditError(&err, &err_num, &err_file, &err_str);
165*572c4311Sfengbojiang lua_pushstring(L, err_str);
166*572c4311Sfengbojiang }
167*572c4311Sfengbojiang
168*572c4311Sfengbojiang
errorfromcode(NSObjectFileImageReturnCode ret)169*572c4311Sfengbojiang static const char *errorfromcode (NSObjectFileImageReturnCode ret) {
170*572c4311Sfengbojiang switch (ret) {
171*572c4311Sfengbojiang case NSObjectFileImageInappropriateFile:
172*572c4311Sfengbojiang return "file is not a bundle";
173*572c4311Sfengbojiang case NSObjectFileImageArch:
174*572c4311Sfengbojiang return "library is for wrong CPU type";
175*572c4311Sfengbojiang case NSObjectFileImageFormat:
176*572c4311Sfengbojiang return "bad format";
177*572c4311Sfengbojiang case NSObjectFileImageAccess:
178*572c4311Sfengbojiang return "cannot access file";
179*572c4311Sfengbojiang case NSObjectFileImageFailure:
180*572c4311Sfengbojiang default:
181*572c4311Sfengbojiang return "unable to load library";
182*572c4311Sfengbojiang }
183*572c4311Sfengbojiang }
184*572c4311Sfengbojiang
185*572c4311Sfengbojiang
ll_unloadlib(void * lib)186*572c4311Sfengbojiang static void ll_unloadlib (void *lib) {
187*572c4311Sfengbojiang NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);
188*572c4311Sfengbojiang }
189*572c4311Sfengbojiang
190*572c4311Sfengbojiang
ll_load(lua_State * L,const char * path)191*572c4311Sfengbojiang static void *ll_load (lua_State *L, const char *path) {
192*572c4311Sfengbojiang NSObjectFileImage img;
193*572c4311Sfengbojiang NSObjectFileImageReturnCode ret;
194*572c4311Sfengbojiang /* this would be a rare case, but prevents crashing if it happens */
195*572c4311Sfengbojiang if(!_dyld_present()) {
196*572c4311Sfengbojiang lua_pushliteral(L, "dyld not present");
197*572c4311Sfengbojiang return NULL;
198*572c4311Sfengbojiang }
199*572c4311Sfengbojiang ret = NSCreateObjectFileImageFromFile(path, &img);
200*572c4311Sfengbojiang if (ret == NSObjectFileImageSuccess) {
201*572c4311Sfengbojiang NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE |
202*572c4311Sfengbojiang NSLINKMODULE_OPTION_RETURN_ON_ERROR);
203*572c4311Sfengbojiang NSDestroyObjectFileImage(img);
204*572c4311Sfengbojiang if (mod == NULL) pusherror(L);
205*572c4311Sfengbojiang return mod;
206*572c4311Sfengbojiang }
207*572c4311Sfengbojiang lua_pushstring(L, errorfromcode(ret));
208*572c4311Sfengbojiang return NULL;
209*572c4311Sfengbojiang }
210*572c4311Sfengbojiang
211*572c4311Sfengbojiang
ll_sym(lua_State * L,void * lib,const char * sym)212*572c4311Sfengbojiang static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
213*572c4311Sfengbojiang NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym);
214*572c4311Sfengbojiang if (nss == NULL) {
215*572c4311Sfengbojiang lua_pushfstring(L, "symbol " LUA_QS " not found", sym);
216*572c4311Sfengbojiang return NULL;
217*572c4311Sfengbojiang }
218*572c4311Sfengbojiang return (lua_CFunction)NSAddressOfSymbol(nss);
219*572c4311Sfengbojiang }
220*572c4311Sfengbojiang
221*572c4311Sfengbojiang /* }====================================================== */
222*572c4311Sfengbojiang
223*572c4311Sfengbojiang
224*572c4311Sfengbojiang
225*572c4311Sfengbojiang #else
226*572c4311Sfengbojiang /*
227*572c4311Sfengbojiang ** {======================================================
228*572c4311Sfengbojiang ** Fallback for other systems
229*572c4311Sfengbojiang ** =======================================================
230*572c4311Sfengbojiang */
231*572c4311Sfengbojiang
232*572c4311Sfengbojiang #undef LIB_FAIL
233*572c4311Sfengbojiang #define LIB_FAIL "absent"
234*572c4311Sfengbojiang
235*572c4311Sfengbojiang
236*572c4311Sfengbojiang #define DLMSG "dynamic libraries not enabled; check your Lua installation"
237*572c4311Sfengbojiang
238*572c4311Sfengbojiang
ll_unloadlib(void * lib)239*572c4311Sfengbojiang static void ll_unloadlib (void *lib) {
240*572c4311Sfengbojiang (void)lib; /* to avoid warnings */
241*572c4311Sfengbojiang }
242*572c4311Sfengbojiang
243*572c4311Sfengbojiang
ll_load(lua_State * L,const char * path)244*572c4311Sfengbojiang static void *ll_load (lua_State *L, const char *path) {
245*572c4311Sfengbojiang (void)path; /* to avoid warnings */
246*572c4311Sfengbojiang lua_pushliteral(L, DLMSG);
247*572c4311Sfengbojiang return NULL;
248*572c4311Sfengbojiang }
249*572c4311Sfengbojiang
250*572c4311Sfengbojiang
ll_sym(lua_State * L,void * lib,const char * sym)251*572c4311Sfengbojiang static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
252*572c4311Sfengbojiang (void)lib; (void)sym; /* to avoid warnings */
253*572c4311Sfengbojiang lua_pushliteral(L, DLMSG);
254*572c4311Sfengbojiang return NULL;
255*572c4311Sfengbojiang }
256*572c4311Sfengbojiang
257*572c4311Sfengbojiang /* }====================================================== */
258*572c4311Sfengbojiang #endif
259*572c4311Sfengbojiang
260*572c4311Sfengbojiang
261*572c4311Sfengbojiang
ll_register(lua_State * L,const char * path)262*572c4311Sfengbojiang static void **ll_register (lua_State *L, const char *path) {
263*572c4311Sfengbojiang void **plib;
264*572c4311Sfengbojiang lua_pushfstring(L, "%s%s", LIBPREFIX, path);
265*572c4311Sfengbojiang lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */
266*572c4311Sfengbojiang if (!lua_isnil(L, -1)) /* is there an entry? */
267*572c4311Sfengbojiang plib = (void **)lua_touserdata(L, -1);
268*572c4311Sfengbojiang else { /* no entry yet; create one */
269*572c4311Sfengbojiang lua_pop(L, 1);
270*572c4311Sfengbojiang plib = (void **)lua_newuserdata(L, sizeof(const void *));
271*572c4311Sfengbojiang *plib = NULL;
272*572c4311Sfengbojiang luaL_getmetatable(L, "_LOADLIB");
273*572c4311Sfengbojiang lua_setmetatable(L, -2);
274*572c4311Sfengbojiang lua_pushfstring(L, "%s%s", LIBPREFIX, path);
275*572c4311Sfengbojiang lua_pushvalue(L, -2);
276*572c4311Sfengbojiang lua_settable(L, LUA_REGISTRYINDEX);
277*572c4311Sfengbojiang }
278*572c4311Sfengbojiang return plib;
279*572c4311Sfengbojiang }
280*572c4311Sfengbojiang
281*572c4311Sfengbojiang
282*572c4311Sfengbojiang /*
283*572c4311Sfengbojiang ** __gc tag method: calls library's `ll_unloadlib' function with the lib
284*572c4311Sfengbojiang ** handle
285*572c4311Sfengbojiang */
gctm(lua_State * L)286*572c4311Sfengbojiang static int gctm (lua_State *L) {
287*572c4311Sfengbojiang void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB");
288*572c4311Sfengbojiang if (*lib) ll_unloadlib(*lib);
289*572c4311Sfengbojiang *lib = NULL; /* mark library as closed */
290*572c4311Sfengbojiang return 0;
291*572c4311Sfengbojiang }
292*572c4311Sfengbojiang
293*572c4311Sfengbojiang
ll_loadfunc(lua_State * L,const char * path,const char * sym)294*572c4311Sfengbojiang static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
295*572c4311Sfengbojiang void **reg = ll_register(L, path);
296*572c4311Sfengbojiang if (*reg == NULL) *reg = ll_load(L, path);
297*572c4311Sfengbojiang if (*reg == NULL)
298*572c4311Sfengbojiang return ERRLIB; /* unable to load library */
299*572c4311Sfengbojiang else {
300*572c4311Sfengbojiang lua_CFunction f = ll_sym(L, *reg, sym);
301*572c4311Sfengbojiang if (f == NULL)
302*572c4311Sfengbojiang return ERRFUNC; /* unable to find function */
303*572c4311Sfengbojiang lua_pushcfunction(L, f);
304*572c4311Sfengbojiang return 0; /* return function */
305*572c4311Sfengbojiang }
306*572c4311Sfengbojiang }
307*572c4311Sfengbojiang
308*572c4311Sfengbojiang
ll_loadlib(lua_State * L)309*572c4311Sfengbojiang static int ll_loadlib (lua_State *L) {
310*572c4311Sfengbojiang const char *path = luaL_checkstring(L, 1);
311*572c4311Sfengbojiang const char *init = luaL_checkstring(L, 2);
312*572c4311Sfengbojiang int stat = ll_loadfunc(L, path, init);
313*572c4311Sfengbojiang if (stat == 0) /* no errors? */
314*572c4311Sfengbojiang return 1; /* return the loaded function */
315*572c4311Sfengbojiang else { /* error; error message is on stack top */
316*572c4311Sfengbojiang lua_pushnil(L);
317*572c4311Sfengbojiang lua_insert(L, -2);
318*572c4311Sfengbojiang lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init");
319*572c4311Sfengbojiang return 3; /* return nil, error message, and where */
320*572c4311Sfengbojiang }
321*572c4311Sfengbojiang }
322*572c4311Sfengbojiang
323*572c4311Sfengbojiang
324*572c4311Sfengbojiang
325*572c4311Sfengbojiang /*
326*572c4311Sfengbojiang ** {======================================================
327*572c4311Sfengbojiang ** 'require' function
328*572c4311Sfengbojiang ** =======================================================
329*572c4311Sfengbojiang */
330*572c4311Sfengbojiang
331*572c4311Sfengbojiang
readable(const char * filename)332*572c4311Sfengbojiang static int readable (const char *filename) {
333*572c4311Sfengbojiang FILE *f = fopen(filename, "r"); /* try to open file */
334*572c4311Sfengbojiang if (f == NULL) return 0; /* open failed */
335*572c4311Sfengbojiang fclose(f);
336*572c4311Sfengbojiang return 1;
337*572c4311Sfengbojiang }
338*572c4311Sfengbojiang
339*572c4311Sfengbojiang
pushnexttemplate(lua_State * L,const char * path)340*572c4311Sfengbojiang static const char *pushnexttemplate (lua_State *L, const char *path) {
341*572c4311Sfengbojiang const char *l;
342*572c4311Sfengbojiang while (*path == *LUA_PATHSEP) path++; /* skip separators */
343*572c4311Sfengbojiang if (*path == '\0') return NULL; /* no more templates */
344*572c4311Sfengbojiang l = strchr(path, *LUA_PATHSEP); /* find next separator */
345*572c4311Sfengbojiang if (l == NULL) l = path + strlen(path);
346*572c4311Sfengbojiang lua_pushlstring(L, path, l - path); /* template */
347*572c4311Sfengbojiang return l;
348*572c4311Sfengbojiang }
349*572c4311Sfengbojiang
350*572c4311Sfengbojiang
findfile(lua_State * L,const char * name,const char * pname)351*572c4311Sfengbojiang static const char *findfile (lua_State *L, const char *name,
352*572c4311Sfengbojiang const char *pname) {
353*572c4311Sfengbojiang const char *path;
354*572c4311Sfengbojiang name = luaL_gsub(L, name, ".", LUA_DIRSEP);
355*572c4311Sfengbojiang lua_getfield(L, LUA_ENVIRONINDEX, pname);
356*572c4311Sfengbojiang path = lua_tostring(L, -1);
357*572c4311Sfengbojiang if (path == NULL)
358*572c4311Sfengbojiang luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
359*572c4311Sfengbojiang lua_pushliteral(L, ""); /* error accumulator */
360*572c4311Sfengbojiang while ((path = pushnexttemplate(L, path)) != NULL) {
361*572c4311Sfengbojiang const char *filename;
362*572c4311Sfengbojiang filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
363*572c4311Sfengbojiang lua_remove(L, -2); /* remove path template */
364*572c4311Sfengbojiang if (readable(filename)) /* does file exist and is readable? */
365*572c4311Sfengbojiang return filename; /* return that file name */
366*572c4311Sfengbojiang lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
367*572c4311Sfengbojiang lua_remove(L, -2); /* remove file name */
368*572c4311Sfengbojiang lua_concat(L, 2); /* add entry to possible error message */
369*572c4311Sfengbojiang }
370*572c4311Sfengbojiang return NULL; /* not found */
371*572c4311Sfengbojiang }
372*572c4311Sfengbojiang
373*572c4311Sfengbojiang
loaderror(lua_State * L,const char * filename)374*572c4311Sfengbojiang static void loaderror (lua_State *L, const char *filename) {
375*572c4311Sfengbojiang luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
376*572c4311Sfengbojiang lua_tostring(L, 1), filename, lua_tostring(L, -1));
377*572c4311Sfengbojiang }
378*572c4311Sfengbojiang
379*572c4311Sfengbojiang
loader_Lua(lua_State * L)380*572c4311Sfengbojiang static int loader_Lua (lua_State *L) {
381*572c4311Sfengbojiang const char *filename;
382*572c4311Sfengbojiang const char *name = luaL_checkstring(L, 1);
383*572c4311Sfengbojiang filename = findfile(L, name, "path");
384*572c4311Sfengbojiang if (filename == NULL) return 1; /* library not found in this path */
385*572c4311Sfengbojiang if (luaL_loadfile(L, filename) != 0)
386*572c4311Sfengbojiang loaderror(L, filename);
387*572c4311Sfengbojiang return 1; /* library loaded successfully */
388*572c4311Sfengbojiang }
389*572c4311Sfengbojiang
390*572c4311Sfengbojiang
mkfuncname(lua_State * L,const char * modname)391*572c4311Sfengbojiang static const char *mkfuncname (lua_State *L, const char *modname) {
392*572c4311Sfengbojiang const char *funcname;
393*572c4311Sfengbojiang const char *mark = strchr(modname, *LUA_IGMARK);
394*572c4311Sfengbojiang if (mark) modname = mark + 1;
395*572c4311Sfengbojiang funcname = luaL_gsub(L, modname, ".", LUA_OFSEP);
396*572c4311Sfengbojiang funcname = lua_pushfstring(L, POF"%s", funcname);
397*572c4311Sfengbojiang lua_remove(L, -2); /* remove 'gsub' result */
398*572c4311Sfengbojiang return funcname;
399*572c4311Sfengbojiang }
400*572c4311Sfengbojiang
401*572c4311Sfengbojiang
loader_C(lua_State * L)402*572c4311Sfengbojiang static int loader_C (lua_State *L) {
403*572c4311Sfengbojiang const char *funcname;
404*572c4311Sfengbojiang const char *name = luaL_checkstring(L, 1);
405*572c4311Sfengbojiang const char *filename = findfile(L, name, "cpath");
406*572c4311Sfengbojiang if (filename == NULL) return 1; /* library not found in this path */
407*572c4311Sfengbojiang funcname = mkfuncname(L, name);
408*572c4311Sfengbojiang if (ll_loadfunc(L, filename, funcname) != 0)
409*572c4311Sfengbojiang loaderror(L, filename);
410*572c4311Sfengbojiang return 1; /* library loaded successfully */
411*572c4311Sfengbojiang }
412*572c4311Sfengbojiang
413*572c4311Sfengbojiang
loader_Croot(lua_State * L)414*572c4311Sfengbojiang static int loader_Croot (lua_State *L) {
415*572c4311Sfengbojiang const char *funcname;
416*572c4311Sfengbojiang const char *filename;
417*572c4311Sfengbojiang const char *name = luaL_checkstring(L, 1);
418*572c4311Sfengbojiang const char *p = strchr(name, '.');
419*572c4311Sfengbojiang int stat;
420*572c4311Sfengbojiang if (p == NULL) return 0; /* is root */
421*572c4311Sfengbojiang lua_pushlstring(L, name, p - name);
422*572c4311Sfengbojiang filename = findfile(L, lua_tostring(L, -1), "cpath");
423*572c4311Sfengbojiang if (filename == NULL) return 1; /* root not found */
424*572c4311Sfengbojiang funcname = mkfuncname(L, name);
425*572c4311Sfengbojiang if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {
426*572c4311Sfengbojiang if (stat != ERRFUNC) loaderror(L, filename); /* real error */
427*572c4311Sfengbojiang lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
428*572c4311Sfengbojiang name, filename);
429*572c4311Sfengbojiang return 1; /* function not found */
430*572c4311Sfengbojiang }
431*572c4311Sfengbojiang return 1;
432*572c4311Sfengbojiang }
433*572c4311Sfengbojiang
434*572c4311Sfengbojiang
loader_preload(lua_State * L)435*572c4311Sfengbojiang static int loader_preload (lua_State *L) {
436*572c4311Sfengbojiang const char *name = luaL_checkstring(L, 1);
437*572c4311Sfengbojiang lua_getfield(L, LUA_ENVIRONINDEX, "preload");
438*572c4311Sfengbojiang if (!lua_istable(L, -1))
439*572c4311Sfengbojiang luaL_error(L, LUA_QL("package.preload") " must be a table");
440*572c4311Sfengbojiang lua_getfield(L, -1, name);
441*572c4311Sfengbojiang if (lua_isnil(L, -1)) /* not found? */
442*572c4311Sfengbojiang lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
443*572c4311Sfengbojiang return 1;
444*572c4311Sfengbojiang }
445*572c4311Sfengbojiang
446*572c4311Sfengbojiang
447*572c4311Sfengbojiang static const int sentinel_ = 0;
448*572c4311Sfengbojiang #define sentinel ((void *)&sentinel_)
449*572c4311Sfengbojiang
450*572c4311Sfengbojiang
ll_require(lua_State * L)451*572c4311Sfengbojiang static int ll_require (lua_State *L) {
452*572c4311Sfengbojiang const char *name = luaL_checkstring(L, 1);
453*572c4311Sfengbojiang int i;
454*572c4311Sfengbojiang lua_settop(L, 1); /* _LOADED table will be at index 2 */
455*572c4311Sfengbojiang lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
456*572c4311Sfengbojiang lua_getfield(L, 2, name);
457*572c4311Sfengbojiang if (lua_toboolean(L, -1)) { /* is it there? */
458*572c4311Sfengbojiang if (lua_touserdata(L, -1) == sentinel) /* check loops */
459*572c4311Sfengbojiang luaL_error(L, "loop or previous error loading module " LUA_QS, name);
460*572c4311Sfengbojiang return 1; /* package is already loaded */
461*572c4311Sfengbojiang }
462*572c4311Sfengbojiang /* else must load it; iterate over available loaders */
463*572c4311Sfengbojiang lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
464*572c4311Sfengbojiang if (!lua_istable(L, -1))
465*572c4311Sfengbojiang luaL_error(L, LUA_QL("package.loaders") " must be a table");
466*572c4311Sfengbojiang lua_pushliteral(L, ""); /* error message accumulator */
467*572c4311Sfengbojiang for (i=1; ; i++) {
468*572c4311Sfengbojiang lua_rawgeti(L, -2, i); /* get a loader */
469*572c4311Sfengbojiang if (lua_isnil(L, -1))
470*572c4311Sfengbojiang luaL_error(L, "module " LUA_QS " not found:%s",
471*572c4311Sfengbojiang name, lua_tostring(L, -2));
472*572c4311Sfengbojiang lua_pushstring(L, name);
473*572c4311Sfengbojiang lua_call(L, 1, 1); /* call it */
474*572c4311Sfengbojiang if (lua_isfunction(L, -1)) /* did it find module? */
475*572c4311Sfengbojiang break; /* module loaded successfully */
476*572c4311Sfengbojiang else if (lua_isstring(L, -1)) /* loader returned error message? */
477*572c4311Sfengbojiang lua_concat(L, 2); /* accumulate it */
478*572c4311Sfengbojiang else
479*572c4311Sfengbojiang lua_pop(L, 1);
480*572c4311Sfengbojiang }
481*572c4311Sfengbojiang lua_pushlightuserdata(L, sentinel);
482*572c4311Sfengbojiang lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
483*572c4311Sfengbojiang lua_pushstring(L, name); /* pass name as argument to module */
484*572c4311Sfengbojiang lua_call(L, 1, 1); /* run loaded module */
485*572c4311Sfengbojiang if (!lua_isnil(L, -1)) /* non-nil return? */
486*572c4311Sfengbojiang lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
487*572c4311Sfengbojiang lua_getfield(L, 2, name);
488*572c4311Sfengbojiang if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
489*572c4311Sfengbojiang lua_pushboolean(L, 1); /* use true as result */
490*572c4311Sfengbojiang lua_pushvalue(L, -1); /* extra copy to be returned */
491*572c4311Sfengbojiang lua_setfield(L, 2, name); /* _LOADED[name] = true */
492*572c4311Sfengbojiang }
493*572c4311Sfengbojiang return 1;
494*572c4311Sfengbojiang }
495*572c4311Sfengbojiang
496*572c4311Sfengbojiang /* }====================================================== */
497*572c4311Sfengbojiang
498*572c4311Sfengbojiang
499*572c4311Sfengbojiang
500*572c4311Sfengbojiang /*
501*572c4311Sfengbojiang ** {======================================================
502*572c4311Sfengbojiang ** 'module' function
503*572c4311Sfengbojiang ** =======================================================
504*572c4311Sfengbojiang */
505*572c4311Sfengbojiang
506*572c4311Sfengbojiang
setfenv(lua_State * L)507*572c4311Sfengbojiang static void setfenv (lua_State *L) {
508*572c4311Sfengbojiang lua_Debug ar;
509*572c4311Sfengbojiang if (lua_getstack(L, 1, &ar) == 0 ||
510*572c4311Sfengbojiang lua_getinfo(L, "f", &ar) == 0 || /* get calling function */
511*572c4311Sfengbojiang lua_iscfunction(L, -1))
512*572c4311Sfengbojiang luaL_error(L, LUA_QL("module") " not called from a Lua function");
513*572c4311Sfengbojiang lua_pushvalue(L, -2);
514*572c4311Sfengbojiang lua_setfenv(L, -2);
515*572c4311Sfengbojiang lua_pop(L, 1);
516*572c4311Sfengbojiang }
517*572c4311Sfengbojiang
518*572c4311Sfengbojiang
dooptions(lua_State * L,int n)519*572c4311Sfengbojiang static void dooptions (lua_State *L, int n) {
520*572c4311Sfengbojiang int i;
521*572c4311Sfengbojiang for (i = 2; i <= n; i++) {
522*572c4311Sfengbojiang lua_pushvalue(L, i); /* get option (a function) */
523*572c4311Sfengbojiang lua_pushvalue(L, -2); /* module */
524*572c4311Sfengbojiang lua_call(L, 1, 0);
525*572c4311Sfengbojiang }
526*572c4311Sfengbojiang }
527*572c4311Sfengbojiang
528*572c4311Sfengbojiang
modinit(lua_State * L,const char * modname)529*572c4311Sfengbojiang static void modinit (lua_State *L, const char *modname) {
530*572c4311Sfengbojiang const char *dot;
531*572c4311Sfengbojiang lua_pushvalue(L, -1);
532*572c4311Sfengbojiang lua_setfield(L, -2, "_M"); /* module._M = module */
533*572c4311Sfengbojiang lua_pushstring(L, modname);
534*572c4311Sfengbojiang lua_setfield(L, -2, "_NAME");
535*572c4311Sfengbojiang dot = strrchr(modname, '.'); /* look for last dot in module name */
536*572c4311Sfengbojiang if (dot == NULL) dot = modname;
537*572c4311Sfengbojiang else dot++;
538*572c4311Sfengbojiang /* set _PACKAGE as package name (full module name minus last part) */
539*572c4311Sfengbojiang lua_pushlstring(L, modname, dot - modname);
540*572c4311Sfengbojiang lua_setfield(L, -2, "_PACKAGE");
541*572c4311Sfengbojiang }
542*572c4311Sfengbojiang
543*572c4311Sfengbojiang
ll_module(lua_State * L)544*572c4311Sfengbojiang static int ll_module (lua_State *L) {
545*572c4311Sfengbojiang const char *modname = luaL_checkstring(L, 1);
546*572c4311Sfengbojiang int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
547*572c4311Sfengbojiang lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
548*572c4311Sfengbojiang lua_getfield(L, loaded, modname); /* get _LOADED[modname] */
549*572c4311Sfengbojiang if (!lua_istable(L, -1)) { /* not found? */
550*572c4311Sfengbojiang lua_pop(L, 1); /* remove previous result */
551*572c4311Sfengbojiang /* try global variable (and create one if it does not exist) */
552*572c4311Sfengbojiang if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
553*572c4311Sfengbojiang return luaL_error(L, "name conflict for module " LUA_QS, modname);
554*572c4311Sfengbojiang lua_pushvalue(L, -1);
555*572c4311Sfengbojiang lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */
556*572c4311Sfengbojiang }
557*572c4311Sfengbojiang /* check whether table already has a _NAME field */
558*572c4311Sfengbojiang lua_getfield(L, -1, "_NAME");
559*572c4311Sfengbojiang if (!lua_isnil(L, -1)) /* is table an initialized module? */
560*572c4311Sfengbojiang lua_pop(L, 1);
561*572c4311Sfengbojiang else { /* no; initialize it */
562*572c4311Sfengbojiang lua_pop(L, 1);
563*572c4311Sfengbojiang modinit(L, modname);
564*572c4311Sfengbojiang }
565*572c4311Sfengbojiang lua_pushvalue(L, -1);
566*572c4311Sfengbojiang setfenv(L);
567*572c4311Sfengbojiang dooptions(L, loaded - 1);
568*572c4311Sfengbojiang return 0;
569*572c4311Sfengbojiang }
570*572c4311Sfengbojiang
571*572c4311Sfengbojiang
ll_seeall(lua_State * L)572*572c4311Sfengbojiang static int ll_seeall (lua_State *L) {
573*572c4311Sfengbojiang luaL_checktype(L, 1, LUA_TTABLE);
574*572c4311Sfengbojiang if (!lua_getmetatable(L, 1)) {
575*572c4311Sfengbojiang lua_createtable(L, 0, 1); /* create new metatable */
576*572c4311Sfengbojiang lua_pushvalue(L, -1);
577*572c4311Sfengbojiang lua_setmetatable(L, 1);
578*572c4311Sfengbojiang }
579*572c4311Sfengbojiang lua_pushvalue(L, LUA_GLOBALSINDEX);
580*572c4311Sfengbojiang lua_setfield(L, -2, "__index"); /* mt.__index = _G */
581*572c4311Sfengbojiang return 0;
582*572c4311Sfengbojiang }
583*572c4311Sfengbojiang
584*572c4311Sfengbojiang
585*572c4311Sfengbojiang /* }====================================================== */
586*572c4311Sfengbojiang
587*572c4311Sfengbojiang
588*572c4311Sfengbojiang
589*572c4311Sfengbojiang /* auxiliary mark (for internal use) */
590*572c4311Sfengbojiang #define AUXMARK "\1"
591*572c4311Sfengbojiang
setpath(lua_State * L,const char * fieldname,const char * envname,const char * def)592*572c4311Sfengbojiang static void setpath (lua_State *L, const char *fieldname, const char *envname,
593*572c4311Sfengbojiang const char *def) {
594*572c4311Sfengbojiang const char *path = getenv(envname);
595*572c4311Sfengbojiang if (path == NULL) /* no environment variable? */
596*572c4311Sfengbojiang lua_pushstring(L, def); /* use default */
597*572c4311Sfengbojiang else {
598*572c4311Sfengbojiang /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
599*572c4311Sfengbojiang path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
600*572c4311Sfengbojiang LUA_PATHSEP AUXMARK LUA_PATHSEP);
601*572c4311Sfengbojiang luaL_gsub(L, path, AUXMARK, def);
602*572c4311Sfengbojiang lua_remove(L, -2);
603*572c4311Sfengbojiang }
604*572c4311Sfengbojiang setprogdir(L);
605*572c4311Sfengbojiang lua_setfield(L, -2, fieldname);
606*572c4311Sfengbojiang }
607*572c4311Sfengbojiang
608*572c4311Sfengbojiang
609*572c4311Sfengbojiang static const luaL_Reg pk_funcs[] = {
610*572c4311Sfengbojiang {"loadlib", ll_loadlib},
611*572c4311Sfengbojiang {"seeall", ll_seeall},
612*572c4311Sfengbojiang {NULL, NULL}
613*572c4311Sfengbojiang };
614*572c4311Sfengbojiang
615*572c4311Sfengbojiang
616*572c4311Sfengbojiang static const luaL_Reg ll_funcs[] = {
617*572c4311Sfengbojiang {"module", ll_module},
618*572c4311Sfengbojiang {"require", ll_require},
619*572c4311Sfengbojiang {NULL, NULL}
620*572c4311Sfengbojiang };
621*572c4311Sfengbojiang
622*572c4311Sfengbojiang
623*572c4311Sfengbojiang static const lua_CFunction loaders[] =
624*572c4311Sfengbojiang {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
625*572c4311Sfengbojiang
626*572c4311Sfengbojiang
luaopen_package(lua_State * L)627*572c4311Sfengbojiang LUALIB_API int luaopen_package (lua_State *L) {
628*572c4311Sfengbojiang int i;
629*572c4311Sfengbojiang /* create new type _LOADLIB */
630*572c4311Sfengbojiang luaL_newmetatable(L, "_LOADLIB");
631*572c4311Sfengbojiang lua_pushcfunction(L, gctm);
632*572c4311Sfengbojiang lua_setfield(L, -2, "__gc");
633*572c4311Sfengbojiang /* create `package' table */
634*572c4311Sfengbojiang luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
635*572c4311Sfengbojiang #if defined(LUA_COMPAT_LOADLIB)
636*572c4311Sfengbojiang lua_getfield(L, -1, "loadlib");
637*572c4311Sfengbojiang lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
638*572c4311Sfengbojiang #endif
639*572c4311Sfengbojiang lua_pushvalue(L, -1);
640*572c4311Sfengbojiang lua_replace(L, LUA_ENVIRONINDEX);
641*572c4311Sfengbojiang /* create `loaders' table */
642*572c4311Sfengbojiang lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
643*572c4311Sfengbojiang /* fill it with pre-defined loaders */
644*572c4311Sfengbojiang for (i=0; loaders[i] != NULL; i++) {
645*572c4311Sfengbojiang lua_pushcfunction(L, loaders[i]);
646*572c4311Sfengbojiang lua_rawseti(L, -2, i+1);
647*572c4311Sfengbojiang }
648*572c4311Sfengbojiang lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
649*572c4311Sfengbojiang setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
650*572c4311Sfengbojiang setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
651*572c4311Sfengbojiang /* store config information */
652*572c4311Sfengbojiang lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
653*572c4311Sfengbojiang LUA_EXECDIR "\n" LUA_IGMARK);
654*572c4311Sfengbojiang lua_setfield(L, -2, "config");
655*572c4311Sfengbojiang /* set field `loaded' */
656*572c4311Sfengbojiang luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
657*572c4311Sfengbojiang lua_setfield(L, -2, "loaded");
658*572c4311Sfengbojiang /* set field `preload' */
659*572c4311Sfengbojiang lua_newtable(L);
660*572c4311Sfengbojiang lua_setfield(L, -2, "preload");
661*572c4311Sfengbojiang lua_pushvalue(L, LUA_GLOBALSINDEX);
662*572c4311Sfengbojiang luaL_register(L, NULL, ll_funcs); /* open lib into global table */
663*572c4311Sfengbojiang lua_pop(L, 1);
664*572c4311Sfengbojiang return 1; /* return 'package' table */
665*572c4311Sfengbojiang }
666*572c4311Sfengbojiang
667