Skip to content

Commit

Permalink
refactor _d_print_throwable() (#16666)
Browse files Browse the repository at this point in the history
  • Loading branch information
0-v-0 authored Jul 6, 2024
1 parent 265a77b commit 4699dd2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
27 changes: 11 additions & 16 deletions druntime/src/rt/dmain2.d
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,15 @@ extern (C) void _d_print_throwable(Throwable t)
void sink(in char[] s) scope nothrow
{
if (!s.length) return;
int swlen = MultiByteToWideChar(
const swlen = MultiByteToWideChar(
CP_UTF8, 0, s.ptr, cast(int)s.length, null, 0);
if (!swlen) return;

auto newPtr = cast(WCHAR*)realloc(ptr,
(this.len + swlen + 1) * WCHAR.sizeof);
if (!newPtr) return;
ptr = newPtr;
auto written = MultiByteToWideChar(
const written = MultiByteToWideChar(
CP_UTF8, 0, s.ptr, cast(int)s.length, ptr+len, swlen);
len += written;
}
Expand All @@ -622,10 +622,6 @@ extern (C) void _d_print_throwable(Throwable t)
return _fdToHandle(fd);
}

auto hStdErr = windowsHandle(fileno(stderr));
CONSOLE_SCREEN_BUFFER_INFO sbi;
bool isConsole = GetConsoleScreenBufferInfo(hStdErr, &sbi) != 0;

// ensure the exception is shown at the beginning of the line, while also
// checking whether stderr is a valid file
int written = fprintf(stderr, "\n");
Expand All @@ -642,33 +638,32 @@ extern (C) void _d_print_throwable(Throwable t)

// Avoid static user32.dll dependency for console applications
// by loading it dynamically as needed
auto user32 = LoadLibraryW("user32.dll");
if (user32)
if (auto user32 = LoadLibraryW("user32.dll"))
{
alias typeof(&MessageBoxW) PMessageBoxW;
auto pMessageBoxW = cast(PMessageBoxW)
GetProcAddress(user32, "MessageBoxW");
if (pMessageBoxW)
if (auto pMessageBoxW = cast(PMessageBoxW) GetProcAddress(user32, "MessageBoxW"))
pMessageBoxW(null, buf.get(), caption.get(), MB_ICONERROR);
FreeLibrary(user32);
}
FreeLibrary(user32);
caption.free();
buf.free();
}
return;
}
else if (isConsole)
auto hStdErr = windowsHandle(fileno(stderr));
CONSOLE_SCREEN_BUFFER_INFO sbi = void;
const isConsole = GetConsoleScreenBufferInfo(hStdErr, &sbi) != 0;
if (isConsole)
{
WSink buf;
formatThrowable(t, &buf.sink);

if (buf.ptr)
{
uint codepage = GetConsoleOutputCP();
int slen = WideCharToMultiByte(codepage, 0,
const slen = WideCharToMultiByte(codepage, 0,
buf.ptr, cast(int)buf.len, null, 0, null, null);
auto sptr = cast(char*)malloc(slen * char.sizeof);
if (sptr)
if (auto sptr = cast(char*)malloc(slen * char.sizeof))
{
WideCharToMultiByte(codepage, 0,
buf.ptr, cast(int)buf.len, sptr, slen, null, null);
Expand Down
8 changes: 3 additions & 5 deletions druntime/src/rt/sections_win64.d
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ version (Shared)
auto size = tlsdir.EndAddressOfRawData - tlsdir.StartAddressOfRawData + tlsdir.SizeOfZeroFill;

if (conservative)
dg( beg, beg + size);
dg(beg, beg + size);
else
scanTLSPrecise(cast(uint*)&sec._tpSection[0], cast(uint*)&sec._tpSection[$], beg, dg);
}
Expand Down Expand Up @@ -435,8 +435,7 @@ void* initLibrary(void* mod)
// (What? LoadLibrary() is a Windows API call, it shouldn't call rt_init().)
if (mod is null)
return mod;
gcSetFn gcSet = cast(gcSetFn) GetProcAddress(mod, "gc_setProxy");
if (gcSet !is null)
if (auto gcSet = cast(gcSetFn) GetProcAddress(mod, "gc_setProxy"))
{ // BUG: Set proxy, but too late
gcSet(gc_getProxy());
}
Expand All @@ -453,8 +452,7 @@ void* initLibrary(void* mod)
*/
extern (C) int rt_unloadLibrary(void* ptr)
{
gcClrFn gcClr = cast(gcClrFn) GetProcAddress(ptr, "gc_clrProxy");
if (gcClr !is null)
if (auto gcClr = cast(gcClrFn) GetProcAddress(ptr, "gc_clrProxy"))
gcClr();
return FreeLibrary(ptr) != 0;
}
Expand Down

0 comments on commit 4699dd2

Please sign in to comment.