Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

def-script.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
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  * char *debugStack(error_message)
00033  */
00034     int
00035 script_debugStack(lua_State *L)
00036 {
00037     //NOTE: stolen from "lua/ldblib.c"
00038     /* size of the first part of the stack */
00039     static const int LEVELS1 = 12;
00040     /* size of the second part of the stack */
00041     static const int LEVELS2 = 10;
00042 
00043     int level = 1;  /* skip level 0 (it's this function) */
00044     int firstpart = 1;  /* still before eventual `...' */
00045     lua_Debug ar;
00046     if (lua_gettop(L) == 0)
00047       lua_pushliteral(L, "");
00048     else if (!lua_isstring(L, 1)) return 1;  /* no string message */
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         /* no more than `LEVELS2' more levels? */
00054         if (!lua_getstack(L, level+LEVELS2, &ar))
00055           level--;  /* keep going */
00056         else {
00057           lua_pushliteral(L, "\n\t...");  /* too many levels */
00058           while (lua_getstack(L, level+LEVELS2, &ar))  /* find last levels */
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':  /* global */ 
00071         case 'l':  /* local */
00072         case 'f':  /* field */
00073         case 'm':  /* method */
00074           lua_pushfstring(L, " in function `%s'", ar.name);
00075           break;
00076         default: {
00077           if (*ar.what == 'm')  /* main? */
00078             lua_pushfstring(L, " in main chunk");
00079           else if (*ar.what == 'C' || *ar.what == 't')
00080             lua_pushliteral(L, " ?");  /* C function or tail call */
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     //NOTE: return debug_message
00090     return 1;
00091 }
00092 
00093 //-----------------------------------------------------------------
00094 /**
00095  * void file_include(filename)
00096  *
00097  * Do file in usedir or systemdir.
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  * bool file_exists(filename)
00112  *
00113  * Returns true when such file exists in userdir or systemdir.
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     //NOTE: return exists
00125     return 1;
00126 }
00127 

Generated on Wed Jun 1 09:54:30 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2