summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lua/rocklua.c
blob: 650c4d6daeca45ad5dc6dd34ee0f23b9d84ea574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 Dan Everton (safetydan)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "rocklib.h"
#include "rocklib_img.h"
#include "luadir.h"
#include "rocklib_events.h"

static lua_State *Ls = NULL;
static int lu_status = 0;

static const luaL_Reg lualibs[] = {
  {"",              luaopen_base},
  {LUA_LOADLIBNAME, luaopen_package},
  {LUA_TABLIBNAME,  luaopen_table},
  {LUA_STRLIBNAME,  luaopen_string},
  {LUA_BITLIBNAME,  luaopen_bit},
  {LUA_IOLIBNAME,   luaopen_io},
  {LUA_MATHLIBNAME, luaopen_math},
  {LUA_OSLIBNAME,   luaopen_os},
  {LUA_ROCKLIBNAME, luaopen_rock},
  {LUA_ROCKLIBNAME, luaopen_rock_img},
  {LUA_ROCKEVENTSNAME, luaopen_rockevents},
  {LUA_DIRLIBNAME,  luaopen_luadir},
  {NULL, NULL}
};

static void rocklua_openlibs(lua_State *L) {
  const luaL_Reg *lib = lualibs;
  for (; lib->func; lib++) {
    lua_pushcfunction(L, lib->func);
    lua_pushstring(L, lib->name);
    lua_call(L, 1, 0);
  }
}

/* ldlib.c */
static lua_State *getthread (lua_State *L, int *arg) {
  if (lua_isthread(L, 1)) {
    *arg = 1;
    return lua_tothread(L, 1);
  }
  else {
    *arg = 0;
    return L;
  }
}

#define LEVELS1 12      /* size of the first part of the stack */
#define LEVELS2 10      /* size of the second part of the stack */

static int db_errorfb (lua_State *L) {
  int level;
  int firstpart = 1;  /* still before eventual `...' */
  int arg;
  lua_State *L1 = getthread(L, &arg);
  lua_Debug ar;
  if (lua_isnumber(L, arg+2)) {
    level = (int)lua_tointeger(L, arg+2);
    lua_pop(L, 1);
  }
  else
    level = (L == L1) ? 1 : 0;  /* level 0 may be this own function */
  if (lua_gettop(L) == arg)
    lua_pushliteral(L, "");
  else if (!lua_isstring(L, arg+1)) return 1;  /* message is not a string */
  else lua_pushliteral(L, "\n\n");
  lua_pushliteral(L, "stack traceback: ");
  while (lua_getstack(L1, level++, &ar)) {
    if (level > LEVELS1 && firstpart) {
      /* no more than `LEVELS2' more levels? */
      if (!lua_getstack(L1, level+LEVELS2, &ar))
        level--;  /* keep going */
      else {
        lua_pushliteral(L, "\n\t...");  /* too many levels */
        while (lua_getstack(L1, level+LEVELS2, &ar))  /* find last levels */
          level++;
      }
      firstpart = 0;
      continue;
    }
    lua_pushliteral(L, "\n\n\t");
    lua_getinfo(L1, "Snl", &ar);
    char* filename = rb->strrchr(ar.short_src, '/'); /* remove path */
    lua_pushfstring(L, "%s:", filename ? filename : ar.short_src);
    if (ar.currentline > 0)
      lua_pushfstring(L, "%d:", ar.currentline);
    if (*ar.namewhat != '\0')  /* is there a name? */
        lua_pushfstring(L, " in function " LUA_QS, ar.name);
    else {
      if (*ar.what == 'm')  /* main? */
        lua_pushfstring(L, " in main chunk");
      else if (*ar.what == 'C' || *ar.what == 't')
        lua_pushliteral(L, " ?");  /* C function or tail call */
      else
        lua_pushfstring(L, " in function <%s:%d>",
                           ar.short_src, ar.linedefined);
    }

    lua_concat(L, lua_gettop(L) - arg);
  }
  lua_pushfstring(L, "\n\nRam Used: %d Kb", lua_gc (L, LUA_GCCOUNT, 0));
  lua_concat(L, lua_gettop(L) - arg);
  return 1;
}

/* lua.c */
static int traceback (lua_State *L) {
  lua_pushcfunction(L, db_errorfb);
  lua_pushvalue(L, 1);  /* pass error message */
  lua_pushinteger(L, 2);  /* skip this function and traceback */
  lua_call(L, 2, 1);  /* call debug.traceback */
  return 1;
}

static int docall (lua_State *L) {
  int status;
  int base = lua_gettop(L);  /* function index */
  lua_pushcfunction(L, traceback);  /* push traceback function */
  lua_insert(L, base);  /* put it under chunk and args */
  status = lua_pcall(L, 0, 0, base);
  lua_remove(L, base);  /* remove traceback function */
  /* force a complete garbage collection in case of errors */
  if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
  return status;
}

