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