1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "lib/pluginlib_exit.h"
28 #include "rockmalloc.h"
32 static const luaL_Reg lualibs
[] = {
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 {LUA_MATHLIBNAME
, luaopen_math
},
45 static void rocklua_openlibs(lua_State
*L
) {
46 const luaL_Reg
*lib
= lualibs
;
47 for (; lib
->func
; lib
++) {
48 lua_pushcfunction(L
, lib
->func
);
49 lua_pushstring(L
, lib
->name
);
55 static lua_State
*getthread (lua_State
*L
, int *arg
) {
56 if (lua_isthread(L
, 1)) {
58 return lua_tothread(L
, 1);
66 #define LEVELS1 12 /* size of the first part of the stack */
67 #define LEVELS2 10 /* size of the second part of the stack */
69 static int db_errorfb (lua_State
*L
) {
71 int firstpart
= 1; /* still before eventual `...' */
73 lua_State
*L1
= getthread(L
, &arg
);
75 if (lua_isnumber(L
, arg
+2)) {
76 level
= (int)lua_tointeger(L
, arg
+2);
80 level
= (L
== L1
) ? 1 : 0; /* level 0 may be this own function */
81 if (lua_gettop(L
) == arg
)
82 lua_pushliteral(L
, "");
83 else if (!lua_isstring(L
, arg
+1)) return 1; /* message is not a string */
84 else lua_pushliteral(L
, "\n");
85 lua_pushliteral(L
, "stack traceback:");
86 while (lua_getstack(L1
, level
++, &ar
)) {
87 if (level
> LEVELS1
&& firstpart
) {
88 /* no more than `LEVELS2' more levels? */
89 if (!lua_getstack(L1
, level
+LEVELS2
, &ar
))
90 level
--; /* keep going */
92 lua_pushliteral(L
, "\n\t..."); /* too many levels */
93 while (lua_getstack(L1
, level
+LEVELS2
, &ar
)) /* find last levels */
99 lua_pushliteral(L
, "\n\t");
100 lua_getinfo(L1
, "Snl", &ar
);
101 lua_pushfstring(L
, "%s:", ar
.short_src
);
102 if (ar
.currentline
> 0)
103 lua_pushfstring(L
, "%d:", ar
.currentline
);
104 if (*ar
.namewhat
!= '\0') /* is there a name? */
105 lua_pushfstring(L
, " in function " LUA_QS
, ar
.name
);
107 if (*ar
.what
== 'm') /* main? */
108 lua_pushfstring(L
, " in main chunk");
109 else if (*ar
.what
== 'C' || *ar
.what
== 't')
110 lua_pushliteral(L
, " ?"); /* C function or tail call */
112 lua_pushfstring(L
, " in function <%s:%d>",
113 ar
.short_src
, ar
.linedefined
);
115 lua_concat(L
, lua_gettop(L
) - arg
);
117 lua_concat(L
, lua_gettop(L
) - arg
);
122 static int traceback (lua_State
*L
) {
123 lua_pushcfunction(L
, db_errorfb
);
124 lua_pushvalue(L
, 1); /* pass error message */
125 lua_pushinteger(L
, 2); /* skip this function and traceback */
126 lua_call(L
, 2, 1); /* call debug.traceback */
130 static int docall (lua_State
*L
) {
132 int base
= lua_gettop(L
); /* function index */
133 lua_pushcfunction(L
, traceback
); /* push traceback function */
134 lua_insert(L
, base
); /* put it under chunk and args */
135 status
= lua_pcall(L
, 0, 0, base
);
136 lua_remove(L
, base
); /* remove traceback function */
137 /* force a complete garbage collection in case of errors */
138 if (status
!= 0) lua_gc(L
, LUA_GCCOLLECT
, 0);
143 /***************** Plugin Entry Point *****************/
144 enum plugin_status
plugin_start(const void* parameter
)
146 const char* filename
;
151 if (parameter
== NULL
)
153 rb
->splash(HZ
, "Play a .lua file!");
158 filename
= (char*) parameter
;
160 lua_State
*L
= luaL_newstate();
163 status
= luaL_loadfile(L
, filename
);
165 rb
->lcd_clear_display();
172 DEBUGF("%s\n", lua_tostring(L
, -1));
173 rb
->splashf(5 * HZ
, "%s", lua_tostring(L
, -1));