Skip to content

Commit 1ca95df

Browse files
committed
Exported class_info to scripts
Fixed lua stack Fixed dump userdata
1 parent b7b3401 commit 1ca95df

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

src/xrScriptEngine/script_engine.cpp

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdarg.h>
1919
#include "Common/Noncopyable.hpp"
2020
#include "xrCore/ModuleLookup.hpp"
21+
#include "luabind/class_info.hpp"
2122

2223
Flags32 g_LuaDebug;
2324

@@ -194,33 +195,24 @@ void CScriptEngine::print_stack(lua_State* L)
194195
if (L == nullptr)
195196
L = lua();
196197

197-
lua_Debug l_tDebugInfo;
198-
for (int i = 0; lua_getstack(L, i, &l_tDebugInfo); i++)
198+
if (strstr(Core.Params, "-luadumpstate"))
199199
{
200200
Log("\nSCRIPT ERROR");
201-
lua_getinfo(L, "nSlu", &l_tDebugInfo);
202-
if (!l_tDebugInfo.name)
201+
lua_Debug l_tDebugInfo;
202+
for (int i = 0; lua_getstack(L, i, &l_tDebugInfo); i++)
203203
{
204-
script_log(LuaMessageType::Error, "%2d : [%s] %s(%d) : %s", i, l_tDebugInfo.what, l_tDebugInfo.short_src,
205-
l_tDebugInfo.currentline, "");
206-
}
207-
else if (!xr_strcmp(l_tDebugInfo.what, "C"))
208-
script_log(LuaMessageType::Error, "%2d : [C ] %s", i, l_tDebugInfo.name);
209-
else
210-
{
211-
script_log(LuaMessageType::Error, "%2d : [%s] %s(%d) : %s", i, l_tDebugInfo.what, l_tDebugInfo.short_src,
212-
l_tDebugInfo.currentline, l_tDebugInfo.name);
213-
}
214-
215-
pcstr lua_error_text = lua_tostring(L, -1); // lua-error text
216-
luaL_traceback(L, L, make_string("! [LUA][Error]: %s\n", lua_error_text).c_str(), 1); // add lua traceback to it
217-
pcstr sErrorText = lua_tostring(L, -1); // get combined error text from lua stack
218-
Log(sErrorText);
219-
lua_pop(L, 1); // restore lua stack
204+
lua_getinfo(L, "nSlu", &l_tDebugInfo);
205+
if (!l_tDebugInfo.name)
206+
Msg("%2d : [%s] %s(%d)", i, l_tDebugInfo.what, l_tDebugInfo.short_src, l_tDebugInfo.currentline);
207+
else if (!xr_strcmp(l_tDebugInfo.what, "C"))
208+
Msg("%2d : [C ] %s", i, l_tDebugInfo.name);
209+
else
210+
{
211+
Msg("%2d : [%s] %s(%d) : %s", i, l_tDebugInfo.what, l_tDebugInfo.short_src, l_tDebugInfo.currentline,
212+
l_tDebugInfo.name);
213+
}
220214

221-
// Giperion: verbose log
222-
if (strstr(Core.Params, "-luadumpstate"))
223-
{
215+
// Giperion: verbose log
224216
Log("\nLua state dump:\n\tLocals: ");
225217
pcstr name = nullptr;
226218
int VarID = 1;
@@ -237,9 +229,16 @@ void CScriptEngine::print_stack(lua_State* L)
237229
{
238230
Log("Can't dump lua state - Engine corrupted");
239231
}
240-
Log("\tEnd\nEnd of Lua state dump.\n");
232+
Log("End of Lua state dump.\n");
233+
// -Giperion
241234
}
242-
// -Giperion
235+
}
236+
else
237+
{
238+
luaL_traceback(L, L, nullptr, 1); // add lua traceback to it
239+
pcstr sErrorText = lua_tostring(L, -1); // get combined error text from lua stack
240+
Log(sErrorText);
241+
lua_pop(L, 1); // restore lua stack
243242
}
244243

245244
m_stack_is_ready = true;
@@ -266,6 +265,7 @@ void CScriptEngine::LogTable(lua_State* luaState, pcstr S, int level)
266265

