From 703e5d5be1766bad68a8ad89c57880c8c06b2f4a Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Wed, 4 Mar 2026 20:19:15 +0100 Subject: [PATCH] feat: add thespian_exit to the C API --- include/thespian/c/instance.h | 2 ++ src/c/instance.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/thespian/c/instance.h b/include/thespian/c/instance.h index 29e5977..8e48239 100644 --- a/include/thespian/c/instance.h +++ b/include/thespian/c/instance.h @@ -16,6 +16,8 @@ typedef void (*thespian_receiver_dtor)(thespian_behaviour_state); void thespian_receive(thespian_receiver, thespian_behaviour_state, thespian_receiver_dtor); +thespian_result thespian_exit(const char *status); + bool thespian_get_trap(); bool thespian_set_trap(bool); void thespian_link(thespian_handle); diff --git a/src/c/instance.cpp b/src/c/instance.cpp index 5007f58..a8b058b 100644 --- a/src/c/instance.cpp +++ b/src/c/instance.cpp @@ -33,6 +33,20 @@ void thespian_receive(thespian_receiver r, thespian_behaviour_state s, auto thespian_get_trap() -> bool { return thespian::trap(); } auto thespian_set_trap(bool on) -> bool { return thespian::trap(on); } +namespace { +thread_local cbor::buffer exit_msg_buf; // NOLINT +thread_local thespian_error exit_error_buf; // NOLINT +} // namespace + +auto thespian_exit(const char *status) -> thespian_result { + auto r = thespian::exit(status); + if (r) + return nullptr; + exit_msg_buf = r.error(); + exit_error_buf = {exit_msg_buf.data(), exit_msg_buf.size()}; + return &exit_error_buf; +} + void thespian_link(thespian_handle h) { thespian::handle *h_{ reinterpret_cast( // NOLINT(*-reinterpret-cast)