36378988ad
Prior to this patch the Lua plugin used version 5.1.4. This change reduces the number of modifications in the Lua source using some new defines and because the upstream source is now more flexible. Unless otherwise stated, l*.[ch] files are taken unmodified from the upstream lua-5.2.3. fscanf.c: file descriptors in rockbox are just ints, they are hidden behind a void* now so liolib requires less modifications. fscanf is updated to use void* too. getc.c: this is a new file required for getc implementation in lauxlib.c lauxlib.c: LoadF replaced FILE* with int, the rockbox file descriptor int are cast to FILE* (actually void* due to typedef). getc uses the PREFIX version. stdin is not used, as per 5.1.4. lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now rather than being commented out. lbitlib.c: use the built-in version from 5.2.3 rather than Reuben Thomas's external library. Backwards compatible and adds some new bit operations. ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have been removed from rockconf.h liolib.c: here the implementation has changed to use the LStream from the original source, and cast the FILE* pointers to int. This has reduced the number of modifications from the upstream version. llex.c: the only change from upstream is to remove the locale include. lmathlib.c: updated from the 5.2.3 version and re-applied the changes that were made vs 5.1.4 for random numbers and to remove unsupported float functions. loadlib.c: upstream version, with the 5.1.4 changes for missing functions. lobject.c: upstream version, with ctype.h added and sprintf changed to snprintf. loslib.c: upstream version with locale.h removed and 5.1.4 changes for unsupportable functions. lstrlib.c: sprintf changed to snprintf. ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp in luai_hashnum. luaconf.h: updated to 5.2.3 version, restored relevant parts from the original 5.1.4 configuration. The COMPAT defines that are no longer available are not included. lundump.c: VERSION macro conflicts with the core Rockbox equivalent. rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg equivalent. Moved checkboolean/optboolean functions to this file and out of core lua files. luaL_getn is no longer available, replaced by luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs replacements. rli_init has to be called before setting up the newlib to avoid overwriting the rb table. rocklib_aux.pl: use rli_checkboolean from rocklib.c. rocklua.c: new default bits library used, update the library loading code with idiomatic 5.2 code. strcspn.c: no longer needed, but strspn.c is required for strspn in lbaselib.c Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651 Reviewed-on: http://gerrit.rockbox.org/774 Tested: Richard Quirk <richard.quirk@gmail.com> Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
178 lines
5.2 KiB
C
178 lines
5.2 KiB
C
/***************************************************************************
|
|
* __________ __ ___.
|
|
* 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 "luadir.h"
|
|
|
|
#undef LUA_BITLIBNAME
|
|
#define LUA_BITLIBNAME "bit"
|
|
|
|
static const luaL_Reg lualibs[] = {
|
|
{"", luaopen_base},
|
|
{LUA_TABLIBNAME, luaopen_table},
|
|
{LUA_STRLIBNAME, luaopen_string},
|
|
{LUA_OSLIBNAME, luaopen_os},
|
|
{LUA_ROCKLIBNAME, luaopen_rock},
|
|
{LUA_BITLIBNAME, luaopen_bit32},
|
|
{LUA_IOLIBNAME, luaopen_io},
|
|
{LUA_LOADLIBNAME, luaopen_package},
|
|
{LUA_MATHLIBNAME, luaopen_math},
|
|
{LUA_DIRLIBNAME, luaopen_luadir},
|
|
{NULL, NULL}
|
|
};
|
|
|
|
static void rocklua_openlibs(lua_State *L) {
|
|
const luaL_Reg *lib;
|
|
for (lib = lualibs; lib->func; lib++) {
|
|
luaL_requiref(L, lib->name, lib->func, 1);
|
|
lua_pop(L, 1); /* remove lib */
|
|
}
|
|
}
|
|
|
|
/* 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");
|
|
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\t");
|
|
lua_getinfo(L1, "Snl", &ar);
|
|
lua_pushfstring(L, "%s:", 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_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;
|
|
}
|
|
|
|
|
|
/***************** Plugin Entry Point *****************/
|
|
enum plugin_status plugin_start(const void* parameter)
|
|
{
|
|
const char* filename;
|
|
int status;
|
|
|
|
if (parameter == NULL)
|
|
{
|
|
rb->splash(HZ, "Play a .lua file!");
|
|
return PLUGIN_ERROR;
|
|
}
|
|
else
|
|
{
|
|
filename = (char*) parameter;
|
|
|
|
lua_State *L = luaL_newstate();
|
|
|
|
rocklua_openlibs(L);
|
|
status = luaL_loadfile(L, filename);
|
|
if (!status) {
|
|
rb->lcd_clear_display();
|
|
status = docall(L);
|
|
}
|
|
|
|
if (status) {
|
|
DEBUGF("%s\n", lua_tostring(L, -1));
|
|
rb->splashf(5 * HZ, "%s", lua_tostring(L, -1));
|
|
lua_pop(L, 1);
|
|
}
|
|
|
|
lua_close(L);
|
|
}
|
|
|
|
return PLUGIN_OK;
|
|
}
|
|
|