1 | /*
|
---|
2 | ** error.c - Exception class
|
---|
3 | **
|
---|
4 | ** See Copyright Notice in mruby.h
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <errno.h>
|
---|
8 | #include <stdarg.h>
|
---|
9 | #include <stdlib.h>
|
---|
10 | #include <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>
|
---|
19 |
|
---|
20 | MRB_API mrb_value
|
---|
21 | mrb_exc_new(mrb_state *mrb, struct RClass *c, const char *ptr, size_t len)
|
---|
22 | {
|
---|
23 | mrb_value arg = mrb_str_new(mrb, ptr, len);
|
---|
24 | return mrb_obj_new(mrb, c, 1, &arg);
|
---|
25 | }
|
---|
26 |
|
---|
27 | MRB_API mrb_value
|
---|
28 | mrb_exc_new_str(mrb_state *mrb, struct RClass* c, mrb_value str)
|
---|
29 | {
|
---|
30 | mrb_to_str(mrb, str);
|
---|
31 | return mrb_obj_new(mrb, c, 1, &str);
|
---|
32 | }
|
---|
33 |
|
---|
34 | /*
|
---|
35 | * call-seq:
|
---|
36 | * Exception.new(msg = nil) -> exception
|
---|
37 | *
|
---|
38 | * Construct a new Exception object, optionally passing in
|
---|
39 | * a message.
|
---|
40 | */
|
---|
41 |
|
---|
42 | static mrb_value
|
---|
43 | exc_initialize(mrb_state *mrb, mrb_value exc)
|
---|
44 | {
|
---|
45 | mrb_value mesg;
|
---|
46 |
|
---|
47 | if (mrb_get_args(mrb, "|o", &mesg) == 1) {
|
---|
48 | mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "mesg"), mesg);
|
---|
49 | }
|
---|
50 | return exc;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Document-method: exception
|
---|
55 | *
|
---|
56 | * call-seq:
|
---|
57 | * exc.exception(string) -> an_exception or exc
|
---|
58 | *
|
---|
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>.
|
---|
63 | *
|
---|
64 | */
|
---|
65 |
|
---|
66 | static mrb_value
|
---|
67 | exc_exception(mrb_state *mrb, mrb_value self)
|
---|
68 | {
|
---|
69 | mrb_value exc;
|
---|
70 | mrb_value a;
|
---|
71 | mrb_int argc;
|
---|
72 |
|
---|
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);
|
---|
78 |
|
---|
79 | return exc;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * call-seq:
|
---|
84 | * exception.to_s -> string
|
---|
85 | *
|
---|
86 | * Returns exception's message (or the name of the exception if
|
---|
87 | * no message is set).
|
---|
88 | */
|
---|
89 |
|
---|
90 | mrb_value
|
---|
91 | exc_to_s(mrb_state *mrb, mrb_value exc)
|
---|
92 | {
|
---|
93 | mrb_value mesg = mrb_attr_get(mrb, exc, mrb_intern_lit(mrb, "mesg"));
|
---|
94 | struct RObject *p;
|
---|
95 |
|
---|
96 | if (!mrb_string_p(mesg)) {
|
---|
97 | return mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, exc));
|
---|
98 | }
|
---|
99 | p = mrb_obj_ptr(mesg);
|
---|
100 | if (!p->c) {
|
---|
101 | p->c = mrb->string_class;
|
---|
102 | }
|
---|
103 | return mesg;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * call-seq:
|
---|
108 | * exception.message -> string
|
---|
109 | *
|
---|
110 | * Returns the result of invoking <code>exception.to_s</code>.
|
---|
111 | * Normally this returns the exception's message or name.
|
---|
112 | */
|
---|
113 |
|
---|
114 | static mrb_value
|
---|
115 | exc_message(mrb_state *mrb, mrb_value exc)
|
---|
116 | {
|
---|
117 | return mrb_funcall(mrb, exc, "to_s", 0);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * call-seq:
|
---|
122 | * exception.inspect -> string
|
---|
123 | *
|
---|
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.
|
---|
128 | */
|
---|
129 |
|
---|
130 | mrb_value
|
---|
131 | mrb_exc_inspect(mrb_state *mrb, mrb_value exc)
|
---|
132 | {
|
---|
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);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void mrb_keep_backtrace(mrb_state *mrb, mrb_value exc);
|
---|
140 |
|
---|
141 | static void
|
---|
142 | set_backtrace(mrb_state *mrb, mrb_value exc, mrb_value backtrace)
|
---|
143 | {
|
---|
144 | if (!mrb_array_p(backtrace)) {
|
---|
145 | type_err:
|
---|
146 | mrb_raise(mrb, E_TYPE_ERROR, "backtrace must be Array of String");
|
---|
147 | }
|
---|
148 | else {
|
---|
149 | const mrb_value *p = RARRAY_PTR(backtrace);
|
---|
150 | const mrb_value *pend = p + RARRAY_LEN(backtrace);
|
---|
151 |
|
---|
152 | while (p < pend) {
|
---|
153 | if (!mrb_string_p(*p)) goto type_err;
|
---|
154 | p++;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "backtrace"), backtrace);
|
---|
158 | }
|
---|
159 |
|
---|
160 | static mrb_value
|
---|
161 | exc_set_backtrace(mrb_state *mrb, mrb_value exc)
|
---|
162 | {
|
---|
163 | mrb_value backtrace;
|
---|
164 |
|
---|
165 | mrb_get_args(mrb, "o", &backtrace);
|
---|
166 | set_backtrace(mrb, exc, backtrace);
|
---|
167 | return backtrace;
|
---|
168 | }
|
---|
169 |
|
---|
170 | void
|
---|
171 | mrb_exc_set(mrb_state *mrb, mrb_value exc)
|
---|
172 | {
|
---|
173 | if (mrb_nil_p(exc)) {
|
---|
174 | mrb->exc = 0;
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | mrb->exc = mrb_obj_ptr(exc);
|
---|
178 | if (mrb->gc.arena_idx > 0 &&
|
---|
179 | (struct RBasic*)mrb->exc == mrb->gc.arena[mrb->gc.arena_idx-1]) {
|
---|
180 | mrb->gc.arena_idx--;
|
---|
181 | }
|
---|
182 | if (!mrb->gc.out_of_memory && !mrb_frozen_p(mrb->exc)) {
|
---|
183 | mrb_keep_backtrace(mrb, exc);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | MRB_API mrb_noreturn void
|
---|
189 | mrb_exc_raise(mrb_state *mrb, mrb_value exc)
|
---|
190 | {
|
---|
191 | if (mrb_break_p(exc)) {
|
---|
192 | mrb->exc = mrb_obj_ptr(exc);
|
---|
193 | }
|
---|
194 | else {
|
---|
195 | if (!mrb_obj_is_kind_of(mrb, exc, mrb->eException_class)) {
|
---|
196 | mrb_raise(mrb, E_TYPE_ERROR, "exception object expected");
|
---|
197 | }
|
---|
198 | mrb_exc_set(mrb, exc);
|
---|
199 | }
|
---|
200 | if (!mrb->jmp) {
|
---|
201 | mrb_p(mrb, exc);
|
---|
202 | abort();
|
---|
203 | }
|
---|
204 | MRB_THROW(mrb->jmp);
|
---|
205 | }
|
---|
206 |
|
---|
207 | MRB_API mrb_noreturn void
|
---|
208 | mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg)
|
---|
209 | {
|
---|
210 | mrb_exc_raise(mrb, mrb_exc_new_str(mrb, c, mrb_str_new_cstr(mrb, msg)));
|
---|
211 | }
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * <code>vsprintf</code> like formatting.
|
---|
215 | *
|
---|
216 | * The syntax of a format sequence is as follows.
|
---|
217 | *
|
---|
218 | * %[modifier]specifier
|
---|
219 | *
|
---|
220 | * The modifiers are:
|
---|
221 | *
|
---|
222 | * ----------+------------------------------------------------------------
|
---|
223 | * Modifier | Meaning
|
---|
224 | * ----------+------------------------------------------------------------
|
---|
225 | * ! | Convert to string by corresponding `inspect` instead of
|
---|
226 | * | corresponding `to_s`.
|
---|
227 | * ----------+------------------------------------------------------------
|
---|
228 | *
|
---|
229 | * The specifiers are:
|
---|
230 | *
|
---|
231 | * ----------+----------------+--------------------------------------------
|
---|
232 | * Specifier | Argument Type | Note
|
---|
233 | * ----------+----------------+--------------------------------------------
|
---|
234 | * c | char |
|
---|
235 | * d | int |
|
---|
236 | * f | mrb_float |
|
---|
237 | * i | mrb_int |
|
---|
238 | * l | char*, size_t | Arguments are string and length.
|
---|
239 | * n | mrb_sym |
|
---|
240 | * s | char* | Argument is NUL terminated string.
|
---|
241 | * t | mrb_value | Convert to type (class) of object.
|
---|
242 | * v,S | mrb_value |
|
---|
243 | * C | struct RClass* |
|
---|
244 | * T | mrb_value | Convert to real type (class) of object.
|
---|
245 | * Y | mrb_value | Same as `!v` if argument is `true`, `false`
|
---|
246 | * | | or `nil`, otherwise same as `T`.
|
---|
247 | * % | - | Convert to percent sign itself (no argument
|
---|
248 | * | | taken).
|
---|
249 | * ----------+----------------+--------------------------------------------
|
---|
250 | */
|
---|
251 | MRB_API mrb_value
|
---|
252 | mrb_vformat(mrb_state *mrb, const char *format, va_list ap)
|
---|
253 | {
|
---|
254 | const char *chars, *p = format, *b = format, *e;
|
---|
255 | char ch;
|
---|
256 | size_t len;
|
---|
257 | mrb_int i;
|
---|
258 | struct RClass *cls;
|
---|
259 | mrb_bool inspect = FALSE;
|
---|
260 | mrb_value result = mrb_str_new_capa(mrb, 128), obj, str;
|
---|
261 | int ai = mrb_gc_arena_save(mrb);
|
---|
262 |
|
---|
263 | while (*p) {
|
---|
264 | const char c = *p++;
|
---|
265 | e = p;
|
---|
266 | if (c == '%') {
|
---|
267 | if (*p == '!') {
|
---|
268 | inspect = TRUE;
|
---|
269 | ++p;
|
---|
270 | }
|
---|
271 | if (!*p) break;
|
---|
272 | switch (*p) {
|
---|
273 | case 'c':
|
---|
274 | ch = (char)va_arg(ap, int);
|
---|
275 | chars = &ch;
|
---|
276 | len = 1;
|
---|
277 | goto L_cat;
|
---|
278 | case 'd': case 'i':
|
---|
279 | #if MRB_INT_MAX < INT_MAX
|
---|
280 | i = (mrb_int)va_arg(ap, int);
|
---|
281 | #else
|
---|
282 | i = *p == 'd' ? (mrb_int)va_arg(ap, int) : va_arg(ap, mrb_int);
|
---|
283 | #endif
|
---|
284 | obj = mrb_fixnum_value(i);
|
---|
285 | goto L_cat_obj;
|
---|
286 | #ifndef MRB_WITHOUT_FLOAT
|
---|
287 | case 'f':
|
---|
288 | obj = mrb_float_value(mrb, (mrb_float)va_arg(ap, double));
|
---|
289 | goto L_cat_obj;
|
---|
290 | #endif
|
---|
291 | case 'l':
|
---|
292 | chars = va_arg(ap, char*);
|
---|
293 | len = va_arg(ap, size_t);
|
---|
294 | L_cat:
|
---|
295 | if (inspect) {
|
---|
296 | obj = mrb_str_new(mrb, chars, len);
|
---|
297 | goto L_cat_obj;
|
---|
298 | }
|
---|
299 | mrb_str_cat(mrb, result, b, e - b - 1);
|
---|
300 | mrb_str_cat(mrb, result, chars, len);
|
---|
301 | b = ++p;
|
---|
302 | mrb_gc_arena_restore(mrb, ai);
|
---|
303 | break;
|
---|
304 | case 'n':
|
---|
305 | #if UINT32_MAX < INT_MAX
|
---|
306 | obj = mrb_symbol_value((mrb_sym)va_arg(ap, int));
|
---|
307 | #else
|
---|
308 | obj = mrb_symbol_value(va_arg(ap, mrb_sym));
|
---|
309 | #endif
|
---|
310 | goto L_cat_obj;
|
---|
311 | case 's':
|
---|
312 | chars = va_arg(ap, char*);
|
---|
313 | len = strlen(chars);
|
---|
314 | goto L_cat;
|
---|
315 | case 't':
|
---|
316 | cls = mrb_class(mrb, va_arg(ap, mrb_value));
|
---|
317 | goto L_cat_class;
|
---|
318 | case 'v': case 'S':
|
---|
319 | obj = va_arg(ap, mrb_value);
|
---|
320 | L_cat_obj:
|
---|
321 | str = (inspect ? mrb_inspect : mrb_obj_as_string)(mrb, obj);
|
---|
322 | chars = RSTRING_PTR(str);
|
---|
323 | len = RSTRING_LEN(str);
|
---|
324 | inspect = FALSE;
|
---|
325 | goto L_cat;
|
---|
326 | case 'C':
|
---|
327 | cls = va_arg(ap, struct RClass*);
|
---|
328 | L_cat_class:
|
---|
329 | obj = mrb_obj_value(cls);
|
---|
330 | goto L_cat_obj;
|
---|
331 | case 'T':
|
---|
332 | obj = va_arg(ap, mrb_value);
|
---|
333 | L_cat_real_class_of:
|
---|
334 | cls = mrb_obj_class(mrb, obj);
|
---|
335 | goto L_cat_class;
|
---|
336 | case 'Y':
|
---|
337 | obj = va_arg(ap, mrb_value);
|
---|
338 | if (!mrb_test(obj) || mrb_true_p(obj)) {
|
---|
339 | inspect = TRUE;
|
---|
340 | goto L_cat_obj;
|
---|
341 | }
|
---|
342 | else {
|
---|
343 | goto L_cat_real_class_of;
|
---|
344 | }
|
---|
345 | case '%':
|
---|
346 | L_cat_current:
|
---|
347 | chars = p;
|
---|
348 | len = 1;
|
---|
349 | goto L_cat;
|
---|
350 | default:
|
---|
351 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "malformed format string - %%%c", *p);
|
---|
352 | }
|
---|
353 | }
|
---|
354 | else if (c == '\\') {
|
---|
355 | if (!*p) break;
|
---|
356 | goto L_cat_current;
|
---|
357 |
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | mrb_str_cat(mrb, result, b, p - b);
|
---|
362 | return result;
|
---|
363 | }
|
---|
364 |
|
---|
365 | MRB_API mrb_value
|
---|
366 | mrb_format(mrb_state *mrb, const char *format, ...)
|
---|
367 | {
|
---|
368 | va_list ap;
|
---|
369 | mrb_value str;
|
---|
370 |
|
---|
371 | va_start(ap, format);
|
---|
372 | str = mrb_vformat(mrb, format, ap);
|
---|
373 | va_end(ap);
|
---|
374 |
|
---|
375 | return str;
|
---|
376 | }
|
---|
377 |
|
---|
378 | static mrb_noreturn void
|
---|
379 | raise_va(mrb_state *mrb, struct RClass *c, const char *fmt, va_list ap, int argc, mrb_value *argv)
|
---|
380 | {
|
---|
381 | mrb_value mesg;
|
---|
382 | static int called = 0;
|
---|
383 |
|
---|
384 | mesg = mrb_vformat(mrb, fmt, ap);
|
---|
385 | if (argv == NULL) {
|
---|
386 | argv = &mesg;
|
---|
387 | }
|
---|
388 | else {
|
---|
389 | argv[0] = mesg;
|
---|
390 | }
|
---|
391 | if (called)
|
---|
392 | mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
|
---|
393 | called = 1;
|
---|
394 | mrb_exc_raise(mrb, mrb_obj_new(mrb, c, argc+1, argv));
|
---|
395 | }
|
---|
396 |
|
---|
397 | MRB_API mrb_noreturn void
|
---|
398 | mrb_raisef(mrb_state *mrb, struct RClass *c, const char *fmt, ...)
|
---|
399 | {
|
---|
400 | va_list args;
|
---|
401 |
|
---|
402 | va_start(args, fmt);
|
---|
403 | raise_va(mrb, c, fmt, args, 0, NULL);
|
---|
404 | va_end(args);
|
---|
405 | }
|
---|
406 |
|
---|
407 | MRB_API mrb_noreturn void
|
---|
408 | mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...)
|
---|
409 | {
|
---|
410 | mrb_value argv[2];
|
---|
411 | va_list args;
|
---|
412 |
|
---|
413 | va_start(args, fmt);
|
---|
414 | argv[1] = mrb_symbol_value(id);
|
---|
415 | raise_va(mrb, E_NAME_ERROR, fmt, args, 1, argv);
|
---|
416 | va_end(args);
|
---|
417 | }
|
---|
418 |
|
---|
419 | MRB_API void
|
---|
420 | mrb_warn(mrb_state *mrb, const char *fmt, ...)
|
---|
421 | {
|
---|
422 | #ifndef MRB_DISABLE_STDIO
|
---|
423 | va_list ap;
|
---|
424 | mrb_value str;
|
---|
425 |
|
---|
426 | va_start(ap, fmt);
|
---|
427 | str = mrb_vformat(mrb, fmt, ap);
|
---|
428 | fputs("warning: ", stderr);
|
---|
429 | fwrite(RSTRING_PTR(str), RSTRING_LEN(str), 1, stderr);
|
---|
430 | putc('\n', stderr);
|
---|
431 | va_end(ap);
|
---|
432 | #endif
|
---|
433 | }
|
---|
434 |
|
---|
435 | MRB_API mrb_noreturn void
|
---|
436 | mrb_bug(mrb_state *mrb, const char *fmt, ...)
|
---|
437 | {
|
---|
438 | #ifndef MRB_DISABLE_STDIO
|
---|
439 | va_list ap;
|
---|
440 | mrb_value str;
|
---|
441 |
|
---|
442 | va_start(ap, fmt);
|
---|
443 | str = mrb_vformat(mrb, fmt, ap);
|
---|
444 | fputs("bug: ", stderr);
|
---|
445 | fwrite(RSTRING_PTR(str), RSTRING_LEN(str), 1, stderr);
|
---|
446 | va_end(ap);
|
---|
447 | #endif
|
---|
448 | exit(EXIT_FAILURE);
|
---|
449 | }
|
---|
450 |
|
---|
451 | MRB_API mrb_value
|
---|
452 | mrb_make_exception(mrb_state *mrb, mrb_int argc, const mrb_value *argv)
|
---|
453 | {
|
---|
454 | mrb_value mesg;
|
---|
455 | int n;
|
---|
456 |
|
---|
457 | mesg = mrb_nil_value();
|
---|
458 | switch (argc) {
|
---|
459 | case 0:
|
---|
460 | break;
|
---|
461 | case 1:
|
---|
462 | if (mrb_nil_p(argv[0]))
|
---|
463 | break;
|
---|
464 | if (mrb_string_p(argv[0])) {
|
---|
465 | mesg = mrb_exc_new_str(mrb, E_RUNTIME_ERROR, argv[0]);
|
---|
466 | break;
|
---|
467 | }
|
---|
468 | n = 0;
|
---|
469 | goto exception_call;
|
---|
470 |
|
---|
471 | case 2:
|
---|
472 | case 3:
|
---|
473 | n = 1;
|
---|
474 | exception_call:
|
---|
475 | {
|
---|
476 | mrb_sym exc = mrb_intern_lit(mrb, "exception");
|
---|
477 | if (mrb_respond_to(mrb, argv[0], exc)) {
|
---|
478 | mesg = mrb_funcall_argv(mrb, argv[0], exc, n, argv+1);
|
---|
479 | }
|
---|
480 | else {
|
---|
481 | /* undef */
|
---|
482 | mrb_raise(mrb, E_TYPE_ERROR, "exception class/object expected");
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | break;
|
---|
487 | default:
|
---|
488 | mrb_argnum_error(mrb, argc, 0, 3);
|
---|
489 | break;
|
---|
490 | }
|
---|
491 | if (argc > 0) {
|
---|
492 | if (!mrb_obj_is_kind_of(mrb, mesg, mrb->eException_class))
|
---|
493 | mrb_raise(mrb, mrb->eException_class, "exception object expected");
|
---|
494 | if (argc > 2)
|
---|
495 | set_backtrace(mrb, mesg, argv[2]);
|
---|
496 | }
|
---|
497 |
|
---|
498 | return mesg;
|
---|
499 | }
|
---|
500 |
|
---|
501 | MRB_API void
|
---|
502 | mrb_sys_fail(mrb_state *mrb, const char *mesg)
|
---|
503 | {
|
---|
504 | struct RClass *sce;
|
---|
505 | mrb_int no;
|
---|
506 |
|
---|
507 | no = (mrb_int)errno;
|
---|
508 | if (mrb_class_defined(mrb, "SystemCallError")) {
|
---|
509 | sce = mrb_class_get(mrb, "SystemCallError");
|
---|
510 | if (mesg != NULL) {
|
---|
511 | mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 2, mrb_fixnum_value(no), mrb_str_new_cstr(mrb, mesg));
|
---|
512 | }
|
---|
513 | else {
|
---|
514 | mrb_funcall(mrb, mrb_obj_value(sce), "_sys_fail", 1, mrb_fixnum_value(no));
|
---|
515 | }
|
---|
516 | }
|
---|
517 | else {
|
---|
518 | mrb_raise(mrb, E_RUNTIME_ERROR, mesg);
|
---|
519 | }
|
---|
520 | }
|
---|
521 |
|
---|
522 | MRB_API mrb_noreturn void
|
---|
523 | mrb_no_method_error(mrb_state *mrb, mrb_sym id, mrb_value args, char const* fmt, ...)
|
---|
524 | {
|
---|
525 | mrb_value exc;
|
---|
526 | mrb_value argv[3];
|
---|
527 | va_list ap;
|
---|
528 |
|
---|
529 | va_start(ap, fmt);
|
---|
530 | argv[0] = mrb_vformat(mrb, fmt, ap);
|
---|
531 | argv[1] = mrb_symbol_value(id);
|
---|
532 | argv[2] = args;
|
---|
533 | va_end(ap);
|
---|
534 | exc = mrb_obj_new(mrb, E_NOMETHOD_ERROR, 3, argv);
|
---|
535 | mrb_exc_raise(mrb, exc);
|
---|
536 | }
|
---|
537 |
|
---|
538 | MRB_API mrb_noreturn void
|
---|
539 | mrb_frozen_error(mrb_state *mrb, void *frozen_obj)
|
---|
540 | {
|
---|
541 | mrb_raisef(mrb, E_FROZEN_ERROR, "can't modify frozen %t", mrb_obj_value(frozen_obj));
|
---|
542 | }
|
---|
543 |
|
---|
544 | MRB_API mrb_noreturn void
|
---|
545 | mrb_argnum_error(mrb_state *mrb, mrb_int argc, int min, int max)
|
---|
546 | {
|
---|
547 | #define FMT(exp) "wrong number of arguments (given %i, expected " exp ")"
|
---|
548 | if (min == max)
|
---|
549 | mrb_raisef(mrb, E_ARGUMENT_ERROR, FMT("%d"), argc, min);
|
---|
550 | else if (max < 0)
|
---|
551 | mrb_raisef(mrb, E_ARGUMENT_ERROR, FMT("%d+"), argc, min);
|
---|
552 | else
|
---|
553 | mrb_raisef(mrb, E_ARGUMENT_ERROR, FMT("%d..%d"), argc, min, max);
|
---|
554 | #undef FMT
|
---|
555 | }
|
---|
556 |
|
---|
557 | void
|
---|
558 | mrb_init_exception(mrb_state *mrb)
|
---|
559 | {
|
---|
560 | struct RClass *exception, *script_error, *stack_error, *nomem_error;
|
---|
561 |
|
---|
562 | mrb->eException_class = exception = mrb_define_class(mrb, "Exception", mrb->object_class); /* 15.2.22 */
|
---|
563 | MRB_SET_INSTANCE_TT(exception, MRB_TT_EXCEPTION);
|
---|
564 | mrb_define_class_method(mrb, exception, "exception", mrb_instance_new, MRB_ARGS_OPT(1));
|
---|
565 | mrb_define_method(mrb, exception, "exception", exc_exception, MRB_ARGS_OPT(1));
|
---|
566 | mrb_define_method(mrb, exception, "initialize", exc_initialize, MRB_ARGS_OPT(1));
|
---|
567 | mrb_define_method(mrb, exception, "to_s", exc_to_s, MRB_ARGS_NONE());
|
---|
568 | mrb_define_method(mrb, exception, "message", exc_message, MRB_ARGS_NONE());
|
---|
569 | mrb_define_method(mrb, exception, "inspect", mrb_exc_inspect, MRB_ARGS_NONE());
|
---|
570 | mrb_define_method(mrb, exception, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE());
|
---|
571 | mrb_define_method(mrb, exception, "set_backtrace", exc_set_backtrace, MRB_ARGS_REQ(1));
|
---|
572 |
|
---|
573 | mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */
|
---|
574 | mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */
|
---|
575 | script_error = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */
|
---|
576 | mrb_define_class(mrb, "SyntaxError", script_error); /* 15.2.38 */
|
---|
577 | stack_error = mrb_define_class(mrb, "SystemStackError", exception);
|
---|
578 | mrb->stack_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, stack_error, "stack level too deep"));
|
---|
579 |
|
---|
580 | nomem_error = mrb_define_class(mrb, "NoMemoryError", exception);
|
---|
581 | mrb->nomem_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, nomem_error, "Out of memory"));
|
---|
582 | #ifdef MRB_GC_FIXED_ARENA
|
---|
583 | mrb->arena_err = mrb_obj_ptr(mrb_exc_new_str_lit(mrb, nomem_error, "arena overflow error"));
|
---|
584 | #endif
|
---|
585 | }
|
---|