summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-06-25 13:26:05 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-06-25 13:26:05 +0000
commit3ff84e5e4ff402b550b7fa768e010a3586dded10 (patch)
tree34a6c480c7ce02063ebb22c1c1e73d41b0fb7073
parent48f4512518c60456d02b3802d0bae41e6095ec21 (diff)
downloadrockbox-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
-rw-r--r--apps/plugins/helloworld.lua2
-rw-r--r--apps/plugins/lua/SOURCES1
-rw-r--r--apps/plugins/lua/lauxlib.c50
-rw-r--r--apps/plugins/lua/loadlib.c313
-rw-r--r--apps/plugins/lua/rockconf.h3
-rw-r--r--apps/plugins/lua/rocklib.c30
-rw-r--r--apps/plugins/lua/rocklua.c1
7 files changed, 345 insertions, 55 deletions
diff --git a/apps/plugins/helloworld.lua b/apps/plugins/helloworld.lua
index c0e75540c7..ed8726c022 100644
--- a/apps/plugins/helloworld.lua
+++ b/apps/plugins/helloworld.lua
@@ -21,7 +21,7 @@
21 21
22]]-- 22]]--
23 23
24dofile("actions.lua") -- Contains rb.actions & rb.contexts 24require("actions") -- Contains rb.actions & rb.contexts
25 25
26-- Example function which splashes a message for x seconds 26-- Example function which splashes a message for x seconds
27function sayhello(seconds) 27function sayhello(seconds)
diff --git a/apps/plugins/lua/SOURCES b/apps/plugins/lua/SOURCES
index b443ce8437..5755b6c42e 100644
--- a/apps/plugins/lua/SOURCES
+++ b/apps/plugins/lua/SOURCES
@@ -10,6 +10,7 @@ lfunc.c
10lgc.c 10lgc.c
11llex.c 11llex.c
12lmem.c 12lmem.c
13loadlib.c
13lobject.c 14lobject.c
14lopcodes.c 15lopcodes.c
15loslib.c 16loslib.c
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c
index 0c987cfa30..b8020b7475 100644
--- a/apps/plugins/lua/lauxlib.c
+++ b/apps/plugins/lua/lauxlib.c
@@ -560,62 +560,14 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
560 return LUA_ERRFILE; 560 return LUA_ERRFILE;
561} 561}
562 562
563bool get_cur_path(lua_State *L, char* dest, size_t dest_size)
564{
565 lua_Debug ar;
566 if(lua_getstack(L, 1, &ar))
567 {
568 /* Try determining the base path of the current Lua chunk
569 and write it to dest. */
570 lua_getinfo(L, "S", &ar);
571
572 char* curfile = (char*) &ar.source[1];
573 char* pos = rb->strrchr(curfile, '/');
574 if(pos != NULL)
575 {
576 unsigned int len = (unsigned int)(pos - curfile);
577 len = len + 1 > dest_size ? dest_size - 1 : len;
578
579 if(len > 0)
580 memcpy(dest, curfile, len);
581
582 dest[len] = '/';
583 dest[len+1] = '\0';
584
585 return true;
586 }
587 else
588 return false;
589 }
590 else
591 return false;
592}
593
594LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { 563LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
595 LoadF lf; 564 LoadF lf;
596 int status; 565 int status;
597 char buffer[MAX_PATH];
598 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ 566 int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
599 lf.extraline = 0; 567 lf.extraline = 0;
600 lf.f = rb->open(filename, O_RDONLY); 568 lf.f = rb->open(filename, O_RDONLY);
601 lua_pushfstring(L, "@%s", filename); 569 lua_pushfstring(L, "@%s", filename);
602 if(lf.f < 0) { 570 if (lf.f < 0) return errfile(L, "open", fnameindex);
603 /* Fallback */
604
605 if(get_cur_path(L, buffer, sizeof(buffer))) {
606 strncat(buffer, filename, sizeof(buffer) - strlen(buffer));
607 lf.f = rb->open(buffer, O_RDONLY);
608 }
609
610 if(lf.f < 0) {
611 snprintf(buffer, sizeof(buffer), "%s/%s", VIEWERS_DIR, filename);
612 lf.f = rb->open(buffer, O_RDONLY);
613
614 if(lf.f < 0)
615 return errfile(L, "open", fnameindex);
616 }
617 }
618
619 status = lua_load(L, getF, &lf, lua_tostring(L, -1)); 571 status = lua_load(L, getF, &lf, lua_tostring(L, -1));
620 rb->close(lf.f); 572 rb->close(lf.f);
621 lua_remove(L, fnameindex); 573 lua_remove(L, fnameindex);
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
35static 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
43static 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
54static 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
77static 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
83static 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
94static 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
106static const int sentinel_ = 0;
107#define sentinel ((void *)&sentinel_)
108
109
110static 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
166static 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
178static 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
188static 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
203static 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
231static 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
251static 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
260static const luaL_Reg pk_funcs[] = {
261 {"seeall", ll_seeall},
262 {NULL, NULL}
263};
264
265
266static const luaL_Reg ll_funcs[] = {
267 {"module", ll_module},
268 {"require", ll_require},
269 {NULL, NULL}
270};
271
272
273static const lua_CFunction loaders[] =
274 {loader_preload, loader_Lua, NULL};
275
276
277LUALIB_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
diff --git a/apps/plugins/lua/rockconf.h b/apps/plugins/lua/rockconf.h
index 1b267c78e6..40f7d74554 100644
--- a/apps/plugins/lua/rockconf.h
+++ b/apps/plugins/lua/rockconf.h
@@ -28,6 +28,9 @@
28#undef LUAI_TRY 28#undef LUAI_TRY
29#undef luai_jmpbuf 29#undef luai_jmpbuf
30 30
31#undef LUA_PATH_DEFAULT
32#define LUA_PATH_DEFAULT "./?.lua;" VIEWERS_DIR"/?.lua;"
33
31#ifndef SIMULATOR 34#ifndef SIMULATOR
32#include "../../codecs/lib/setjmp.h" 35#include "../../codecs/lib/setjmp.h"
33#else 36#else
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index a3a42af9f6..f22bd01b48 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -866,13 +866,33 @@ RB_WRAP(read_bmp_file)
866RB_WRAP(current_path) 866RB_WRAP(current_path)
867{ 867{
868 char buffer[MAX_PATH]; 868 char buffer[MAX_PATH];
869 if(get_cur_path(L, buffer, sizeof(buffer))) 869 lua_Debug ar;
870
871 if(lua_getstack(L, 1, &ar))
870 { 872 {
871 lua_pushstring(L, buffer); 873 /* Try determining the base path of the current Lua chunk
872 return 1; 874 and write it to dest. */
875 lua_getinfo(L, "S", &ar);
876
877 char* curfile = (char*) &ar.source[1];
878 char* pos = rb->strrchr(curfile, '/');
879 if(pos != NULL)
880 {
881 unsigned int len = (unsigned int)(pos - curfile);
882 len = len + 1 > sizeof(buffer) ? sizeof(buffer) - 1 : len;
883
884 if(len > 0)
885 memcpy(buffer, curfile, len);
886
887 buffer[len] = '/';
888 buffer[len+1] = '\0';
889
890 lua_pushstring(L, buffer);
891 return 1;
892 }
873 } 893 }
874 else 894
875 return 0; 895 return 0;
876} 896}
877 897
878#define R(NAME) {#NAME, rock_##NAME} 898#define R(NAME) {#NAME, rock_##NAME}
diff --git a/apps/plugins/lua/rocklua.c b/apps/plugins/lua/rocklua.c
index 98ffea8123..cc99f9236e 100644
--- a/apps/plugins/lua/rocklua.c
+++ b/apps/plugins/lua/rocklua.c
@@ -36,6 +36,7 @@ static const luaL_Reg lualibs[] = {
36 {LUA_OSLIBNAME, luaopen_os}, 36 {LUA_OSLIBNAME, luaopen_os},
37 {LUA_ROCKLIBNAME, luaopen_rock}, 37 {LUA_ROCKLIBNAME, luaopen_rock},
38 {LUA_BITLIBNAME, luaopen_bit}, 38 {LUA_BITLIBNAME, luaopen_bit},
39 {LUA_LOADLIBNAME, luaopen_package},
39 {NULL, NULL} 40 {NULL, NULL}
40}; 41};
41 42