Fixed clang compilation and added support for making assertions and erros throw exceptions for easier testing.

This commit is contained in:
Patrick 2024-12-06 18:36:49 +01:00
parent 34adf3f006
commit f232728f69
6 changed files with 88 additions and 3 deletions

View File

@ -10,6 +10,11 @@
#include "../internal/common.hpp"
#if MIJIN_THROWING_ASSERTS
#include <sstream>
#include <stdexcept> // I'd prefer mijin Exceptions here, but that would result in a circual include :/
#endif
#ifdef _WIN32
#pragma comment(lib, "kernel32")
extern "C" __declspec(dllimport) void __stdcall DebugBreak();
@ -127,6 +132,49 @@ enum class ErrorHandling
ABORT = 2
};
#if MIJIN_THROWING_ASSERTS
namespace impl
{
inline std::string formatAssertionExceptionMessage(const char* condition, const char* message, const std::source_location& location)
{
std::ostringstream oss;
oss << "Assertion failed!\n"
<< "Condition: " << condition << "\n"
<< "Message: " << message << "\n"
<< "Location: " << location.file_name() << ":" << location.line() << ":" << location.column() << "\n"
<< "Function: " << location.function_name();
return oss.str();
}
inline std::string formatErrorExceptionMessage(const char* message, const std::source_location& location)
{
std::ostringstream oss;
oss << "Mijin error!\n"
<< "Message: " << message << "\n"
<< "Location: " << location.file_name() << ":" << location.line() << ":" << location.column() << "\n"
<< "Function: " << location.function_name();
return oss.str();
}
}
class AssertionException : public std::runtime_error
{
public:
AssertionException(const char* condition, const char* message, const std::source_location& location)
: std::runtime_error(impl::formatAssertionExceptionMessage(condition, message, location))
{
}
};
class ErrorException : public std::runtime_error
{
public:
ErrorException(const char* message, const std::source_location& location)
: std::runtime_error(impl::formatErrorExceptionMessage(message, location))
{
}
};
#endif
//
// public functions
//
@ -134,9 +182,14 @@ enum class ErrorHandling
#ifdef MIJIN_USE_CUSTOM_ASSERTION_HANDLER
AssertionResult handleAssert(const char* condition,
const char* message, const std::source_location& location);
#elif MIJIN_THROWING_ASSERTS
inline AssertionResult handleAssert(const char* condition, const char* message, const std::source_location& location)
{
throw AssertionException(condition, message, location);
}
#else
constexpr AssertionResult handleAssert(const char* /* condition */,
const char* /* message */, const std::source_location& /* location */)
constexpr AssertionResult handleAssert(const char* /* condition */, const char* /* message */,
const std::source_location& /* location */)
{
return AssertionResult::ERROR;
}
@ -147,11 +200,15 @@ ErrorHandling handleError(const char* message, const std::source_location& locat
#else
inline ErrorHandling handleError(const char* message, const std::source_location& location) MIJIN_NOEXCEPT
{
#if MIJIN_THROWING_ASSERTS
throw ErrorException(message, location);
#else
std::puts(message);
std::printf("Function: %s\n", location.function_name());
std::printf("Location: %s:%d:%d\n", location.file_name(), location.line(), location.column());
(void) std::fflush(stdout);
return ErrorHandling::TRAP;
#endif
}
#endif

View File

@ -8,6 +8,14 @@
#define MIJIN_WITH_EXCEPTIONS 0
#endif
#if !defined(MIJIN_THROWING_ASSERTS)
#define MIJIN_THROWING_ASSERTS 0
#endif
#if MIJIN_THROWING_ASSERTS && !defined(MIJIN_TEST_NO_NOEXCEPT)
#define MIJIN_TEST_NO_NOEXCEPT
#endif
#if MIJIN_WITH_EXCEPTIONS
#error "Maybe someday"
#else

View File

@ -14,6 +14,11 @@
#include <netdb.h>
#endif
#if MIJIN_COMPILER == MIJIN_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#endif // MIJIN_COMPILER == MIJIN_COMPILER_CLANG
namespace mijin
{
namespace
@ -289,3 +294,7 @@ Task<StreamResult<std::vector<ip_address_t>>> c_resolveHostname(std::string host
co_return osResolveResult(resolveHandle);
}
}
#if MIJIN_COMPILER == MIJIN_COMPILER_CLANG
#pragma clang diagnostic pop
#endif // MIJIN_COMPILER == MIJIN_COMPILER_CLANG

View File

@ -131,6 +131,7 @@ Task<StreamResult<HTTPResponse>> c_request(const URL& url, HTTPRequestOptions op
#undef HANDLE_CURL_RESULT
HTTPResponse response = {
.version = {},
.body = std::move(body)
};
if (const curl::Result<int> httpVersion = easy.getHTTPVersion(); httpVersion.isSuccess())

View File

@ -6,6 +6,11 @@
#include "../internal/common.hpp"
#include "../util/variant.hpp"
#if MIJIN_COMPILER == MIJIN_COMPILER_CLANG
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#endif // MIJIN_COMPILER == MIJIN_COMPILER_CLANG
namespace mijin
{
namespace
@ -472,3 +477,7 @@ Task<StreamResult<std::unique_ptr<Socket>>> TCPServerSocket::c_waitForConnection
co_return StreamError::CONNECTION_CLOSED;
}
}
#if MIJIN_COMPILER == MIJIN_COMPILER_CLANG
#pragma clang diagnostic pop
#endif // MIJIN_COMPILER == MIJIN_COMPILER_CLANG

View File

@ -13,6 +13,7 @@
#include "../container/map_view.hpp"
#include "../container/optional.hpp"
#include "../container/vector_map.hpp"
#include "../internal/common.hpp"
#include "../util/traits.hpp"
#include "../util/variant.hpp"
@ -39,7 +40,7 @@ struct ArrayValue
template<typename TScriptValue>
struct MapValue
{
std::unordered_map<std::string, TScriptValue> values;
VectorMap<std::string, TScriptValue> values;
auto operator<=>(const MapValue&) const MIJIN_NOEXCEPT = default;
};