diff --git a/CMakeLists.txt b/CMakeLists.txt index cebfd9f1..c12c50db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,17 +60,16 @@ set(INKCPP_INKLECATE CACHE STRING "If inklecate should be downloaded automatically from the official release page. \ NONE -> No, OS -> Yes, but only for the current OS, ALL -> Yes, for all availible OSs") set_property(CACHE INKCPP_INKLECATE PROPERTY STRINGS "NONE" "OS" "ALL") -option(INKCPP_NO_EH "Disable try/catch in runtime. Used to build without error handling." OFF) option(INKCPP_NO_RTTI "Disable real time type information depended code. Used to build without RTTI." OFF) +option(INKCPP_NO_EXCEPTIONS "Used to build without support for exceptions, disables try/catch blocks and throws" OFF) -if(INKCPP_NO_EH) - add_definitions(-DINKCPP_NO_EH) -endif() if(INKCPP_NO_RTTI) add_definitions(-DINKCPP_NO_RTTI) endif() - +if(INKCPP_NO_EXCEPTIONS) + add_definitions(-DINKCPP_NO_EXCEPTIONS) +endif() string(TOUPPER "${INKCPP_INKLECATE}" inkcpp_inklecate_upper) if(inkcpp_inklecate_upper STREQUAL "ALL") FetchContent_MakeAvailable(inklecate_windows inklecate_mac inklecate_linux) diff --git a/inkcpp/runner_impl.cpp b/inkcpp/runner_impl.cpp index 05be3fd7..c9f05bbc 100644 --- a/inkcpp/runner_impl.cpp +++ b/inkcpp/runner_impl.cpp @@ -868,7 +868,7 @@ bool runner_impl::line_step() void runner_impl::step() { -#ifdef INK_ENABLE_EH +#ifdef INK_ENABLE_EXCEPTIONS try #endif { @@ -1500,7 +1500,7 @@ void runner_impl::step() } #endif } -#ifdef INK_ENABLE_EH +#ifdef INK_ENABLE_EXCEPTIONS catch (...) { // Reset our whole state as it's probably corrupt reset(); diff --git a/inkcpp/snapshot_impl.cpp b/inkcpp/snapshot_impl.cpp index f18bc9c5..d9c44c03 100644 --- a/inkcpp/snapshot_impl.cpp +++ b/inkcpp/snapshot_impl.cpp @@ -27,7 +27,7 @@ snapshot* snapshot::from_file(const char* filename) { std::ifstream ifs(filename, std::ios::binary | std::ios::ate); if (! ifs.is_open()) { - throw ink_exception("Failed to open snapshot file: " + std::string(filename)); + ink_assert(false, "Failed to open snapshot file: %s", filename); } size_t length = static_cast(ifs.tellg()); @@ -43,7 +43,7 @@ void snapshot::write_to_file(const char* filename) const { std::ofstream ofs(filename, std::ios::binary); if (! ofs.is_open()) { - throw ink_exception("Failed to open file to write snapshot: " + std::string(filename)); + ink_assert(false, "Failed to open file to write snapshot: %s", filename); } ofs.write(reinterpret_cast(get_data()), get_data_len()); } diff --git a/inkcpp/story_impl.cpp b/inkcpp/story_impl.cpp index f072d95a..6a292c8d 100644 --- a/inkcpp/story_impl.cpp +++ b/inkcpp/story_impl.cpp @@ -36,7 +36,7 @@ unsigned char* read_file_into_memory(const char* filename, size_t* read) ifstream ifs(filename, ios::binary | ios::ate); if (! ifs.is_open()) { - throw ink_exception("Failed to open file: " + std::string(filename)); + ink_assert(false, "Failed to open file: %s", filename); } ifstream::pos_type pos = ifs.tellg(); diff --git a/inkcpp/value.cpp b/inkcpp/value.cpp index d16c4452..a40efffb 100644 --- a/inkcpp/value.cpp +++ b/inkcpp/value.cpp @@ -173,7 +173,7 @@ void append(std::ostream& os, const value& val, const list_ std::ostream& value::write(std::ostream& os, const list_table* lists) const { if (type() < value_type::PRINT_BEGIN || type() >= value_type::PRINT_END) { - throw ink_exception("printing this type is not supported"); + ink_assert(false, "printing this type is not supported"); } append(os, *this, lists); return os; diff --git a/shared/public/config.h b/shared/public/config.h index e2d14ea4..17c7f89a 100644 --- a/shared/public/config.h +++ b/shared/public/config.h @@ -9,8 +9,8 @@ // The UE build process will define INKCPP_API #ifdef INKCPP_API # define INK_ENABLE_UNREAL -# define INKCPP_NO_EH # define INKCPP_NO_RTTI +# define INKCPP_NO_EXCEPTIONS #elif INKCPP_BUILD_CLIB # define INK_ENABLE_CSTD #else @@ -18,14 +18,14 @@ # define INK_ENABLE_CSTD #endif -#ifndef INKCPP_NO_EH -# define INK_ENABLE_EH -#endif - #ifndef INKCPP_NO_RTTI # define INK_ENABLE_RTTI #endif +#ifndef INKCPP_NO_EXCEPTIONS +# define INK_ENABLE_EXCEPTIONS +#endif + // Only turn on if you have json.hpp and you want to use it with the compiler // #define INK_EXPOSE_JSON diff --git a/shared/public/system.h b/shared/public/system.h index 25040751..2848e0a6 100644 --- a/shared/public/system.h +++ b/shared/public/system.h @@ -15,7 +15,9 @@ # include "Hash/CityHash.h" #endif #ifdef INK_ENABLE_STL -# include +# ifdef INK_ENABLE_EXCEPTIONS +# include +# endif # include # include # include @@ -30,17 +32,21 @@ // Platform specific defines // #ifdef INK_ENABLE_UNREAL -# define inkZeroMemory(buff, len) FMemory::Memset(buff, 0, len) -# define inkAssert(condition, text, ...) checkf(condition, TEXT(text), ##__VA_ARGS__) -# define inkFail(text, ...) checkf(false, TEXT(text), ##__VA_ARGS__) -# define FORMAT_STRING_STR "%hs" +# define inkZeroMemory(buff, len) FMemory::Memset(buff, 0, len) +# define FORMAT_STRING_STR "%hs" #else # define inkZeroMemory ink::internal::zero_memory -# define inkAssert ink::ink_assert -# define inkFail(...) ink::ink_assert(false, __VA_ARGS__) # define FORMAT_STRING_STR "%s" #endif +#ifdef INK_ENABLE_UNREAL +# define inkAssert(condition, text, ...) checkf(condition, TEXT(text), ##__VA_ARGS__) +# define inkFail(text, ...) checkf(false, TEXT(text), ##__VA_ARGS__) +#else +# define inkAssert ink::ink_assert +# define inkFail(...) ink::ink_assert(false, __VA_ARGS__) +#endif + namespace ink { /** define basic numeric type @@ -175,7 +181,7 @@ namespace internal #endif } // namespace internal -#ifdef INK_ENABLE_STL +#ifdef INK_ENABLE_EXCEPTIONS /** exception type thrown if something goes wrong */ using ink_exception = std::runtime_error; #else @@ -209,10 +215,17 @@ void ink_assert(bool condition, const char* msg = nullptr, Args... args) size_t size = snprintf(nullptr, 0, msg, args...) + 1; char* message = static_cast(malloc(size)); snprintf(message, size, msg, args...); - throw ink_exception(message); - } else { - throw ink_exception(msg); + msg = message; } + +# ifdef INK_ENABLE_EXCEPTIONS + throw ink_exception(msg); +# elif defined(INK_ENABLE_CSTD) + fprintf(stderr, "Ink Assert: %s\n", msg); + abort(); +# else +# error "This path needs a way to warn and then terminate, otherwise it'll silently fail" +# endif } }