267266
void CScriptEngine::LogVariable(lua_State* luaState, pcstr name, int level)
268267
{
268+
using namespace luabind::detail;
269269
const int ntype = lua_type(luaState, -1);
270270
const pcstr type = lua_typename(luaState, ntype);
271271

@@ -316,6 +316,7 @@ void CScriptEngine::LogVariable(lua_State* luaState, pcstr name, int level)
316316
// case LUA_TLIGHTUSERDATA:
317317
case LUA_TUSERDATA:
318318
{
319+
/*
319320
lua_getmetatable(luaState, -1); // Maybe we can do this in another way
320321
if (lua_istable(luaState, -1))
321322
{
@@ -325,6 +326,26 @@ void CScriptEngine::LogVariable(lua_State* luaState, pcstr name, int level)
325326
return;
326327
}
327328
//[[fallthrough]]
329+
*/
330+
object_rep* object = get_instance(luaState, -1);
331+
if (!object)
332+
{
333+
xr_strcpy(value, "Error! Can't get instance!");
334+
break;
335+
}
336+
337+
class_rep* rep = object->crep();
338+
if (!rep)
339+
{
340+
xr_strcpy(value, "Error! Class userdata is null!");
341+
break;
342+
}
343+
344+
pcstr className = rep->name();
345+
if (className)
346+
xr_sprintf(value, "'%s'", className);
347+
348+
break;
328349
}
329350

330351
default:
@@ -648,7 +669,7 @@ bool CScriptEngine::print_output(lua_State* L, pcstr caScriptFileName, int error
648669
{
649670
if (!errorCode)
650671
scriptEngine->script_log(LuaMessageType::Info, "Output from %s", caScriptFileName);
651-
scriptEngine->script_log(errorCode ? LuaMessageType::Error : LuaMessageType::Message, "%s", S);
672+
// scriptEngine->script_log(errorCode ? LuaMessageType::Error : LuaMessageType::Message, "%s", S);
652673
#if defined(USE_DEBUGGER) && !defined(USE_LUA_STUDIO)
653674
if (debugger() && debugger()->Active())
654675
{
@@ -669,22 +690,22 @@ void CScriptEngine::print_error(lua_State* L, int iErrorCode)
669690
switch (iErrorCode)
670691
{
671692
case LUA_ERRRUN:
672-
scriptEngine->script_log(LuaMessageType::Error, "SCRIPT RUNTIME ERROR");
693+
Log("\n\nSCRIPT RUNTIME ERROR");
673694
break;
674695
case LUA_ERRMEM:
675-
scriptEngine->script_log(LuaMessageType::Error, "SCRIPT ERROR (memory allocation)");
696+
Log("\n\nSCRIPT ERROR (memory allocation)");
676697
break;
677698
case LUA_ERRERR:
678-
scriptEngine->script_log(LuaMessageType::Error, "SCRIPT ERROR (while running the error handler function)");
699+
Log("\n\nSCRIPT ERROR (while running the error handler function)");
679700
break;
680701
case LUA_ERRFILE:
681-
scriptEngine->script_log(LuaMessageType::Error, "SCRIPT ERROR (while running file)");
702+
Log("\n\nSCRIPT ERROR (while running file)");
682703
break;
683704
case LUA_ERRSYNTAX:
684-
scriptEngine->script_log(LuaMessageType::Error, "SCRIPT SYNTAX ERROR");
705+
Log("\n\nSCRIPT SYNTAX ERROR");
685706
break;
686707
case LUA_YIELD:
687-
scriptEngine->script_log(LuaMessageType::Info, "Thread is yielded");
708+
Log("\n\nThread is yielded");
688709
break;
689710
default: NODEFAULT;
690711
}
@@ -850,11 +871,7 @@ void CScriptEngine::onErrorCallback(lua_State* L, pcstr scriptName, int errorCod
850871
print_output(L, scriptName, errorCode, err);
851872
on_error(L);
852873

853-
#if !XRAY_EXCEPTIONS
854874
xrDebug::Fatal(DEBUG_INFO, "LUA error: %s", err);
855-
#else
856-
throw err;
857-
#endif
858875
}
859876

860877
int CScriptEngine::lua_panic(lua_State* L)
@@ -880,7 +897,7 @@ int CScriptEngine::lua_pcall_failed(lua_State* L)
880897
lua_pop(L, 1);
881898
return LUA_ERRRUN;
882899
}
883-
#if !XRAY_EXCEPTIONS
900+
#if 1 //!XRAY_EXCEPTIONS
884901
void CScriptEngine::lua_cast_failed(lua_State* L, const luabind::type_id& info)
885902
{
886903
string128 buf;
@@ -968,6 +985,7 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace)
968985
luabind::open(lua());
969986
// XXX: temporary workaround to preserve backwards compatibility with game scripts
970987
luabind::disable_super_deprecation();
988+
luabind::bind_class_info(lua());
971989
setup_callbacks();
972990
if (exporterFunc)
973991
exporterFunc(lua());

src/xrScriptEngine/script_engine.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class XRSCRIPTENGINE_API CScriptEngine
160160
static int lua_panic(lua_State* L);
161161
static void lua_error(lua_State* L);
162162
static int lua_pcall_failed(lua_State* L);
163-
#if !XRAY_EXCEPTIONS
163+
#if 1 //!XRAY_EXCEPTIONS
164164
static void lua_cast_failed(lua_State* L, const luabind::type_id& info);
165165
#endif
166166
#ifdef DEBUG

0 commit comments

Comments
 (0)