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/debug.h>
17 #include <mruby/error.h>
18 #include <mruby/class.h>
19 #include <mruby/throw.h>
22 mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, size_t len)
24 mrb_value arg = mrb_str_new(mrb, ptr, len);
25 return mrb_obj_new(mrb, c, 1, &arg);
29 mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str)
32 return mrb_obj_new(mrb, c, 1, &str);
37 * Exception.new(msg = nil) -> exception
39 * Construct a new Exception object, optionally passing in
44 exc_initialize(mrb_state *mrb, mrb_value exc)
50 if (mrb_get_args(mrb, "|o*!", &mesg, &argv, &argc) >= 1) {
51 mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), mesg);
57 * Document-method: exception
60 * exc.exception(string) -> an_exception or exc
62 * With no argument, or if the argument is the same as the receiver,
63 * return the receiver. Otherwise, create a new
64 * exception object of the same class as the receiver, but with a
65 * message equal to <code>string</code>.
70 exc_exception(mrb_state *mrb, mrb_value self)
76 argc = mrb_get_args(mrb, "|o", &a);
77 if (argc == 0) return self;
78 if (mrb_obj_equal(mrb, self, a)) return self;
79 exc = mrb_obj_clone(mrb, self);
80 mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), a);
87 * exception.to_s -> string
89 * Returns exception's message (or the name of the exception if
94 exc_to_s(mrb_state *mrb, mrb_value exc)
96 mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
99 if (!mrb_string_p(mesg)) {
100 return mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, exc));
102 p = mrb_obj_ptr(mesg);
104 p->c = mrb->string_class;
111 * exception.message -> string
113 * Returns the result of invoking <code>exception.to_s</code>.
114 * Normally this returns the exception's message or name.
118 exc_message(mrb_state *mrb, mrb_value exc)
120 return mrb_funcall(mrb, exc, "to_s", 0);
125 * exception.inspect -> string
127 * Returns this exception's file name, line number,
128 * message and class name.
129 * If file name or line number is not set,
130 * returns message and class name.
134 exc_inspect(mrb_state *mrb, mrb_value exc)
136 mrb_value str, mesg, file, line;
137 mrb_bool append_mesg;
140 mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
141 file = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "file"));
142 line = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "line"));
144 append_mesg = !mrb_nil_p(mesg);
146 mesg = mrb_obj_as_string(mrb, mesg);
147 append_mesg = RSTRING_LEN(mesg) > 0;
150 cname = mrb_obj_classname(mrb, exc);
151 str = mrb_str_new_cstr(mrb, cname);
152 if (mrb_string_p(file) && mrb_fixnum_p(line)) {
154 str = mrb_format(mrb, "%S:%S: %S (%S)", file, line, mesg, str);
157 str = mrb_format(mrb, "%S:%S: %S", file, line, str);
160 else if (append_mesg) {
161 str = mrb_format(mrb, "%S: %S", str, mesg);
166 void mrb_keep_backtrace(mrb_state *mrb, mrb_value exc);
169 set_backtrace(mrb_state *mrb, mrb_value exc, mrb_value backtrace)
171 if (!mrb_array_p(backtrace)) {
173 mrb_raise(mrb, E_TYPE_ERROR, "backtrace must be Array of String");
176 const mrb_value *p = RARRAY_PTR(backtrace);
177 const mrb_value *pend = p + RARRAY_LEN(backtrace);
180 if (!mrb_string_p(*p)) goto type_err;
184 mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "backtrace"), backtrace);
188 exc_set_backtrace(mrb_state *mrb, mrb_value exc)
192 mrb_get_args(mrb, "o", &backtrace);
193 set_backtrace(mrb, exc, backtrace);
198 exc_debug_info(mrb_state *mrb, struct RObject *exc)
200 mrb_callinfo *ci = mrb->c->ci;
201 mrb_code *pc = ci->pc;
203 if (mrb_obj_iv_defined(mrb, exc, mrb_intern_lit(mrb, "file"))) return;
204 while (ci >= mrb->c->cibase) {
205 mrb_code *err = ci->err;
207 if (!err && pc) err = pc - 1;
208 if (err && ci->proc && !MRB_PROC_CFUNC_P(ci->proc)) {
209 mrb_irep *irep = ci->proc->body.irep;
211 int32_t const line = mrb_debug_get_line(mrb, irep, err - irep->iseq);
212 char const* file = mrb_debug_get_filename(mrb, irep, err - irep->iseq);
213 if (line != -1 && file) {
214 mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "file"), mrb_str_new_cstr(mrb, file));
215 mrb_obj_iv_set(mrb, exc, mrb_intern_lit(mrb, "line"), mrb_fixnum_value(line));
225 mrb_exc_set(mrb_state *mrb, mrb_value exc)
227 if (mrb_nil_p(exc)) {
231 mrb->exc = mrb_obj_ptr(exc);
232 if (mrb->gc.arena_idx > 0 &&
233 (struct RBasic*)mrb->exc == mrb->gc.arena[mrb->gc.arena_idx-1]) {
236 if (!mrb->gc.out_of_memory && !MRB_FROZEN_P(mrb->exc)) {
237 exc_debug_info(mrb, mrb->exc);
238 mrb_keep_backtrace(mrb, exc);
243 MRB_API mrb_noreturn void
244 mrb_exc_raise(mrb_state *mrb, mrb_value exc)
246 if (!mrb_obj_is_kind_of(mrb, exc, mrb->eException_class)) {
247 mrb_raise(mrb, E_TYPE_ERROR, "exception object expected");
249 mrb_exc_set(mrb, exc);
257 MRB_API mrb_noreturn void
258 mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg)
260 mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mrb_str_new_cstr(mrb, msg)));
264 mrb_vformat(mrb_state *mrb, const char *format, va_list ap)
266 const char *p = format;
269 int ai0 = mrb_gc_arena_save(mrb);
270 mrb_value ary = mrb_ary_new_capa(mrb, 4);
271 int ai = mrb_gc_arena_save(mrb);
281 mrb_ary_push(mrb, ary, mrb_str_new(mrb, b, size));
282 val = va_arg(ap, mrb_value);
283 mrb_ary_push(mrb, ary, mrb_obj_as_string(mrb, val));
287 else if (c == '\\') {
290 mrb_ary_push(mrb, ary, mrb_str_new(mrb, b, size));
291 mrb_ary_push(mrb, ary, mrb_str_new(mrb, p, 1));
298 mrb_gc_arena_restore(mrb, ai);
301 mrb_gc_arena_restore(mrb, ai0);
302 return mrb_str_new_cstr(mrb, format);
309 mrb_ary_push(mrb, ary, mrb_str_new(mrb, b, size));
311 val = mrb_ary_join(mrb, ary, mrb_nil_value());
312 mrb_gc_arena_restore(mrb, ai0);
313 mrb_gc_protect(mrb, val);
319 mrb_format(mrb_state *mrb, const char *format, ...)
324 va_start(ap, format);
325 str = mrb_vformat(mrb, format, ap);
331 static mrb_noreturn void
332 raise_va(mrb_state *mrb, struct RClass *c, const char *fmt, va_list ap, int argc, mrb_value *argv)
336 mesg = mrb_vformat(mrb, fmt, ap);
343 mrb_exc_raise(mrb, mrb_obj_new(mrb, c, argc+1, argv));
346 MRB_API mrb_noreturn void
347 mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...)
352 raise_va(mrb, c, fmt, args, 0, NULL);
356 MRB_API mrb_noreturn void
357 mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...)
363 argv[1] = mrb_symbol_value(id);
364 raise_va(mrb, E_NAME_ERROR, fmt, args, 1, argv);
369 mrb_warn(mrb_state *mrb, const char *fmt, ...)
371 #ifndef MRB_DISABLE_STDIO
376 str = mrb_vformat(mrb, fmt, ap);
377 fputs("warning: ", stderr);
378 fwrite(RSTRING_PTR(str), RSTRING_LEN(str), 1, stderr);
384 MRB_API mrb_noreturn void
385 mrb_bug(mrb_state *mrb, const char *fmt, ...)
387 #ifndef MRB_DISABLE_STDIO
392 str = mrb_vformat(mrb, fmt, ap);
393 fputs("bug: ", stderr);
394 fwrite(RSTRING_PTR(str), RSTRING_LEN(str), 1, stderr);
401 mrb_make_exception(mrb_state *mrb, mrb_int argc, const mrb_value *argv)
406 mesg = mrb_nil_value();
411 if (mrb_nil_p(argv[0]))
413 if (mrb_string_p(argv[0])) {
414 mesg = mrb_exc_new_str(mrb, E_RUNTIME_ERROR, argv[0]);
425 mrb_sym exc = mrb_intern_lit(mrb, "exception");
426 if (mrb_respond_to(mrb, argv[0], exc)) {
427 mesg = mrb_funcall_argv(mrb, argv[0], exc, n, argv+1);
431 mrb_raise(mrb, E_TYPE_ERROR, "exception class/object expected");
437 mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 0..3)", mrb_fixnum_value(argc));
441 if (!mrb_obj_is_kind_of(mrb, mesg, mrb->eException_class))
442 mrb_raise(mrb, mrb->eException_class, "exception object expected");
444 set_backtrace(mrb, mesg, argv[2]);
451 mrb_sys_fail(mrb_state *mrb, const char *mesg)
457 if (mrb_class_defined(mrb, "SystemCallError")) {
458 sce = mrb_class_get(mrb, "SystemCallError");
460 mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 2, mrb_fixnum_value(no), mrb_str_new_cstr(mrb, mesg));
463 mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 1, mrb_fixnum_value(no));
467 mrb_raise(mrb, E_RUNTIME_ERROR, mesg);
471 MRB_API mrb_noreturn void
472 mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, char const* fmt, ...)
479 argv[0] = mrb_vformat(mrb, fmt, ap);
480 argv[1] = mrb_symbol_value(id);
483 exc = mrb_obj_new(mrb, E_NOMETHOD_ERROR, 3, argv);
484 mrb_exc_raise(mrb, exc);
488 mrb_init_exception(mrb_state *mrb)
490 struct RClass *exception, *script_error, *stack_error, *nomem_error;
492 mrb->eException_class = exception = mrb_define_class(mrb, "Exception", mrb->object_class); /* 15.2.22 */
493 MRB_SET_INSTANCE_TT(exception, MRB_TT_EXCEPTION);
494 mrb_define_class_method(mrb, exception, "exception", mrb_instance_new, MRB_ARGS_ANY());
495 mrb_define_method(mrb, exception, "exception", exc_exception, MRB_ARGS_ANY());
496 mrb_define_method(mrb, exception, "initialize", exc_initialize, MRB_ARGS_ANY());
497 mrb_define_method(mrb, exception, "to_s", exc_to_s, MRB_ARGS_NONE());
498 mrb_define_method(mrb, exception, "message", exc_message, MRB_ARGS_NONE());
499 mrb_define_method(mrb, exception, "inspect", exc_inspect, MRB_ARGS_NONE());
500 mrb_define_method(mrb, exception, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE());
501 mrb_define_method(mrb, exception, "set_backtrace", exc_set_backtrace, MRB_ARGS_REQ(1));
503 mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */
504 mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */
505 script_error = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */
506 mrb_define_class(mrb, "SyntaxError", script_error); /* 15.2.38 */
507 stack_error = mrb_define_class(mrb, "SystemStackError", exception);
508 mrb->stack_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, stack_error, "stack level too deep"));
510 nomem_error = mrb_define_class(mrb, "NoMemoryError", exception);
511 mrb->nomem_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, nomem_error, "Out of memory"));
512 #ifdef MRB_GC_FIXED_ARENA
513 mrb->arena_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, nomem_error, "arena overflow error"));