Skip to content

Commit bc0e083

Browse files
committed
Work on Lua error output
1 parent 9409e25 commit bc0e083

File tree

2 files changed

+49
-39
lines changed

2 files changed

+49
-39
lines changed

src/xrScriptEngine/script_engine.cpp

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ void CScriptEngine::print_stack(lua_State* L)
201201
lua_Debug l_tDebugInfo;
202202
for (int i = 0; lua_getstack(L, i, &l_tDebugInfo); i++)
203203
{
204+
Log("\nSCRIPT ERROR");
204205
lua_getinfo(L, "nSlu", &l_tDebugInfo);
205206
if (!l_tDebugInfo.name)
206207
{
@@ -214,17 +215,31 @@ void CScriptEngine::print_stack(lua_State* L)
214215
script_log(LuaMessageType::Error, "%2d : [%s] %s(%d) : %s", i, l_tDebugInfo.what, l_tDebugInfo.short_src,
215216
l_tDebugInfo.currentline, l_tDebugInfo.name);
216217
}
218+
219+
pcstr lua_error_text = lua_tostring(L, -1); // lua-error text
220+
luaL_traceback(L, L, make_string("! [LUA][Error]: %s\n", lua_error_text).c_str(), 1); // add lua traceback to it
221+
pcstr sErrorText = lua_tostring(L, -1); // get combined error text from lua stack
222+
Log(sErrorText);
223+
lua_pop(L, 1); // restore lua stack
224+
217225
// Giperion: verbose log
218-
Msg("\tLocals: ");
226+
Log("\nLua state dump:\n\tLocals: ");
219227
pcstr name = nullptr;
220228
int VarID = 1;
221-
while ((name = lua_getlocal(L, &l_tDebugInfo, VarID++)) != nullptr)
229+
try
222230
{
223-
LogVariable(L, name, 1, true);
231+
while ((name = lua_getlocal(L, &l_tDebugInfo, VarID++)) != nullptr)
232+
{
233+
LogVariable(L, name, 1, true);
224234

225-
lua_pop(L, 1); /* remove variable value */
235+
lua_pop(L, 1); /* remove variable value */
236+
}
226237
}
227-
Msg("\tEnd");
238+
catch (...)
239+
{
240+
Log("Can't dump lua state - Engine corrupted");
241+
}
242+
Log("\tEnd\nEnd of Lua state dump.\n");
228243
// -Giperion
229244
}
230245

@@ -298,8 +313,7 @@ void CScriptEngine::LogVariable(lua_State* luaState, pcstr name, int level, bool
298313
lua_pop(luaState, 1); //Remove userobject
299314
return;
300315
}*/
301-
Msg("%s TODO: Fix userdata retrieval", tabBuffer);
302-
xr_strcpy(value, "[not available]");
316+
xr_strcpy(value, "[TODO: Fix userdata retrieval]");
303317
}
304318
break;
305319

@@ -319,8 +333,10 @@ int CScriptEngine::script_log(LuaMessageType message, LPCSTR caFormat, ...)
319333
int result = vscript_log(message, caFormat, marker);
320334
va_end(marker);
321335

336+
#ifdef DEBUG
322337
if (message == LuaMessageType::Error)
323338
print_stack();
339+
#endif
324340

325341
return result;
326342
}
@@ -382,8 +398,7 @@ lua_State* L, LPCSTR caBuffer, size_t tSize, LPCSTR caScriptName, LPCSTR caNameS
382398
l_iErrorCode = luaL_loadbuffer(L, caBuffer, tSize, caScriptName);
383399
if (l_iErrorCode)
384400
{
385-
print_output(L, caScriptName, l_iErrorCode);
386-
on_error(L);
401+
onErrorCallback(L, caScriptName, l_iErrorCode);
387402
return false;
388403
}
389404
return true;
@@ -435,9 +450,7 @@ bool CScriptEngine::do_file(LPCSTR caScriptName, LPCSTR caNameSpaceName)
435450
#endif
436451
if (l_iErrorCode)
437452
{
438-
print_output(lua(), caScriptName, l_iErrorCode);
439-
on_error(lua());
440-
lua_settop(lua(), start);
453+
onErrorCallback(lua(), caScriptName, l_iErrorCode);
441454
return false;
442455
}
443456
return true;
@@ -599,7 +612,7 @@ struct raii_guard : private Noncopyable
599612
}
600613
};
601614

602-
bool CScriptEngine::print_output(lua_State* L, LPCSTR caScriptFileName, int errorCode, const char* caErrorText)
615+
bool CScriptEngine::print_output(lua_State* L, pcstr caScriptFileName, int errorCode, pcstr caErrorText)
603616
{
604617
CScriptEngine* scriptEngine = GetInstance(L);
605618
VERIFY(scriptEngine);
@@ -823,42 +836,38 @@ void CScriptEngine::unload()
823836
*m_last_no_file = 0;
824837
}
825838

