Repository: LadybirdBrowser/ladybird
File: AK/Assertions.cpp (C++)

Code before:
    return true;
#elif defined(AK_OS_WINDOWS)
    HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
    if (hStdErr == INVALID_HANDLE_VALUE) {
        return false;
    }
    DWORD dwMode = 0;
    if (!GetConsoleMode(hStdErr, &dwMode)) {
        return false;
    }
    DWORD mask = ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;

    return (dwMode & mask) == mask;
#else
    return isatty(STDERR_FILENO) == 1;
#endif
}

NEVER_INLINE void ak_trap(void)
{
#if defined(AK_HAS_BACKTRACE_HEADER) || defined(AK_HAS_CPPTRACE)
    // Skip 3 frames to get to caller. That is dump_backtrace, ak_trap, and ak_verification_failed.
    dump_backtrace(3, 100);
#endif
    __builtin_trap();
}

#ifndef AK_OS_WINDOWS
[[gnu::weak]] void ak_assertion_handler(char const* message);
#endif

using AssertionHandlerFunc = void (*)(char const*);
static AssertionHandlerFunc get_custom_assertion_handler()
{
#ifndef AK_OS_WINDOWS
    return ak_assertion_handler;
#else
    // Windows doesn't support weak symbols as nicely as ELF platforms.
    // Instead, rely on the fact that we only want this to be overridden from
    // the main executable, and grab it from there if present.
    if (HMODULE module = GetModuleHandle(nullptr)) {
#    pragma clang diagnostic push
#    pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
        auto handler = reinterpret_cast<AssertionHandlerFunc>(GetProcAddress(module, "ak_assertion_handler"));
#    pragma clang diagnostic pop
        return handler;
    }
    return nullptr;

#endif
}

Review comment (refactor):
You remove this `FreeLibrary()` call in this commit, but don't mention it in the commit message. This should probably be a separate commit with the message you put in your PR description.

Code after:
        return false;
    }
    DWORD dwMode = 0;
    if (!GetConsoleMode(hStdErr, &dwMode)) {
        return false;
    }
    DWORD mask = ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;

    return (dwMode & mask) == mask;
#else
    return isatty(STDERR_FILENO) == 1;
#endif
}

NEVER_INLINE void ak_trap(void)
{
#if defined(AK_HAS_BACKTRACE_HEADER) || defined(AK_HAS_CPPTRACE)
    // Skip 3 frames to get to caller. That is dump_backtrace, ak_trap, and ak_verification_failed.
    dump_backtrace(3, 100);
#endif
    __builtin_trap();
}

#ifndef AK_OS_WINDOWS
[[gnu::weak]] void ak_assertion_handler(char const* message);
#endif

using AssertionHandlerFunc = void (*)(char const*);
static AssertionHandlerFunc get_custom_assertion_handler()
{
#ifndef AK_OS_WINDOWS
    return ak_assertion_handler;
#else
    // Windows doesn't support weak symbols as nicely as ELF platforms.
    // Instead, rely on the fact that we only want this to be overridden from
    // the main executable, and grab it from there if present.
    if (HMODULE module = GetModuleHandle(nullptr)) {
#    pragma clang diagnostic push
#    pragma clang diagnostic ignored "-Wcast-function-type-mismatch"
        auto handler = reinterpret_cast<AssertionHandlerFunc>(GetProcAddress(module, "ak_assertion_handler"));
#    pragma clang diagnostic pop
        return handler;
    }
    return nullptr;

#endif
}

void ak_verification_failed(char const* message)
{
    if (auto assertion_handler = get_custom_assertion_handler()) {