| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "def-script.h" |
| 10 |
|
| 11 |
#include "Path.h" |
| 12 |
#include "Scripter.h" |
| 13 |
|
| 14 |
//----------------------------------------------------------------- |
| 15 |
Scripter * |
| 16 |
script_getLeader(lua_State *L) |
| 17 |
{ |
| 18 |
lua_pushstring(L, script_getLeaderName()); |
| 19 |
lua_rawget(L, LUA_REGISTRYINDEX); |
| 20 |
if (lua_isnil(L, -1)) { |
| 21 |
luaL_error(L, ExInfo("no leader") |
| 22 |
.addInfo("key", script_getLeaderName()).what()); |
| 23 |
} |
| 24 |
luaL_checktype(L, -1, LUA_TLIGHTUSERDATA); |
| 25 |
Scripter *result = static_cast<Scripter*>(lua_touserdata(L, -1)); |
| 26 |
lua_pop(L, 1); |
| 27 |
|
| 28 |
return result; |
| 29 |
} |
| 30 |
//----------------------------------------------------------------- |
| 31 |
/** |
| 32 |
* char *debugStack(error_message) |
| 33 |
*/ |
| 34 |
int |
| 35 |
script_debugStack(lua_State *L) |
| 36 |
{ |
| 37 |
//NOTE: stolen from "lua/ldblib.c" |
| 38 |
/* size of the first part of the stack */ |
| 39 |
static const int LEVELS1 = 12; |
| 40 |
/* size of the second part of the stack */ |
| 41 |
static const int LEVELS2 = 10; |
| 42 |
|
| 43 |
int level = 1; /* skip level 0 (it's this function) */ |
| 44 |
int firstpart = 1; /* still before eventual `...' */ |
| 45 |
lua_Debug ar; |
| 46 |
if (lua_gettop(L) == 0) |
| 47 |
lua_pushliteral(L, ""); |
| 48 |
else if (!lua_isstring(L, 1)) return 1; /* no string message */ |
| 49 |
else lua_pushliteral(L, "\n"); |
| 50 |
lua_pushliteral(L, "stack traceback:"); |
| 51 |
while (lua_getstack(L, level++, &ar)) { |
| 52 |
if (level > LEVELS1 && firstpart) { |
| 53 |
/* no more than `LEVELS2' more levels? */ |
| 54 |
if (!lua_getstack(L, level+LEVELS2, &ar)) |
| 55 |
level--; /* keep going */ |
| 56 |
else { |
| 57 |
lua_pushliteral(L, "\n\t..."); /* too many levels */ |
| 58 |
while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */ |
| 59 |
level++; |
| 60 |
} |
| 61 |
firstpart = 0; |
| 62 |
continue; |
| 63 |
} |
| 64 |
lua_pushliteral(L, "\n\t"); |
| 65 |
lua_getinfo(L, "Snl", &ar); |
| 66 |
lua_pushfstring(L, "%s:", ar.short_src); |
| 67 |
if (ar.currentline > 0) |
| 68 |
lua_pushfstring(L, "%d:", ar.currentline); |
| 69 |
switch (*ar.namewhat) { |
| 70 |
case 'g': /* global */ |
| 71 |
case 'l': /* local */ |
| 72 |
case 'f': /* field */ |
| 73 |
case 'm': /* method */ |
| 74 |
lua_pushfstring(L, " in function `%s'", ar.name); |
| 75 |
break; |
| 76 |
default: { |
| 77 |
if (*ar.what == 'm') /* main? */ |
| 78 |
lua_pushfstring(L, " in main chunk"); |
| 79 |
else if (*ar.what == 'C' || *ar.what == 't') |
| 80 |
lua_pushliteral(L, " ?"); /* C function or tail call */ |
| 81 |
else |
| 82 |
lua_pushfstring(L, " in function <%s:%d>", |
| 83 |
ar.short_src, ar.linedefined); |
| 84 |
} |
| 85 |
} |
| 86 |
lua_concat(L, lua_gettop(L)); |
| 87 |
} |
| 88 |
lua_concat(L, lua_gettop(L)); |
| 89 |
//NOTE: return debug_message |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
//----------------------------------------------------------------- |
| 94 |
/** |
| 95 |
* void file_include(filename) |
| 96 |
* |
| 97 |
* Do file in usedir or systemdir. |
| 98 |
*/ |
| 99 |
int |
| 100 |
script_file_include(lua_State *L) throw() |
| 101 |
{ |
| 102 |
BEGIN_NOEXCEPTION; |
| 103 |
const char *filename = luaL_checkstring(L, 1); |
| 104 |
|
| 105 |
script_getLeader(L)->scriptInclude(Path::dataReadPath(filename)); |
| 106 |
END_NOEXCEPTION; |
| 107 |
return 0; |
| 108 |
} |
| 109 |
//----------------------------------------------------------------- |
| 110 |
/** |
| 111 |
* bool file_exists(filename) |
| 112 |
* |
| 113 |
* Returns true when such file exists in userdir or systemdir. |
| 114 |
*/ |
| 115 |
int |
| 116 |
script_file_exists(lua_State *L) throw() |
| 117 |
{ |
| 118 |
BEGIN_NOEXCEPTION; |
| 119 |
const char *filename = luaL_checkstring(L, 1); |
| 120 |
|
| 121 |
bool exists = Path::dataReadPath(filename).exists(); |
| 122 |
lua_pushboolean(L, exists); |
| 123 |
END_NOEXCEPTION; |
| 124 |
//NOTE: return exists |
| 125 |
return 1; |
| 126 |
} |
| 127 |
|