00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "def-script.h"
00010
00011 #include "Path.h"
00012 #include "Scripter.h"
00013
00014
00015 Scripter *
00016 script_getLeader(lua_State *L)
00017 {
00018 lua_pushstring(L, script_getLeaderName());
00019 lua_rawget(L, LUA_REGISTRYINDEX);
00020 if (lua_isnil(L, -1)) {
00021 luaL_error(L, ExInfo("no leader")
00022 .addInfo("key", script_getLeaderName()).what());
00023 }
00024 luaL_checktype(L, -1, LUA_TLIGHTUSERDATA);
00025 Scripter *result = static_cast<Scripter*>(lua_touserdata(L, -1));
00026 lua_pop(L, 1);
00027
00028 return result;
00029 }
00030
00031
00032
00033
00034 int
00035 script_debugStack(lua_State *L)
00036 {
00037
00038
00039 static const int LEVELS1 = 12;
00040
00041 static const int LEVELS2 = 10;
00042
00043 int level = 1;
00044 int firstpart = 1;
00045 lua_Debug ar;
00046 if (lua_gettop(L) == 0)
00047 lua_pushliteral(L, "");
00048 else if (!lua_isstring(L, 1)) return 1;
00049 else lua_pushliteral(L, "\n");
00050 lua_pushliteral(L, "stack traceback:");
00051 while (lua_getstack(L, level++, &ar)) {
00052 if (level > LEVELS1 && firstpart) {
00053
00054 if (!lua_getstack(L, level+LEVELS2, &ar))
00055 level--;
00056 else {
00057 lua_pushliteral(L, "\n\t...");
00058 while (lua_getstack(L, level+LEVELS2, &ar))
00059 level++;
00060 }
00061 firstpart = 0;
00062 continue;
00063 }
00064 lua_pushliteral(L, "\n\t");
00065 lua_getinfo(L, "Snl", &ar);
00066 lua_pushfstring(L, "%s:", ar.short_src);
00067 if (ar.currentline > 0)
00068 lua_pushfstring(L, "%d:", ar.currentline);
00069 switch (*ar.namewhat) {
00070 case 'g':
00071 case 'l':
00072 case 'f':
00073 case 'm':
00074 lua_pushfstring(L, " in function `%s'", ar.name);
00075 break;
00076 default: {
00077 if (*ar.what == 'm')
00078 lua_pushfstring(L, " in main chunk");
00079 else if (*ar.what == 'C' || *ar.what == 't')
00080 lua_pushliteral(L, " ?");
00081 else
00082 lua_pushfstring(L, " in function <%s:%d>",
00083 ar.short_src, ar.linedefined);
00084 }
00085 }
00086 lua_concat(L, lua_gettop(L));
00087 }
00088 lua_concat(L, lua_gettop(L));
00089
00090 return 1;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 int
00100 script_file_include(lua_State *L) throw()
00101 {
00102 BEGIN_NOEXCEPTION;
00103 const char *filename = luaL_checkstring(L, 1);
00104
00105 script_getLeader(L)->scriptInclude(Path::dataReadPath(filename));
00106 END_NOEXCEPTION;
00107 return 0;
00108 }
00109
00110
00111
00112
00113
00114
00115 int
00116 script_file_exists(lua_State *L) throw()
00117 {
00118 BEGIN_NOEXCEPTION;
00119 const char *filename = luaL_checkstring(L, 1);
00120
00121 bool exists = Path::dataReadPath(filename).exists();
00122 lua_pushboolean(L, exists);
00123 END_NOEXCEPTION;
00124
00125 return 1;
00126 }
00127