1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Enforced mod global naming convention and better error reporting

This commit is contained in:
Perttu Ahola 2011-12-03 02:45:55 +02:00
parent 581f950e10
commit d96cd236f3
7 changed files with 258 additions and 140 deletions

View file

@ -31,6 +31,27 @@ extern "C" {
#include <lauxlib.h>
}
LuaError::LuaError(lua_State *L, const std::string &s)
{
m_s = "LuaError: ";
m_s += s + "\n";
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if(lua_istable(L, -1)){
lua_getfield(L, -1, "traceback");
if(lua_isfunction(L, -1)){
lua_call(L, 0, 1);
if(lua_isstring(L, -1)){
m_s += lua_tostring(L, -1);
}
lua_pop(L, 1);
}
else{
lua_pop(L, 1);
}
}
lua_pop(L, 1);
}
void script_error(lua_State *L, const char *fmt, ...)
{
va_list argp;
@ -39,13 +60,34 @@ void script_error(lua_State *L, const char *fmt, ...)
vsnprintf(buf, 10000, fmt, argp);
va_end(argp);
//errorstream<<"SCRIPT ERROR: "<<buf;
throw LuaError(buf);
throw LuaError(L, buf);
}
int luaErrorHandler(lua_State *L) {
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
return 1;
}
lua_getfield(L, -1, "traceback");
if (!lua_isfunction(L, -1)) {
lua_pop(L, 2);
return 1;
}
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
return 1;
}
bool script_load(lua_State *L, const char *path)
{
infostream<<"Loading and running script from "<<path<<std::endl;
int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, 0);
lua_pushcfunction(L, luaErrorHandler);
int errorhandler = lua_gettop(L);
int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, errorhandler);
if(ret){
errorstream<<"Failed to load and run script from "<<path<<":"<<std::endl;
errorstream<<"[LUA] "<<std::endl;