839+
void CScriptEngine::onErrorCallback(lua_State* L, pcstr scriptName, int errorCode, pcstr err)
840+
{
841+
print_output(L, scriptName, errorCode, err);
842+
on_error(L);
843+
844+
#if !XRAY_EXCEPTIONS
845+
xrDebug::Fatal(DEBUG_INFO, "LUA error: %s", err);
846+
#else
847+
throw err;
848+
#endif
849+
}
850+
826851
int CScriptEngine::lua_panic(lua_State* L)
827852
{
828-
print_output(L, "", LUA_ERRRUN, "PANIC");
853+
onErrorCallback(L, "", LUA_ERRRUN, "PANIC");
829854
return 0;
830855
}
831856

832857
void CScriptEngine::lua_error(lua_State* L)
833858
{
834859
pcstr err = lua_tostring(L, -1);
835-
print_output(L, "", LUA_ERRRUN, err);
836-
on_error(L);
837-
#if !XRAY_EXCEPTIONS
838-
xrDebug::Fatal(DEBUG_INFO, "LUA error: %s", err);
839-
#else
840-
throw lua_tostring(L, -1);
841-
#endif
860+
onErrorCallback(L, "", LUA_ERRRUN, err);
842861
}
843862

844863
int CScriptEngine::lua_pcall_failed(lua_State* L)
845864
{
846-
const char* sErrorText = NULL;
847-
848-
#ifndef DEBUG // Debug already do it
849-
const char* lua_error_text = lua_tostring(L, -1); // lua-error text
850-
luaL_traceback(L, L, make_string("[LUA][Error]: %s\n", lua_error_text).c_str(), 1); // add lua traceback to it
851-
sErrorText = lua_tostring(L, -1); // get combined error text from lua stack
852-
lua_pop(L, 1); // restore lua stack
853-
#endif
865+
const bool isString = lua_isstring(L, -1);
866+
const pcstr err = isString ? lua_tostring(L, -1) : "";
854867

855-
print_output(L, "", LUA_ERRRUN, sErrorText);
856-
on_error(L);
868+
onErrorCallback(L, "", LUA_ERRRUN, err);
857869

858-
#if !XRAY_EXCEPTIONS
859-
xrDebug::Fatal(DEBUG_INFO, "LUA error: %s", lua_isstring(L, -1) ? lua_tostring(L, -1) : "");
860-
#endif
861-
if (lua_isstring(L, -1))
870+
if (isString)
862871
lua_pop(L, 1);
863872
return LUA_ERRRUN;
864873
}
@@ -867,7 +876,7 @@ void CScriptEngine::lua_cast_failed(lua_State* L, const luabind::type_id& info)
867876
{
868877
string128 buf;
869878
xr_sprintf(buf, "LUA error: cannot cast lua value to %s", info.name());
870-
print_output(L, "", LUA_ERRRUN,buf);
879+
onErrorCallback(L, "", LUA_ERRRUN, buf);
871880
}
872881
#endif
873882

@@ -988,7 +997,7 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace)
988997
luajit::open_lib(lua(), LUA_STRLIBNAME, luaopen_string);
989998
luajit::open_lib(lua(), LUA_BITLIBNAME, luaopen_bit);
990999
luajit::open_lib(lua(), LUA_FFILIBNAME, luaopen_ffi);
991-
#if 1//def DEBUG
1000+
#ifdef DEBUG
9921001
luajit::open_lib(lua(), LUA_DBLIBNAME, luaopen_debug);
9931002
#endif
9941003
// XXX nitrocaster: with vanilla scripts, '-nojit' option requires script profiler to be disabled. The reason
@@ -1261,7 +1270,7 @@ void CScriptEngine::on_error(lua_State* state)
12611270

12621271
CScriptProcess* CScriptEngine::CreateScriptProcess(shared_str name, shared_str scripts)
12631272
{
1264-
return new CScriptProcess(this, name, scripts);
1273+
return new CScriptProcess(this, name, scripts);
12651274
}
12661275

12671276
CScriptThread* CScriptEngine::CreateScriptThread(LPCSTR caNamespaceName, bool do_string, bool reload)

src/xrScriptEngine/script_engine.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ class XRSCRIPTENGINE_API CScriptEngine
137137
luabind::object name_space(LPCSTR namespace_name);
138138
int error_log(LPCSTR caFormat, ...);
139139
int script_log(LuaMessageType message, LPCSTR caFormat, ...);
140-
static bool print_output(lua_State* L, LPCSTR caScriptName, int iErrorCode = 0, const char* caErrorText = NULL);
140+
static bool print_output(lua_State* L, pcstr caScriptName, int iErrorCode = 0, pcstr caErrorText = nullptr);
141141

142142
private:
143143
static void print_error(lua_State* L, int iErrorCode);
144+
static void onErrorCallback(lua_State* L, pcstr scriptName, int errorCode, pcstr err = nullptr);
144145

145146
public:
146147
static void on_error(lua_State* state);

0 commit comments

Comments
 (0)