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