@@ -144,33 +144,43 @@ void CScriptEngine::reinit()
144
144
145
145
void CScriptEngine::print_stack (lua_State* L)
146
146
{
147
- if (!m_stack_is_ready || logReenterability)
148
- return ;
149
-
150
- logReenterability = true ;
151
- m_stack_is_ready = false ;
152
-
153
147
if (L == nullptr )
154
148
L = lua ();
155
149
156
- if (strstr (Core.Params , " -luadumpstate" ))
150
+ static bool dump = strstr (Core.Params , " -luadumpstate" );
151
+
152
+ if (lua_isstring (L, -1 ))
157
153
{
158
- Log (" \n SCRIPT ERROR" );
159
- lua_Debug l_tDebugInfo;
160
- for (int i = 0 ; lua_getstack (L, i, &l_tDebugInfo); i++)
154
+ pcstr err = lua_tostring (L, -1 );
155
+ script_log (LuaMessageType::Error, " %s" , err);
156
+ }
157
+
158
+ lua_Debug l_tDebugInfo;
159
+ for (int i = 0 ; lua_getstack (L, i, &l_tDebugInfo); i++)
160
+ {
161
+ lua_getinfo (L, " nSlu" , &l_tDebugInfo);
162
+
163
+ if (!xr_strcmp (l_tDebugInfo.what , " C" ))
161
164
{
162
- lua_getinfo (L, " nSlu" , &l_tDebugInfo);
163
- if (!l_tDebugInfo.name )
164
- Msg (" %2d : [%s] %s(%d)" , i, l_tDebugInfo.what , l_tDebugInfo.short_src , l_tDebugInfo.currentline );
165
- else if (!xr_strcmp (l_tDebugInfo.what , " C" ))
166
- Msg (" %2d : [C ] %s" , i, l_tDebugInfo.name );
165
+ script_log (LuaMessageType::Error, " %2d : [C ] %s" , i, l_tDebugInfo.name ? l_tDebugInfo.name : " " );
166
+ }
167
+ else
168
+ {
169
+ string_path temp;
170
+ if (l_tDebugInfo.name )
171
+ xr_sprintf (temp, " %s(%d)" , l_tDebugInfo.name , l_tDebugInfo.linedefined );
167
172
else
168
- {
169
- Msg (" %2d : [%s] %s(%d) : %s" , i, l_tDebugInfo.what , l_tDebugInfo.short_src , l_tDebugInfo.currentline ,
170
- l_tDebugInfo.name );
171
- }
173
+ xr_sprintf (temp, " function <%s:%d>" , l_tDebugInfo.short_src , l_tDebugInfo.linedefined );
174
+
175
+ script_log (LuaMessageType::Error, " %2d : [%3s] %s(%d) : %s" , i, l_tDebugInfo.what ,
176
+ l_tDebugInfo.short_src , l_tDebugInfo.currentline , temp);
177
+ }
178
+
179
+ // Giperion: verbose log
180
+ if (dump)
181
+ {
182
+ const auto top = lua_gettop (L);
172
183
173
- // Giperion: verbose log
174
184
Log (" \n Lua state dump:\n\t Locals: " );
175
185
pcstr name = nullptr ;
176
186
int VarID = 1 ;
@@ -188,19 +198,11 @@ void CScriptEngine::print_stack(lua_State* L)
188
198
Log (" Can't dump lua state - Engine corrupted" );
189
199
}
190
200
Log (" End of Lua state dump.\n " );
191
- // -Giperion
201
+
202
+ luabind::detail::stack_pop{ L, lua_gettop (L) - top }; // restore lua stack
192
203
}
204
+ // -Giperion
193
205
}
194
- else
195
- {
196
- luaL_traceback (L, L, nullptr , 1 ); // add lua traceback to it
197
- pcstr sErrorText = lua_tostring (L, -1 ); // get combined error text from lua stack
198
- Log (sErrorText );
199
- lua_pop (L, 1 ); // restore lua stack
200
- }
201
-
202
- m_stack_is_ready = true ;
203
- logReenterability = false ;
204
206
}
205
207
206
208
void CScriptEngine::LogTable (lua_State* luaState, pcstr S, int level)
@@ -372,7 +374,8 @@ lua_State* L, LPCSTR caBuffer, size_t tSize, LPCSTR caScriptName, LPCSTR caNameS
372
374
l_iErrorCode = luaL_loadbuffer (L, caBuffer, tSize, caScriptName);
373
375
if (l_iErrorCode)
374
376
{
375
- onErrorCallback (L, caScriptName, l_iErrorCode);
377
+ print_output (L, caScriptName, l_iErrorCode);
378
+ on_error (L);
376
379
return false ;
377
380
}
378
381
return true ;
@@ -424,7 +427,9 @@ bool CScriptEngine::do_file(LPCSTR caScriptName, LPCSTR caNameSpaceName)
424
427
#endif
425
428
if (l_iErrorCode)
426
429
{
427
- onErrorCallback (lua (), caScriptName, l_iErrorCode);
430
+ print_output (lua (), caScriptName, l_iErrorCode);
431
+ on_error (lua ());
432
+ lua_settop (lua (), start);
428
433
return false ;
429
434
}
430
435
return true ;
@@ -554,53 +559,25 @@ luabind::object CScriptEngine::name_space(LPCSTR namespace_name)
554
559
}
555
560
}
556
561
557
- struct raii_guard : private Noncopyable
562
+ bool CScriptEngine::print_output (lua_State* L, pcstr caScriptFileName, int errorCode, pcstr caErrorText)
558
563
{
559
- CScriptEngine* m_script_engine;
560
- int m_error_code;
561
- const char *& m_error_description;
562
-
563
- raii_guard (CScriptEngine* scriptEngine, int error_code, const char *& error_description)
564
- : m_script_engine(scriptEngine), m_error_code(error_code), m_error_description(error_description)
565
- {}
564
+ CScriptEngine* scriptEngine = GetInstance (L);
565
+ VERIFY (scriptEngine);
566
566
567
- ~raii_guard ( )
567
+ if (caErrorText )
568
568
{
569
- #if defined(USE_DEBUGGER) && defined(USE_LUA_STUDIO)
570
- const bool lua_studio_connected = !!m_script_engine->debugger ();
571
- if (!lua_studio_connected)
572
- #endif
573
- {
574
- #ifdef DEBUG
575
- static const bool break_on_assert = !!strstr (Core.Params , " -break_on_assert" );
576
- #else
577
- static const bool break_on_assert = true ; // xxx: there is no point to set it true\false in Release, since
578
- // game will crash anyway in most cases due to XRAY_EXCEPTIONS
579
- // disabled in Release build.
580
- #endif
581
- if (!m_error_code)
582
- return ; // Check "lua_pcall_failed" before changing this!
583
-
584
- if (break_on_assert)
585
- R_ASSERT2 (!m_error_code, m_error_description);
586
- else
587
- Msg (" ! SCRIPT ERROR: %s" , m_error_description);
588
- }
569
+ const auto [logHeader, luaLogHeader] = get_message_headers (LuaMessageType::Error);
570
+ Msg (" %sSCRIPT ERROR: %s\n " , logHeader, caErrorText);
589
571
}
590
- };
591
572
592
- bool CScriptEngine::print_output (lua_State* L, pcstr caScriptFileName, int errorCode, pcstr caErrorText)
593
- {
594
- CScriptEngine* scriptEngine = GetInstance (L);
595
- VERIFY (scriptEngine);
596
573
if (errorCode)
597
574
print_error (L, errorCode);
598
- scriptEngine->print_stack (L);
599
- pcstr S = " see call_stack for details!" ;
600
- raii_guard guard (scriptEngine, errorCode, caErrorText ? caErrorText : S);
575
+
601
576
if (!lua_isstring (L, -1 ))
602
577
return false ;
603
- S = lua_tostring (L, -1 );
578
+
579
+ const auto S = lua_tostring (L, -1 );
580
+
604
581
if (!xr_strcmp (S, " cannot resume dead coroutine" ))
605
582
{
606
583
VERIFY2 (" Please do not return any values from main!!!" , caScriptFileName);
@@ -616,7 +593,6 @@ bool CScriptEngine::print_output(lua_State* L, pcstr caScriptFileName, int error
616
593
{
617
594
if (!errorCode)
618
595
scriptEngine->script_log (LuaMessageType::Info, " Output from %s" , caScriptFileName);
619
- // scriptEngine->script_log(errorCode ? LuaMessageType::Error : LuaMessageType::Message, "%s", S);
620
596
#if defined(USE_DEBUGGER) && !defined(USE_LUA_STUDIO)
621
597
if (debugger () && debugger ()->Active ())
622
598
{
@@ -625,34 +601,34 @@ bool CScriptEngine::print_output(lua_State* L, pcstr caScriptFileName, int error
625
601
}
626
602
#endif
627
603
}
628
- if (caErrorText)
629
- S = caErrorText;
604
+
630
605
return true ;
631
606
}
632
607
633
608
void CScriptEngine::print_error (lua_State* L, int iErrorCode)
634
609
{
635
- VERIFY (GetInstance (L));
610
+ CScriptEngine* scriptEngine = GetInstance (L);
611
+ VERIFY (scriptEngine);
636
612
637
613
switch (iErrorCode)
638
614
{
639
615
case LUA_ERRRUN:
640
- Log ( " \n\n SCRIPT RUNTIME ERROR" );
616
+ scriptEngine-> script_log (LuaMessageType::Error, " SCRIPT RUNTIME ERROR" );
641
617
break ;
642
618
case LUA_ERRMEM:
643
- Log ( " \n\n SCRIPT ERROR (memory allocation)" );
619
+ scriptEngine-> script_log (LuaMessageType::Error, " SCRIPT ERROR (memory allocation)" );
644
620
break ;
645
621
case LUA_ERRERR:
646
- Log ( " \n\n SCRIPT ERROR (while running the error handler function)" );
622
+ scriptEngine-> script_log (LuaMessageType::Error, " SCRIPT ERROR (while running the error handler function)" );
647
623
break ;
648
624
case LUA_ERRFILE:
649
- Log ( " \n\n SCRIPT ERROR (while running file)" );
625
+ scriptEngine-> script_log (LuaMessageType::Error, " SCRIPT ERROR (while running file)" );
650
626
break ;
651
627
case LUA_ERRSYNTAX:
652
- Log ( " \n\n SCRIPT SYNTAX ERROR" );
628
+ scriptEngine-> script_log (LuaMessageType::Error, " SCRIPT SYNTAX ERROR" );
653
629
break ;
654
630
case LUA_YIELD:
655
- Log ( " \n\n Thread is yielded" );
631
+ scriptEngine-> script_log (LuaMessageType::Info, " Thread is yielded" );
656
632
break ;
657
633
default : NODEFAULT;
658
634
}
@@ -762,7 +738,6 @@ CScriptEngine::CScriptEngine(bool is_editor, bool is_with_profiler)
762
738
luabind::allocator = &luabind_allocator;
763
739
luabind::allocator_context = nullptr ;
764
740
m_current_thread = nullptr ;
765
- m_stack_is_ready = false ;
766
741
m_virtual_machine = nullptr ;
767
742
m_profiler = is_with_profiler && !is_editor ? xr_new<CScriptProfiler>(this ) : nullptr ;
768
743
m_stack_level = 0 ;
@@ -817,50 +792,53 @@ void CScriptEngine::unload()
817
792
*m_last_no_file = 0 ;
818
793
}
819
794
820
- bool CScriptEngine::onErrorCallback (lua_State* L, pcstr scriptName, int errorCode, pcstr err)
821
- {
822
- print_output (L, scriptName, errorCode, err);
823
- on_error (L);
824
-
825
- bool ignoreAlways;
826
- const auto result = xrDebug::Fail (ignoreAlways, DEBUG_INFO, " LUA error" , err);
827
-
828
- return result == AssertionResult::ignore;
829
- }
830
-
831
795
int CScriptEngine::lua_panic (lua_State* L)
832
796
{
833
- onErrorCallback (L, " " , LUA_ERRRUN, " PANIC" );
797
+ print_output (L, " " , LUA_ERRRUN, " PANIC" );
798
+ FATAL (" Lua panic" );
834
799
return 0 ;
835
800
}
836
801
837
802
void CScriptEngine::lua_error (lua_State* L)
838
803
{
839
- pcstr err = lua_tostring (L, -1 );
840
- onErrorCallback (L, " " , LUA_ERRRUN, err);
804
+ print_output (L, " " , LUA_ERRRUN);
805
+ on_error (L);
806
+
807
+ #if !XRAY_EXCEPTIONS
808
+ xrDebug::Fatal (DEBUG_INFO, " LUA error: %s" , lua_tostring (L, -1 ));
809
+ #else
810
+ throw lua_tostring (L, -1 );
811
+ #endif
841
812
}
842
813
843
814
int CScriptEngine::lua_pcall_failed (lua_State* L)
844
815
{
845
- const bool isString = lua_isstring (L, - 1 );
846
- const pcstr err = isString ? lua_tostring (L, - 1 ) : " " ;
816
+ print_output (L, " " , LUA_ERRRUN );
817
+ on_error (L) ;
847
818
848
- const bool result = onErrorCallback (L, " " , LUA_ERRRUN, err);
819
+ luabind::detail::stack_pop pop{ L, lua_isstring (L, -1 ) ? 1 : 0 };
820
+
821
+ if (xrDebug::WouldShowErrorMessage ())
822
+ {
823
+ const auto err = lua_tostring (L, -1 );
849
824
850
- if (isString)
851
- lua_pop (L, 1 );
825
+ static bool ignoreAlways;
826
+ const auto result = xrDebug::Fail (ignoreAlways, DEBUG_INFO, " LUA error " , err );
852
827
853
- if (result)
854
- return LUA_OK;
828
+ if (result == AssertionResult::tryAgain || result == AssertionResult::ignore)
829
+ return LUA_OK;
830
+ }
855
831
856
832
return LUA_ERRRUN;
857
833
}
858
- #if 1 // !XRAY_EXCEPTIONS
834
+
835
+ #if !XRAY_EXCEPTIONS
859
836
void CScriptEngine::lua_cast_failed (lua_State* L, const luabind::type_id& info)
860
837
{
861
838
string128 buf;
862
- xr_sprintf (buf, " LUA error: cannot cast lua value to %s" , info.name ());
863
- onErrorCallback (L, " " , LUA_ERRRUN, buf);
839
+ xr_sprintf (buf, " cannot cast lua value to %s" , info.name ());
840
+ print_output (L, " " , LUA_ERRRUN, buf);
841
+ xrDebug::Fatal (DEBUG_INFO, " LUA error: cannot cast lua value to %s" , info.name ());
864
842
}
865
843
#endif
866
844
@@ -898,8 +876,6 @@ void CScriptEngine::lua_hook_call(lua_State* L, lua_Debug* dbg)
898
876
#ifdef DEBUG
899
877
if (scriptEngine->current_thread ())
900
878
scriptEngine->current_thread ()->script_hook (L, dbg);
901
- else
902
- scriptEngine->m_stack_is_ready = true ;
903
879
#endif
904
880
905
881
if (scriptEngine->m_profiler )
@@ -1067,7 +1043,6 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace)
1067
1043
}
1068
1044
#endif
1069
1045
setup_auto_load ();
1070
- m_stack_is_ready = true ;
1071
1046
1072
1047
#if defined(DEBUG) && !defined(USE_LUA_STUDIO)
1073
1048
#if defined(USE_DEBUGGER)
@@ -1129,7 +1104,6 @@ bool CScriptEngine::process_file_if_exists(LPCSTR file_name, bool warn_if_not_ex
1129
1104
{
1130
1105
print_stack ();
1131
1106
Msg (" ! WARNING: Access to nonexistent variable '%s' or loading nonexistent script '%s'" , file_name, S1);
1132
- m_stack_is_ready = true ;
1133
1107
}
1134
1108
#endif
1135
1109
add_no_file (file_name, string_length);
0 commit comments