From 6160d4e6ac194be9722477fb96ccd3011669b0d7 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sun, 11 May 2025 06:41:08 +0200 Subject: [PATCH] Fixed static init issues with CheckErrorHandler --- BeefLibs/corlib/src/Console.bf | 13 +++++++++++-- BeefLibs/corlib/src/Runtime.bf | 27 ++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/BeefLibs/corlib/src/Console.bf b/BeefLibs/corlib/src/Console.bf index f5f8fd3c..d00c379c 100644 --- a/BeefLibs/corlib/src/Console.bf +++ b/BeefLibs/corlib/src/Console.bf @@ -85,10 +85,19 @@ namespace System static void SetupOutStringEx() { - OutString = => OutString_Ex; + sOutString = => OutString_Ex; } - static function void(StringView str) OutString = => OutString_Simple; + static function void(StringView str) sOutString; + static function void(StringView str) OutString + { + get + { + if (sOutString == null) + sOutString = => OutString_Simple; + return sOutString; + } + } #if !BF_RUNTIME_DISABLE && !BF_PLATFORM_WASM private static extern void PutChars(char8* c, int32 len); diff --git a/BeefLibs/corlib/src/Runtime.bf b/BeefLibs/corlib/src/Runtime.bf index 9582395b..f140ed5e 100644 --- a/BeefLibs/corlib/src/Runtime.bf +++ b/BeefLibs/corlib/src/Runtime.bf @@ -14,7 +14,6 @@ namespace System public bool AVX, AVX2, AVX512; } - [StaticInitPriority(201)] static class Runtime { const int32 cVersion = 10; @@ -284,7 +283,7 @@ namespace System mDebugMessageData_SetupProfilerCmd = => DebugMessageData_SetupProfilerCmd; mDebugMessageData_Fatal = => DebugMessageData_Fatal; mDebugMessageData_Clear = => DebugMessageData_Clear; - mCheckErrorHandler = => CheckErrorHandler; + mCheckErrorHandler = => CheckErrorHandler_Thunk; } }; @@ -389,13 +388,23 @@ namespace System public static bool sInsideErrorHandler; } + + [AlwaysInclude, StaticInitPriority(201)] + static struct RuntimeInit + { + public static this() + { + Runtime.Init(); + } + } + static RtFlags sExtraFlags; static bool sQueriedFeatures = false; static RuntimeFeatures sFeatures; static function void() sThreadInit; - public static this() + static void Init() { #if !BF_RUNTIME_DISABLE BfRtCallbacks.sCallbacks.Init(); @@ -422,6 +431,11 @@ namespace System #endif } + public static this() + { + + } + [NoReturn] public static void FatalError(String msg = "Fatal error encountered", String filePath = Compiler.CallerFilePath, int line = Compiler.CallerLineNum) { @@ -497,6 +511,13 @@ namespace System public static function int32(char8* kind, char8* arg1, char8* arg2, int arg3) CheckErrorHandler; public static function void*(char8* filePath) LibraryLoadCallback; + public static int32 CheckErrorHandler_Thunk(char8* kind, char8* arg1, char8* arg2, int arg3) + { + if (CheckErrorHandler != null) + return CheckErrorHandler(kind, arg1, arg2, arg3); + return 0; + } + static ErrorHandlerResult CheckAssertError_Impl(AssertError.Kind kind, String error, String filePath, int lineNum) { return CheckErrorHandlers(scope AssertError(kind, error, filePath, lineNum));