ARM DSP: fore some reason I neglected dsp_downsample completely. Do a small reordering.
[kugel-rb.git] / apps / plugins / lua / rocklua.c
blob395cde8d9d667e97c35cfb0da4148e5725f6754d
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 {LUA_MATHLIBNAME, luaopen_math},
42 {NULL, NULL}
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);
50 lua_call(L, 1, 0);
54 /* ldlib.c */
55 static lua_State *getthread (lua_State *L, int *arg) {
56 if (lua_isthread(L, 1)) {
57 *arg = 1;
58 return lua_tothread(L, 1);
60 else {
61 *arg = 0;
62 return L;
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) {
70 int level;
71 int firstpart = 1; /* still before eventual `...' */
72 int arg;
73 lua_State *L1 = getthread(L, &arg);
74 lua_Debug ar;
75 if (lua_isnumber(L, arg+2)) {
76 level = (int)lua_tointeger(L, arg+2);
77 lua_pop(L, 1);
79 else
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 */
91 else {
92 lua_pushliteral(L, "\n\t..."); /* too many levels */
93 while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
94 level++;
96 firstpart = 0;
97 continue;
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);
106 else {
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 */
111 else
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);
118 return 1;
121 /* lua.c */
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 */
127 return 1;
130 static int docall (lua_State *L) {
131 int status;
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);
139 return status;
143 /***************** Plugin Entry Point *****************/
144 enum plugin_status plugin_start(const void* parameter)
146 const char* filename;
147 int status;
149 PLUGINLIB_EXIT_INIT
151 if (parameter == NULL)
153 rb->splash(HZ, "Play a .lua file!");
154 return PLUGIN_ERROR;
156 else
158 filename = (char*) parameter;
160 lua_State *L = luaL_newstate();
162 rocklua_openlibs(L);
163 status = luaL_loadfile(L, filename);
164 if (!status) {
165 rb->lcd_clear_display();
166 status = docall(L);
169 dlmalloc_stats();
171 if (status) {
172 DEBUGF("%s\n", lua_tostring(L, -1));
173 rb->splashf(5 * HZ, "%s", lua_tostring(L, -1));
174 lua_pop(L, 1);
177 lua_close(L);
180 return PLUGIN_OK;