FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / apps / plugins / lua / rocklua.c
blob266fa4305e3a2ecbb45eba0c38e591d9982adf00
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 Dan Everton (safetydan)
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "lib/pluginlib_exit.h"
24 #include "lua.h"
25 #include "lauxlib.h"
26 #include "lualib.h"
27 #include "rocklib.h"
28 #include "rockmalloc.h"
30 PLUGIN_HEADER
32 static const luaL_Reg lualibs[] = {
33 {"", luaopen_base},
34 {LUA_TABLIBNAME, luaopen_table},
35 {LUA_STRLIBNAME, luaopen_string},
36 {LUA_OSLIBNAME, luaopen_os},
37 {LUA_ROCKLIBNAME, luaopen_rock},
38 {LUA_BITLIBNAME, luaopen_bit},
39 {LUA_IOLIBNAME, luaopen_io},
40 {LUA_LOADLIBNAME, luaopen_package},
41 {NULL, NULL}
44 static void rocklua_openlibs(lua_State *L) {
45 const luaL_Reg *lib = lualibs;
46 for (; lib->func; lib++) {
47 lua_pushcfunction(L, lib->func);
48 lua_pushstring(L, lib->name);
49 lua_call(L, 1, 0);
53 /* ldlib.c */
54 static lua_State *getthread (lua_State *L, int *arg) {
55 if (lua_isthread(L, 1)) {
56 *arg = 1;
57 return lua_tothread(L, 1);
59 else {
60 *arg = 0;
61 return L;
65 #define LEVELS1 12 /* size of the first part of the stack */
66 #define LEVELS2 10 /* size of the second part of the stack */
68 static int db_errorfb (lua_State *L) {
69 int level;
70 int firstpart = 1; /* still before eventual `...' */
71 int arg;
72 lua_State *L1 = getthread(L, &arg);
73 lua_Debug ar;
74 if (lua_isnumber(L, arg+2)) {
75 level = (int)lua_tointeger(L, arg+2);
76 lua_pop(L, 1);
78 else
79 level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
80 if (lua_gettop(L) == arg)
81 lua_pushliteral(L, "");
82 else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
83 else lua_pushliteral(L, "\n");
84 lua_pushliteral(L, "stack traceback:");
85 while (lua_getstack(L1, level++, &ar)) {
86 if (level > LEVELS1 && firstpart) {
87 /* no more than `LEVELS2' more levels? */
88 if (!lua_getstack(L1, level+LEVELS2, &ar))
89 level--; /* keep going */
90 else {
91 lua_pushliteral(L, "\n\t..."); /* too many levels */
92 while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
93 level++;
95 firstpart = 0;
96 continue;
98 lua_pushliteral(L, "\n\t");
99 lua_getinfo(L1, "Snl", &ar);
100 lua_pushfstring(L, "%s:", ar.short_src);
101 if (ar.currentline > 0)
102 lua_pushfstring(L, "%d:", ar.currentline);
103 if (*ar.namewhat != '\0') /* is there a name? */
104 lua_pushfstring(L, " in function " LUA_QS, ar.name);
105 else {
106 if (*ar.what == 'm') /* main? */
107 lua_pushfstring(L, " in main chunk");
108 else if (*ar.what == 'C' || *ar.what == 't')
109 lua_pushliteral(L, " ?"); /* C function or tail call */
110 else
111 lua_pushfstring(L, " in function <%s:%d>",
112 ar.short_src, ar.linedefined);
114 lua_concat(L, lua_gettop(L) - arg);
116 lua_concat(L, lua_gettop(L) - arg);
117 return 1;
120 /* lua.c */
121 static int traceback (lua_State *L) {
122 lua_pushcfunction(L, db_errorfb);
123 lua_pushvalue(L, 1); /* pass error message */
124 lua_pushinteger(L, 2); /* skip this function and traceback */
125 lua_call(L, 2, 1); /* call debug.traceback */
126 return 1;
129 static int docall (lua_State *L) {
130 int status;
131 int base = lua_gettop(L); /* function index */
132 lua_pushcfunction(L, traceback); /* push traceback function */
133 lua_insert(L, base); /* put it under chunk and args */
134 status = lua_pcall(L, 0, 0, base);
135 lua_remove(L, base); /* remove traceback function */
136 /* force a complete garbage collection in case of errors */
137 if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
138 return status;
142 /***************** Plugin Entry Point *****************/
143 enum plugin_status plugin_start(const void* parameter)
145 const char* filename;
146 int status;
148 PLUGINLIB_EXIT_INIT
150 if (parameter == NULL)
152 rb->splash(HZ, "Play a .lua file!");
153 return PLUGIN_ERROR;
155 else
157 filename = (char*) parameter;
159 lua_State *L = luaL_newstate();
161 rocklua_openlibs(L);
162 status = luaL_loadfile(L, filename);
163 if (!status) {
164 rb->lcd_clear_display();
165 status = docall(L);
168 dlmalloc_stats();
170 if (status) {
171 DEBUGF("%s\n", lua_tostring(L, -1));
172 rb->splashf(5 * HZ, "%s", lua_tostring(L, -1));
173 lua_pop(L, 1);
176 lua_close(L);
179 return PLUGIN_OK;