From 2adc0c5a57deff6b624f86a3ebfd3944d1e18e8a Mon Sep 17 00:00:00 2001 From: Harry Rose Date: Sat, 13 Dec 2025 15:15:29 +0000 Subject: [PATCH 1/8] Add option for inkcpp to compile without exceptions --- CMakeLists.txt | 6 +++++- shared/public/config.h | 5 +++++ shared/public/system.h | 24 ++++++++++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cebfd9f1..698db212 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,8 @@ 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." OFF) if(INKCPP_NO_EH) add_definitions(-DINKCPP_NO_EH) @@ -70,7 +72,9 @@ 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/shared/public/config.h b/shared/public/config.h index e2d14ea4..b09212de 100644 --- a/shared/public/config.h +++ b/shared/public/config.h @@ -11,6 +11,7 @@ # 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 @@ -26,6 +27,10 @@ # 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..bc6eead8 100644 --- a/shared/public/system.h +++ b/shared/public/system.h @@ -15,7 +15,9 @@ # include "Hash/CityHash.h" #endif #ifdef INK_ENABLE_STL +#ifdef INK_ENABLE_EXCEPTIONS # include +#endif # include # include # include @@ -31,14 +33,18 @@ #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" #else # define inkZeroMemory ink::internal::zero_memory +# 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__) -# define FORMAT_STRING_STR "%s" #endif namespace ink @@ -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,9 +215,19 @@ 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...); +#ifdef INK_ENABLE_EXCEPTIONS throw ink_exception(message); +#else + fprintf(stderr, "Ink Assert: %s\n", message); + abort(); +#endif } else { +#ifdef INK_ENABLE_EXCEPTIONS throw ink_exception(msg); +#else + fprintf(stderr, "Ink Assert: %s\n", msg); + abort(); +#endif } } } From 320a5e60deb0445603cda7f848bc8cb7f6c91c3f Mon Sep 17 00:00:00 2001 From: Harry Rose Date: Sat, 13 Dec 2025 15:23:05 +0000 Subject: [PATCH 2/8] Run clang-format --- shared/public/system.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/shared/public/system.h b/shared/public/system.h index bc6eead8..3fbfc414 100644 --- a/shared/public/system.h +++ b/shared/public/system.h @@ -15,9 +15,9 @@ # include "Hash/CityHash.h" #endif #ifdef INK_ENABLE_STL -#ifdef INK_ENABLE_EXCEPTIONS -# include -#endif +# ifdef INK_ENABLE_EXCEPTIONS +# include +# endif # include # include # include @@ -32,8 +32,8 @@ // Platform specific defines // #ifdef INK_ENABLE_UNREAL -# define inkZeroMemory(buff, len) FMemory::Memset(buff, 0, len) -# 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 FORMAT_STRING_STR "%s" @@ -43,8 +43,8 @@ # 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__) +# define inkAssert ink::ink_assert +# define inkFail(...) ink::ink_assert(false, __VA_ARGS__) #endif namespace ink @@ -215,19 +215,19 @@ 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...); -#ifdef INK_ENABLE_EXCEPTIONS +# ifdef INK_ENABLE_EXCEPTIONS throw ink_exception(message); -#else +# else fprintf(stderr, "Ink Assert: %s\n", message); abort(); -#endif +# endif } else { -#ifdef INK_ENABLE_EXCEPTIONS +# ifdef INK_ENABLE_EXCEPTIONS throw ink_exception(msg); -#else +# else fprintf(stderr, "Ink Assert: %s\n", msg); abort(); -#endif +# endif } } } From 38506f20a809ea3f88d3827f6052ec6123493b3b Mon Sep 17 00:00:00 2001 From: Harry <4920526+harryr0se@users.noreply.github.com> Date: Sat, 13 Dec 2025 15:52:29 +0000 Subject: [PATCH 3/8] Fix throw in story_impl.cpp --- inkcpp/story_impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); From 92eb0c847ce624b1cda752f4351fd06349ba4a86 Mon Sep 17 00:00:00 2001 From: Harry <4920526+harryr0se@users.noreply.github.com> Date: Sat, 13 Dec 2025 15:53:27 +0000 Subject: [PATCH 4/8] Fix throws in snapshot_impl.cpp --- inkcpp/snapshot_impl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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()); } From 162dc161698f87a1180551d58cb0216700a8958c Mon Sep 17 00:00:00 2001 From: Harry Rose Date: Sun, 14 Dec 2025 13:16:32 +0000 Subject: [PATCH 5/8] Combine INK_ENABLE_EH and INK_ENABLE_EXCEPTIONS defines/options --- CMakeLists.txt | 7 +------ inkcpp/runner_impl.cpp | 4 ++-- shared/public/config.h | 5 ----- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 698db212..c12c50db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,15 +60,10 @@ 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." 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() 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/shared/public/config.h b/shared/public/config.h index b09212de..17c7f89a 100644 --- a/shared/public/config.h +++ b/shared/public/config.h @@ -9,7 +9,6 @@ // 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 @@ -19,10 +18,6 @@ # define INK_ENABLE_CSTD #endif -#ifndef INKCPP_NO_EH -# define INK_ENABLE_EH -#endif - #ifndef INKCPP_NO_RTTI # define INK_ENABLE_RTTI #endif From e0edd71aec3491a20bd510fc8e6e19b926562c33 Mon Sep 17 00:00:00 2001 From: Harry Rose Date: Sun, 14 Dec 2025 15:32:50 +0000 Subject: [PATCH 6/8] Wrap new fprintf, abort code in INK_ENABLE_CSTD --- shared/public/system.h | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/shared/public/system.h b/shared/public/system.h index 3fbfc414..98a0d464 100644 --- a/shared/public/system.h +++ b/shared/public/system.h @@ -215,20 +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...); + msg = message; + } + # ifdef INK_ENABLE_EXCEPTIONS - throw ink_exception(message); -# else - fprintf(stderr, "Ink Assert: %s\n", message); - abort(); -# endif - } else { -# ifdef INK_ENABLE_EXCEPTIONS - throw ink_exception(msg); -# else - fprintf(stderr, "Ink Assert: %s\n", msg); - abort(); + 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 - } } } From 87b1f20b353b7972ab60b378af2cca911a147f80 Mon Sep 17 00:00:00 2001 From: Harry Rose Date: Sun, 14 Dec 2025 15:39:58 +0000 Subject: [PATCH 7/8] Pick up one more throw in value.cpp --- inkcpp/value.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From 274f6cea4922fbedccc25247e5b82d455659584b Mon Sep 17 00:00:00 2001 From: Harry Rose Date: Sun, 14 Dec 2025 15:51:41 +0000 Subject: [PATCH 8/8] Formatting --- shared/public/system.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared/public/system.h b/shared/public/system.h index 98a0d464..2848e0a6 100644 --- a/shared/public/system.h +++ b/shared/public/system.h @@ -223,8 +223,8 @@ void ink_assert(bool condition, const char* msg = nullptr, Args... args) # 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" +# else +# error "This path needs a way to warn and then terminate, otherwise it'll silently fail" # endif } }