From 8c8bbd94dce3b3eabcf72c674e690516c075b0ee Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 3 Feb 2023 13:41:10 +0900 Subject: [PATCH] error.c: add new error handling API functions; ref #2837 - mrb_clear_error(): clear error status of mrb_state - mrb_check_error(): check if error caused in the previous API Note that `mrb_check_error` clears error status, so if you call `mrb_check_error` more than once, latter calls will return FALSE. --- include/mruby/error.h | 9 +++++++++ src/error.c | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/mruby/error.h b/include/mruby/error.h index ccf2cdb6e..16e9305cc 100644 --- a/include/mruby/error.h +++ b/include/mruby/error.h @@ -74,6 +74,15 @@ mrb_break_value_set(struct RBreak *brk, mrb_value val) #define mrb_break_proc_get(brk) ((brk)->proc) #define mrb_break_proc_set(brk, p) ((brk)->proc = p) +/** + * Error check + * + */ +/* clear error status in the mrb_state structure */ +MRB_API void mrb_clear_error(mrb_state *mrb); +/* returns TRUE if error in the previous call; internally calls mrb_clear_error() */ +MRB_API mrb_bool mrb_check_error(mrb_state *mrb); + /** * Protect * diff --git a/src/error.c b/src/error.c index a0e23fad4..1e74f6c46 100644 --- a/src/error.c +++ b/src/error.c @@ -649,6 +649,24 @@ mrb_print_error(mrb_state *mrb) #endif } +/* clear error status in the mrb_state structure */ +MRB_API void +mrb_clear_error(mrb_state *mrb) +{ + mrb->exc = NULL; +} + +/* returns TRUE if error in the previous call; internally calls mrb_clear_error() */ +MRB_API mrb_bool +mrb_check_error(mrb_state *mrb) +{ + if (mrb->exc) { + mrb_clear_error(mrb); + return TRUE; + } + return FALSE; +} + void mrb_init_exception(mrb_state *mrb) {