Skip to content

Commit 763a119

Browse files
authored
Add option for inkcpp to compile without exceptions (#136)
This PR separates that out into its own `INKCPP_NO_EXCEPTIONS` define with a path that prints to `stderr` and then calls `abort()`
1 parent ea71901 commit 763a119

File tree

7 files changed

+39
-27
lines changed

7 files changed

+39
-27
lines changed

CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,16 @@ set(INKCPP_INKLECATE
6060
CACHE STRING "If inklecate should be downloaded automatically from the official release page. \
6161
NONE -> No, OS -> Yes, but only for the current OS, ALL -> Yes, for all availible OSs")
6262
set_property(CACHE INKCPP_INKLECATE PROPERTY STRINGS "NONE" "OS" "ALL")
63-
option(INKCPP_NO_EH "Disable try/catch in runtime. Used to build without error handling." OFF)
6463
option(INKCPP_NO_RTTI
6564
"Disable real time type information depended code. Used to build without RTTI." OFF)
65+
option(INKCPP_NO_EXCEPTIONS "Used to build without support for exceptions, disables try/catch blocks and throws" OFF)
6666

67-
if(INKCPP_NO_EH)
68-
add_definitions(-DINKCPP_NO_EH)
69-
endif()
7067
if(INKCPP_NO_RTTI)
7168
add_definitions(-DINKCPP_NO_RTTI)
7269
endif()
73-
70+
if(INKCPP_NO_EXCEPTIONS)
71+
add_definitions(-DINKCPP_NO_EXCEPTIONS)
72+
endif()
7473
string(TOUPPER "${INKCPP_INKLECATE}" inkcpp_inklecate_upper)
7574
if(inkcpp_inklecate_upper STREQUAL "ALL")
7675
FetchContent_MakeAvailable(inklecate_windows inklecate_mac inklecate_linux)

inkcpp/runner_impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ bool runner_impl::line_step()
868868

869869
void runner_impl::step()
870870
{
871-
#ifdef INK_ENABLE_EH
871+
#ifdef INK_ENABLE_EXCEPTIONS
872872
try
873873
#endif
874874
{
@@ -1500,7 +1500,7 @@ void runner_impl::step()
15001500
}
15011501
#endif
15021502
}
1503-
#ifdef INK_ENABLE_EH
1503+
#ifdef INK_ENABLE_EXCEPTIONS
15041504
catch (...) {
15051505
// Reset our whole state as it's probably corrupt
15061506
reset();

inkcpp/snapshot_impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ snapshot* snapshot::from_file(const char* filename)
2727
{
2828
std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
2929
if (! ifs.is_open()) {
30-
throw ink_exception("Failed to open snapshot file: " + std::string(filename));
30+
ink_assert(false, "Failed to open snapshot file: %s", filename);
3131
}
3232

3333
size_t length = static_cast<size_t>(ifs.tellg());
@@ -43,7 +43,7 @@ void snapshot::write_to_file(const char* filename) const
4343
{
4444
std::ofstream ofs(filename, std::ios::binary);
4545
if (! ofs.is_open()) {
46-
throw ink_exception("Failed to open file to write snapshot: " + std::string(filename));
46+
ink_assert(false, "Failed to open file to write snapshot: %s", filename);
4747
}
4848
ofs.write(reinterpret_cast<const char*>(get_data()), get_data_len());
4949
}

inkcpp/story_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ unsigned char* read_file_into_memory(const char* filename, size_t* read)
3636
ifstream ifs(filename, ios::binary | ios::ate);
3737

3838
if (! ifs.is_open()) {
39-
throw ink_exception("Failed to open file: " + std::string(filename));
39+
ink_assert(false, "Failed to open file: %s", filename);
4040
}
4141

4242
ifstream::pos_type pos = ifs.tellg();

inkcpp/value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void append<value_type::boolean>(std::ostream& os, const value& val, const list_
173173
std::ostream& value::write(std::ostream& os, const list_table* lists) const
174174
{
175175
if (type() < value_type::PRINT_BEGIN || type() >= value_type::PRINT_END) {
176-
throw ink_exception("printing this type is not supported");
176+
ink_assert(false, "printing this type is not supported");
177177
}
178178
append(os, *this, lists);
179179
return os;

shared/public/config.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@
99
// The UE build process will define INKCPP_API
1010
#ifdef INKCPP_API
1111
# define INK_ENABLE_UNREAL
12-
# define INKCPP_NO_EH
1312
# define INKCPP_NO_RTTI
13+
# define INKCPP_NO_EXCEPTIONS
1414
#elif INKCPP_BUILD_CLIB
1515
# define INK_ENABLE_CSTD
1616
#else
1717
# define INK_ENABLE_STL
1818
# define INK_ENABLE_CSTD
1919
#endif
2020

21-
#ifndef INKCPP_NO_EH
22-
# define INK_ENABLE_EH
23-
#endif
24-
2521
#ifndef INKCPP_NO_RTTI
2622
# define INK_ENABLE_RTTI
2723
#endif
2824

25+
#ifndef INKCPP_NO_EXCEPTIONS
26+
# define INK_ENABLE_EXCEPTIONS
27+
#endif
28+
2929
// Only turn on if you have json.hpp and you want to use it with the compiler
3030
// #define INK_EXPOSE_JSON
3131

shared/public/system.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
# include "Hash/CityHash.h"
1616
#endif
1717
#ifdef INK_ENABLE_STL
18-
# include <exception>
18+
# ifdef INK_ENABLE_EXCEPTIONS
19+
# include <exception>
20+
# endif
1921
# include <stdexcept>
2022
# include <optional>
2123
# include <cctype>
@@ -30,17 +32,21 @@
3032
// Platform specific defines //
3133

3234
#ifdef INK_ENABLE_UNREAL
33-
# define inkZeroMemory(buff, len) FMemory::Memset(buff, 0, len)
34-
# define inkAssert(condition, text, ...) checkf(condition, TEXT(text), ##__VA_ARGS__)
35-
# define inkFail(text, ...) checkf(false, TEXT(text), ##__VA_ARGS__)
36-
# define FORMAT_STRING_STR "%hs"
35+
# define inkZeroMemory(buff, len) FMemory::Memset(buff, 0, len)
36+
# define FORMAT_STRING_STR "%hs"
3737
#else
3838
# define inkZeroMemory ink::internal::zero_memory
39-
# define inkAssert ink::ink_assert
40-
# define inkFail(...) ink::ink_assert(false, __VA_ARGS__)
4139
# define FORMAT_STRING_STR "%s"
4240
#endif
4341

42+
#ifdef INK_ENABLE_UNREAL
43+
# define inkAssert(condition, text, ...) checkf(condition, TEXT(text), ##__VA_ARGS__)
44+
# define inkFail(text, ...) checkf(false, TEXT(text), ##__VA_ARGS__)
45+
#else
46+
# define inkAssert ink::ink_assert
47+
# define inkFail(...) ink::ink_assert(false, __VA_ARGS__)
48+
#endif
49+
4450
namespace ink
4551
{
4652
/** define basic numeric type
@@ -175,7 +181,7 @@ namespace internal
175181
#endif
176182
} // namespace internal
177183

178-
#ifdef INK_ENABLE_STL
184+
#ifdef INK_ENABLE_EXCEPTIONS
179185
/** exception type thrown if something goes wrong */
180186
using ink_exception = std::runtime_error;
181187
#else
@@ -209,10 +215,17 @@ void ink_assert(bool condition, const char* msg = nullptr, Args... args)
209215
size_t size = snprintf(nullptr, 0, msg, args...) + 1;
210216
char* message = static_cast<char*>(malloc(size));
211217
snprintf(message, size, msg, args...);
212-
throw ink_exception(message);
213-
} else {
214-
throw ink_exception(msg);
218+
msg = message;
215219
}
220+
221+
# ifdef INK_ENABLE_EXCEPTIONS
222+
throw ink_exception(msg);
223+
# elif defined(INK_ENABLE_CSTD)
224+
fprintf(stderr, "Ink Assert: %s\n", msg);
225+
abort();
226+
# else
227+
# error "This path needs a way to warn and then terminate, otherwise it'll silently fail"
228+
# endif
216229
}
217230
}
218231

0 commit comments

Comments
 (0)