2 ** error.c - Exception class
4 ** See Copyright Notice in mruby.h
11 #include <mruby/array.h>
12 #include <mruby/irep.h>
13 #include <mruby/proc.h>
14 #include <mruby/string.h>
15 #include <mruby/variable.h>
16 #include <mruby/error.h>
17 #include <mruby/class.h>
18 #include <mruby/throw.h>
21 mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, size_t len)
23 mrb_value arg = mrb_str_new(mrb, ptr, len);
24 return mrb_obj_new(mrb, c, 1, &arg);
28 mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str)
31 return mrb_obj_new(mrb, c, 1, &str);
36 * Exception.new(msg = nil) -> exception
38 * Construct a new Exception object, optionally passing in
43 exc_initialize(mrb_state *mrb, mrb_value exc)
47 if (mrb_get_args(mrb, "|o", &mesg) == 1) {
48 mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), mesg);
54 * Document-method: exception
57 * exc.exception(string) -> an_exception or exc
59 * With no argument, or if the argument is the same as the receiver,
60 * return the receiver. Otherwise, create a new
61 * exception object of the same class as the receiver, but with a
62 * message equal to <code>string</code>.
67 exc_exception(mrb_state *mrb, mrb_value self)
73 argc = mrb_get_args(mrb, "|o", &a);
74 if (argc == 0) return self;
75 if (mrb_obj_equal(mrb, self, a)) return self;
76 exc = mrb_obj_clone(mrb, self);
77 mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), a);
84 * exception.to_s -> string
86 * Returns exception's message (or the name of the exception if
91 exc_to_s(mrb_state *mrb, mrb_value exc)
93 mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
96 if (!mrb_string_p(mesg)) {
97 return mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, exc));
99 p = mrb_obj_ptr(mesg);
101 p->c = mrb->string_class;
108 * exception.message -> string
110 * Returns the result of invoking <code>exception.to_s</code>.
111 * Normally this returns the exception's message or name.
115 exc_message(mrb_state *mrb, mrb_value exc)
117 return mrb_funcall(mrb, exc, "to_s", 0);
122 * exception.inspect -> string
124 * Returns this exception's file name, line number,
125 * message and class name.
126 * If file name or line number is not set,
127 * returns message and class name.
131 mrb_exc_inspect(mrb_state *mrb, mrb_value exc)
133 mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
134 mrb_value cname = mrb_mod_to_s(mrb, mrb_obj_value(mrb_obj_class(mrb, exc)));
135 mesg = mrb_obj_as_string(mrb, mesg);
136 return RSTRING_LEN(mesg) == 0 ? cname : mrb_format(mrb, "%v (%v)", mesg, cname);
139 void mrb_keep_backtrace(mrb_state *mrb, mrb_value exc);
142 set_backtrace(mrb_state *mrb, mrb_value exc, mrb_value backtrace)
144 if (!mrb_array_p(backtrace)) {
146 mrb_raise(mrb, E_TYPE_ERROR, "backtrace must be Array of String");
149 const mrb_value *p = RARRAY_PTR(backtrace);
150 const mrb_value *pend = p + RARRAY_LEN(backtrace);
153 if (!mrb_string_p(*p)) goto type_err;
157 mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "backtrace"), backtrace);
161 exc_set_backtrace(mrb_state *mrb, mrb_value exc)
163 mrb_value backtrace = mrb_get_arg1(mrb);
165 set_backtrace(mrb, exc, backtrace);
170 mrb_exc_set(mrb_state *mrb, mrb_value exc)
172 if (mrb_nil_p(exc)) {
176 mrb->exc = mrb_obj_ptr(exc);
177 if (mrb->gc.arena_idx > 0 &&
178 (struct RBasic*)mrb->exc == mrb->gc.arena[mrb->gc.arena_idx-1]) {
181 if (!mrb->gc.out_of_memory && !mrb_frozen_p(mrb->exc)) {
182 mrb_keep_backtrace(mrb, exc);
187 static mrb_noreturn void
188 exc_throw(mrb_state *mrb, mrb_value exc)
197 MRB_API mrb_noreturn void
198 mrb_exc_raise(mrb_state *mrb, mrb_value exc)
200 if (mrb_break_p(exc)) {
201 mrb->exc = mrb_obj_ptr(exc);
204 if (!mrb_obj_is_kind_of(mrb, exc, mrb->eException_class)) {
205 mrb_raise(mrb, E_TYPE_ERROR, "exception object expected");
207 mrb_exc_set(mrb, exc);
212 MRB_API mrb_noreturn void
213 mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg)
215 mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mrb_str_new_cstr(mrb, msg)));
219 * <code>vsprintf</code> like formatting.
221 * The syntax of a format sequence is as follows.
223 * %[modifier]specifier
227 * ----------+------------------------------------------------------------
229 * ----------+------------------------------------------------------------
230 * ! | Convert to string by corresponding `inspect` instead of
231 * | corresponding `to_s`.
232 * ----------+------------------------------------------------------------
234 * The specifiers are:
236 * ----------+----------------+--------------------------------------------
237 * Specifier | Argument Type | Note
238 * ----------+----------------+--------------------------------------------
243 * l | char*, size_t | Arguments are string and length.
245 * s | char* | Argument is NUL terminated string.
246 * t | mrb_value | Convert to type (class) of object.
248 * C | struct RClass* |
249 * T | mrb_value | Convert to real type (class) of object.
250 * Y | mrb_value | Same as `!v` if argument is `true`, `false`
251 * | | or `nil`, otherwise same as `T`.
252 * % | - | Convert to percent sign itself (no argument
254 * ----------+----------------+--------------------------------------------
257 mrb_vformat(mrb_state *mrb, const char *format, va_list ap)
259 const char *chars, *p = format, *b = format, *e;
264 mrb_bool inspect = FALSE;
265 mrb_value result = mrb_str_new_capa(mrb, 128), obj, str;
266 int ai = mrb_gc_arena_save(mrb);
279 ch = (char)va_arg(ap, int);
284 #if MRB_INT_MAX < INT_MAX
285 i = (mrb_int)va_arg(ap, int);
287 i = *p == 'd' ? (mrb_int)va_arg(ap, int) : va_arg(ap, mrb_int);
289 obj = mrb_fixnum_value(i);
291 #ifndef MRB_WITHOUT_FLOAT
293 obj = mrb_float_value(mrb, (mrb_float)va_arg(ap, double));
297 chars = va_arg(ap, char*);
298 len = va_arg(ap, size_t);
301 obj = mrb_str_new(mrb, chars, len);
304 mrb_str_cat(mrb, result, b, e - b - 1);
305 mrb_str_cat(mrb, result, chars, len);
307 mrb_gc_arena_restore(mrb, ai);
310 #if UINT32_MAX < INT_MAX
311 obj = mrb_symbol_value((mrb_sym)va_arg(ap, int));
313 obj = mrb_symbol_value(va_arg(ap, mrb_sym));
317 chars = va_arg(ap, char*);
321 cls = mrb_class(mrb, va_arg(ap, mrb_value));
324 obj = va_arg(ap, mrb_value);
326 str = (inspect ? mrb_inspect : mrb_obj_as_string)(mrb, obj);
327 chars = RSTRING_PTR(str);
328 len = RSTRING_LEN(str);
332 cls = va_arg(ap, struct RClass*);
334 obj = mrb_obj_value(cls);
337 obj = va_arg(ap, mrb_value);
339 cls = mrb_obj_class(mrb, obj);
342 obj = va_arg(ap, mrb_value);
343 if (!mrb_test(obj) || mrb_true_p(obj)) {
348 goto L_cat_real_class_of;
356 mrb_raisef(mrb, E_ARGUMENT_ERROR, "malformed format string - %%%c", *p);
359 else if (c == '\\') {
366 mrb_str_cat(mrb, result, b, p - b);
371 mrb_format(mrb_state *mrb, const char *format, ...)
376 va_start(ap, format);
377 str = mrb_vformat(mrb, format, ap);
383 static mrb_noreturn void
384 raise_va(mrb_state *mrb, struct RClass *c, const char *fmt, va_list ap, int argc, mrb_value *argv)
388 mesg = mrb_vformat(mrb, fmt, ap);
395 mrb_exc_raise(mrb, mrb_obj_new(mrb, c, argc+1, argv));
398 MRB_API mrb_noreturn void
399 mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...)
404 raise_va(mrb, c, fmt, args, 0, NULL);
408 MRB_API mrb_noreturn void
409 mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...)
415 argv[1] = mrb_symbol_value(id);
416 raise_va(mrb, E_NAME_ERROR, fmt, args, 1, argv);
421 mrb_warn(mrb_state *mrb, const char *fmt, ...)
423 #ifndef MRB_DISABLE_STDIO
428 str = mrb_vformat(mrb, fmt, ap);
429 fputs("warning: ", stderr);
430 fwrite(RSTRING_PTR(str), RSTRING_LEN(str), 1, stderr);
436 MRB_API mrb_noreturn void
437 mrb_bug(mrb_state *mrb, const char *fmt, ...)
439 #ifndef MRB_DISABLE_STDIO
444 str = mrb_vformat(mrb, fmt, ap);
445 fputs("bug: ", stderr);
446 fwrite(RSTRING_PTR(str), RSTRING_LEN(str), 1, stderr);
453 mrb_make_exception(mrb_state *mrb, mrb_int argc, const mrb_value *argv)
458 mesg = mrb_nil_value();
463 if (mrb_nil_p(argv[0]))
465 if (mrb_string_p(argv[0])) {
466 mesg = mrb_exc_new_str(mrb, E_RUNTIME_ERROR, argv[0]);
477 mrb_sym exc = mrb_intern_lit(mrb, "exception");
478 if (mrb_respond_to(mrb, argv[0], exc)) {
479 mesg = mrb_funcall_argv(mrb, argv[0], exc, n, argv+1);
483 mrb_raise(mrb, E_TYPE_ERROR, "exception class/object expected");
489 mrb_argnum_error(mrb, argc, 0, 3);
493 if (!mrb_obj_is_kind_of(mrb, mesg, mrb->eException_class))
494 mrb_raise(mrb, mrb->eException_class, "exception object expected");
496 set_backtrace(mrb, mesg, argv[2]);
503 mrb_sys_fail(mrb_state *mrb, const char *mesg)
509 if (mrb_class_defined(mrb, "SystemCallError")) {
510 sce = mrb_class_get(mrb, "SystemCallError");
512 mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 2, mrb_fixnum_value(no), mrb_str_new_cstr(mrb, mesg));
515 mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 1, mrb_fixnum_value(no));
519 mrb_raise(mrb, E_RUNTIME_ERROR, mesg);
523 MRB_API mrb_noreturn void
524 mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, char const* fmt, ...)
531 argv[0] = mrb_vformat(mrb, fmt, ap);
532 argv[1] = mrb_symbol_value(id);
535 exc = mrb_obj_new(mrb, E_NOMETHOD_ERROR, 3, argv);
536 mrb_exc_raise(mrb, exc);
539 MRB_API mrb_noreturn void
540 mrb_frozen_error(mrb_state *mrb, void *frozen_obj)
542 mrb_raisef(mrb, E_FROZEN_ERROR, "can't modify frozen %t", mrb_obj_value(frozen_obj));
545 MRB_API mrb_noreturn void
546 mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max)
548 #define FMT(exp) "wrong number of arguments (given %i, expected " exp ")"
550 mrb_raisef(mrb, E_ARGUMENT_ERROR, FMT("%d"), argc, min);
552 mrb_raisef(mrb, E_ARGUMENT_ERROR, FMT("%d+"), argc, min);
554 mrb_raisef(mrb, E_ARGUMENT_ERROR, FMT("%d..%d"), argc, min, max);
558 void mrb_core_init_printabort(void);
561 mrb_core_init_protect(mrb_state *mrb, void (*body)(mrb_state *, void *), void *opaque)
563 struct mrb_jmpbuf *prev_jmp = mrb->jmp;
564 struct mrb_jmpbuf c_jmp;
571 } MRB_CATCH(&c_jmp) {
573 mrb_p(mrb, mrb_obj_value(mrb->exc));
577 mrb_core_init_printabort();
579 } MRB_END_EXC(&c_jmp);
587 mrb_core_init_abort(mrb_state *mrb)
590 exc_throw(mrb, mrb_nil_value());
594 mrb_raise_nomemory(mrb_state *mrb)
596 if (mrb->nomem_err) {
597 mrb_exc_raise(mrb, mrb_obj_value(mrb->nomem_err));
600 mrb_core_init_abort(mrb);
605 mrb_init_exception(mrb_state *mrb)
607 struct RClass *exception, *script_error, *stack_error, *nomem_error;
609 mrb->eException_class = exception = mrb_define_class(mrb, "Exception", mrb->object_class); /* 15.2.22 */
610 MRB_SET_INSTANCE_TT(exception, MRB_TT_EXCEPTION);
611 mrb_define_class_method(mrb, exception, "exception", mrb_instance_new, MRB_ARGS_OPT(1));
612 mrb_define_method(mrb, exception, "exception", exc_exception, MRB_ARGS_OPT(1));
613 mrb_define_method(mrb, exception, "initialize", exc_initialize, MRB_ARGS_OPT(1));
614 mrb_define_method(mrb, exception, "to_s", exc_to_s, MRB_ARGS_NONE());
615 mrb_define_method(mrb, exception, "message", exc_message, MRB_ARGS_NONE());
616 mrb_define_method(mrb, exception, "inspect", mrb_exc_inspect, MRB_ARGS_NONE());
617 mrb_define_method(mrb, exception, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE());
618 mrb_define_method(mrb, exception, "set_backtrace", exc_set_backtrace, MRB_ARGS_REQ(1));
620 mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */
621 mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */
622 script_error = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */
623 mrb_define_class(mrb, "SyntaxError", script_error); /* 15.2.38 */
624 stack_error = mrb_define_class(mrb, "SystemStackError", exception);
625 mrb->stack_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, stack_error, "stack level too deep"));
627 nomem_error = mrb_define_class(mrb, "NoMemoryError", exception);
628 mrb->nomem_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, nomem_error, "Out of memory"));
629 #ifdef MRB_GC_FIXED_ARENA
630 mrb->arena_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, nomem_error, "arena overflow error"));