static void lua_atexit(void);
static int lua_split_arguments(lua_State *L, const char *filename);

static int loadfile_newstate(lua_State **L, const char *filename)
{
  const char *file;
  int ret;

  *L = luaL_newstate();
  rb_atexit(lua_atexit);

  lua_gc(*L, LUA_GCSTOP, 0);  /* stop collector during initialization */
  rocklua_openlibs(*L);

  lua_split_arguments(*L, filename);
  lua_setglobal (*L, "_arguments");
  file = lua_tostring (*L, -1);
  lua_setglobal (*L, "_fullpath");
  /* lua manual -> no guarantee pointer valid after value is removed from stack */
  ret = luaL_loadfile(*L, file);
  lua_gc(*L, LUA_GCRESTART, 0);

  return ret;
}

static void lua_atexit(void)
{
  char *filename;
  int err_n;
  if(Ls && lua_gettop(Ls) > 1)
  {
    err_n = lua_tointeger(Ls, -1); /* os.exit? */
    if (Ls == lua_touserdata(Ls, -1)) /* signal from restart_lua */
    {
      filename = (char *) malloc((MAX_PATH * 2) + 1);

      if (filename) {/* out of memory? */
        filename[MAX_PATH * 2] = '\0';
        rb->strlcpy(filename, lua_tostring(Ls, -2), MAX_PATH * 2);
      } else {
        goto ERR_RUN;
      }
      lua_close(Ls); /* close old state */

      lu_status = loadfile_newstate(&Ls, filename);

      free(filename);
      plugin_start(NULL);
    }
    else if (err_n >= PLUGIN_USB_CONNECTED) /* INTERNAL PLUGIN RETVAL */
    {
      lua_close(Ls);
      _exit(err_n);  /* don't call exit handler */
    }
    else if (err_n != 0)
    {
ERR_RUN:
      lu_status = LUA_ERRRUN;
      lua_pop(Ls, 1); /* put exit string on top of stack */
      plugin_start(NULL);
    }
    else
      lua_close(Ls);
  }
  _exit(PLUGIN_OK); /* don't call exit handler */
}

/* split filename at argchar
 * remainder of filename pushed on stack (-1)
* argument string pushed on stack or nil if doesn't exist (-2)
 */
static int lua_split_arguments(lua_State *L, const char *filename)
{
  const char argchar = '?';
  const char* arguments = strchr(filename, argchar);
  if(arguments) {
    lua_pushstring(L, (arguments + 1));
  } else {
    arguments = strlen(filename) + filename;
    lua_pushnil(L);
  }
  lua_pushlstring(L, filename, arguments - filename);
  lua_insert(L, -2); /* swap filename and argument */
  return 2;
}

static void display_traceback(const char *errstr)
{
#if 1
  splash_scroller(HZ * 5, errstr); /*rockaux.c*/
#else
  rb->splash(10 * HZ, errstr);
#endif
}

int browse_scripts(void)
{
    static char buf[MAX_PATH];
    const char *fname = rb->plugin_get_current_filename();
    /* strip plugin dir to save space in the param buffer */
    if (rb->strncmp(fname, PLUGIN_DIR, sizeof(PLUGIN_DIR) - 1) == 0)
        fname += sizeof(PLUGIN_DIR) - 1; /* leave slash */
    /* -r return to this plugin, -f looking for lua files,
       -s start in lua_scripts, -d lock to that directory */
    snprintf(buf, sizeof(buf), "-r'%s'-f'.lua'-s'%s'-d",
             fname, PLUGIN_DEMOS_DIR"/lua_scripts/");
    return rb->plugin_open(VIEWERS_DIR "/file_picker.rock", buf);
}
/***************** Plugin Entry Point *****************/
enum plugin_status plugin_start(const void* parameter)
{
    const char* filename;

    if (parameter == NULL)
    {
      if (!Ls)
      {
        rb->splash(HZ, "Play a .lua file!");
        return browse_scripts();
      }
    }
    else
    {
        filename = (char*) parameter;
        lu_status = loadfile_newstate(&Ls, filename);
    }

    if (Ls)
    {
        if (!lu_status) {
            rb->lcd_scroll_stop(); /* rb doesn't like bg change while scroll */
            rb->lcd_clear_display();
            lu_status= docall(Ls);
        }

        if (lu_status) {
            DEBUGF("%s\n", lua_tostring(Ls, -1));
            display_traceback(lua_tostring(Ls, -1));
            //rb->splash(10 * HZ, lua_tostring(Ls, -1));
            /*lua_pop(Ls, 1);*/
        }
        lua_close(Ls);
    }
    else
      return PLUGIN_ERROR;

    return PLUGIN_OK;
}