mrb_open: return mrb_state with exc set on init failure

changed mrb_open() and mrb_open_core() to return mrb_state with mrb->exc
set (instead of NULL) when initialization fails. this allows callers to
programmatically inspect error details, which is essential for embedded
systems without stderr. return NULL only for true allocation failure.

added MRB_OPEN_SUCCESS(mrb) macro to check initialization success, since
mrb != NULL no longer guarantees success. updated all binary tools
(mruby, mirb, mrdb, mrbtest) to use new pattern: check MRB_OPEN_SUCCESS,
print exception details via mrb_print_error if available, then mrb_close.

mrb_core_init_protect now preserves exception in mrb->exc instead of
printing and clearing it, giving caller control over error handling.

breaking change: callers must use MRB_OPEN_SUCCESS(mrb) or check both
mrb != NULL && mrb->exc == NULL. old NULL-only checks will miss
initialization failures.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-11-13 19:10:46 +09:00
parent 92640097ab
commit 05ffe0c441
7 changed files with 71 additions and 21 deletions
+23
View File
@@ -1270,6 +1270,29 @@ MRB_API mrb_state* mrb_open_core(void);
MRB_API void mrb_close(mrb_state *mrb);
MRB_API void mrb_method_cache_clear(mrb_state *mrb);
/**
* Check if mrb_open() succeeded
*
* @param mrb
* Pointer returned from mrb_open() or mrb_open_core().
* @return
* Non-zero if initialization succeeded, 0 if failed.
* @note
* mrb_open() may return non-NULL even on failure (with mrb->exc set).
* Use this macro to check for success:
* @code
* mrb_state *mrb = mrb_open();
* if (!MRB_OPEN_SUCCESS(mrb)) {
* if (mrb) {
* // Inspect mrb->exc for error details
* mrb_close(mrb);
* }
* return EXIT_FAILURE;
* }
* @endcode
*/
#define MRB_OPEN_SUCCESS(mrb) ((mrb) && !(mrb)->exc)
/**
* The memory allocation function. You can redefine this function for your own allocator.
*