@@ -120,7 +120,7 @@ void CScriptEngine::reinit()
120
120
m_virtual_machine = luaL_newstate ();
121
121
if (!m_virtual_machine)
122
122
{
123
- Msg (" ! ERROR : Cannot initialize script virtual machine!" );
123
+ Log (" ! ERROR : Cannot initialize script virtual machine!" );
124
124
return ;
125
125
}
126
126
RegisterState (m_virtual_machine, this );
@@ -134,9 +134,8 @@ void CScriptEngine::reinit()
134
134
135
135
int CScriptEngine::vscript_log (LuaMessageType luaMessageType, LPCSTR caFormat, va_list marker)
136
136
{
137
- #ifdef DEBUG
138
- if (!g_LuaDebug.test (1 ) && luaMessageType != LuaMessageType::Error)
139
- return 0 ;
137
+ // if (!g_LuaDebug.test(1) && luaMessageType != LuaMessageType::Error)
138
+ // return 0;
140
139
LPCSTR S = " " , SS = " " ;
141
140
LPSTR S1;
142
141
string4096 S2;
@@ -186,17 +185,19 @@ int CScriptEngine::vscript_log(LuaMessageType luaMessageType, LPCSTR caFormat, v
186
185
xr_strcat (S2, " \r\n " );
187
186
m_output.w (S2, xr_strlen (S2));
188
187
return l_iResult;
189
- #else
190
- return 0 ;
191
- #endif
192
188
}
193
189
194
- void CScriptEngine::print_stack ()
190
+ void CScriptEngine::print_stack (lua_State* L )
195
191
{
196
- if (!m_stack_is_ready)
192
+ if (!m_stack_is_ready || logReenterability )
197
193
return ;
194
+
195
+ logReenterability = true ;
198
196
m_stack_is_ready = false ;
199
- lua_State* L = lua ();
197
+
198
+ if (L == nullptr )
199
+ L = lua ();
200
+
200
201
lua_Debug l_tDebugInfo;
201
202
for (int i = 0 ; lua_getstack (L, i, &l_tDebugInfo); i++)
202
203
{
@@ -213,23 +214,114 @@ void CScriptEngine::print_stack()
213
214
script_log (LuaMessageType::Error, " %2d : [%s] %s(%d) : %s" , i, l_tDebugInfo.what , l_tDebugInfo.short_src ,
214
215
l_tDebugInfo.currentline , l_tDebugInfo.name );
215
216
}
217
+ // Giperion: verbose log
218
+ Msg (" \t Locals: " );
219
+ pcstr name = nullptr ;
220
+ int VarID = 1 ;
221
+ while ((name = lua_getlocal (L, &l_tDebugInfo, VarID++)) != nullptr )
222
+ {
223
+ LogVariable (L, name, 1 , true );
224
+
225
+ lua_pop (L, 1 ); /* remove variable value */
226
+ }
227
+ Msg (" \t End" );
228
+ // -Giperion
229
+ }
230
+
231
+ m_stack_is_ready = true ;
232
+ logReenterability = false ;
233
+ }
234
+
235
+ void CScriptEngine::LogTable (lua_State* luaState, pcstr S, int level)
236
+ {
237
+ if (!lua_istable (luaState, -1 ))
238
+ return ;
239
+
240
+ lua_pushnil (luaState); /* first key */
241
+ while (lua_next (luaState, -2 ) != 0 )
242
+ {
243
+ char sname[256 ];
244
+ char sFullName [256 ];
245
+ xr_sprintf (sname, " %s" , lua_tostring (luaState, -2 ));
246
+ xr_sprintf (sFullName , " %s.%s" , S, sname);
247
+ LogVariable (luaState, sFullName , level + 1 , false );
248
+
249
+ lua_pop (luaState, 1 ); /* removes `value'; keeps `key' for next iteration */
216
250
}
217
251
}
218
252
253
+ void CScriptEngine::LogVariable (lua_State* luaState, pcstr name, int level, bool bOpenTable)
254
+ {
255
+ const int ntype = lua_type (luaState, -1 );
256
+ const pcstr type = lua_typename (luaState, ntype);
257
+
258
+ char tabBuffer[32 ] = {0 };
259
+ memset (tabBuffer, ' \t ' , level);
260
+
261
+ char value[128 ];
262
+
263
+ switch (ntype)
264
+ {
265
+ case LUA_TNUMBER:
266
+ xr_sprintf (value, " %f" , lua_tonumber (luaState, -1 ));
267
+ break ;
268
+
269
+ case LUA_TBOOLEAN:
270
+ xr_sprintf (value, " %s" , lua_toboolean (luaState, -1 ) ? " true" : " false" );
271
+ break ;
272
+
273
+ case LUA_TSTRING:
274
+ xr_sprintf (value, " %.127s" , lua_tostring (luaState, -1 ));
275
+ break ;
276
+
277
+ case LUA_TTABLE:
278
+ {
279
+ if (bOpenTable)
280
+ {
281
+ Msg (" %s Table: %s" , tabBuffer, name);
282
+ LogTable (luaState, name, level + 1 );
283
+ return ;
284
+ }
285
+ xr_sprintf (value, " [...]" );
286
+ }
287
+ break ;
288
+
289
+ case LUA_TUSERDATA:
290
+ {
291
+ /* luabind::detail::object_rep* obj = static_cast<luabind::detail::object_rep*>(lua_touserdata(luaState, -1));
292
+ luabind::detail::lua_reference& r = obj->get_lua_table(); // XXX: No such method
293
+ if (r.is_valid())
294
+ {
295
+ r.get(luaState);
296
+ Msg("%s Userdata: %s", tabBuffer, name);
297
+ LogTable(luaState, name, level + 1);
298
+ lua_pop(luaState, 1); //Remove userobject
299
+ return;
300
+ }*/
301
+ Msg (" %s TODO: Fix userdata retrieval" , tabBuffer);
302
+ xr_strcpy (value, " [not available]" );
303
+ }
304
+ break ;
305
+
306
+ default :
307
+ xr_strcpy (value, " [not available]" );
308
+ break ;
309
+ }
310
+
311
+
312
+ Msg (" %s %s %s : %s" , tabBuffer, type, name, value);
313
+ }
314
+
219
315
int CScriptEngine::script_log (LuaMessageType message, LPCSTR caFormat, ...)
220
316
{
221
317
va_list marker;
222
318
va_start (marker, caFormat);
223
319
int result = vscript_log (message, caFormat, marker);
224
320
va_end (marker);
225
- #ifdef DEBUG
226
- if (message == LuaMessageType::Error && !logReenterability)
227
- {
228
- logReenterability = true ;
321
+
322
+ if (message == LuaMessageType::Error)
229
323
print_stack ();
230
- logReenterability = false ;
231
- }
232
- #endif
324
+
233
325
return result;
234
326
}
235
327
@@ -290,9 +382,7 @@ lua_State* L, LPCSTR caBuffer, size_t tSize, LPCSTR caScriptName, LPCSTR caNameS
290
382
l_iErrorCode = luaL_loadbuffer (L, caBuffer, tSize, caScriptName);
291
383
if (l_iErrorCode)
292
384
{
293
- #ifdef DEBUG
294
385
print_output (L, caScriptName, l_iErrorCode);
295
- #endif
296
386
on_error (L);
297
387
return false ;
298
388
}
@@ -345,9 +435,7 @@ bool CScriptEngine::do_file(LPCSTR caScriptName, LPCSTR caNameSpaceName)
345
435
#endif
346
436
if (l_iErrorCode)
347
437
{
348
- #ifdef DEBUG
349
438
print_output (lua (), caScriptName, l_iErrorCode);
350
- #endif
351
439
on_error (lua ());
352
440
lua_settop (lua (), start);
353
441
return false ;
@@ -478,20 +566,18 @@ luabind::object CScriptEngine::name_space(LPCSTR namespace_name)
478
566
479
567
struct raii_guard : private Noncopyable
480
568
{
481
- CScriptEngine* scriptEngine ;
569
+ CScriptEngine* m_script_engine ;
482
570
int m_error_code;
483
571
const char *& m_error_description;
484
572
485
573
raii_guard (CScriptEngine* scriptEngine, int error_code, const char *& error_description)
486
- : m_error_code(error_code), m_error_description(error_description)
487
- {
488
- this ->scriptEngine = scriptEngine;
489
- }
574
+ : m_script_engine(scriptEngine), m_error_code(error_code), m_error_description(error_description)
575
+ {}
490
576
491
577
~raii_guard ()
492
578
{
493
579
#ifdef DEBUG
494
- bool lua_studio_connected = !!scriptEngine ->debugger ();
580
+ const bool lua_studio_connected = !!m_script_engine ->debugger ();
495
581
if (!lua_studio_connected)
496
582
#endif
497
583
{
@@ -519,8 +605,9 @@ bool CScriptEngine::print_output(lua_State* L, LPCSTR caScriptFileName, int erro
519
605
VERIFY (scriptEngine);
520
606
if (errorCode)
521
607
print_error (L, errorCode);
522
- LPCSTR S = " see call_stack for details!" ;
523
- raii_guard guard (scriptEngine, errorCode, S);
608
+ scriptEngine->print_stack (L);
609
+ pcstr S = " see call_stack for details!" ;
610
+ raii_guard guard (scriptEngine, errorCode, caErrorText ? caErrorText : S);
524
611
if (!lua_isstring (L, -1 ))
525
612
return false ;
526
613
S = lua_tostring (L, -1 );
@@ -690,9 +777,7 @@ CScriptEngine::CScriptEngine(bool is_editor)
690
777
luabind::allocator = &luabind_allocator;
691
778
luabind::allocator_context = nullptr ;
692
779
m_current_thread = nullptr ;
693
- #ifdef DEBUG
694
780
m_stack_is_ready = false ;
695
- #endif
696
781
m_virtual_machine = nullptr ;
697
782
m_stack_level = 0 ;
698
783
m_reload_modules = false ;
@@ -740,16 +825,17 @@ void CScriptEngine::unload()
740
825
741
826
int CScriptEngine::lua_panic (lua_State* L)
742
827
{
743
- print_output (L, " PANIC " , LUA_ERRRUN);
828
+ print_output (L, " " , LUA_ERRRUN, " PANIC " );
744
829
return 0 ;
745
830
}
746
831
747
832
void CScriptEngine::lua_error (lua_State* L)
748
833
{
749
- print_output (L, " " , LUA_ERRRUN);
834
+ pcstr err = lua_tostring (L, -1 );
835
+ print_output (L, " " , LUA_ERRRUN, err);
750
836
on_error (L);
751
837
#if !XRAY_EXCEPTIONS
752
- xrDebug::Fatal (DEBUG_INFO, " LUA error: %s" , lua_tostring (L, - 1 ) );
838
+ xrDebug::Fatal (DEBUG_INFO, " LUA error: %s" , err );
753
839
#else
754
840
throw lua_tostring (L, -1 );
755
841
#endif
@@ -779,8 +865,9 @@ int CScriptEngine::lua_pcall_failed(lua_State* L)
779
865
#if !XRAY_EXCEPTIONS
780
866
void CScriptEngine::lua_cast_failed (lua_State* L, const luabind::type_id& info)
781
867
{
782
- print_output (L, " " , LUA_ERRRUN);
783
- xrDebug::Fatal (DEBUG_INFO, " LUA error: cannot cast lua value to %s" , info.name ());
868
+ string128 buf;
869
+ xr_sprintf (buf, " LUA error: cannot cast lua value to %s" , info.name ());
870
+ print_output (L, " " , LUA_ERRRUN,buf);
784
871
}
785
872
#endif
786
873
@@ -901,7 +988,7 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace)
901
988
luajit::open_lib (lua (), LUA_STRLIBNAME, luaopen_string);
902
989
luajit::open_lib (lua (), LUA_BITLIBNAME, luaopen_bit);
903
990
luajit::open_lib (lua (), LUA_FFILIBNAME, luaopen_ffi);
904
- #ifdef DEBUG
991
+ #if 1 // def DEBUG
905
992
luajit::open_lib (lua (), LUA_DBLIBNAME, luaopen_debug);
906
993
#endif
907
994
// XXX nitrocaster: with vanilla scripts, '-nojit' option requires script profiler to be disabled. The reason
@@ -928,9 +1015,8 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace)
928
1015
}
929
1016
#endif
930
1017
setup_auto_load ();
931
- #ifdef DEBUG
932
1018
m_stack_is_ready = true ;
933
- # endif
1019
+
934
1020
#if defined(DEBUG) && !defined(USE_LUA_STUDIO)
935
1021
#if defined(USE_DEBUGGER)
936
1022
if (!debugger () || !debugger ()->Active ())
0 commit comments