diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-06-25 13:26:05 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2009-06-25 13:26:05 +0000 |
commit | 3ff84e5e4ff402b550b7fa768e010a3586dded10 (patch) | |
tree | 34a6c480c7ce02063ebb22c1c1e73d41b0fb7073 /apps/plugins/lua/loadlib.c | |
parent | 48f4512518c60456d02b3802d0bae41e6095ec21 (diff) | |
download | rockbox-3ff84e5e4ff402b550b7fa768e010a3586dded10.tar.gz rockbox-3ff84e5e4ff402b550b7fa768e010a3586dded10.zip |
Lua: add the package library
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21506 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/lua/loadlib.c')
-rw-r--r-- | apps/plugins/lua/loadlib.c | 313 |
1 files changed, 313 insertions, 0 deletions
diff --git a/apps/plugins/lua/loadlib.c b/apps/plugins/lua/loadlib.c new file mode 100644 index 0000000000..035116dc71 --- /dev/null +++ b/apps/plugins/lua/loadlib.c | |||
@@ -0,0 +1,313 @@ | |||
1 | /* | ||
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 | ||
5 | ** | ||
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. | ||
9 | */ | ||
10 | |||
11 | |||
12 | #include <stdlib.h> | ||
13 | #include <string.h> | ||
14 | |||
15 | |||
16 | #define loadlib_c | ||
17 | #define LUA_LIB | ||
18 | |||
19 | #include "lua.h" | ||
20 | |||
21 | #include "lauxlib.h" | ||
22 | #include "lualib.h" | ||
23 | |||
24 | |||
25 | #define setprogdir(L) ((void)0) | ||
26 | |||
27 | |||
28 | /* | ||
29 | ** {====================================================== | ||
30 | ** 'require' function | ||
31 | ** ======================================================= | ||
32 | */ | ||
33 | |||
34 | |||
35 | static int readable (const char *filename) { | ||
36 | int f = rb->open(filename, O_RDONLY); /* try to open file */ | ||
37 | if (f < 0) return 0; /* open failed */ | ||
38 | rb->close(f); | ||
39 | return 1; | ||
40 | } | ||
41 | |||
42 | |||
43 | static const char *pushnexttemplate (lua_State *L, const char *path) { | ||
44 | const char *l; | ||
45 | while (*path == *LUA_PATHSEP) path++; /* skip separators */ | ||
46 | if (*path == '\0') return NULL; /* no more templates */ | ||
47 | l = strchr(path, *LUA_PATHSEP); /* find next separator */ | ||
48 | if (l == NULL) l = path + strlen(path); | ||
49 | lua_pushlstring(L, path, l - path); /* template */ | ||
50 | return l; | ||
51 | } | ||
52 | |||
53 | |||
54 | static const char *findfile (lua_State *L, const char *name, | ||
55 | const char *pname) { | ||
56 | const char *path; | ||
57 | name = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
58 | lua_getfield(L, LUA_ENVIRONINDEX, pname); | ||
59 | path = lua_tostring(L, -1); | ||
60 | if (path == NULL) | ||
61 | luaL_error(L, LUA_QL("package.%s") " must be a string", pname); | ||
62 | lua_pushliteral(L, ""); /* error accumulator */ | ||
63 | while ((path = pushnexttemplate(L, path)) != NULL) { | ||
64 | const char *filename; | ||
65 | filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); | ||
66 | lua_remove(L, -2); /* remove path template */ | ||
67 | if (readable(filename)) /* does file exist and is readable? */ | ||
68 | return filename; /* return that file name */ | ||
69 | lua_pushfstring(L, "\n\tno file " LUA_QS, filename); | ||
70 | lua_remove(L, -2); /* remove file name */ | ||
71 | lua_concat(L, 2); /* add entry to possible error message */ | ||
72 | } | ||
73 | return NULL; /* not found */ | ||
74 | } | ||
75 | |||
76 | |||
77 | static void loaderror (lua_State *L, const char *filename) { | ||
78 | luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", | ||
79 | lua_tostring(L, 1), filename, lua_tostring(L, -1)); | ||
80 | } | ||
81 | |||
82 | |||
83 | static int loader_Lua (lua_State *L) { | ||
84 | const char *filename; | ||
85 | const char *name = luaL_checkstring(L, 1); | ||
86 | filename = findfile(L, name, "path"); | ||
87 | if (filename == NULL) return 1; /* library not found in this path */ | ||
88 | if (luaL_loadfile(L, filename) != 0) | ||
89 | loaderror(L, filename); | ||
90 | return 1; /* library loaded successfully */ | ||
91 | } | ||
92 | |||
93 | |||
94 | static int loader_preload (lua_State *L) { | ||
95 | const char *name = luaL_checkstring(L, 1); | ||
96 | lua_getfield(L, LUA_ENVIRONINDEX, "preload"); | ||
97 | if (!lua_istable(L, -1)) | ||
98 | luaL_error(L, LUA_QL("package.preload") " must be a table"); | ||
99 | lua_getfield(L, -1, name); | ||
100 | if (lua_isnil(L, -1)) /* not found? */ | ||
101 | lua_pushfstring(L, "\n\tno field package.preload['%s']", name); | ||
102 | return 1; | ||
103 | } | ||
104 | |||
105 | |||
106 | static const int sentinel_ = 0; | ||
107 | #define sentinel ((void *)&sentinel_) | ||
108 | |||
109 | |||
110 | static int ll_require (lua_State *L) { | ||
111 | const char *name = luaL_checkstring(L, 1); | ||
112 | int i; | ||
113 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ | ||
114 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
115 | lua_getfield(L, 2, name); | ||
116 | if (lua_toboolean(L, -1)) { /* is it there? */ | ||
117 | if (lua_touserdata(L, -1) == sentinel) /* check loops */ | ||
118 | luaL_error(L, "loop or previous error loading module " LUA_QS, name); | ||
119 | return 1; /* package is already loaded */ | ||
120 | } | ||
121 | /* else must load it; iterate over available loaders */ | ||
122 | lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); | ||
123 | if (!lua_istable(L, -1)) | ||
124 | luaL_error(L, LUA_QL("package.loaders") " must be a table"); | ||
125 | lua_pushliteral(L, ""); /* error message accumulator */ | ||
126 | for (i=1; ; i++) { | ||
127 | lua_rawgeti(L, -2, i); /* get a loader */ | ||
128 | if (lua_isnil(L, -1)) | ||
129 | luaL_error(L, "module " LUA_QS " not found:%s", | ||
130 | name, lua_tostring(L, -2)); | ||
131 | lua_pushstring(L, name); | ||
132 | lua_call(L, 1, 1); /* call it */ | ||
133 | if (lua_isfunction(L, -1)) /* did it find module? */ | ||
134 | break; /* module loaded successfully */ | ||
135 | else if (lua_isstring(L, -1)) /* loader returned error message? */ | ||
136 | lua_concat(L, 2); /* accumulate it */ | ||
137 | else | ||
138 | lua_pop(L, 1); | ||
139 | } | ||
140 | lua_pushlightuserdata(L, sentinel); | ||
141 | lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ | ||
142 | lua_pushstring(L, name); /* pass name as argument to module */ | ||
143 | lua_call(L, 1, 1); /* run loaded module */ | ||
144 | if (!lua_isnil(L, -1)) /* non-nil return? */ | ||
145 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ | ||
146 | lua_getfield(L, 2, name); | ||
147 | if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ | ||
148 | lua_pushboolean(L, 1); /* use true as result */ | ||
149 | lua_pushvalue(L, -1); /* extra copy to be returned */ | ||
150 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ | ||
151 | } | ||
152 | return 1; | ||
153 | } | ||
154 | |||
155 | /* }====================================================== */ | ||
156 | |||
157 | |||
158 | |||
159 | /* | ||
160 | ** {====================================================== | ||
161 | ** 'module' function | ||
162 | ** ======================================================= | ||
163 | */ | ||
164 | |||
165 | |||
166 | static void setfenv (lua_State *L) { | ||
167 | lua_Debug ar; | ||
168 | if (lua_getstack(L, 1, &ar) == 0 || | ||
169 | lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ | ||
170 | lua_iscfunction(L, -1)) | ||
171 | luaL_error(L, LUA_QL("module") " not called from a Lua function"); | ||
172 | lua_pushvalue(L, -2); | ||
173 | lua_setfenv(L, -2); | ||
174 | lua_pop(L, 1); | ||
175 | } | ||
176 | |||
177 | |||
178 | static void dooptions (lua_State *L, int n) { | ||
179 | int i; | ||
180 | for (i = 2; i <= n; i++) { | ||
181 | lua_pushvalue(L, i); /* get option (a function) */ | ||
182 | lua_pushvalue(L, -2); /* module */ | ||
183 | lua_call(L, 1, 0); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | |||
188 | static void modinit (lua_State *L, const char *modname) { | ||
189 | const char *dot; | ||
190 | lua_pushvalue(L, -1); | ||
191 | lua_setfield(L, -2, "_M"); /* module._M = module */ | ||
192 | lua_pushstring(L, modname); | ||
193 | lua_setfield(L, -2, "_NAME"); | ||
194 | dot = rb->strrchr(modname, '.'); /* look for last dot in module name */ | ||
195 | if (dot == NULL) dot = modname; | ||
196 | else dot++; | ||
197 | /* set _PACKAGE as package name (full module name minus last part) */ | ||
198 | lua_pushlstring(L, modname, dot - modname); | ||
199 | lua_setfield(L, -2, "_PACKAGE"); | ||
200 | } | ||
201 | |||
202 | |||
203 | static int ll_module (lua_State *L) { | ||
204 | const char *modname = luaL_checkstring(L, 1); | ||
205 | int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ | ||
206 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
207 | lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ | ||
208 | if (!lua_istable(L, -1)) { /* not found? */ | ||
209 | lua_pop(L, 1); /* remove previous result */ | ||
210 | /* try global variable (and create one if it does not exist) */ | ||
211 | if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) | ||
212 | return luaL_error(L, "name conflict for module " LUA_QS, modname); | ||
213 | lua_pushvalue(L, -1); | ||
214 | lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ | ||
215 | } | ||
216 | /* check whether table already has a _NAME field */ | ||
217 | lua_getfield(L, -1, "_NAME"); | ||
218 | if (!lua_isnil(L, -1)) /* is table an initialized module? */ | ||
219 | lua_pop(L, 1); | ||
220 | else { /* no; initialize it */ | ||
221 | lua_pop(L, 1); | ||
222 | modinit(L, modname); | ||
223 | } | ||
224 | lua_pushvalue(L, -1); | ||
225 | setfenv(L); | ||
226 | dooptions(L, loaded - 1); | ||
227 | return 0; | ||
228 | } | ||
229 | |||
230 | |||
231 | static int ll_seeall (lua_State *L) { | ||
232 | luaL_checktype(L, 1, LUA_TTABLE); | ||
233 | if (!lua_getmetatable(L, 1)) { | ||
234 | lua_createtable(L, 0, 1); /* create new metatable */ | ||
235 | lua_pushvalue(L, -1); | ||
236 | lua_setmetatable(L, 1); | ||
237 | } | ||
238 | lua_pushvalue(L, LUA_GLOBALSINDEX); | ||
239 | lua_setfield(L, -2, "__index"); /* mt.__index = _G */ | ||
240 | return 0; | ||
241 | } | ||
242 | |||
243 | |||
244 | /* }====================================================== */ | ||
245 | |||
246 | |||
247 | |||
248 | /* auxiliary mark (for internal use) */ | ||
249 | #define AUXMARK "\1" | ||
250 | |||
251 | static void setpath (lua_State *L, const char *fieldname, const char *envname, | ||
252 | const char *def) { | ||
253 | (void)envname; | ||
254 | lua_pushstring(L, def); /* use default */ | ||
255 | setprogdir(L); | ||
256 | lua_setfield(L, -2, fieldname); | ||
257 | } | ||
258 | |||
259 | |||
260 | static const luaL_Reg pk_funcs[] = { | ||
261 | {"seeall", ll_seeall}, | ||
262 | {NULL, NULL} | ||
263 | }; | ||
264 | |||
265 | |||
266 | static const luaL_Reg ll_funcs[] = { | ||
267 | {"module", ll_module}, | ||
268 | {"require", ll_require}, | ||
269 | {NULL, NULL} | ||
270 | }; | ||
271 | |||
272 | |||
273 | static const lua_CFunction loaders[] = | ||
274 | {loader_preload, loader_Lua, NULL}; | ||
275 | |||
276 | |||
277 | LUALIB_API int luaopen_package (lua_State *L) { | ||
278 | int i; | ||
279 | /* create new type _LOADLIB */ | ||
280 | luaL_newmetatable(L, "_LOADLIB"); | ||
281 | /* create `package' table */ | ||
282 | luaL_register(L, LUA_LOADLIBNAME, pk_funcs); | ||
283 | #if defined(LUA_COMPAT_LOADLIB) | ||
284 | lua_getfield(L, -1, "loadlib"); | ||
285 | lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); | ||
286 | #endif | ||
287 | lua_pushvalue(L, -1); | ||
288 | lua_replace(L, LUA_ENVIRONINDEX); | ||
289 | /* create `loaders' table */ | ||
290 | lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1); | ||
291 | /* fill it with pre-defined loaders */ | ||
292 | for (i=0; loaders[i] != NULL; i++) { | ||
293 | lua_pushcfunction(L, loaders[i]); | ||
294 | lua_rawseti(L, -2, i+1); | ||
295 | } | ||
296 | lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ | ||
297 | setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ | ||
298 | /* store config information */ | ||
299 | lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" | ||
300 | LUA_EXECDIR "\n" LUA_IGMARK); | ||
301 | lua_setfield(L, -2, "config"); | ||
302 | /* set field `loaded' */ | ||
303 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); | ||
304 | lua_setfield(L, -2, "loaded"); | ||
305 | /* set field `preload' */ | ||
306 | lua_newtable(L); | ||
307 | lua_setfield(L, -2, "preload"); | ||
308 | lua_pushvalue(L, LUA_GLOBALSINDEX); | ||
309 | luaL_register(L, NULL, ll_funcs); /* open lib into global table */ | ||
310 | lua_pop(L, 1); | ||
311 | return 1; /* return 'package' table */ | ||
312 | } | ||
313 | |||