Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

error.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 error.c -
4
5 $Author$
6 created at: Mon Aug 9 16:11:34 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10 **********************************************************************/
11
12 #include "ruby/encoding.h"
13 #include "ruby/st.h"
14 #include "internal.h"
15 #include "ruby_assert.h"
16 #include "vm_core.h"
17 #include "builtin.h"
18
19 #include <stdio.h>
20 #include <stdarg.h>
21 #ifdef HAVE_STDLIB_H
22 #include <stdlib.h>
23 #endif
24 #include <errno.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #if defined __APPLE__
30 # include <AvailabilityMacros.h>
31 #endif
32
38 #ifndef EXIT_SUCCESS
39 #define EXIT_SUCCESS 0
40 #endif
41
42 #ifndef WIFEXITED
43 #define WIFEXITED(status) 1
44 #endif
45
46 #ifndef WEXITSTATUS
47 #define WEXITSTATUS(status) (status)
48 #endif
49
50 VALUE rb_iseqw_local_variables(VALUE iseqval);
51 VALUE rb_iseqw_new(const rb_iseq_t *);
52 int rb_str_end_with_asciichar(VALUE str, int c);
53
54 VALUE rb_eEAGAIN;
55 VALUE rb_eEWOULDBLOCK;
56 VALUE rb_eEINPROGRESS;
57 static VALUE rb_mWarning;
58 static VALUE rb_cWarningBuffer;
59
60 static ID id_warn;
61
62 extern const char ruby_description[];
63
64 static const char *
65 rb_strerrno(int err)
66 {
67 #define defined_error(name, num) if (err == (num)) return (name);
68 #define undefined_error(name)
69 #include "known_errors.inc"
70 #undef defined_error
71 #undef undefined_error
72 return NULL;
73 }
74
75 static int
76 err_position_0(char *buf, long len, const char *file, int line)
77 {
78 if (!file) {
79 return 0;
80 }
81 else if (line == 0) {
82 return snprintf(buf, len, "%s: ", file);
83 }
84 else {
85 return snprintf(buf, len, "%s:%d: ", file, line);
86 }
87 }
88
89 static VALUE
90 err_vcatf(VALUE str, const char *pre, const char *file, int line,
91 const char *fmt, va_list args)
92 {
93 if (file) {
94 rb_str_cat2(str, file);
95 if (line) rb_str_catf(str, ":%d", line);
96 rb_str_cat2(str, ": ");
97 }
98 if (pre) rb_str_cat2(str, pre);
99 rb_str_vcatf(str, fmt, args);
100 return str;
101 }
102
103 VALUE
104 rb_syntax_error_append(VALUE exc, VALUE file, int line, int column,
105 rb_encoding *enc, const char *fmt, va_list args)
106 {
107 const char *fn = NIL_P(file) ? NULL : RSTRING_PTR(file);
108 if (!exc) {
109 VALUE mesg = rb_enc_str_new(0, 0, enc);
110 err_vcatf(mesg, NULL, fn, line, fmt, args);
111 rb_str_cat2(mesg, "\n");
112 rb_write_error_str(mesg);
113 }
114 else {
115 VALUE mesg;
116 if (NIL_P(exc)) {
117 mesg = rb_enc_str_new(0, 0, enc);
118 exc = rb_class_new_instance(1, &mesg, rb_eSyntaxError);
119 }
120 else {
121 mesg = rb_attr_get(exc, idMesg);
122 if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n')
123 rb_str_cat_cstr(mesg, "\n");
124 }
125 err_vcatf(mesg, NULL, fn, line, fmt, args);
126 }
127
128 return exc;
129 }
130
131 static unsigned int warning_disabled_categories;
132
133 static unsigned int
134 rb_warning_category_mask(VALUE category)
135 {
136 return 1U << rb_warning_category_from_name(category);
137 }
138
139 rb_warning_category_t
140 rb_warning_category_from_name(VALUE category)
141 {
142 rb_warning_category_t cat = RB_WARN_CATEGORY_NONE;
143 Check_Type(category, T_SYMBOL);
144 if (category == ID2SYM(rb_intern("deprecated"))) {
145 cat = RB_WARN_CATEGORY_DEPRECATED;
146 }
147 else if (category == ID2SYM(rb_intern("experimental"))) {
148 cat = RB_WARN_CATEGORY_EXPERIMENTAL;
149 }
150 else {
151 rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category);
152 }
153 return cat;
154 }
155
156 void
157 rb_warning_category_update(unsigned int mask, unsigned int bits)
158 {
159 warning_disabled_categories &= ~mask;
160 warning_disabled_categories |= mask & ~bits;
161 }
162
163 MJIT_FUNC_EXPORTED bool
164 rb_warning_category_enabled_p(rb_warning_category_t category)
165 {
166 return !(warning_disabled_categories & (1U << category));
167 }
168
169 /*
170 * call-seq
171 * Warning[category] -> true or false
172 *
173 * Returns the flag to show the warning messages for +category+.
174 * Supported categories are:
175 *
176 * +:deprecated+ :: deprecation warnings
177 * * assignment of non-nil value to <code>$,</code> and <code>$;</code>
178 * * keyword arguments
179 * * proc/lambda without block
180 * etc.
181 *
182 * +:experimental+ :: experimental features
183 * * Pattern matching
184 */
185
186 static VALUE
187 rb_warning_s_aref(VALUE mod, VALUE category)
188 {
189 rb_warning_category_t cat = rb_warning_category_from_name(category);
190 if (rb_warning_category_enabled_p(cat))
191 return Qtrue;
192 return Qfalse;
193 }
194
195 /*
196 * call-seq
197 * Warning[category] = flag -> flag
198 *
199 * Sets the warning flags for +category+.
200 * See Warning.[] for the categories.
201 */
202
203 static VALUE
204 rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag)
205 {
206 unsigned int mask = rb_warning_category_mask(category);
207 unsigned int disabled = warning_disabled_categories;
208 if (!RTEST(flag))
209 disabled |= mask;
210 else
211 disabled &= ~mask;
212 warning_disabled_categories = disabled;
213 return flag;
214 }
215
216 /*
217 * call-seq:
218 * warn(msg) -> nil
219 *
220 * Writes warning message +msg+ to $stderr. This method is called by
221 * Ruby for all emitted warnings.
222 */
223
224 static VALUE
225 rb_warning_s_warn(VALUE mod, VALUE str)
226 {
227 Check_Type(str, T_STRING);
228 rb_must_asciicompat(str);
229 rb_write_error_str(str);
230 return Qnil;
231 }
232
233 /*
234 * Document-module: Warning
235 *
236 * The Warning module contains a single method named #warn, and the
237 * module extends itself, making Warning.warn available.
238 * Warning.warn is called for all warnings issued by Ruby.
239 * By default, warnings are printed to $stderr.
240 *
241 * By overriding Warning.warn, you can change how warnings are
242 * handled by Ruby, either filtering some warnings, and/or outputting
243 * warnings somewhere other than $stderr. When Warning.warn is
244 * overridden, super can be called to get the default behavior of
245 * printing the warning to $stderr.
246 */
247
248 static VALUE
249 rb_warning_warn(VALUE mod, VALUE str)
250 {
251 return rb_funcallv(mod, id_warn, 1, &str);
252 }
253
254 static void
255 rb_write_warning_str(VALUE str)
256 {
257 rb_warning_warn(rb_mWarning, str);
258 }
259
260 static VALUE
261 warn_vsprintf(rb_encoding *enc, const char *file, int line, const char *fmt, va_list args)
262 {
263 VALUE str = rb_enc_str_new(0, 0, enc);
264
265 err_vcatf(str, "warning: ", file, line, fmt, args);
266 return rb_str_cat2(str, "\n");
267 }
268
269 void
270 rb_compile_warn(const char *file, int line, const char *fmt, ...)
271 {
272 VALUE str;
273 va_list args;
274
275 if (NIL_P(ruby_verbose)) return;
276
277 va_start(args, fmt);
278 str = warn_vsprintf(NULL, file, line, fmt, args);
279 va_end(args);
280 rb_write_warning_str(str);
281 }
282
283 /* rb_compile_warning() reports only in verbose mode */
284 void
285 rb_compile_warning(const char *file, int line, const char *fmt, ...)
286 {
287 VALUE str;
288 va_list args;
289
290 if (!RTEST(ruby_verbose)) return;
291
292 va_start(args, fmt);
293 str = warn_vsprintf(NULL, file, line, fmt, args);
294 va_end(args);
295 rb_write_warning_str(str);
296 }
297
298 static VALUE
299 warning_string(rb_encoding *enc, const char *fmt, va_list args)
300 {
301 int line;
302 const char *file = rb_source_location_cstr(&line);
303 return warn_vsprintf(enc, file, line, fmt, args);
304 }
305
306 #define with_warning_string(mesg, enc, fmt) \
307 VALUE mesg; \
308 va_list args; va_start(args, fmt); \
309 mesg = warning_string(enc, fmt, args); \
310 va_end(args);
311
312 void
313 rb_warn(const char *fmt, ...)
314 {
315 if (!NIL_P(ruby_verbose)) {
316 with_warning_string(mesg, 0, fmt) {
317 rb_write_warning_str(mesg);
318 }
319 }
320 }
321
322 void
323 rb_enc_warn(rb_encoding *enc, const char *fmt, ...)
324 {
325 if (!NIL_P(ruby_verbose)) {
326 with_warning_string(mesg, enc, fmt) {
327 rb_write_warning_str(mesg);
328 }
329 }
330 }
331
332 /* rb_warning() reports only in verbose mode */
333 void
334 rb_warning(const char *fmt, ...)
335 {
336 if (RTEST(ruby_verbose)) {
337 with_warning_string(mesg, 0, fmt) {
338 rb_write_warning_str(mesg);
339 }
340 }
341 }
342
343 VALUE
344 rb_warning_string(const char *fmt, ...)
345 {
346 with_warning_string(mesg, 0, fmt) {
347 }
348 return mesg;
349 }
350
351 #if 0
352 void
353 rb_enc_warning(rb_encoding *enc, const char *fmt, ...)
354 {
355 if (RTEST(ruby_verbose)) {
356 with_warning_string(mesg, enc, fmt) {
357 rb_write_warning_str(mesg);
358 }
359 }
360 }
361 #endif
362
363 void
364 rb_warn_deprecated(const char *fmt, const char *suggest, ...)
365 {
366 if (NIL_P(ruby_verbose)) return;
367 if (!rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) return;
368 va_list args;
369 va_start(args, suggest);
370 VALUE mesg = warning_string(0, fmt, args);
371 va_end(args);
372 rb_str_set_len(mesg, RSTRING_LEN(mesg) - 1);
373 rb_str_cat_cstr(mesg, " is deprecated");
374 if (suggest) rb_str_catf(mesg, "; use %s instead", suggest);
375 rb_str_cat_cstr(mesg, "\n");
376 rb_write_warning_str(mesg);
377 }
378
379 static inline int
380 end_with_asciichar(VALUE str, int c)
381 {
382 return RB_TYPE_P(str, T_STRING) &&
383 rb_str_end_with_asciichar(str, c);
384 }
385
386 /* :nodoc: */
387 static VALUE
388 warning_write(int argc, VALUE *argv, VALUE buf)
389 {
390 while (argc-- > 0) {
391 rb_str_append(buf, *argv++);
392 }
393 return buf;
394 }
395
396 VALUE rb_ec_backtrace_location_ary(rb_execution_context_t *ec, long lev, long n);
397 static VALUE
398 rb_warn_m(rb_execution_context_t *ec, VALUE exc, VALUE msgs, VALUE uplevel)
399 {
400 VALUE location = Qnil;
401 int argc = RARRAY_LENINT(msgs);
402 const VALUE *argv = RARRAY_CONST_PTR(msgs);
403
404 if (!NIL_P(ruby_verbose) && argc > 0) {
405 VALUE str = argv[0];
406 if (!NIL_P(uplevel)) {
407 long lev = NUM2LONG(uplevel);
408 if (lev < 0) {
409 rb_raise(rb_eArgError, "negative level (%ld)", lev);
410 }
411 location = rb_ec_backtrace_location_ary(ec, lev + 1, 1);
412 if (!NIL_P(location)) {
413 location = rb_ary_entry(location, 0);
414 }
415 }
416 if (argc > 1 || !NIL_P(uplevel) || !end_with_asciichar(str, '\n')) {
417 VALUE path;
418 if (NIL_P(uplevel)) {
419 str = rb_str_tmp_new(0);
420 }
421 else if (NIL_P(location) ||
422 NIL_P(path = rb_funcall(location, rb_intern("path"), 0))) {
423 str = rb_str_new_cstr("warning: ");
424 }
425 else {
426 str = rb_sprintf("%s:%ld: warning: ",
427 rb_string_value_ptr(&path),
428 NUM2LONG(rb_funcall(location, rb_intern("lineno"), 0)));
429 }
430 RBASIC_SET_CLASS(str, rb_cWarningBuffer);
431 rb_io_puts(argc, argv, str);
432 RBASIC_SET_CLASS(str, rb_cString);
433 }
434 if (exc == rb_mWarning) {
435 rb_must_asciicompat(str);
436 rb_write_error_str(str);
437 }
438 else {
439 rb_write_warning_str(str);
440 }
441 }
442 return Qnil;
443 }
444
445 #define MAX_BUG_REPORTERS 0x100
446
447 static struct bug_reporters {
448 void (*func)(FILE *out, void *data);
449 void *data;
450 } bug_reporters[MAX_BUG_REPORTERS];
451
452 static int bug_reporters_size;
453
454 int
455 rb_bug_reporter_add(void (*func)(FILE *, void *), void *data)
456 {
457 struct bug_reporters *reporter;
458 if (bug_reporters_size >= MAX_BUG_REPORTERS) {
459 return 0; /* failed to register */
460 }
461 reporter = &bug_reporters[bug_reporters_size++];
462 reporter->func = func;
463 reporter->data = data;
464
465 return 1;
466 }
467
468 /* SIGSEGV handler might have a very small stack. Thus we need to use it carefully. */
469 #define REPORT_BUG_BUFSIZ 256
470 static FILE *
471 bug_report_file(const char *file, int line)
472 {
473 char buf[REPORT_BUG_BUFSIZ];
474 FILE *out = stderr;
475 int len = err_position_0(buf, sizeof(buf), file, line);
476
477 if ((ssize_t)fwrite(buf, 1, len, out) == (ssize_t)len ||
478 (ssize_t)fwrite(buf, 1, len, (out = stdout)) == (ssize_t)len) {
479 return out;
480 }
481
482 return NULL;
483 }
484
485 FUNC_MINIMIZED(static void bug_important_message(FILE *out, const char *const msg, size_t len));
486
487 static void
488 bug_important_message(FILE *out, const char *const msg, size_t len)
489 {
490 const char *const endmsg = msg + len;
491 const char *p = msg;
492
493 if (!len) return;
494 if (isatty(fileno(out))) {
495 static const char red[] = "\033[;31;1;7m";
496 static const char green[] = "\033[;32;7m";
497 static const char reset[] = "\033[m";
498 const char *e = strchr(p, '\n');
499 const int w = (int)(e - p);
500 do {
501 int i = (int)(e - p);
502 fputs(*p == ' ' ? green : red, out);
503 fwrite(p, 1, e - p, out);
504 for (; i < w; ++i) fputc(' ', out);
505 fputs(reset, out);
506 fputc('\n', out);
507 } while ((p = e + 1) < endmsg && (e = strchr(p, '\n')) != 0 && e > p + 1);
508 }
509 fwrite(p, 1, endmsg - p, out);
510 }
511
512 static void
513 preface_dump(FILE *out)
514 {
515 #if defined __APPLE__
516 static const char msg[] = ""
517 "-- Crash Report log information "
518 "--------------------------------------------\n"
519 " See Crash Report log file under the one of following:\n"
520 # if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
521 " * ~/Library/Logs/CrashReporter\n"
522 " * /Library/Logs/CrashReporter\n"
523 # endif
524 " * ~/Library/Logs/DiagnosticReports\n"
525 " * /Library/Logs/DiagnosticReports\n"
526 " for more details.\n"
527 "Don't forget to include the above Crash Report log file in bug reports.\n"
528 "\n";
529 const size_t msglen = sizeof(msg) - 1;
530 #else
531 const char *msg = NULL;
532 const size_t msglen = 0;
533 #endif
534 bug_important_message(out, msg, msglen);
535 }
536
537 static void
538 postscript_dump(FILE *out)
539 {
540 #if defined __APPLE__
541 static const char msg[] = ""
542 "[IMPORTANT]"
543 /*" ------------------------------------------------"*/
544 "\n""Don't forget to include the Crash Report log file under\n"
545 # if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
546 "CrashReporter or "
547 # endif
548 "DiagnosticReports directory in bug reports.\n"
549 /*"------------------------------------------------------------\n"*/
550 "\n";
551 const size_t msglen = sizeof(msg) - 1;
552 #else
553 const char *msg = NULL;
554 const size_t msglen = 0;
555 #endif
556 bug_important_message(out, msg, msglen);
557 }
558
559 static void
560 bug_report_begin_valist(FILE *out, const char *fmt, va_list args)
561 {
562 char buf[REPORT_BUG_BUFSIZ];
563
564 fputs("[BUG] ", out);
565 vsnprintf(buf, sizeof(buf), fmt, args);
566 fputs(buf, out);
567 snprintf(buf, sizeof(buf), "\n%s\n\n", ruby_description);
568 fputs(buf, out);
569 preface_dump(out);
570
571 #if RUBY_DEVEL
572 const char *cmd = getenv("RUBY_ON_BUG");
573 if (cmd) {
574 snprintf(buf, sizeof(buf), "%s %"PRI_PIDT_PREFIX"d", cmd, getpid());
575 int r = system(buf);
576 if (r == -1) {
577 snprintf(buf, sizeof(buf), "Launching RUBY_ON_BUG command failed.");
578 }
579 }
580 #endif
581 }
582
583 #define bug_report_begin(out, fmt) do { \
584 va_list args; \
585 va_start(args, fmt); \
586 bug_report_begin_valist(out, fmt, args); \
587 va_end(args); \
588 } while (0)
589
590 static void
591 bug_report_end(FILE *out)
592 {
593 /* call additional bug reporters */
594 {
595 int i;
596 for (i=0; i<bug_reporters_size; i++) {
597 struct bug_reporters *reporter = &bug_reporters[i];
598 (*reporter->func)(out, reporter->data);
599 }
600 }
601 postscript_dump(out);
602 }
603
604 #define report_bug(file, line, fmt, ctx) do { \
605 FILE *out = bug_report_file(file, line); \
606 if (out) { \
607 bug_report_begin(out, fmt); \
608 rb_vm_bugreport(ctx); \
609 bug_report_end(out); \
610 } \
611 } while (0) \
612
613 #define report_bug_valist(file, line, fmt, ctx, args) do { \
614 FILE *out = bug_report_file(file, line); \
615 if (out) { \
616 bug_report_begin_valist(out, fmt, args); \
617 rb_vm_bugreport(ctx); \
618 bug_report_end(out); \
619 } \
620 } while (0) \
621
622 NORETURN(static void die(void));
623 static void
624 die(void)
625 {
626 #if defined(_WIN32) && defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 80
627 _set_abort_behavior( 0, _CALL_REPORTFAULT);
628 #endif
629
630 abort();
631 }
632
633 void
634 rb_bug(const char *fmt, ...)
635 {
636 const char *file = NULL;
637 int line = 0;
638
639 if (GET_EC()) {
640 file = rb_source_location_cstr(&line);
641 }
642
643 report_bug(file, line, fmt, NULL);
644
645 die();
646 }
647
648 void
649 rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *ctx, const char *fmt, ...)
650 {
651 const char *file = NULL;
652 int line = 0;
653
654 if (GET_EC()) {
655 file = rb_source_location_cstr(&line);
656 }
657
658 report_bug(file, line, fmt, ctx);
659
660 if (default_sighandler) default_sighandler(sig);
661
662 die();
663 }
664
665
666 void
667 rb_bug_errno(const char *mesg, int errno_arg)
668 {
669 if (errno_arg == 0)
670 rb_bug("%s: errno == 0 (NOERROR)", mesg);
671 else {
672 const char *errno_str = rb_strerrno(errno_arg);
673 if (errno_str)
674 rb_bug("%s: %s (%s)", mesg, strerror(errno_arg), errno_str);
675 else
676 rb_bug("%s: %s (%d)", mesg, strerror(errno_arg), errno_arg);
677 }
678 }
679
680 /*
681 * this is safe to call inside signal handler and timer thread
682 * (which isn't a Ruby Thread object)
683 */
684 #define write_or_abort(fd, str, len) (write((fd), (str), (len)) < 0 ? abort() : (void)0)
685 #define WRITE_CONST(fd,str) write_or_abort((fd),(str),sizeof(str) - 1)
686
687 void
688 rb_async_bug_errno(const char *mesg, int errno_arg)
689 {
690 WRITE_CONST(2, "[ASYNC BUG] ");
691 write_or_abort(2, mesg, strlen(mesg));
692 WRITE_CONST(2, "\n");
693
694 if (errno_arg == 0) {
695 WRITE_CONST(2, "errno == 0 (NOERROR)\n");
696 }
697 else {
698 const char *errno_str = rb_strerrno(errno_arg);
699
700 if (!errno_str)
701 errno_str = "undefined errno";
702 write_or_abort(2, errno_str, strlen(errno_str));
703 }
704 WRITE_CONST(2, "\n\n");
705 write_or_abort(2, ruby_description, strlen(ruby_description));
706 abort();
707 }
708
709 void
710 rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args)
711 {
712 report_bug_valist(RSTRING_PTR(file), line, fmt, NULL, args);
713 }
714
715 MJIT_FUNC_EXPORTED void
716 rb_assert_failure(const char *file, int line, const char *name, const char *expr)
717 {
718 FILE *out = stderr;
719 fprintf(out, "Assertion Failed: %s:%d:", file, line);
720 if (name) fprintf(out, "%s:", name);
721 fprintf(out, "%s\n%s\n\n", expr, ruby_description);
722 preface_dump(out);
723 rb_vm_bugreport(NULL);
724 bug_report_end(out);
725 die();
726 }
727
728 static const char builtin_types[][10] = {
729 "", /* 0x00, */
730 "Object",
731 "Class",
732 "Module",
733 "Float",
734 "String",
735 "Regexp",
736 "Array",
737 "Hash",
738 "Struct",
739 "Bignum",
740 "File",
741 "Data", /* internal use: wrapped C pointers */
742 "MatchData", /* data of $~ */
743 "Complex",
744 "Rational",
745 "", /* 0x10 */
746 "nil",
747 "true",
748 "false",
749 "Symbol", /* :symbol */
750 "Fixnum",
751 "undef", /* internal use: #undef; should not happen */
752 "", /* 0x17 */
753 "", /* 0x18 */
754 "", /* 0x19 */
755 "Memo", /* internal use: general memo */
756 "Node", /* internal use: syntax tree node */
757 "iClass", /* internal use: mixed-in module holder */
758 };
759
760 const char *
761 rb_builtin_type_name(int t)
762 {
763 const char *name;
764 if ((unsigned int)t >= numberof(builtin_types)) return 0;
765 name = builtin_types[t];
766 if (*name) return name;
767 return 0;
768 }
769
770 static const char *
771 builtin_class_name(VALUE x)
772 {
773 const char *etype;
774
775 if (NIL_P(x)) {
776 etype = "nil";
777 }
778 else if (FIXNUM_P(x)) {
779 etype = "Integer";
780 }
781 else if (SYMBOL_P(x)) {
782 etype = "Symbol";
783 }
784 else if (RB_TYPE_P(x, T_TRUE)) {
785 etype = "true";
786 }
787 else if (RB_TYPE_P(x, T_FALSE)) {
788 etype = "false";
789 }
790 else {
791 etype = NULL;
792 }
793 return etype;
794 }
795
796 const char *
797 rb_builtin_class_name(VALUE x)
798 {
799 const char *etype = builtin_class_name(x);
800
801 if (!etype) {
802 etype = rb_obj_classname(x);
803 }
804 return etype;
805 }
806
807 NORETURN(static void unexpected_type(VALUE, int, int));
808 #define UNDEF_LEAKED "undef leaked to the Ruby space"
809
810 static void
811 unexpected_type(VALUE x, int xt, int t)
812 {
813 const char *tname = rb_builtin_type_name(t);
814 VALUE mesg, exc = rb_eFatal;
815
816 if (tname) {
817 const char *cname = builtin_class_name(x);
818 if (cname)
819 mesg = rb_sprintf("wrong argument type %s (expected %s)",
820 cname, tname);
821 else
822 mesg = rb_sprintf("wrong argument type %"PRIsVALUE" (expected %s)",
823 rb_obj_class(x), tname);
824 exc = rb_eTypeError;
825 }
826 else if (xt > T_MASK && xt <= 0x3f) {
827 mesg = rb_sprintf("unknown type 0x%x (0x%x given, probably comes"
828 " from extension library for ruby 1.8)", t, xt);
829 }
830 else {
831 mesg = rb_sprintf("unknown type 0x%x (0x%x given)", t, xt);
832 }
833 rb_exc_raise(rb_exc_new_str(exc, mesg));
834 }
835
836 void
837 rb_check_type(VALUE x, int t)
838 {
839 int xt;
840
841 if (x == Qundef) {
842 rb_bug(UNDEF_LEAKED);
843 }
844
845 xt = TYPE(x);
846 if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
847 unexpected_type(x, xt, t);
848 }
849 }
850
851 void
852 rb_unexpected_type(VALUE x, int t)
853 {
854 if (x == Qundef) {
855 rb_bug(UNDEF_LEAKED);
856 }
857
858 unexpected_type(x, TYPE(x), t);
859 }
860
861 int
862 rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent)
863 {
864 while (child) {
865 if (child == parent) return 1;
866 child = child->parent;
867 }
868 return 0;
869 }
870
871 int
872 rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
873 {
874 if (!RB_TYPE_P(obj, T_DATA) ||
875 !RTYPEDDATA_P(obj) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
876 return 0;
877 }
878 return 1;
879 }
880
881 #undef rb_typeddata_is_instance_of
882 int
883 rb_typeddata_is_instance_of(VALUE obj, const rb_data_type_t *data_type)
884 {
885 return rb_typeddata_is_instance_of_inline(obj, data_type);
886 }
887
888 void *
889 rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
890 {
891 const char *etype;
892
893 if (!RB_TYPE_P(obj, T_DATA)) {
894 wrong_type:
895 etype = builtin_class_name(obj);
896 if (!etype)
897 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
898 rb_obj_class(obj), data_type->wrap_struct_name);
899 wrong_datatype:
900 rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
901 etype, data_type->wrap_struct_name);
902 }
903 if (!RTYPEDDATA_P(obj)) {
904 goto wrong_type;
905 }
906 else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
907 etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
908 goto wrong_datatype;
909 }
910 return DATA_PTR(obj);
911 }
912
913 /* exception classes */
914 VALUE rb_eException;
915 VALUE rb_eSystemExit;
916 VALUE rb_eInterrupt;
917 VALUE rb_eSignal;
918 VALUE rb_eFatal;
919 VALUE rb_eStandardError;
920 VALUE rb_eRuntimeError;
921 VALUE rb_eFrozenError;
922 VALUE rb_eTypeError;
923 VALUE rb_eArgError;
924 VALUE rb_eIndexError;
925 VALUE rb_eKeyError;
926 VALUE rb_eRangeError;
927 VALUE rb_eNameError;
928 VALUE rb_eEncodingError;
929 VALUE rb_eEncCompatError;
930 VALUE rb_eNoMethodError;
931 VALUE rb_eSecurityError;
932 VALUE rb_eNotImpError;
933 VALUE rb_eNoMemError;
934 VALUE rb_cNameErrorMesg;
935 VALUE rb_eNoMatchingPatternError;
936
937 VALUE rb_eScriptError;
938 VALUE rb_eSyntaxError;
939 VALUE rb_eLoadError;
940
941 VALUE rb_eSystemCallError;
942 VALUE rb_mErrno;
943 static VALUE rb_eNOERROR;
944
945 ID ruby_static_id_cause;
946 #define id_cause ruby_static_id_cause
947 static ID id_message, id_backtrace;
948 static ID id_key, id_args, id_Errno, id_errno, id_i_path;
949 static ID id_receiver, id_recv, id_iseq, id_local_variables;
950 static ID id_private_call_p, id_top, id_bottom;
951 #define id_bt idBt
952 #define id_bt_locations idBt_locations
953 #define id_mesg idMesg
954 #define id_name idName
955
956 #undef rb_exc_new_cstr
957
958 VALUE
959 rb_exc_new(VALUE etype, const char *ptr, long len)
960 {
961 VALUE mesg = rb_str_new(ptr, len);
962 return rb_class_new_instance(1, &mesg, etype);
963 }
964
965 VALUE
966 rb_exc_new_cstr(VALUE etype, const char *s)
967 {
968 return rb_exc_new(etype, s, strlen(s));
969 }
970
971 VALUE
972 rb_exc_new_str(VALUE etype, VALUE str)
973 {
974 StringValue(str);
975 return rb_class_new_instance(1, &str, etype);
976 }
977
978 static VALUE
979 exc_init(VALUE exc, VALUE mesg)
980 {
981 rb_ivar_set(exc, id_mesg, mesg);
982 rb_ivar_set(exc, id_bt, Qnil);
983
984 return exc;
985 }
986
987 /*
988 * call-seq:
989 * Exception.new(msg = nil) -> exception
990 *
991 * Construct a new Exception object, optionally passing in
992 * a message.
993 */
994
995 static VALUE
996 exc_initialize(int argc, VALUE *argv, VALUE exc)
997 {
998 VALUE arg;
999
1000 arg = (!rb_check_arity(argc, 0, 1) ? Qnil : argv[0]);
1001 return exc_init(exc, arg);
1002 }
1003
1004 /*
1005 * Document-method: exception
1006 *
1007 * call-seq:
1008 * exc.exception(string) -> an_exception or exc
1009 *
1010 * With no argument, or if the argument is the same as the receiver,
1011 * return the receiver. Otherwise, create a new
1012 * exception object of the same class as the receiver, but with a
1013 * message equal to <code>string.to_str</code>.
1014 *
1015 */
1016
1017 static VALUE
1018 exc_exception(int argc, VALUE *argv, VALUE self)
1019 {
1020 VALUE exc;
1021
1022 argc = rb_check_arity(argc, 0, 1);
1023 if (argc == 0) return self;
1024 if (argc == 1 && self == argv[0]) return self;
1025 exc = rb_obj_clone(self);
1026 rb_ivar_set(exc, id_mesg, argv[0]);
1027 return exc;
1028 }
1029
1030 /*
1031 * call-seq:
1032 * exception.to_s -> string
1033 *
1034 * Returns exception's message (or the name of the exception if
1035 * no message is set).
1036 */
1037
1038 static VALUE
1039 exc_to_s(VALUE exc)
1040 {
1041 VALUE mesg = rb_attr_get(exc, idMesg);
1042
1043 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
1044 return rb_String(mesg);
1045 }
1046
1047 /* FIXME: Include eval_error.c */
1048 void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlight, VALUE reverse);
1049
1050 VALUE
1051 rb_get_message(VALUE exc)
1052 {
1053 VALUE e = rb_check_funcall(exc, id_message, 0, 0);
1054 if (e == Qundef) return Qnil;
1055 if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
1056 return e;
1057 }
1058
1059 /*
1060 * call-seq:
1061 * Exception.to_tty? -> true or false
1062 *
1063 * Returns +true+ if exception messages will be sent to a tty.
1064 */
1065 static VALUE
1066 exc_s_to_tty_p(VALUE self)
1067 {
1068 return rb_stderr_tty_p() ? Qtrue : Qfalse;
1069 }
1070
1071 /*
1072 * call-seq:
1073 * exception.full_message(highlight: bool, order: [:top or :bottom]) -> string
1074 *
1075 * Returns formatted string of _exception_.
1076 * The returned string is formatted using the same format that Ruby uses
1077 * when printing an uncaught exceptions to stderr.
1078 *
1079 * If _highlight_ is +true+ the default error handler will send the
1080 * messages to a tty.
1081 *
1082 * _order_ must be either of +:top+ or +:bottom+, and places the error
1083 * message and the innermost backtrace come at the top or the bottom.
1084 *
1085 * The default values of these options depend on <code>$stderr</code>
1086 * and its +tty?+ at the timing of a call.
1087 */
1088
1089 static VALUE
1090 exc_full_message(int argc, VALUE *argv, VALUE exc)
1091 {
1092 VALUE opt, str, emesg, errat;
1093 enum {kw_highlight, kw_order, kw_max_};
1094 static ID kw[kw_max_];
1095 VALUE args[kw_max_] = {Qnil, Qnil};
1096
1097 rb_scan_args(argc, argv, "0:", &opt);
1098 if (!NIL_P(opt)) {
1099 if (!kw[0]) {
1100 #define INIT_KW(n) kw[kw_##n] = rb_intern_const(#n)
1101 INIT_KW(highlight);
1102 INIT_KW(order);
1103 #undef INIT_KW
1104 }
1105 rb_get_kwargs(opt, kw, 0, kw_max_, args);
1106 switch (args[kw_highlight]) {
1107 default:
1108 rb_raise(rb_eArgError, "expected true or false as "
1109 "highlight: %+"PRIsVALUE, args[kw_highlight]);
1110 case Qundef: args[kw_highlight] = Qnil; break;
1111 case Qtrue: case Qfalse: case Qnil: break;
1112 }
1113 if (args[kw_order] == Qundef) {
1114 args[kw_order] = Qnil;
1115 }
1116 else {
1117 ID id = rb_check_id(&args[kw_order]);
1118 if (id == id_bottom) args[kw_order] = Qtrue;
1119 else if (id == id_top) args[kw_order] = Qfalse;
1120 else {
1121 rb_raise(rb_eArgError, "expected :top or :bottom as "
1122 "order: %+"PRIsVALUE, args[kw_order]);
1123 }
1124 }
1125 }
1126 str = rb_str_new2("");
1127 errat = rb_get_backtrace(exc);
1128 emesg = rb_get_message(exc);
1129
1130 rb_error_write(exc, emesg, errat, str, args[kw_highlight], args[kw_order]);
1131 return str;
1132 }
1133
1134 /*
1135 * call-seq:
1136 * exception.message -> string
1137 *
1138 * Returns the result of invoking <code>exception.to_s</code>.
1139 * Normally this returns the exception's message or name.
1140 */
1141
1142 static VALUE
1143 exc_message(VALUE exc)
1144 {
1145 return rb_funcallv(exc, idTo_s, 0, 0);
1146 }
1147
1148 /*
1149 * call-seq:
1150 * exception.inspect -> string
1151 *
1152 * Return this exception's class name and message.
1153 */
1154
1155 static VALUE
1156 exc_inspect(VALUE exc)
1157 {
1158 VALUE str, klass;
1159
1160 klass = CLASS_OF(exc);
1161 exc = rb_obj_as_string(exc);
1162 if (RSTRING_LEN(exc) == 0) {
1163 return rb_class_name(klass);
1164 }
1165
1166 str = rb_str_buf_new2("#<");
1167 klass = rb_class_name(klass);
1168 rb_str_buf_append(str, klass);
1169 rb_str_buf_cat(str, ": ", 2);
1170 rb_str_buf_append(str, exc);
1171 rb_str_buf_cat(str, ">", 1);
1172
1173 return str;
1174 }
1175
1176 /*
1177 * call-seq:
1178 * exception.backtrace -> array or nil
1179 *
1180 * Returns any backtrace associated with the exception. The backtrace
1181 * is an array of strings, each containing either ``filename:lineNo: in
1182 * `method''' or ``filename:lineNo.''
1183 *
1184 * def a
1185 * raise "boom"
1186 * end
1187 *
1188 * def b
1189 * a()
1190 * end
1191 *
1192 * begin
1193 * b()
1194 * rescue => detail
1195 * print detail.backtrace.join("\n")
1196 * end
1197 *
1198 * <em>produces:</em>
1199 *
1200 * prog.rb:2:in `a'
1201 * prog.rb:6:in `b'
1202 * prog.rb:10
1203 *
1204 * In the case no backtrace has been set, +nil+ is returned
1205 *
1206 * ex = StandardError.new
1207 * ex.backtrace
1208 * #=> nil
1209 */
1210
1211 static VALUE
1212 exc_backtrace(VALUE exc)
1213 {
1214 VALUE obj;
1215
1216 obj = rb_attr_get(exc, id_bt);
1217
1218 if (rb_backtrace_p(obj)) {
1219 obj = rb_backtrace_to_str_ary(obj);
1220 /* rb_ivar_set(exc, id_bt, obj); */
1221 }
1222
1223 return obj;
1224 }
1225
1226 static VALUE rb_check_backtrace(VALUE);
1227
1228 VALUE
1229 rb_get_backtrace(VALUE exc)
1230 {
1231 ID mid = id_backtrace;
1232 VALUE info;
1233 if (rb_method_basic_definition_p(CLASS_OF(exc), id_backtrace)) {
1234 VALUE klass = rb_eException;
1235 rb_execution_context_t *ec = GET_EC();
1236 if (NIL_P(exc))
1237 return Qnil;
1238 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, exc, mid, mid, klass, Qundef);
1239 info = exc_backtrace(exc);
1240 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, exc, mid, mid, klass, info);
1241 }
1242 else {
1243 info = rb_funcallv(exc, mid, 0, 0);
1244 }
1245 if (NIL_P(info)) return Qnil;
1246 return rb_check_backtrace(info);
1247 }
1248
1249 /*
1250 * call-seq:
1251 * exception.backtrace_locations -> array or nil
1252 *
1253 * Returns any backtrace associated with the exception. This method is
1254 * similar to Exception#backtrace, but the backtrace is an array of
1255 * Thread::Backtrace::Location.
1256 *
1257 * This method is not affected by Exception#set_backtrace().
1258 */
1259 static VALUE
1260 exc_backtrace_locations(VALUE exc)
1261 {
1262 VALUE obj;
1263
1264 obj = rb_attr_get(exc, id_bt_locations);
1265 if (!NIL_P(obj)) {
1266 obj = rb_backtrace_to_location_ary(obj);
1267 }
1268 return obj;
1269 }
1270
1271 static VALUE
1272 rb_check_backtrace(VALUE bt)
1273 {
1274 long i;
1275 static const char err[] = "backtrace must be Array of String";
1276
1277 if (!NIL_P(bt)) {
1278 if (RB_TYPE_P(bt, T_STRING)) return rb_ary_new3(1, bt);
1279 if (rb_backtrace_p(bt)) return bt;
1280 if (!RB_TYPE_P(bt, T_ARRAY)) {
1281 rb_raise(rb_eTypeError, err);
1282 }
1283 for (i=0;i<RARRAY_LEN(bt);i++) {
1284 VALUE e = RARRAY_AREF(bt, i);
1285 if (!RB_TYPE_P(e, T_STRING)) {
1286 rb_raise(rb_eTypeError, err);
1287 }
1288 }
1289 }
1290 return bt;
1291 }
1292
1293 /*
1294 * call-seq:
1295 * exc.set_backtrace(backtrace) -> array
1296 *
1297 * Sets the backtrace information associated with +exc+. The +backtrace+ must
1298 * be an array of String objects or a single String in the format described
1299 * in Exception#backtrace.
1300 *
1301 */
1302
1303 static VALUE
1304 exc_set_backtrace(VALUE exc, VALUE bt)
1305 {
1306 return rb_ivar_set(exc, id_bt, rb_check_backtrace(bt));
1307 }
1308
1309 MJIT_FUNC_EXPORTED VALUE
1310 rb_exc_set_backtrace(VALUE exc, VALUE bt)
1311 {
1312 return exc_set_backtrace(exc, bt);
1313 }
1314
1315 /*
1316 * call-seq:
1317 * exception.cause -> an_exception or nil
1318 *
1319 * Returns the previous exception ($!) at the time this exception was raised.
1320 * This is useful for wrapping exceptions and retaining the original exception
1321 * information.
1322 */
1323
1324 static VALUE
1325 exc_cause(VALUE exc)
1326 {
1327 return rb_attr_get(exc, id_cause);
1328 }
1329
1330 static VALUE
1331 try_convert_to_exception(VALUE obj)
1332 {
1333 return rb_check_funcall(obj, idException, 0, 0);
1334 }
1335
1336 /*
1337 * call-seq:
1338 * exc == obj -> true or false
1339 *
1340 * Equality---If <i>obj</i> is not an Exception, returns
1341 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
1342 * <i>obj</i> share same class, messages, and backtrace.
1343 */
1344
1345 static VALUE
1346 exc_equal(VALUE exc, VALUE obj)
1347 {
1348 VALUE mesg, backtrace;
1349
1350 if (exc == obj) return Qtrue;
1351
1352 if (rb_obj_class(exc) != rb_obj_class(obj)) {
1353 int state;
1354
1355 obj = rb_protect(try_convert_to_exception, obj, &state);
1356 if (state || obj == Qundef) {
1357 rb_set_errinfo(Qnil);
1358 return Qfalse;
1359 }
1360 if (rb_obj_class(exc) != rb_obj_class(obj)) return Qfalse;
1361 mesg = rb_check_funcall(obj, id_message, 0, 0);
1362 if (mesg == Qundef) return Qfalse;
1363 backtrace = rb_check_funcall(obj, id_backtrace, 0, 0);
1364 if (backtrace == Qundef) return Qfalse;
1365 }
1366 else {
1367 mesg = rb_attr_get(obj, id_mesg);
1368 backtrace = exc_backtrace(obj);
1369 }
1370
1371 if (!rb_equal(rb_attr_get(exc, id_mesg), mesg))
1372 return Qfalse;
1373 if (!rb_equal(exc_backtrace(exc), backtrace))
1374 return Qfalse;
1375 return Qtrue;
1376 }
1377
1378 /*
1379 * call-seq:
1380 * SystemExit.new -> system_exit
1381 * SystemExit.new(status) -> system_exit
1382 * SystemExit.new(status, msg) -> system_exit
1383 * SystemExit.new(msg) -> system_exit
1384 *
1385 * Create a new +SystemExit+ exception with the given status and message.
1386 * Status is true, false, or an integer.
1387 * If status is not given, true is used.
1388 */
1389
1390 static VALUE
1391 exit_initialize(int argc, VALUE *argv, VALUE exc)
1392 {
1393 VALUE status;
1394 if (argc > 0) {
1395 status = *argv;
1396
1397 switch (status) {
1398 case Qtrue:
1399 status = INT2FIX(EXIT_SUCCESS);
1400 ++argv;
1401 --argc;
1402 break;
1403 case Qfalse:
1404 status = INT2FIX(EXIT_FAILURE);
1405 ++argv;
1406 --argc;
1407 break;
1408 default:
1409 status = rb_check_to_int(status);
1410 if (NIL_P(status)) {
1411 status = INT2FIX(EXIT_SUCCESS);
1412 }
1413 else {
1414 #if EXIT_SUCCESS != 0
1415 if (status == INT2FIX(0))
1416 status = INT2FIX(EXIT_SUCCESS);
1417 #endif
1418 ++argv;
1419 --argc;
1420 }
1421 break;
1422 }
1423 }
1424 else {
1425 status = INT2FIX(EXIT_SUCCESS);
1426 }
1427 rb_call_super(argc, argv);
1428 rb_ivar_set(exc, id_status, status);
1429 return exc;
1430 }
1431
1432
1433 /*
1434 * call-seq:
1435 * system_exit.status -> integer
1436 *
1437 * Return the status value associated with this system exit.
1438 */
1439
1440 static VALUE
1441 exit_status(VALUE exc)
1442 {
1443 return rb_attr_get(exc, id_status);
1444 }
1445
1446
1447 /*
1448 * call-seq:
1449 * system_exit.success? -> true or false
1450 *
1451 * Returns +true+ if exiting successful, +false+ if not.
1452 */
1453
1454 static VALUE
1455 exit_success_p(VALUE exc)
1456 {
1457 VALUE status_val = rb_attr_get(exc, id_status);
1458 int status;
1459
1460 if (NIL_P(status_val))
1461 return Qtrue;
1462 status = NUM2INT(status_val);
1463 if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS)
1464 return Qtrue;
1465
1466 return Qfalse;
1467 }
1468
1469 static VALUE
1470 err_init_recv(VALUE exc, VALUE recv)
1471 {
1472 if (recv != Qundef) rb_ivar_set(exc, id_recv, recv);
1473 return exc;
1474 }
1475
1476 /*
1477 * call-seq:
1478 * FrozenError.new(msg=nil, receiver: nil) -> frozen_error
1479 *
1480 * Construct a new FrozenError exception. If given the <i>receiver</i>
1481 * parameter may subsequently be examined using the FrozenError#receiver
1482 * method.
1483 *
1484 * a = [].freeze
1485 * raise FrozenError.new("can't modify frozen array", receiver: a)
1486 */
1487
1488 static VALUE
1489 frozen_err_initialize(int argc, VALUE *argv, VALUE self)
1490 {
1491 ID keywords[1];
1492 VALUE values[numberof(keywords)], options;
1493
1494 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
1495 keywords[0] = id_receiver;
1496 rb_get_kwargs(options, keywords, 0, numberof(values), values);
1497 rb_call_super(argc, argv);
1498 err_init_recv(self, values[0]);
1499 return self;
1500 }
1501
1502 /*
1503 * Document-method: FrozenError#receiver
1504 * call-seq:
1505 * frozen_error.receiver -> object
1506 *
1507 * Return the receiver associated with this FrozenError exception.
1508 */
1509
1510 #define frozen_err_receiver name_err_receiver
1511
1512 void
1513 rb_name_error(ID id, const char *fmt, ...)
1514 {
1515 VALUE exc, argv[2];
1516 va_list args;
1517
1518 va_start(args, fmt);
1519 argv[0] = rb_vsprintf(fmt, args);
1520 va_end(args);
1521
1522 argv[1] = ID2SYM(id);
1523 exc = rb_class_new_instance(2, argv, rb_eNameError);
1524 rb_exc_raise(exc);
1525 }
1526
1527 void
1528 rb_name_error_str(VALUE str, const char *fmt, ...)
1529 {
1530 VALUE exc, argv[2];
1531 va_list args;
1532
1533 va_start(args, fmt);
1534 argv[0] = rb_vsprintf(fmt, args);
1535 va_end(args);
1536
1537 argv[1] = str;
1538 exc = rb_class_new_instance(2, argv, rb_eNameError);
1539 rb_exc_raise(exc);
1540 }
1541
1542 static VALUE
1543 name_err_init_attr(VALUE exc, VALUE recv, VALUE method)
1544 {
1545 const rb_execution_context_t *ec = GET_EC();
1546 rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
1547 cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
1548 rb_ivar_set(exc, id_name, method);
1549 err_init_recv(exc, recv);
1550 if (cfp) rb_ivar_set(exc, id_iseq, rb_iseqw_new(cfp->iseq));
1551 return exc;
1552 }
1553
1554 /*
1555 * call-seq:
1556 * NameError.new(msg=nil, name=nil, receiver: nil) -> name_error
1557 *
1558 * Construct a new NameError exception. If given the <i>name</i>
1559 * parameter may subsequently be examined using the NameError#name
1560 * method. <i>receiver</i> parameter allows to pass object in
1561 * context of which the error happened. Example:
1562 *
1563 * [1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array
1564 * [1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
1565 */
1566
1567 static VALUE
1568 name_err_initialize(int argc, VALUE *argv, VALUE self)
1569 {
1570 ID keywords[1];
1571 VALUE values[numberof(keywords)], name, options;
1572
1573 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
1574 keywords[0] = id_receiver;
1575 rb_get_kwargs(options, keywords, 0, numberof(values), values);
1576 name = (argc > 1) ? argv[--argc] : Qnil;
1577 rb_call_super(argc, argv);
1578 name_err_init_attr(self, values[0], name);
1579 return self;
1580 }
1581
1582 static VALUE rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method);
1583
1584 static VALUE
1585 name_err_init(VALUE exc, VALUE mesg, VALUE recv, VALUE method)
1586 {
1587 exc_init(exc, rb_name_err_mesg_new(mesg, recv, method));
1588 return name_err_init_attr(exc, recv, method);
1589 }
1590
1591 VALUE
1592 rb_name_err_new(VALUE mesg, VALUE recv, VALUE method)
1593 {
1594 VALUE exc = rb_obj_alloc(rb_eNameError);
1595 return name_err_init(exc, mesg, recv, method);
1596 }
1597
1598 /*
1599 * call-seq:
1600 * name_error.name -> string or nil
1601 *
1602 * Return the name associated with this NameError exception.
1603 */
1604
1605 static VALUE
1606 name_err_name(VALUE self)
1607 {
1608 return rb_attr_get(self, id_name);
1609 }
1610
1611 /*
1612 * call-seq:
1613 * name_error.local_variables -> array
1614 *
1615 * Return a list of the local variable names defined where this
1616 * NameError exception was raised.
1617 *
1618 * Internal use only.
1619 */
1620
1621 static VALUE
1622 name_err_local_variables(VALUE self)
1623 {
1624 VALUE vars = rb_attr_get(self, id_local_variables);
1625
1626 if (NIL_P(vars)) {
1627 VALUE iseqw = rb_attr_get(self, id_iseq);
1628 if (!NIL_P(iseqw)) vars = rb_iseqw_local_variables(iseqw);
1629 if (NIL_P(vars)) vars = rb_ary_new();
1630 rb_ivar_set(self, id_local_variables, vars);
1631 }
1632 return vars;
1633 }
1634
1635 static VALUE
1636 nometh_err_init_attr(VALUE exc, VALUE args, int priv)
1637 {
1638 rb_ivar_set(exc, id_args, args);
1639 rb_ivar_set(exc, id_private_call_p, priv ? Qtrue : Qfalse);
1640 return exc;
1641 }
1642
1643 /*
1644 * call-seq:
1645 * NoMethodError.new(msg=nil, name=nil, args=nil, private=false, receiver: nil) -> no_method_error
1646 *
1647 * Construct a NoMethodError exception for a method of the given name
1648 * called with the given arguments. The name may be accessed using
1649 * the <code>#name</code> method on the resulting object, and the
1650 * arguments using the <code>#args</code> method.
1651 *
1652 * If <i>private</i> argument were passed, it designates method was
1653 * attempted to call in private context, and can be accessed with
1654 * <code>#private_call?</code> method.
1655 *
1656 * <i>receiver</i> argument stores an object whose method was called.
1657 */
1658
1659 static VALUE
1660 nometh_err_initialize(int argc, VALUE *argv, VALUE self)
1661 {
1662 int priv;
1663 VALUE args, options;
1664 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
1665 priv = (argc > 3) && (--argc, RTEST(argv[argc]));
1666 args = (argc > 2) ? argv[--argc] : Qnil;
1667 if (!NIL_P(options)) argv[argc++] = options;
1668 rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
1669 return nometh_err_init_attr(self, args, priv);
1670 }
1671
1672 VALUE
1673 rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv)
1674 {
1675 VALUE exc = rb_obj_alloc(rb_eNoMethodError);
1676 name_err_init(exc, mesg, recv, method);
1677 return nometh_err_init_attr(exc, args, priv);
1678 }
1679
1680 /* :nodoc: */
1681 enum {
1682 NAME_ERR_MESG__MESG,
1683 NAME_ERR_MESG__RECV,
1684 NAME_ERR_MESG__NAME,
1685 NAME_ERR_MESG_COUNT
1686 };
1687
1688 static void
1689 name_err_mesg_mark(void *p)
1690 {
1691 VALUE *ptr = p;
1692 rb_gc_mark_locations(ptr, ptr+NAME_ERR_MESG_COUNT);
1693 }
1694
1695 #define name_err_mesg_free RUBY_TYPED_DEFAULT_FREE
1696
1697 static size_t
1698 name_err_mesg_memsize(const void *p)
1699 {
1700 return NAME_ERR_MESG_COUNT * sizeof(VALUE);
1701 }
1702
1703 static const rb_data_type_t name_err_mesg_data_type = {
1704 "name_err_mesg",
1705 {
1706 name_err_mesg_mark,
1707 name_err_mesg_free,
1708 name_err_mesg_memsize,
1709 },
1710 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1711 };
1712
1713 /* :nodoc: */
1714 static VALUE
1715 rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method)
1716 {
1717 VALUE result = TypedData_Wrap_Struct(rb_cNameErrorMesg, &name_err_mesg_data_type, 0);
1718 VALUE *ptr = ALLOC_N(VALUE, NAME_ERR_MESG_COUNT);
1719
1720 ptr[NAME_ERR_MESG__MESG] = mesg;
1721 ptr[NAME_ERR_MESG__RECV] = recv;
1722 ptr[NAME_ERR_MESG__NAME] = method;
1723 RTYPEDDATA_DATA(result) = ptr;
1724 return result;
1725 }
1726
1727 /* :nodoc: */
1728 static VALUE
1729 name_err_mesg_equal(VALUE obj1, VALUE obj2)
1730 {
1731 VALUE *ptr1, *ptr2;
1732 int i;
1733
1734 if (obj1 == obj2) return Qtrue;
1735 if (rb_obj_class(obj2) != rb_cNameErrorMesg)
1736 return Qfalse;
1737
1738 TypedData_Get_Struct(obj1, VALUE, &name_err_mesg_data_type, ptr1);
1739 TypedData_Get_Struct(obj2, VALUE, &name_err_mesg_data_type, ptr2);
1740 for (i=0; i<NAME_ERR_MESG_COUNT; i++) {
1741 if (!rb_equal(ptr1[i], ptr2[i]))
1742 return Qfalse;
1743 }
1744 return Qtrue;
1745 }
1746
1747 /* :nodoc: */
1748 static VALUE
1749 name_err_mesg_to_str(VALUE obj)
1750 {
1751 VALUE *ptr, mesg;
1752 TypedData_Get_Struct(obj, VALUE, &name_err_mesg_data_type, ptr);
1753
1754 mesg = ptr[NAME_ERR_MESG__MESG];
1755 if (NIL_P(mesg)) return Qnil;
1756 else {
1757 struct RString s_str, d_str;
1758 VALUE c, s, d = 0, args[4];
1759 int state = 0, singleton = 0;
1760 rb_encoding *usascii = rb_usascii_encoding();
1761
1762 #define FAKE_CSTR(v, str) rb_setup_fake_str((v), (str), rb_strlen_lit(str), usascii)
1763 obj = ptr[NAME_ERR_MESG__RECV];
1764 switch (obj) {
1765 case Qnil:
1766 d = FAKE_CSTR(&d_str, "nil");
1767 break;
1768 case Qtrue:
1769 d = FAKE_CSTR(&d_str, "true");
1770 break;
1771 case Qfalse:
1772 d = FAKE_CSTR(&d_str, "false");
1773 break;
1774 default:
1775 d = rb_protect(rb_inspect, obj, &state);
1776 if (state)
1777 rb_set_errinfo(Qnil);
1778 if (NIL_P(d) || RSTRING_LEN(d) > 65) {
1779 d = rb_any_to_s(obj);
1780 }
1781 singleton = (RSTRING_LEN(d) > 0 && RSTRING_PTR(d)[0] == '#');
1782 break;
1783 }
1784 if (!singleton) {
1785 s = FAKE_CSTR(&s_str, ":");
1786 c = rb_class_name(CLASS_OF(obj));
1787 }
1788 else {
1789 c = s = FAKE_CSTR(&s_str, "");
1790 }
1791 args[0] = rb_obj_as_string(ptr[NAME_ERR_MESG__NAME]);
1792 args[1] = d;
1793 args[2] = s;
1794 args[3] = c;
1795 mesg = rb_str_format(4, args, mesg);
1796 }
1797 return mesg;
1798 }
1799
1800 /* :nodoc: */
1801 static VALUE
1802 name_err_mesg_dump(VALUE obj, VALUE limit)
1803 {
1804 return name_err_mesg_to_str(obj);
1805 }
1806
1807 /* :nodoc: */
1808 static VALUE
1809 name_err_mesg_load(VALUE klass, VALUE str)
1810 {
1811 return str;
1812 }
1813
1814 /*
1815 * call-seq:
1816 * name_error.receiver -> object
1817 *
1818 * Return the receiver associated with this NameError exception.
1819 */
1820
1821 static VALUE
1822 name_err_receiver(VALUE self)
1823 {
1824 VALUE *ptr, recv, mesg;
1825
1826 recv = rb_ivar_lookup(self, id_recv, Qundef);
1827 if (recv != Qundef) return recv;
1828
1829 mesg = rb_attr_get(self, id_mesg);
1830 if (!rb_typeddata_is_kind_of(mesg, &name_err_mesg_data_type)) {
1831 rb_raise(rb_eArgError, "no receiver is available");
1832 }
1833 ptr = DATA_PTR(mesg);
1834 return ptr[NAME_ERR_MESG__RECV];
1835 }
1836
1837 /*
1838 * call-seq:
1839 * no_method_error.args -> obj
1840 *
1841 * Return the arguments passed in as the third parameter to
1842 * the constructor.
1843 */
1844
1845 static VALUE
1846 nometh_err_args(VALUE self)
1847 {
1848 return rb_attr_get(self, id_args);
1849 }
1850
1851 /*
1852 * call-seq:
1853 * no_method_error.private_call? -> true or false
1854 *
1855 * Return true if the caused method was called as private.
1856 */
1857
1858 static VALUE
1859 nometh_err_private_call_p(VALUE self)
1860 {
1861 return rb_attr_get(self, id_private_call_p);
1862 }
1863
1864 void
1865 rb_invalid_str(const char *str, const char *type)
1866 {
1867 VALUE s = rb_str_new2(str);
1868
1869 rb_raise(rb_eArgError, "invalid value for %s: %+"PRIsVALUE, type, s);
1870 }
1871
1872 /*
1873 * call-seq:
1874 * key_error.receiver -> object
1875 *
1876 * Return the receiver associated with this KeyError exception.
1877 */
1878
1879 static VALUE
1880 key_err_receiver(VALUE self)
1881 {
1882 VALUE recv;
1883
1884 recv = rb_ivar_lookup(self, id_receiver, Qundef);
1885 if (recv != Qundef) return recv;
1886 rb_raise(rb_eArgError, "no receiver is available");
1887 }
1888
1889 /*
1890 * call-seq:
1891 * key_error.key -> object
1892 *
1893 * Return the key caused this KeyError exception.
1894 */
1895
1896 static VALUE
1897 key_err_key(VALUE self)
1898 {
1899 VALUE key;
1900
1901 key = rb_ivar_lookup(self, id_key, Qundef);
1902 if (key != Qundef) return key;
1903 rb_raise(rb_eArgError, "no key is available");
1904 }
1905
1906 VALUE
1907 rb_key_err_new(VALUE mesg, VALUE recv, VALUE key)
1908 {
1909 VALUE exc = rb_obj_alloc(rb_eKeyError);
1910 rb_ivar_set(exc, id_mesg, mesg);
1911 rb_ivar_set(exc, id_bt, Qnil);
1912 rb_ivar_set(exc, id_key, key);
1913 rb_ivar_set(exc, id_receiver, recv);
1914 return exc;
1915 }
1916
1917 /*
1918 * call-seq:
1919 * KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
1920 *
1921 * Construct a new +KeyError+ exception with the given message,
1922 * receiver and key.
1923 */
1924
1925 static VALUE
1926 key_err_initialize(int argc, VALUE *argv, VALUE self)
1927 {
1928 VALUE options;
1929
1930 rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
1931
1932 if (!NIL_P(options)) {
1933 ID keywords[2];
1934 VALUE values[numberof(keywords)];
1935 int i;
1936 keywords[0] = id_receiver;
1937 keywords[1] = id_key;
1938 rb_get_kwargs(options, keywords, 0, numberof(values), values);
1939 for (i = 0; i < numberof(values); ++i) {
1940 if (values[i] != Qundef) {
1941 rb_ivar_set(self, keywords[i], values[i]);
1942 }
1943 }
1944 }
1945
1946 return self;
1947 }
1948
1949 /*
1950 * call-seq:
1951 * SyntaxError.new([msg]) -> syntax_error
1952 *
1953 * Construct a SyntaxError exception.
1954 */
1955
1956 static VALUE
1957 syntax_error_initialize(int argc, VALUE *argv, VALUE self)
1958 {
1959 VALUE mesg;
1960 if (argc == 0) {
1961 mesg = rb_fstring_lit("compile error");
1962 argc = 1;
1963 argv = &mesg;
1964 }
1965 return rb_call_super(argc, argv);
1966 }
1967
1968 /*
1969 * Document-module: Errno
1970 *
1971 * Ruby exception objects are subclasses of Exception. However,
1972 * operating systems typically report errors using plain
1973 * integers. Module Errno is created dynamically to map these
1974 * operating system errors to Ruby classes, with each error number
1975 * generating its own subclass of SystemCallError. As the subclass
1976 * is created in module Errno, its name will start
1977 * <code>Errno::</code>.
1978 *
1979 * The names of the <code>Errno::</code> classes depend on the
1980 * environment in which Ruby runs. On a typical Unix or Windows
1981 * platform, there are Errno classes such as Errno::EACCES,
1982 * Errno::EAGAIN, Errno::EINTR, and so on.
1983 *
1984 * The integer operating system error number corresponding to a
1985 * particular error is available as the class constant
1986 * <code>Errno::</code><em>error</em><code>::Errno</code>.
1987 *
1988 * Errno::EACCES::Errno #=> 13
1989 * Errno::EAGAIN::Errno #=> 11
1990 * Errno::EINTR::Errno #=> 4
1991 *
1992 * The full list of operating system errors on your particular platform
1993 * are available as the constants of Errno.
1994 *
1995 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
1996 */
1997
1998 static st_table *syserr_tbl;
1999
2000 static VALUE
2001 set_syserr(int n, const char *name)
2002 {
2003 st_data_t error;
2004
2005 if (!st_lookup(syserr_tbl, n, &error)) {
2006 error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError);
2007
2008 /* capture nonblock errnos for WaitReadable/WaitWritable subclasses */
2009 switch (n) {
2010 case EAGAIN:
2011 rb_eEAGAIN = error;
2012
2013 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
2014 break;
2015 case EWOULDBLOCK:
2016 #endif
2017
2018 rb_eEWOULDBLOCK = error;
2019 break;
2020 case EINPROGRESS:
2021 rb_eEINPROGRESS = error;
2022 break;
2023 }
2024
2025 rb_define_const(error, "Errno", INT2NUM(n));
2026 st_add_direct(syserr_tbl, n, error);
2027 }
2028 else {
2029 rb_define_const(rb_mErrno, name, error);
2030 }
2031 return error;
2032 }
2033
2034 static VALUE
2035 get_syserr(int n)
2036 {
2037 st_data_t error;
2038
2039 if (!st_lookup(syserr_tbl, n, &error)) {
2040 char name[8]; /* some Windows' errno have 5 digits. */
2041
2042 snprintf(name, sizeof(name), "E%03d", n);
2043 error = set_syserr(n, name);
2044 }
2045 return error;
2046 }
2047
2048 /*
2049 * call-seq:
2050 * SystemCallError.new(msg, errno) -> system_call_error_subclass
2051 *
2052 * If _errno_ corresponds to a known system error code, constructs the
2053 * appropriate Errno class for that error, otherwise constructs a
2054 * generic SystemCallError object. The error number is subsequently
2055 * available via the #errno method.
2056 */
2057
2058 static VALUE
2059 syserr_initialize(int argc, VALUE *argv, VALUE self)
2060 {
2061 #if !defined(_WIN32)
2062 char *strerror();
2063 #endif
2064 const char *err;
2065 VALUE mesg, error, func, errmsg;
2066 VALUE klass = rb_obj_class(self);
2067
2068 if (klass == rb_eSystemCallError) {
2069 st_data_t data = (st_data_t)klass;
2070 rb_scan_args(argc, argv, "12", &mesg, &error, &func);
2071 if (argc == 1 && FIXNUM_P(mesg)) {
2072 error = mesg; mesg = Qnil;
2073 }
2074 if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) {
2075 klass = (VALUE)data;
2076 /* change class */
2077 if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */
2078 rb_raise(rb_eTypeError, "invalid instance type");
2079 }
2080 RBASIC_SET_CLASS(self, klass);
2081 }
2082 }
2083 else {
2084 rb_scan_args(argc, argv, "02", &mesg, &func);
2085 error = rb_const_get(klass, id_Errno);
2086 }
2087 if (!NIL_P(error)) err = strerror(NUM2INT(error));
2088 else err = "unknown error";
2089
2090 errmsg = rb_enc_str_new_cstr(err, rb_locale_encoding());
2091 if (!NIL_P(mesg)) {
2092 VALUE str = StringValue(mesg);
2093
2094 if (!NIL_P(func)) rb_str_catf(errmsg, " @ %"PRIsVALUE, func);
2095 rb_str_catf(errmsg, " - %"PRIsVALUE, str);
2096 }
2097 mesg = errmsg;
2098
2099 rb_call_super(1, &mesg);
2100 rb_ivar_set(self, id_errno, error);
2101 return self;
2102 }
2103
2104 /*
2105 * call-seq:
2106 * system_call_error.errno -> integer
2107 *
2108 * Return this SystemCallError's error number.
2109 */
2110
2111 static VALUE
2112 syserr_errno(VALUE self)
2113 {
2114 return rb_attr_get(self, id_errno);
2115 }
2116
2117 /*
2118 * call-seq:
2119 * system_call_error === other -> true or false
2120 *
2121 * Return +true+ if the receiver is a generic +SystemCallError+, or
2122 * if the error numbers +self+ and _other_ are the same.
2123 */
2124
2125 static VALUE
2126 syserr_eqq(VALUE self, VALUE exc)
2127 {
2128 VALUE num, e;
2129
2130 if (!rb_obj_is_kind_of(exc, rb_eSystemCallError)) {
2131 if (!rb_respond_to(exc, id_errno)) return Qfalse;
2132 }
2133 else if (self == rb_eSystemCallError) return Qtrue;
2134
2135 num = rb_attr_get(exc, id_errno);
2136 if (NIL_P(num)) {
2137 num = rb_funcallv(exc, id_errno, 0, 0);
2138 }
2139 e = rb_const_get(self, id_Errno);
2140 if (FIXNUM_P(num) ? num == e : rb_equal(num, e))
2141 return Qtrue;
2142 return Qfalse;
2143 }
2144
2145
2146 /*
2147 * Document-class: StandardError
2148 *
2149 * The most standard error types are subclasses of StandardError. A
2150 * rescue clause without an explicit Exception class will rescue all
2151 * StandardErrors (and only those).
2152 *
2153 * def foo
2154 * raise "Oups"
2155 * end
2156 * foo rescue "Hello" #=> "Hello"
2157 *
2158 * On the other hand:
2159 *
2160 * require 'does/not/exist' rescue "Hi"
2161 *
2162 * <em>raises the exception:</em>
2163 *
2164 * LoadError: no such file to load -- does/not/exist
2165 *
2166 */
2167
2168 /*
2169 * Document-class: SystemExit
2170 *
2171 * Raised by +exit+ to initiate the termination of the script.
2172 */
2173
2174 /*
2175 * Document-class: SignalException
2176 *
2177 * Raised when a signal is received.
2178 *
2179 * begin
2180 * Process.kill('HUP',Process.pid)
2181 * sleep # wait for receiver to handle signal sent by Process.kill
2182 * rescue SignalException => e
2183 * puts "received Exception #{e}"
2184 * end
2185 *
2186 * <em>produces:</em>
2187 *
2188 * received Exception SIGHUP
2189 */
2190
2191 /*
2192 * Document-class: Interrupt
2193 *
2194 * Raised when the interrupt signal is received, typically because the
2195 * user has pressed Control-C (on most posix platforms). As such, it is a
2196 * subclass of +SignalException+.
2197 *
2198 * begin
2199 * puts "Press ctrl-C when you get bored"
2200 * loop {}
2201 * rescue Interrupt => e
2202 * puts "Note: You will typically use Signal.trap instead."
2203 * end
2204 *
2205 * <em>produces:</em>
2206 *
2207 * Press ctrl-C when you get bored
2208 *
2209 * <em>then waits until it is interrupted with Control-C and then prints:</em>
2210 *
2211 * Note: You will typically use Signal.trap instead.
2212 */
2213
2214 /*
2215 * Document-class: TypeError
2216 *
2217 * Raised when encountering an object that is not of the expected type.
2218 *
2219 * [1, 2, 3].first("two")
2220 *
2221 * <em>raises the exception:</em>
2222 *
2223 * TypeError: no implicit conversion of String into Integer
2224 *
2225 */
2226
2227 /*
2228 * Document-class: ArgumentError
2229 *
2230 * Raised when the arguments are wrong and there isn't a more specific
2231 * Exception class.
2232 *
2233 * Ex: passing the wrong number of arguments
2234 *
2235 * [1, 2, 3].first(4, 5)
2236 *
2237 * <em>raises the exception:</em>
2238 *
2239 * ArgumentError: wrong number of arguments (given 2, expected 1)
2240 *
2241 * Ex: passing an argument that is not acceptable:
2242 *
2243 * [1, 2, 3].first(-4)
2244 *
2245 * <em>raises the exception:</em>
2246 *
2247 * ArgumentError: negative array size
2248 */
2249
2250 /*
2251 * Document-class: IndexError
2252 *
2253 * Raised when the given index is invalid.
2254 *
2255 * a = [:foo, :bar]
2256 * a.fetch(0) #=> :foo
2257 * a[4] #=> nil
2258 * a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
2259 *
2260 */
2261
2262 /*
2263 * Document-class: KeyError
2264 *
2265 * Raised when the specified key is not found. It is a subclass of
2266 * IndexError.
2267 *
2268 * h = {"foo" => :bar}
2269 * h.fetch("foo") #=> :bar
2270 * h.fetch("baz") #=> KeyError: key not found: "baz"
2271 *
2272 */
2273
2274 /*
2275 * Document-class: RangeError
2276 *
2277 * Raised when a given numerical value is out of range.
2278 *
2279 * [1, 2, 3].drop(1 << 100)
2280 *
2281 * <em>raises the exception:</em>
2282 *
2283 * RangeError: bignum too big to convert into `long'
2284 */
2285
2286 /*
2287 * Document-class: ScriptError
2288 *
2289 * ScriptError is the superclass for errors raised when a script
2290 * can not be executed because of a +LoadError+,
2291 * +NotImplementedError+ or a +SyntaxError+. Note these type of
2292 * +ScriptErrors+ are not +StandardError+ and will not be
2293 * rescued unless it is specified explicitly (or its ancestor
2294 * +Exception+).
2295 */
2296
2297 /*
2298 * Document-class: SyntaxError
2299 *
2300 * Raised when encountering Ruby code with an invalid syntax.
2301 *
2302 * eval("1+1=2")
2303 *
2304 * <em>raises the exception:</em>
2305 *
2306 * SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
2307 */
2308
2309 /*
2310 * Document-class: LoadError
2311 *
2312 * Raised when a file required (a Ruby script, extension library, ...)
2313 * fails to load.
2314 *
2315 * require 'this/file/does/not/exist'
2316 *
2317 * <em>raises the exception:</em>
2318 *
2319 * LoadError: no such file to load -- this/file/does/not/exist
2320 */
2321
2322 /*
2323 * Document-class: NotImplementedError
2324 *
2325 * Raised when a feature is not implemented on the current platform. For
2326 * example, methods depending on the +fsync+ or +fork+ system calls may
2327 * raise this exception if the underlying operating system or Ruby
2328 * runtime does not support them.
2329 *
2330 * Note that if +fork+ raises a +NotImplementedError+, then
2331 * <code>respond_to?(:fork)</code> returns +false+.
2332 */
2333
2334 /*
2335 * Document-class: NameError
2336 *
2337 * Raised when a given name is invalid or undefined.
2338 *
2339 * puts foo
2340 *
2341 * <em>raises the exception:</em>
2342 *
2343 * NameError: undefined local variable or method `foo' for main:Object
2344 *
2345 * Since constant names must start with a capital:
2346 *
2347 * Integer.const_set :answer, 42
2348 *
2349 * <em>raises the exception:</em>
2350 *
2351 * NameError: wrong constant name answer
2352 */
2353
2354 /*
2355 * Document-class: NoMethodError
2356 *
2357 * Raised when a method is called on a receiver which doesn't have it
2358 * defined and also fails to respond with +method_missing+.
2359 *
2360 * "hello".to_ary
2361 *
2362 * <em>raises the exception:</em>
2363 *
2364 * NoMethodError: undefined method `to_ary' for "hello":String
2365 */
2366
2367 /*
2368 * Document-class: FrozenError
2369 *
2370 * Raised when there is an attempt to modify a frozen object.
2371 *
2372 * [1, 2, 3].freeze << 4
2373 *
2374 * <em>raises the exception:</em>
2375 *
2376 * FrozenError: can't modify frozen Array
2377 */
2378
2379 /*
2380 * Document-class: RuntimeError
2381 *
2382 * A generic error class raised when an invalid operation is attempted.
2383 * Kernel#raise will raise a RuntimeError if no Exception class is
2384 * specified.
2385 *
2386 * raise "ouch"
2387 *
2388 * <em>raises the exception:</em>
2389 *
2390 * RuntimeError: ouch
2391 */
2392
2393 /*
2394 * Document-class: SecurityError
2395 *
2396 * No longer used by internal code.
2397 */
2398
2399 /*
2400 * Document-class: NoMemoryError
2401 *
2402 * Raised when memory allocation fails.
2403 */
2404
2405 /*
2406 * Document-class: SystemCallError
2407 *
2408 * SystemCallError is the base class for all low-level
2409 * platform-dependent errors.
2410 *
2411 * The errors available on the current platform are subclasses of
2412 * SystemCallError and are defined in the Errno module.
2413 *
2414 * File.open("does/not/exist")
2415 *
2416 * <em>raises the exception:</em>
2417 *
2418 * Errno::ENOENT: No such file or directory - does/not/exist
2419 */
2420
2421 /*
2422 * Document-class: EncodingError
2423 *
2424 * EncodingError is the base class for encoding errors.
2425 */
2426
2427 /*
2428 * Document-class: Encoding::CompatibilityError
2429 *
2430 * Raised by Encoding and String methods when the source encoding is
2431 * incompatible with the target encoding.
2432 */
2433
2434 /*
2435 * Document-class: fatal
2436 *
2437 * fatal is an Exception that is raised when Ruby has encountered a fatal
2438 * error and must exit.
2439 */
2440
2441 /*
2442 * Document-class: NameError::message
2443 * :nodoc:
2444 */
2445
2446 /*
2447 * \Class Exception and its subclasses are used to communicate between
2448 * Kernel#raise and +rescue+ statements in <code>begin ... end</code> blocks.
2449 *
2450 * An Exception object carries information about an exception:
2451 * - Its type (the exception's class).
2452 * - An optional descriptive message.
2453 * - Optional backtrace information.
2454 *
2455 * Some built-in subclasses of Exception have additional methods: e.g., NameError#name.
2456 *
2457 * == Defaults
2458 *
2459 * Two Ruby statements have default exception classes:
2460 * - +raise+: defaults to RuntimeError.
2461 * - +rescue+: defaults to StandardError.
2462 *
2463 * == Global Variables
2464 *
2465 * When an exception has been raised but not yet handled (in +rescue+,
2466 * +ensure+, +at_exit+ and +END+ blocks), two global variables are set:
2467 * - <code>$!</code> contains the current exception.
2468 * - <code>$@</code> contains its backtrace.
2469 *
2470 * == Custom Exceptions
2471 *
2472 * To provide additional or alternate information,
2473 * a program may create custom exception classes
2474 * that derive from the built-in exception classes.
2475 *
2476 * A good practice is for a library to create a single "generic" exception class
2477 * (typically a subclass of StandardError or RuntimeError)
2478 * and have its other exception classes derive from that class.
2479 * This allows the user to rescue the generic exception, thus catching all exceptions
2480 * the library may raise even if future versions of the library add new
2481 * exception subclasses.
2482 *
2483 * For example:
2484 *
2485 * class MyLibrary
2486 * class Error < ::StandardError
2487 * end
2488 *
2489 * class WidgetError < Error
2490 * end
2491 *
2492 * class FrobError < Error
2493 * end
2494 *
2495 * end
2496 *
2497 * To handle both MyLibrary::WidgetError and MyLibrary::FrobError the library
2498 * user can rescue MyLibrary::Error.
2499 *
2500 * == Built-In Exception Classes
2501 *
2502 * The built-in subclasses of Exception are:
2503 *
2504 * * NoMemoryError
2505 * * ScriptError
2506 * * LoadError
2507 * * NotImplementedError
2508 * * SyntaxError
2509 * * SecurityError
2510 * * SignalException
2511 * * Interrupt
2512 * * StandardError
2513 * * ArgumentError
2514 * * UncaughtThrowError
2515 * * EncodingError
2516 * * FiberError
2517 * * IOError
2518 * * EOFError
2519 * * IndexError
2520 * * KeyError
2521 * * StopIteration
2522 * * ClosedQueueError
2523 * * LocalJumpError
2524 * * NameError
2525 * * NoMethodError
2526 * * RangeError
2527 * * FloatDomainError
2528 * * RegexpError
2529 * * RuntimeError
2530 * * FrozenError
2531 * * SystemCallError
2532 * * Errno::*
2533 * * ThreadError
2534 * * TypeError
2535 * * ZeroDivisionError
2536 * * SystemExit
2537 * * SystemStackError
2538 * * fatal
2539 */
2540
2541 void
2542 Init_Exception(void)
2543 {
2544 rb_eException = rb_define_class("Exception", rb_cObject);
2545 rb_define_singleton_method(rb_eException, "exception", rb_class_new_instance, -1);
2546 rb_define_singleton_method(rb_eException, "to_tty?", exc_s_to_tty_p, 0);
2547 rb_define_method(rb_eException, "exception", exc_exception, -1);
2548 rb_define_method(rb_eException, "initialize", exc_initialize, -1);
2549 rb_define_method(rb_eException, "==", exc_equal, 1);
2550 rb_define_method(rb_eException, "to_s", exc_to_s, 0);
2551 rb_define_method(rb_eException, "message", exc_message, 0);
2552 rb_define_method(rb_eException, "full_message", exc_full_message, -1);
2553 rb_define_method(rb_eException, "inspect", exc_inspect, 0);
2554 rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
2555 rb_define_method(rb_eException, "backtrace_locations", exc_backtrace_locations, 0);
2556 rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
2557 rb_define_method(rb_eException, "cause", exc_cause, 0);
2558
2559 rb_eSystemExit = rb_define_class("SystemExit", rb_eException);
2560 rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1);
2561 rb_define_method(rb_eSystemExit, "status", exit_status, 0);
2562 rb_define_method(rb_eSystemExit, "success?", exit_success_p, 0);
2563
2564 rb_eFatal = rb_define_class("fatal", rb_eException);
2565 rb_eSignal = rb_define_class("SignalException", rb_eException);
2566 rb_eInterrupt = rb_define_class("Interrupt", rb_eSignal);
2567
2568 rb_eStandardError = rb_define_class("StandardError", rb_eException);
2569 rb_eTypeError = rb_define_class("TypeError", rb_eStandardError);
2570 rb_eArgError = rb_define_class("ArgumentError", rb_eStandardError);
2571 rb_eIndexError = rb_define_class("IndexError", rb_eStandardError);
2572 rb_eKeyError = rb_define_class("KeyError", rb_eIndexError);
2573 rb_define_method(rb_eKeyError, "initialize", key_err_initialize, -1);
2574 rb_define_method(rb_eKeyError, "receiver", key_err_receiver, 0);
2575 rb_define_method(rb_eKeyError, "key", key_err_key, 0);
2576 rb_eRangeError = rb_define_class("RangeError", rb_eStandardError);
2577
2578 rb_eScriptError = rb_define_class("ScriptError", rb_eException);
2579 rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
2580 rb_define_method(rb_eSyntaxError, "initialize", syntax_error_initialize, -1);
2581
2582 rb_eLoadError = rb_define_class("LoadError", rb_eScriptError);
2583 /* the path failed to load */
2584 rb_attr(rb_eLoadError, rb_intern_const("path"), 1, 0, Qfalse);
2585
2586 rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
2587
2588 rb_eNameError = rb_define_class("NameError", rb_eStandardError);
2589 rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1);
2590 rb_define_method(rb_eNameError, "name", name_err_name, 0);
2591 rb_define_method(rb_eNameError, "receiver", name_err_receiver, 0);
2592 rb_define_method(rb_eNameError, "local_variables", name_err_local_variables, 0);
2593 rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cData);
2594 rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1);
2595 rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0);
2596 rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_dump, 1);
2597 rb_define_singleton_method(rb_cNameErrorMesg, "_load", name_err_mesg_load, 1);
2598 rb_eNoMethodError = rb_define_class("NoMethodError", rb_eNameError);
2599 rb_define_method(rb_eNoMethodError, "initialize", nometh_err_initialize, -1);
2600 rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
2601 rb_define_method(rb_eNoMethodError, "private_call?", nometh_err_private_call_p, 0);
2602
2603 rb_eRuntimeError = rb_define_class("RuntimeError", rb_eStandardError);
2604 rb_eFrozenError = rb_define_class("FrozenError", rb_eRuntimeError);
2605 rb_define_method(rb_eFrozenError, "initialize", frozen_err_initialize, -1);
2606 rb_define_method(rb_eFrozenError, "receiver", frozen_err_receiver, 0);
2607 rb_eSecurityError = rb_define_class("SecurityError", rb_eException);
2608 rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
2609 rb_eEncodingError = rb_define_class("EncodingError", rb_eStandardError);
2610 rb_eEncCompatError = rb_define_class_under(rb_cEncoding, "CompatibilityError", rb_eEncodingError);
2611 rb_eNoMatchingPatternError = rb_define_class("NoMatchingPatternError", rb_eRuntimeError);
2612
2613 syserr_tbl = st_init_numtable();
2614 rb_eSystemCallError = rb_define_class("SystemCallError", rb_eStandardError);
2615 rb_define_method(rb_eSystemCallError, "initialize", syserr_initialize, -1);
2616 rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
2617 rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
2618
2619 rb_mErrno = rb_define_module("Errno");
2620
2621 rb_mWarning = rb_define_module("Warning");
2622 rb_define_singleton_method(rb_mWarning, "[]", rb_warning_s_aref, 1);
2623 rb_define_singleton_method(rb_mWarning, "[]=", rb_warning_s_aset, 2);
2624 rb_define_method(rb_mWarning, "warn", rb_warning_s_warn, 1);
2625 rb_extend_object(rb_mWarning, rb_mWarning);
2626
2627 /* :nodoc: */
2628 rb_cWarningBuffer = rb_define_class_under(rb_mWarning, "buffer", rb_cString);
2629 rb_define_method(rb_cWarningBuffer, "write", warning_write, -1);
2630
2631 id_cause = rb_intern_const("cause");
2632 id_message = rb_intern_const("message");
2633 id_backtrace = rb_intern_const("backtrace");
2634 id_key = rb_intern_const("key");
2635 id_args = rb_intern_const("args");
2636 id_receiver = rb_intern_const("receiver");
2637 id_private_call_p = rb_intern_const("private_call?");
2638 id_local_variables = rb_intern_const("local_variables");
2639 id_Errno = rb_intern_const("Errno");
2640 id_errno = rb_intern_const("errno");
2641 id_i_path = rb_intern_const("@path");
2642 id_warn = rb_intern_const("warn");
2643 id_top = rb_intern_const("top");
2644 id_bottom = rb_intern_const("bottom");
2645 id_iseq = rb_make_internal_id();
2646 id_recv = rb_make_internal_id();
2647 }
2648
2649 void
2650 rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
2651 {
2652 va_list args;
2653 VALUE mesg;
2654
2655 va_start(args, fmt);
2656 mesg = rb_enc_vsprintf(enc, fmt, args);
2657 va_end(args);
2658
2659 rb_exc_raise(rb_exc_new3(exc, mesg));
2660 }
2661
2662 void
2663 rb_vraise(VALUE exc, const char *fmt, va_list ap)
2664 {
2665 rb_exc_raise(rb_exc_new3(exc, rb_vsprintf(fmt, ap)));
2666 }
2667
2668 void
2669 rb_raise(VALUE exc, const char *fmt, ...)
2670 {
2671 va_list args;
2672 va_start(args, fmt);
2673 rb_vraise(exc, fmt, args);
2674 va_end(args);
2675 }
2676
2677 NORETURN(static void raise_loaderror(VALUE path, VALUE mesg));
2678
2679 static void
2680 raise_loaderror(VALUE path, VALUE mesg)
2681 {
2682 VALUE err = rb_exc_new3(rb_eLoadError, mesg);
2683 rb_ivar_set(err, id_i_path, path);
2684 rb_exc_raise(err);
2685 }
2686
2687 void
2688 rb_loaderror(const char *fmt, ...)
2689 {
2690 va_list args;
2691 VALUE mesg;
2692
2693 va_start(args, fmt);
2694 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
2695 va_end(args);
2696 raise_loaderror(Qnil, mesg);
2697 }
2698
2699 void
2700 rb_loaderror_with_path(VALUE path, const char *fmt, ...)
2701 {
2702 va_list args;
2703 VALUE mesg;
2704
2705 va_start(args, fmt);
2706 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
2707 va_end(args);
2708 raise_loaderror(path, mesg);
2709 }
2710
2711 void
2712 rb_notimplement(void)
2713 {
2714 rb_raise(rb_eNotImpError,
2715 "%"PRIsVALUE"() function is unimplemented on this machine",
2716 rb_id2str(rb_frame_this_func()));
2717 }
2718
2719 void
2720 rb_fatal(const char *fmt, ...)
2721 {
2722 va_list args;
2723 VALUE mesg;
2724
2725 if (! ruby_thread_has_gvl_p()) {
2726 /* The thread has no GVL. Object allocation impossible (cant run GC),
2727 * thus no message can be printed out. */
2728 fprintf(stderr, "[FATAL] rb_fatal() outside of GVL\n");
2729 rb_print_backtrace();
2730 die();
2731 }
2732
2733 va_start(args, fmt);
2734 mesg = rb_vsprintf(fmt, args);
2735 va_end(args);
2736
2737 rb_exc_fatal(rb_exc_new3(rb_eFatal, mesg));
2738 }
2739
2740 static VALUE
2741 make_errno_exc(const char *mesg)
2742 {
2743 int n = errno;
2744
2745 errno = 0;
2746 if (n == 0) {
2747 rb_bug("rb_sys_fail(%s) - errno == 0", mesg ? mesg : "");
2748 }
2749 return rb_syserr_new(n, mesg);
2750 }
2751
2752 static VALUE
2753 make_errno_exc_str(VALUE mesg)
2754 {
2755 int n = errno;
2756
2757 errno = 0;
2758 if (!mesg) mesg = Qnil;
2759 if (n == 0) {
2760 const char *s = !NIL_P(mesg) ? RSTRING_PTR(mesg) : "";
2761 rb_bug("rb_sys_fail_str(%s) - errno == 0", s);
2762 }
2763 return rb_syserr_new_str(n, mesg);
2764 }
2765
2766 VALUE
2767 rb_syserr_new(int n, const char *mesg)
2768 {
2769 VALUE arg;
2770 arg = mesg ? rb_str_new2(mesg) : Qnil;
2771 return rb_syserr_new_str(n, arg);
2772 }
2773
2774 VALUE
2775 rb_syserr_new_str(int n, VALUE arg)
2776 {
2777 return rb_class_new_instance(1, &arg, get_syserr(n));
2778 }
2779
2780 void
2781 rb_syserr_fail(int e, const char *mesg)
2782 {
2783 rb_exc_raise(rb_syserr_new(e, mesg));
2784 }
2785
2786 void
2787 rb_syserr_fail_str(int e, VALUE mesg)
2788 {
2789 rb_exc_raise(rb_syserr_new_str(e, mesg));
2790 }
2791
2792 void
2793 rb_sys_fail(const char *mesg)
2794 {
2795 rb_exc_raise(make_errno_exc(mesg));
2796 }
2797
2798 void
2799 rb_sys_fail_str(VALUE mesg)
2800 {
2801 rb_exc_raise(make_errno_exc_str(mesg));
2802 }
2803
2804 #ifdef RUBY_FUNCTION_NAME_STRING
2805 void
2806 rb_sys_fail_path_in(const char *func_name, VALUE path)
2807 {
2808 int n = errno;
2809
2810 errno = 0;
2811 rb_syserr_fail_path_in(func_name, n, path);
2812 }
2813
2814 void
2815 rb_syserr_fail_path_in(const char *func_name, int n, VALUE path)
2816 {
2817 VALUE args[2];
2818
2819 if (!path) path = Qnil;
2820 if (n == 0) {
2821 const char *s = !NIL_P(path) ? RSTRING_PTR(path) : "";
2822 if (!func_name) func_name = "(null)";
2823 rb_bug("rb_sys_fail_path_in(%s, %s) - errno == 0",
2824 func_name, s);
2825 }
2826 args[0] = path;
2827 args[1] = rb_str_new_cstr(func_name);
2828 rb_exc_raise(rb_class_new_instance(2, args, get_syserr(n)));
2829 }
2830 #endif
2831
2832 void
2833 rb_mod_sys_fail(VALUE mod, const char *mesg)
2834 {
2835 VALUE exc = make_errno_exc(mesg);
2836 rb_extend_object(exc, mod);
2837 rb_exc_raise(exc);
2838 }
2839
2840 void
2841 rb_mod_sys_fail_str(VALUE mod, VALUE mesg)
2842 {
2843 VALUE exc = make_errno_exc_str(mesg);
2844 rb_extend_object(exc, mod);
2845 rb_exc_raise(exc);
2846 }
2847
2848 void
2849 rb_mod_syserr_fail(VALUE mod, int e, const char *mesg)
2850 {
2851 VALUE exc = rb_syserr_new(e, mesg);
2852 rb_extend_object(exc, mod);
2853 rb_exc_raise(exc);
2854 }
2855
2856 void
2857 rb_mod_syserr_fail_str(VALUE mod, int e, VALUE mesg)
2858 {
2859 VALUE exc = rb_syserr_new_str(e, mesg);
2860 rb_extend_object(exc, mod);
2861 rb_exc_raise(exc);
2862 }
2863
2864 static void
2865 syserr_warning(VALUE mesg, int err)
2866 {
2867 rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
2868 rb_str_catf(mesg, ": %s\n", strerror(err));
2869 rb_write_warning_str(mesg);
2870 }
2871
2872 #if 0
2873 void
2874 rb_sys_warn(const char *fmt, ...)
2875 {
2876 if (!NIL_P(ruby_verbose)) {
2877 int errno_save = errno;
2878 with_warning_string(mesg, 0, fmt) {
2879 syserr_warning(mesg, errno_save);
2880 }
2881 errno = errno_save;
2882 }
2883 }
2884
2885 void
2886 rb_syserr_warn(int err, const char *fmt, ...)
2887 {
2888 if (!NIL_P(ruby_verbose)) {
2889 with_warning_string(mesg, 0, fmt) {
2890 syserr_warning(mesg, err);
2891 }
2892 }
2893 }
2894
2895 void
2896 rb_sys_enc_warn(rb_encoding *enc, const char *fmt, ...)
2897 {
2898 if (!NIL_P(ruby_verbose)) {
2899 int errno_save = errno;
2900 with_warning_string(mesg, enc, fmt) {
2901 syserr_warning(mesg, errno_save);
2902 }
2903 errno = errno_save;
2904 }
2905 }
2906
2907 void
2908 rb_syserr_enc_warn(int err, rb_encoding *enc, const char *fmt, ...)
2909 {
2910 if (!NIL_P(ruby_verbose)) {
2911 with_warning_string(mesg, enc, fmt) {
2912 syserr_warning(mesg, err);
2913 }
2914 }
2915 }
2916 #endif
2917
2918 void
2919 rb_sys_warning(const char *fmt, ...)
2920 {
2921 if (RTEST(ruby_verbose)) {
2922 int errno_save = errno;
2923 with_warning_string(mesg, 0, fmt) {
2924 syserr_warning(mesg, errno_save);
2925 }
2926 errno = errno_save;
2927 }
2928 }
2929
2930 #if 0
2931 void
2932 rb_syserr_warning(int err, const char *fmt, ...)
2933 {
2934 if (RTEST(ruby_verbose)) {
2935 with_warning_string(mesg, 0, fmt) {
2936 syserr_warning(mesg, err);
2937 }
2938 }
2939 }
2940 #endif
2941
2942 void
2943 rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...)
2944 {
2945 if (RTEST(ruby_verbose)) {
2946 int errno_save = errno;
2947 with_warning_string(mesg, enc, fmt) {
2948 syserr_warning(mesg, errno_save);
2949 }
2950 errno = errno_save;
2951 }
2952 }
2953
2954 void
2955 rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...)
2956 {
2957 if (RTEST(ruby_verbose)) {
2958 with_warning_string(mesg, enc, fmt) {
2959 syserr_warning(mesg, err);
2960 }
2961 }
2962 }
2963
2964 void
2965 rb_load_fail(VALUE path, const char *err)
2966 {
2967 VALUE mesg = rb_str_buf_new_cstr(err);
2968 rb_str_cat2(mesg, " -- ");
2969 rb_str_append(mesg, path); /* should be ASCII compatible */
2970 raise_loaderror(path, mesg);
2971 }
2972
2973 void
2974 rb_error_frozen(const char *what)
2975 {
2976 rb_raise(rb_eFrozenError, "can't modify frozen %s", what);
2977 }
2978
2979 void
2980 rb_frozen_error_raise(VALUE frozen_obj, const char *fmt, ...)
2981 {
2982 va_list args;
2983 VALUE exc, mesg;
2984
2985 va_start(args, fmt);
2986 mesg = rb_vsprintf(fmt, args);
2987 va_end(args);
2988 exc = rb_exc_new3(rb_eFrozenError, mesg);
2989 rb_ivar_set(exc, id_recv, frozen_obj);
2990 rb_exc_raise(exc);
2991 }
2992
2993 static VALUE
2994 inspect_frozen_obj(VALUE obj, VALUE mesg, int recur)
2995 {
2996 if (recur) {
2997 rb_str_cat_cstr(mesg, " ...");
2998 }
2999 else {
3000 rb_str_append(mesg, rb_inspect(obj));
3001 }
3002 return mesg;
3003 }
3004
3005 void
3006 rb_error_frozen_object(VALUE frozen_obj)
3007 {
3008 VALUE debug_info;
3009 const ID created_info = id_debug_created_info;
3010 VALUE mesg = rb_sprintf("can't modify frozen %"PRIsVALUE": ",
3011 CLASS_OF(frozen_obj));
3012 VALUE exc = rb_exc_new_str(rb_eFrozenError, mesg);
3013
3014 rb_ivar_set(exc, id_recv, frozen_obj);
3015 rb_exec_recursive(inspect_frozen_obj, frozen_obj, mesg);
3016
3017 if (!NIL_P(debug_info = rb_attr_get(frozen_obj, created_info))) {
3018 VALUE path = rb_ary_entry(debug_info, 0);
3019 VALUE line = rb_ary_entry(debug_info, 1);
3020
3021 rb_str_catf(mesg, ", created at %"PRIsVALUE":%"PRIsVALUE, path, line);
3022 }
3023 rb_exc_raise(exc);
3024 }
3025
3026 #undef rb_check_frozen
3027 void
3028 rb_check_frozen(VALUE obj)
3029 {
3030 rb_check_frozen_internal(obj);
3031 }
3032
3033 void
3034 rb_error_untrusted(VALUE obj)
3035 {
3036 rb_warning("rb_error_untrusted is deprecated and will be removed in Ruby 3.2.");
3037 }
3038
3039 #undef rb_check_trusted
3040 void
3041 rb_check_trusted(VALUE obj)
3042 {
3043 rb_warning("rb_check_trusted is deprecated and will be removed in Ruby 3.2.");
3044 }
3045
3046 void
3047 rb_check_copyable(VALUE obj, VALUE orig)
3048 {
3049 if (!FL_ABLE(obj)) return;
3050 rb_check_frozen_internal(obj);
3051 if (!FL_ABLE(orig)) return;
3052 }
3053
3054 void
3055 Init_syserr(void)
3056 {
3057 rb_eNOERROR = set_syserr(0, "NOERROR");
3058 #define defined_error(name, num) set_syserr((num), (name));
3059 #define undefined_error(name) set_syserr(0, (name));
3060 #include "known_errors.inc"
3061 #undef defined_error
3062 #undef undefined_error
3063 }
3064
3065 #include "warning.rbinc"
3066
3067 void
3068 Init_warning(void)
3069 {
3070 load_warning();
3071 }
3072
rb_eEAGAIN
VALUE rb_eEAGAIN
Definition: error.c:54
rb_str_end_with_asciichar
int rb_str_end_with_asciichar(VALUE str, int c)
Definition: io.c:7685
rb_io_puts
VALUE rb_io_puts(int, const VALUE *, VALUE)
Definition: io.c:7747
rb_get_kwargs
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Definition: class.c:1886
ID
unsigned long ID
Definition: ruby.h:103
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:505
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
sig
int sig
Definition: rb_mjit_min_header-2.7.0.h:10422
T_FALSE
#define T_FALSE
Definition: ruby.h:537
void
void
Definition: rb_mjit_min_header-2.7.0.h:13273
rb_method_basic_definition_p
#define rb_method_basic_definition_p(klass, mid)
Definition: rb_mjit_min_header-2.7.0.h:7900
idTo_s
@ idTo_s
Definition: rb_mjit_min_header-2.7.0.h:8721
Check_Type
#define Check_Type(v, t)
Definition: ruby.h:595
rb_str_vcatf
VALUE rb_str_vcatf(VALUE, const char *, va_list)
Definition: sprintf.c:1210
rb_check_id
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:919
rb_exc_new
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Definition: error.c:959
rb_str_new2
#define rb_str_new2
Definition: intern.h:903
vars
const VALUE int int int int int int VALUE * vars[]
Definition: rb_mjit_min_header-2.7.0.h:6462
rb_eScriptError
VALUE rb_eScriptError
Definition: error.c:937
NAME_ERR_MESG__RECV
@ NAME_ERR_MESG__RECV
Definition: error.c:1683
fwrite
size_t fwrite(const void *__restrict, size_t _size, size_t _n, FILE *)
rb_cData
RUBY_EXTERN VALUE rb_cData
Definition: ruby.h:2018
rb_check_type
void rb_check_type(VALUE x, int t)
Definition: error.c:837
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:972
rb_warning_category_update
void rb_warning_category_update(unsigned int mask, unsigned int bits)
Definition: error.c:157
rb_exc_new3
#define rb_exc_new3
Definition: intern.h:293
vsnprintf
int int vsnprintf(char *__restrict, size_t, const char *__restrict, __gnuc_va_list) __attribute__((__format__(__printf__
RB_PASS_CALLED_KEYWORDS
#define RB_PASS_CALLED_KEYWORDS
Definition: ruby.h:1980
rb_iseq_struct
Definition: vm_core.h:456
path
VALUE path
Definition: rb_mjit_min_header-2.7.0.h:7351
NAME_ERR_MESG_COUNT
@ NAME_ERR_MESG_COUNT
Definition: error.c:1685
id_bt
#define id_bt
Definition: error.c:951
report_bug
#define report_bug(file, line, fmt, ctx)
Definition: error.c:604
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:313
obj2
VALUE obj2
Definition: rb_mjit_min_header-2.7.0.h:7583
rb_bug_reporter_add
int rb_bug_reporter_add(void(*func)(FILE *, void *), void *data)
Definition: error.c:455
rb_warning
void rb_warning(const char *fmt,...)
Definition: error.c:334
rb_scan_args
#define rb_scan_args(argc, argvp, fmt,...)
Definition: rb_mjit_min_header-2.7.0.h:6372
isatty
int isatty(int __fildes)
EWOULDBLOCK
#define EWOULDBLOCK
Definition: rubysocket.h:134
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:263
rb_make_internal_id
ID rb_make_internal_id(void)
Definition: symbol.c:810
T_MASK
#define T_MASK
Definition: md5.c:131
snprintf
int snprintf(char *__restrict, size_t, const char *__restrict,...) __attribute__((__format__(__printf__
strchr
char * strchr(char *, char)
ruby_sighandler_t
RETSIGTYPE(* ruby_sighandler_t)(int)
Definition: vm_core.h:1642
rb_warning_category_enabled_p
MJIT_FUNC_EXPORTED bool rb_warning_category_enabled_p(rb_warning_category_t category)
Definition: error.c:164
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:1009
rb_gc_mark_locations
void rb_gc_mark_locations(const VALUE *start, const VALUE *end)
Definition: gc.c:4699
UNDEF_LEAKED
#define UNDEF_LEAKED
Definition: error.c:808
NUM2LONG
#define NUM2LONG(x)
Definition: ruby.h:679
RTYPEDDATA_TYPE
#define RTYPEDDATA_TYPE(v)
Definition: ruby.h:1178
rb_bug_errno
void rb_bug_errno(const char *mesg, int errno_arg)
Definition: error.c:667
rb_attr_get
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1084
EXEC_EVENT_HOOK
#define EXEC_EVENT_HOOK(ec_, flag_, self_, id_, called_id_, klass_, data_)
Definition: vm_core.h:1935
rb_data_type_struct::parent
const rb_data_type_t * parent
Definition: ruby.h:1158
fileno
int fileno(FILE *)
st_init_numtable
st_table * st_init_numtable(void)
Definition: st.c:653
rb_equal
VALUE rb_equal(VALUE, VALUE)
Same as Object#===, case equality.
Definition: object.c:124
rb_locale_encoding
rb_encoding * rb_locale_encoding(void)
Definition: encoding.c:1372
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_obj_as_string
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1440
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
rb_key_err_new
VALUE rb_key_err_new(VALUE mesg, VALUE recv, VALUE key)
Definition: error.c:1907
encoding.h
NAME_ERR_MESG__NAME
@ NAME_ERR_MESG__NAME
Definition: error.c:1684
ruby_verbose
#define ruby_verbose
Definition: ruby.h:1925
rb_intern
#define rb_intern(str)
rb_eSyntaxError
VALUE rb_eSyntaxError
Definition: error.c:938
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:560
rb_intern_const
#define rb_intern_const(str)
Definition: ruby.h:1879
TYPE
#define TYPE(x)
Definition: ruby.h:554
st_add_direct
void st_add_direct(st_table *tab, st_data_t key, st_data_t value)
Definition: st.c:1251
rb_name_err_new
VALUE rb_name_err_new(VALUE mesg, VALUE recv, VALUE method)
Definition: error.c:1592
rb_enc_vsprintf
VALUE rb_enc_vsprintf(rb_encoding *, const char *, va_list)
Definition: sprintf.c:1145
int
__inline__ int
Definition: rb_mjit_min_header-2.7.0.h:2839
rb_assert_failure
MJIT_FUNC_EXPORTED void rb_assert_failure(const char *file, int line, const char *name, const char *expr)
Definition: error.c:716
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:772
getenv
#define getenv(name)
Definition: win32.c:73
EINPROGRESS
#define EINPROGRESS
Definition: win32.h:498
rb_call_super
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:306
rb_eSignal
VALUE rb_eSignal
Definition: error.c:917
rb_error_write
void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlight, VALUE reverse)
Definition: eval_error.c:300
idException
@ idException
Definition: rb_mjit_min_header-2.7.0.h:8729
rb_inspect
VALUE rb_inspect(VALUE)
Convenient wrapper of Object::inspect.
Definition: object.c:551
StringValue
use StringValue() instead")))
rb_eIndexError
VALUE rb_eIndexError
Definition: error.c:924
RB_WARN_CATEGORY_NONE
@ RB_WARN_CATEGORY_NONE
Definition: internal.h:1559
rb_str_cat2
#define rb_str_cat2
Definition: intern.h:912
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:2314
Qundef
#define Qundef
Definition: ruby.h:470
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1755
rb_mod_syserr_fail
void rb_mod_syserr_fail(VALUE mod, int e, const char *mesg)
Definition: error.c:2849
EXIT_FAILURE
#define EXIT_FAILURE
Definition: eval_intern.h:32
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
GET_EC
#define GET_EC()
Definition: vm_core.h:1766
rb_eNameError
VALUE rb_eNameError
Definition: error.c:927
INT2NUM
#define INT2NUM(x)
Definition: ruby.h:1609
ptr
struct RIMemo * ptr
Definition: debug.c:74
rb_check_frozen
void rb_check_frozen(VALUE obj)
Definition: error.c:3028
T_DATA
#define T_DATA
Definition: ruby.h:538
rb_loaderror
void rb_loaderror(const char *fmt,...)
Definition: error.c:2688
Qfalse
#define Qfalse
Definition: ruby.h:467
ssize_t
_ssize_t ssize_t
Definition: rb_mjit_min_header-2.7.0.h:1329
rb_name_error
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:1513
id_cause
#define id_cause
Definition: error.c:946
rb_ivar_lookup
VALUE rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
Definition: variable.c:1035
rb_id2str
#define rb_id2str(id)
Definition: vm_backtrace.c:30
rb_stderr_tty_p
int rb_stderr_tty_p(void)
Definition: io.c:7958
rb_ary_new3
#define rb_ary_new3
Definition: intern.h:104
st.h
NULL
#define NULL
Definition: _sdbm.c:101
rb_print_backtrace
void rb_print_backtrace(void)
Definition: vm_dump.c:750
Init_syserr
void Init_syserr(void)
Definition: error.c:3055
rb_typeddata_inherited_p
int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent)
Definition: error.c:862
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:166
RBASIC_SET_CLASS
#define RBASIC_SET_CLASS(obj, cls)
Definition: internal.h:1983
name_err_mesg_free
#define name_err_mesg_free
Definition: error.c:1695
REPORT_BUG_BUFSIZ
#define REPORT_BUG_BUFSIZ
Definition: error.c:469
rb_fatal
void rb_fatal(const char *fmt,...)
Definition: error.c:2720
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:414
error
const rb_iseq_t const char * error
Definition: rb_mjit_min_header-2.7.0.h:13506
strlen
size_t strlen(const char *)
rb_check_frozen_internal
#define rb_check_frozen_internal(obj)
Definition: intern.h:305
rb_bug_for_fatal_signal
void rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *ctx, const char *fmt,...)
Definition: error.c:649
T_SYMBOL
#define T_SYMBOL
Definition: ruby.h:540
T_OBJECT
#define T_OBJECT
Definition: ruby.h:523
RB_WARN_CATEGORY_EXPERIMENTAL
@ RB_WARN_CATEGORY_EXPERIMENTAL
Definition: internal.h:1561
rb_eEncodingError
VALUE rb_eEncodingError
Definition: error.c:928
rb_respond_to
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:2190
rb_data_type_struct::wrap_struct_name
const char * wrap_struct_name
Definition: ruby.h:1149
rb_protect
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
Definition: eval.c:1071
rb_check_arity
#define rb_check_arity
Definition: intern.h:347
rb_eNoMatchingPatternError
VALUE rb_eNoMatchingPatternError
Definition: error.c:935
rb_backtrace_to_location_ary
VALUE rb_backtrace_to_location_ary(VALUE obj)
Definition: vm_backtrace.c:686
rb_enc_str_new_cstr
VALUE rb_enc_str_new_cstr(const char *, rb_encoding *)
Definition: string.c:836
RARRAY_LENINT
#define RARRAY_LENINT(ary)
Definition: ruby.h:1071
rb_error_frozen
void rb_error_frozen(const char *what)
Definition: error.c:2974
ALLOC_N
#define ALLOC_N(type, n)
Definition: ruby.h:1663
rb_funcallv
#define rb_funcallv(recv, mid, argc, argv)
Definition: rb_mjit_min_header-2.7.0.h:7899
with_warning_string
#define with_warning_string(mesg, enc, fmt)
Definition: error.c:306
rb_vsprintf
VALUE rb_vsprintf(const char *, va_list)
Definition: sprintf.c:1191
frozen_err_receiver
#define frozen_err_receiver
Definition: error.c:1510
idMesg
@ idMesg
Definition: rb_mjit_min_header-2.7.0.h:8728
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1512
rb_notimplement
void rb_notimplement(void)
Definition: error.c:2712
rb_execution_context_struct::cfp
rb_control_frame_t * cfp
Definition: vm_core.h:847
rb_eRangeError
VALUE rb_eRangeError
Definition: error.c:926
EAGAIN
#define EAGAIN
Definition: rb_mjit_min_header-2.7.0.h:10948
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.0.h:5742
rb_obj_class
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
WRITE_CONST
#define WRITE_CONST(fd, str)
Definition: error.c:685
rb_str_format
VALUE rb_str_format(int, const VALUE *, VALUE)
Definition: sprintf.c:204
rb_vraise
void rb_vraise(VALUE exc, const char *fmt, va_list ap)
Definition: error.c:2663
rb_load_fail
void rb_load_fail(VALUE path, const char *err)
Definition: error.c:2965
rb_syserr_fail
void rb_syserr_fail(int e, const char *mesg)
Definition: error.c:2781
strerror
RUBY_EXTERN char * strerror(int)
Definition: strerror.c:11
WEXITSTATUS
#define WEXITSTATUS(status)
Definition: error.c:47
DATA_PTR
#define DATA_PTR(dta)
Definition: ruby.h:1175
rb_backtrace_to_str_ary
VALUE rb_backtrace_to_str_ary(VALUE obj)
Definition: vm_backtrace.c:620
fprintf
int fprintf(FILE *__restrict, const char *__restrict,...) __attribute__((__format__(__printf__
rb_syserr_new
VALUE rb_syserr_new(int n, const char *mesg)
Definition: error.c:2767
rb_sys_enc_warning
void rb_sys_enc_warning(rb_encoding *enc, const char *fmt,...)
Definition: error.c:2943
FL_ABLE
#define FL_ABLE(x)
Definition: ruby.h:1351
rb_encoding
const typedef OnigEncodingType rb_encoding
Definition: encoding.h:115
rb_must_asciicompat
void rb_must_asciicompat(VALUE)
Definition: string.c:2166
rb_iseqw_new
VALUE rb_iseqw_new(const rb_iseq_t *)
Definition: iseq.c:1157
rb_nomethod_err_new
VALUE rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv)
Definition: error.c:1673
INIT_KW
#define INIT_KW(n)
rb_frame_this_func
ID rb_frame_this_func(void)
The original name of the current method.
Definition: eval.c:1182
rb_error_untrusted
void rb_error_untrusted(VALUE obj)
Definition: error.c:3034
i
uint32_t i
Definition: rb_mjit_min_header-2.7.0.h:5464
system
int system(const char *__string)
rb_ec_backtrace_location_ary
VALUE rb_ec_backtrace_location_ary(rb_execution_context_t *ec, long lev, long n)
rb_iseqw_local_variables
VALUE rb_iseqw_local_variables(VALUE iseqval)
Definition: iseq.c:3321
rb_check_trusted
void rb_check_trusted(VALUE obj)
Definition: error.c:3041
NAME_ERR_MESG__MESG
@ NAME_ERR_MESG__MESG
Definition: error.c:1682
rb_compile_warning
void rb_compile_warning(const char *file, int line, const char *fmt,...)
Definition: error.c:285
st_data_t
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
rb_builtin_type_name
const char * rb_builtin_type_name(int t)
Definition: error.c:761
mask
enum @11::@13::@14 mask
ruby_static_id_cause
ID ruby_static_id_cause
Definition: error.c:945
rb_get_message
VALUE rb_get_message(VALUE exc)
Definition: error.c:1051
Init_Exception
void Init_Exception(void)
Definition: error.c:2542
rb_cEncoding
VALUE rb_cEncoding
Definition: encoding.c:46
va_end
#define va_end(v)
Definition: rb_mjit_min_header-2.7.0.h:3979
rb_mod_sys_fail
void rb_mod_sys_fail(VALUE mod, const char *mesg)
Definition: error.c:2833
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1231
vm_core.h
stderr
#define stderr
Definition: rb_mjit_min_header-2.7.0.h:1485
rb_error_frozen_object
void rb_error_frozen_object(VALUE frozen_obj)
Definition: error.c:3006
rb_sys_fail
void rb_sys_fail(const char *mesg)
Definition: error.c:2793
rb_source_location_cstr
const char * rb_source_location_cstr(int *pline)
Definition: vm.c:1376
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:922
id_status
#define id_status
Definition: internal.h:1588
EXIT_SUCCESS
#define EXIT_SUCCESS
Definition: error.c:39
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:920
MAX_BUG_REPORTERS
#define MAX_BUG_REPORTERS
Definition: error.c:445
mod
#define mod(x, y)
Definition: date_strftime.c:28
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: ruby.h:1101
rb_control_frame_struct
Definition: vm_core.h:760
rb_async_bug_errno
void rb_async_bug_errno(const char *mesg, int errno_arg)
Definition: error.c:688
RTYPEDDATA_DATA
#define RTYPEDDATA_DATA(v)
Definition: ruby.h:1179
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:2692
rb_syserr_new_str
VALUE rb_syserr_new_str(int n, VALUE arg)
Definition: error.c:2775
FIXNUM_P
#define FIXNUM_P(f)
Definition: ruby.h:396
RString
Definition: ruby.h:988
rb_eEWOULDBLOCK
VALUE rb_eEWOULDBLOCK
Definition: error.c:55
rb_typeddata_is_instance_of
int rb_typeddata_is_instance_of(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:883
rb_frozen_error_raise
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Definition: error.c:2980
rb_check_to_int
VALUE rb_check_to_int(VALUE)
Tries to convert val into Integer.
Definition: object.c:3036
PRI_PIDT_PREFIX
#define PRI_PIDT_PREFIX
Definition: rb_mjit_min_header-2.7.0.h:103
rb_str_new_cstr
#define rb_str_new_cstr(str)
Definition: rb_mjit_min_header-2.7.0.h:6117
rb_syntax_error_append
VALUE rb_syntax_error_append(VALUE exc, VALUE file, int line, int column, rb_encoding *enc, const char *fmt, va_list args)
Definition: error.c:104
rb_invalid_str
void rb_invalid_str(const char *str, const char *type)
Definition: error.c:1865
getpid
pid_t getpid(void)
rb_extend_object
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition: eval.c:1700
rb_sys_warning
void rb_sys_warning(const char *fmt,...)
Definition: error.c:2919
key
key
Definition: openssl_missing.h:181
rb_eLoadError
VALUE rb_eLoadError
Definition: error.c:939
rb_report_bug_valist
void rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args)
Definition: error.c:710
RARRAY_CONST_PTR
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:1072
rb_typeddata_is_kind_of
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:872
rb_mod_syserr_fail_str
void rb_mod_syserr_fail_str(VALUE mod, int e, VALUE mesg)
Definition: error.c:2857
rb_eNotImpError
VALUE rb_eNotImpError
Definition: error.c:932
CLASS_OF
#define CLASS_OF(v)
Definition: ruby.h:484
rb_str_buf_append
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2950
fmt
const VALUE int int int int int int VALUE char * fmt
Definition: rb_mjit_min_header-2.7.0.h:6462
rb_eNoMethodError
VALUE rb_eNoMethodError
Definition: error.c:930
U
Definition: dtoa.c:290
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:1070
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:2010
n
const char size_t n
Definition: rb_mjit_min_header-2.7.0.h:5456
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4322
rb_str_cat_cstr
#define rb_str_cat_cstr(str, ptr)
Definition: rb_mjit_min_header-2.7.0.h:6126
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:667
NORETURN
NORETURN(static void die(void))
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1252
rb_funcall
#define rb_funcall(recv, mid, argc,...)
Definition: rb_mjit_min_header-2.7.0.h:6585
FAKE_CSTR
#define FAKE_CSTR(v, str)
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2965
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:634
rb_exc_new_cstr
VALUE rb_exc_new_cstr(VALUE etype, const char *s)
Definition: error.c:966
rb_backtrace_p
int rb_backtrace_p(VALUE obj)
Definition: vm_backtrace.c:446
internal.h
T_ARRAY
#define T_ARRAY
Definition: ruby.h:530
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.0.h:5601
abort
void abort(void) __attribute__((__noreturn__))
argv
char ** argv
Definition: ruby.c:223
rb_set_errinfo
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1895
rb_eSecurityError
VALUE rb_eSecurityError
Definition: error.c:931
rb_write_error_str
RUBY_EXTERN void rb_write_error_str(VALUE mesg)
Definition: io.c:7936
stdout
#define stdout
Definition: rb_mjit_min_header-2.7.0.h:1484
ruby_thread_has_gvl_p
int ruby_thread_has_gvl_p(void)
Definition: thread.c:1705
rb_vm_get_ruby_level_next_cfp
MJIT_FUNC_EXPORTED rb_control_frame_t * rb_vm_get_ruby_level_next_cfp(const rb_execution_context_t *ec, const rb_control_frame_t *cfp)
Definition: vm.c:553
rb_warn_deprecated
void rb_warn_deprecated(const char *fmt, const char *suggest,...)
Definition: error.c:364
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1197
rb_syserr_enc_warning
void rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt,...)
Definition: error.c:2955
klass
VALUE klass
Definition: rb_mjit_min_header-2.7.0.h:13254
st_data_t
unsigned long st_data_t
Definition: rb_mjit_min_header-2.7.0.h:5363
Init_warning
void Init_warning(void)
Definition: error.c:3068
rb_obj_alloc
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
Definition: object.c:1895
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
rb_control_frame_struct::iseq
const rb_iseq_t * iseq
Definition: vm_core.h:763
RUBY_TYPED_FREE_IMMEDIATELY
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1207
rb_cString
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:2044
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
rb_enc_raise
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Definition: error.c:2650
rb_warning_string
VALUE rb_warning_string(const char *fmt,...)
Definition: error.c:344
argc
int argc
Definition: ruby.c:222
write_or_abort
#define write_or_abort(fd, str, len)
Definition: error.c:684
rb_enc_warn
void rb_enc_warn(rb_encoding *enc, const char *fmt,...)
Definition: error.c:323
rb_exec_recursive
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
Definition: thread.c:5075
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:289
rb_sys_fail_str
void rb_sys_fail_str(VALUE mesg)
Definition: error.c:2799
rb_vm_bugreport
void rb_vm_bugreport(const void *)
Definition: vm_dump.c:918
rb_eKeyError
VALUE rb_eKeyError
Definition: error.c:925
rb_name_error_str
void rb_name_error_str(VALUE str, const char *fmt,...)
Definition: error.c:1528
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2880
recur
#define recur(fmt)
Definition: date_strptime.c:152
rb_eSystemCallError
VALUE rb_eSystemCallError
Definition: error.c:941
err
int err
Definition: win32.c:135
id_mesg
#define id_mesg
Definition: error.c:953
fputc
int fputc(int, FILE *)
rb_data_type_struct
Definition: ruby.h:1148
rb_String
VALUE rb_String(VALUE)
Equivalent to Kernel#String in Ruby.
Definition: object.c:3652
rb_eException
VALUE rb_eException
Definition: error.c:914
ruby_description
const char ruby_description[]
Definition: version.c:43
rb_str_new
#define rb_str_new(str, len)
Definition: rb_mjit_min_header-2.7.0.h:6116
rb_eFatal
VALUE rb_eFatal
Definition: error.c:918
MJIT_FUNC_EXPORTED
#define MJIT_FUNC_EXPORTED
Definition: defines.h:396
rb_check_typeddata
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:889
RUBY_EVENT_C_RETURN
#define RUBY_EVENT_C_RETURN
Definition: ruby.h:2248
rb_compile_warn
void rb_compile_warn(const char *file, int line, const char *fmt,...)
Definition: error.c:270
Qtrue
#define Qtrue
Definition: ruby.h:468
errno
int errno
rb_str_catf
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1237
rb_get_backtrace
VALUE rb_get_backtrace(VALUE exc)
Definition: error.c:1229
rb_class_name
VALUE rb_class_name(VALUE)
Definition: variable.c:274
rb_mErrno
VALUE rb_mErrno
Definition: error.c:942
len
uint8_t len
Definition: escape.c:17
rb_call_super_kw
VALUE rb_call_super_kw(int, const VALUE *, int)
Definition: vm_eval.c:298
SYMBOL_P
#define SYMBOL_P(x)
Definition: ruby.h:413
rb_check_copyable
void rb_check_copyable(VALUE obj, VALUE orig)
Definition: error.c:3047
rb_eEncCompatError
VALUE rb_eEncCompatError
Definition: error.c:929
rb_eEINPROGRESS
VALUE rb_eEINPROGRESS
Definition: error.c:56
rb_class_new_instance
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
Definition: object.c:1955
FUNC_MINIMIZED
FUNC_MINIMIZED(static void bug_important_message(FILE *out, const char *const msg, size_t len))
va_start
#define va_start(v, l)
Definition: rb_mjit_min_header-2.7.0.h:3978
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1300
RTYPEDDATA_P
#define RTYPEDDATA_P(v)
Definition: ruby.h:1177
rb_exc_fatal
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
Definition: eval.c:683
T_STRING
#define T_STRING
Definition: ruby.h:528
rb_string_value_ptr
char * rb_string_value_ptr(volatile VALUE *)
Definition: string.c:2186
RUBY_EVENT_C_CALL
#define RUBY_EVENT_C_CALL
Definition: ruby.h:2247
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:698
va_list
__gnuc_va_list va_list
Definition: rb_mjit_min_header-2.7.0.h:836
rb_builtin_class_name
const char * rb_builtin_class_name(VALUE x)
Definition: error.c:797
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:723
fputs
int fputs(const char *__restrict, FILE *__restrict)
RB_WARN_CATEGORY_DEPRECATED
@ RB_WARN_CATEGORY_DEPRECATED
Definition: internal.h:1560
rb_cNameErrorMesg
VALUE rb_cNameErrorMesg
Definition: error.c:934
builtin.h
id_name
#define id_name
Definition: error.c:954
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:715
Qnil
#define Qnil
Definition: ruby.h:469
rb_fstring_lit
#define rb_fstring_lit(str)
Definition: internal.h:2123
exc
const rb_iseq_t const VALUE exc
Definition: rb_mjit_min_header-2.7.0.h:13504
rb_str_buf_cat
#define rb_str_buf_cat
Definition: intern.h:910
st_lookup
int st_lookup(st_table *tab, st_data_t key, st_data_t *value)
Definition: st.c:1101
rb_eInterrupt
VALUE rb_eInterrupt
Definition: error.c:916
rb_warning_category_from_name
rb_warning_category_t rb_warning_category_from_name(VALUE category)
Definition: error.c:140
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:919
numberof
#define numberof(array)
Definition: etc.c:618
rb_str_buf_new_cstr
#define rb_str_buf_new_cstr(str)
Definition: rb_mjit_min_header-2.7.0.h:6125
rb_str_tmp_new
VALUE rb_str_tmp_new(long)
Definition: string.c:1343
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:1005
rb_loaderror_with_path
void rb_loaderror_with_path(VALUE path, const char *fmt,...)
Definition: error.c:2700
rb_eNoMemError
VALUE rb_eNoMemError
Definition: error.c:933
st_table
Definition: st.h:79
ruby_assert.h
id_debug_created_info
@ id_debug_created_info
Definition: id.h:129
RUBY_VM_PREVIOUS_CONTROL_FRAME
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:1384
rb_any_to_s
VALUE rb_any_to_s(VALUE)
Default implementation of #to_s.
Definition: object.c:527
rb_attr
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1163
rb_enc_str_new
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:796
T_TRUE
#define T_TRUE
Definition: ruby.h:536
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
Definition: object.c:692
rb_warning_category_t
rb_warning_category_t
Definition: internal.h:1558
RTEST
#define RTEST(v)
Definition: ruby.h:481
report_bug_valist
#define report_bug_valist(file, line, fmt, ctx, args)
Definition: error.c:613
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:39
rb_str_buf_new2
#define rb_str_buf_new2
Definition: intern.h:908
rb_exc_set_backtrace
MJIT_FUNC_EXPORTED VALUE rb_exc_set_backtrace(VALUE exc, VALUE bt)
Definition: error.c:1310
id_bt_locations
#define id_bt_locations
Definition: error.c:952
rb_unexpected_type
void rb_unexpected_type(VALUE x, int t)
Definition: error.c:852
rb_mod_sys_fail_str
void rb_mod_sys_fail_str(VALUE mod, VALUE mesg)
Definition: error.c:2841
__sFILE
Definition: vsnprintf.c:169
rb_eSystemExit
VALUE rb_eSystemExit
Definition: error.c:915
rb_obj_clone
VALUE rb_obj_clone(VALUE)
Almost same as Object::clone.
Definition: object.c:410
WIFEXITED
#define WIFEXITED(status)
Definition: error.c:43
rb_eFrozenError
VALUE rb_eFrozenError
Definition: error.c:921
rb_usascii_encoding
rb_encoding * rb_usascii_encoding(void)
Definition: encoding.c:1340
rb_syserr_fail_str
void rb_syserr_fail_str(int e, VALUE mesg)
Definition: error.c:2787
RSTRING_END
#define RSTRING_END(str)
Definition: ruby.h:1013
cfp
rb_control_frame_t * cfp
Definition: rb_mjit_min_header-2.7.0.h:14544
name
const char * name
Definition: nkf.c:208
rb_execution_context_struct
Definition: vm_core.h:843
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2387