Ruby 4.0.0dev (2025-12-02 revision d0bb505a04bcd6a3d86c01d7c402c1f6205e69b4)
error.c (d0bb505a04bcd6a3d86c01d7c402c1f6205e69b4)
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#ifdef HAVE_SYS_WAIT_H
27# include <sys/wait.h>
28#endif
29
30#if defined __APPLE__
31# include <AvailabilityMacros.h>
32#endif
33
34#include "internal.h"
35#include "internal/class.h"
36#include "internal/error.h"
37#include "internal/eval.h"
38#include "internal/hash.h"
39#include "internal/io.h"
40#include "internal/load.h"
41#include "internal/object.h"
42#include "internal/process.h"
43#include "internal/string.h"
44#include "internal/symbol.h"
45#include "internal/thread.h"
46#include "internal/variable.h"
47#include "ruby/encoding.h"
48#include "ruby/st.h"
49#include "ruby/util.h"
50#include "ruby_assert.h"
51#include "vm_core.h"
52#include "yjit.h"
53
54#include "builtin.h"
55
61#ifndef EXIT_SUCCESS
62#define EXIT_SUCCESS 0
63#endif
64
65#ifndef WIFEXITED
66#define WIFEXITED(status) 1
67#endif
68
69#ifndef WEXITSTATUS
70#define WEXITSTATUS(status) (status)
71#endif
72
73VALUE rb_iseqw_local_variables(VALUE iseqval);
74VALUE rb_iseqw_new(const rb_iseq_t *);
75int rb_str_end_with_asciichar(VALUE str, int c);
76
77long rb_backtrace_length_limit = -1;
78VALUE rb_eEAGAIN;
79VALUE rb_eEWOULDBLOCK;
80VALUE rb_eEINPROGRESS;
81static VALUE rb_mWarning;
82static VALUE rb_cWarningBuffer;
83
84static ID id_warn;
85static ID id_category;
86static ID id_deprecated;
87static ID id_experimental;
88static ID id_performance;
89static ID id_strict_unused_block;
90static VALUE sym_category;
91static VALUE sym_highlight;
92static struct {
93 st_table *id2enum, *enum2id;
94} warning_categories;
95
96extern const char *rb_dynamic_description;
97
98static const char *
99rb_strerrno(int err)
100{
101#define defined_error(name, num) if (err == (num)) return (name);
102#define undefined_error(name)
103#include "known_errors.inc"
104#undef defined_error
105#undef undefined_error
106 return NULL;
107}
108
109static int
110err_position_0(char *buf, long len, const char *file, int line)
111{
112 if (!file) {
113 return 0;
114 }
115 else if (line == 0) {
116 return snprintf(buf, len, "%s: ", file);
117 }
118 else {
119 return snprintf(buf, len, "%s:%d: ", file, line);
120 }
121}
122
123RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 5, 0)
124static VALUE
125err_vcatf(VALUE str, const char *pre, const char *file, int line,
126 const char *fmt, va_list args)
127{
128 if (file) {
129 rb_str_cat2(str, file);
130 if (line) rb_str_catf(str, ":%d", line);
131 rb_str_cat2(str, ": ");
132 }
133 if (pre) rb_str_cat2(str, pre);
134 rb_str_vcatf(str, fmt, args);
135 return str;
136}
137
138static VALUE syntax_error_with_path(VALUE, VALUE, VALUE*, rb_encoding*);
139
140VALUE
141rb_syntax_error_append(VALUE exc, VALUE file, int line, int column,
142 rb_encoding *enc, const char *fmt, va_list args)
143{
144 const char *fn = NIL_P(file) ? NULL : RSTRING_PTR(file);
145 if (!exc) {
146 VALUE mesg = rb_enc_str_new(0, 0, enc);
147 err_vcatf(mesg, NULL, fn, line, fmt, args);
148 rb_str_cat2(mesg, "\n");
149 rb_write_error_str(mesg);
150 }
151 else {
152 VALUE mesg;
153 exc = syntax_error_with_path(exc, file, &mesg, enc);
154 err_vcatf(mesg, NULL, fn, line, fmt, args);
155 }
156
157 return exc;
158}
159
160static unsigned int warning_disabled_categories = (
162 ~RB_WARN_CATEGORY_DEFAULT_BITS);
163
164static unsigned int
165rb_warning_category_mask(VALUE category)
166{
167 return 1U << rb_warning_category_from_name(category);
168}
169
171rb_warning_category_from_name(VALUE category)
172{
173 st_data_t cat_value;
174 ID cat_id;
175 Check_Type(category, T_SYMBOL);
176 if (!(cat_id = rb_check_id(&category)) ||
177 !st_lookup(warning_categories.id2enum, cat_id, &cat_value)) {
178 rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category);
179 }
180 return (rb_warning_category_t)cat_value;
181}
182
183static VALUE
184rb_warning_category_to_name(rb_warning_category_t category)
185{
186 st_data_t id;
187 if (!st_lookup(warning_categories.enum2id, category, &id)) {
188 rb_raise(rb_eArgError, "invalid category: %d", (int)category);
189 }
190 return id ? ID2SYM(id) : Qnil;
191}
192
193void
194rb_warning_category_update(unsigned int mask, unsigned int bits)
195{
196 warning_disabled_categories &= ~mask;
197 warning_disabled_categories |= mask & ~bits;
198}
199
200bool
201rb_warning_category_enabled_p(rb_warning_category_t category)
202{
203 return !(warning_disabled_categories & (1U << category));
204}
205
206/*
207 * call-seq:
208 * Warning[category] -> true or false
209 *
210 * Returns the flag to show the warning messages for +category+.
211 * Supported categories are:
212 *
213 * +:deprecated+ ::
214 * deprecation warnings
215 * * assignment of non-nil value to <code>$,</code> and <code>$;</code>
216 * * keyword arguments
217 * etc.
218 *
219 * +:experimental+ ::
220 * experimental features
221 *
222 * +:performance+ ::
223 * performance hints
224 * * Shape variation limit
225 */
226
227static VALUE
228rb_warning_s_aref(VALUE mod, VALUE category)
229{
230 rb_warning_category_t cat = rb_warning_category_from_name(category);
231 return RBOOL(rb_warning_category_enabled_p(cat));
232}
233
234/*
235 * call-seq:
236 * Warning[category] = flag -> flag
237 *
238 * Sets the warning flags for +category+.
239 * See Warning.[] for the categories.
240 */
241
242static VALUE
243rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag)
244{
245 unsigned int mask = rb_warning_category_mask(category);
246 unsigned int disabled = warning_disabled_categories;
247 if (!RTEST(flag))
248 disabled |= mask;
249 else
250 disabled &= ~mask;
251 warning_disabled_categories = disabled;
252 return flag;
253}
254
255/*
256 * call-seq:
257 * categories -> array
258 *
259 * Returns a list of the supported category symbols.
260 */
261
262static VALUE
263rb_warning_s_categories(VALUE mod)
264{
265 st_index_t num = warning_categories.id2enum->num_entries;
266 ID *ids = ALLOCA_N(ID, num);
267 num = st_keys(warning_categories.id2enum, ids, num);
268 VALUE ary = rb_ary_new_capa(num);
269 for (st_index_t i = 0; i < num; ++i) {
270 rb_ary_push(ary, ID2SYM(ids[i]));
271 }
272 return rb_ary_freeze(ary);
273}
274
275/*
276 * call-seq:
277 * warn(msg, category: nil) -> nil
278 *
279 * Writes warning message +msg+ to $stderr. This method is called by
280 * Ruby for all emitted warnings. A +category+ may be included with
281 * the warning.
282 *
283 * See the documentation of the Warning module for how to customize this.
284 */
285
286static VALUE
287rb_warning_s_warn(int argc, VALUE *argv, VALUE mod)
288{
289 VALUE str;
290 VALUE opt;
291 VALUE category = Qnil;
292
293 rb_scan_args(argc, argv, "1:", &str, &opt);
294 if (!NIL_P(opt)) rb_get_kwargs(opt, &id_category, 0, 1, &category);
295
296 Check_Type(str, T_STRING);
298 if (!NIL_P(category)) {
299 rb_warning_category_t cat = rb_warning_category_from_name(category);
300 if (!rb_warning_category_enabled_p(cat)) return Qnil;
301 }
302 rb_write_error_str(str);
303 return Qnil;
304}
305
306/*
307 * Document-module: Warning
308 *
309 * The Warning module contains a single method named #warn, and the
310 * module extends itself, making Warning.warn available.
311 * Warning.warn is called for all warnings issued by Ruby.
312 * By default, warnings are printed to $stderr.
313 *
314 * Changing the behavior of Warning.warn is useful to customize how warnings are
315 * handled by Ruby, for instance by filtering some warnings, and/or outputting
316 * warnings somewhere other than <tt>$stderr</tt>.
317 *
318 * If you want to change the behavior of Warning.warn you should use
319 * <tt>Warning.extend(MyNewModuleWithWarnMethod)</tt> and you can use +super+
320 * to get the default behavior of printing the warning to <tt>$stderr</tt>.
321 *
322 * Example:
323 * module MyWarningFilter
324 * def warn(message, category: nil, **kwargs)
325 * if /some warning I want to ignore/.match?(message)
326 * # ignore
327 * else
328 * super
329 * end
330 * end
331 * end
332 * Warning.extend MyWarningFilter
333 *
334 * You should never redefine Warning#warn (the instance method), as that will
335 * then no longer provide a way to use the default behavior.
336 *
337 * The warning[https://rubygems.org/gems/warning] gem provides convenient ways to customize Warning.warn.
338 */
339
340static VALUE
341rb_warning_warn(VALUE mod, VALUE str)
342{
343 return rb_funcallv(mod, id_warn, 1, &str);
344}
345
346
347static int
348rb_warning_warn_arity(void)
349{
350 const rb_method_entry_t *me = rb_method_entry(rb_singleton_class(rb_mWarning), id_warn);
351 return me ? rb_method_entry_arity(me) : 1;
352}
353
354static VALUE
355rb_warn_category(VALUE str, VALUE category)
356{
357 if (RUBY_DEBUG && !NIL_P(category)) {
358 rb_warning_category_from_name(category);
359 }
360
361 if (rb_warning_warn_arity() == 1) {
362 return rb_warning_warn(rb_mWarning, str);
363 }
364 else {
365 VALUE args[2];
366 args[0] = str;
367 args[1] = rb_hash_new();
368 rb_hash_aset(args[1], sym_category, category);
369 return rb_funcallv_kw(rb_mWarning, id_warn, 2, args, RB_PASS_KEYWORDS);
370 }
371}
372
373static void
374rb_write_warning_str(VALUE str)
375{
376 rb_warning_warn(rb_mWarning, str);
377}
378
379RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 4, 0)
380static VALUE
381warn_vsprintf(rb_encoding *enc, const char *file, int line, const char *fmt, va_list args)
382{
383 VALUE str = rb_enc_str_new(0, 0, enc);
384
385 err_vcatf(str, "warning: ", file, line, fmt, args);
386 return rb_str_cat2(str, "\n");
387}
388
389#define with_warn_vsprintf(enc, file, line, fmt) \
390 VALUE str; \
391 va_list args; \
392 va_start(args, fmt); \
393 str = warn_vsprintf(enc, file, line, fmt, args); \
394 va_end(args);
395
396void
397rb_compile_warn(const char *file, int line, const char *fmt, ...)
398{
399 if (!NIL_P(ruby_verbose)) {
400 with_warn_vsprintf(NULL, file, line, fmt) {
401 rb_write_warning_str(str);
402 }
403 }
404}
405
406void
407rb_enc_compile_warn(rb_encoding *enc, const char *file, int line, const char *fmt, ...)
408{
409 if (!NIL_P(ruby_verbose)) {
410 with_warn_vsprintf(enc, file, line, fmt) {
411 rb_write_warning_str(str);
412 }
413 }
414}
415
416/* rb_compile_warning() reports only in verbose mode */
417void
418rb_compile_warning(const char *file, int line, const char *fmt, ...)
419{
420 if (RTEST(ruby_verbose)) {
421 with_warn_vsprintf(NULL, file, line, fmt) {
422 rb_write_warning_str(str);
423 }
424 }
425}
426
427/* rb_enc_compile_warning() reports only in verbose mode */
428void
429rb_enc_compile_warning(rb_encoding *enc, const char *file, int line, const char *fmt, ...)
430{
431 if (RTEST(ruby_verbose)) {
432 with_warn_vsprintf(enc, file, line, fmt) {
433 rb_write_warning_str(str);
434 }
435 }
436}
437
438void
439rb_category_compile_warn(rb_warning_category_t category, const char *file, int line, const char *fmt, ...)
440{
441 if (!NIL_P(ruby_verbose)) {
442 with_warn_vsprintf(NULL, file, line, fmt) {
443 rb_warn_category(str, rb_warning_category_to_name(category));
444 }
445 }
446}
447
448RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
449static VALUE
450warning_string(rb_encoding *enc, const char *fmt, va_list args)
451{
452 int line;
453 const char *file = rb_source_location_cstr(&line);
454 return warn_vsprintf(enc, file, line, fmt, args);
455}
456
457#define with_warning_string(mesg, enc, fmt) \
458 with_warning_string_from(mesg, enc, fmt, fmt)
459#define with_warning_string_from(mesg, enc, fmt, last_arg) \
460 VALUE mesg; \
461 va_list args; va_start(args, last_arg); \
462 mesg = warning_string(enc, fmt, args); \
463 va_end(args);
464
465void
466rb_warn(const char *fmt, ...)
467{
468 if (!NIL_P(ruby_verbose)) {
469 with_warning_string(mesg, 0, fmt) {
470 rb_write_warning_str(mesg);
471 }
472 }
473}
474
475void
476rb_category_warn(rb_warning_category_t category, const char *fmt, ...)
477{
478 if (!NIL_P(ruby_verbose) && rb_warning_category_enabled_p(category)) {
479 with_warning_string(mesg, 0, fmt) {
480 rb_warn_category(mesg, rb_warning_category_to_name(category));
481 }
482 }
483}
484
485void
486rb_enc_warn(rb_encoding *enc, const char *fmt, ...)
487{
488 if (!NIL_P(ruby_verbose)) {
489 with_warning_string(mesg, enc, fmt) {
490 rb_write_warning_str(mesg);
491 }
492 }
493}
494
495/* rb_warning() reports only in verbose mode */
496void
497rb_warning(const char *fmt, ...)
498{
499 if (RTEST(ruby_verbose)) {
500 with_warning_string(mesg, 0, fmt) {
501 rb_write_warning_str(mesg);
502 }
503 }
504}
505
506/* rb_category_warning() reports only in verbose mode */
507void
508rb_category_warning(rb_warning_category_t category, const char *fmt, ...)
509{
510 if (RTEST(ruby_verbose) && rb_warning_category_enabled_p(category)) {
511 with_warning_string(mesg, 0, fmt) {
512 rb_warn_category(mesg, rb_warning_category_to_name(category));
513 }
514 }
515}
516
517VALUE
518rb_warning_string(const char *fmt, ...)
519{
520 with_warning_string(mesg, 0, fmt) {
521 }
522 return mesg;
523}
524
525#if 0
526void
527rb_enc_warning(rb_encoding *enc, const char *fmt, ...)
528{
529 if (RTEST(ruby_verbose)) {
530 with_warning_string(mesg, enc, fmt) {
531 rb_write_warning_str(mesg);
532 }
533 }
534}
535#endif
536
537static bool
538deprecation_warning_enabled(void)
539{
540 if (NIL_P(ruby_verbose)) return false;
541 if (!rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) return false;
542 return true;
543}
544
545static void
546warn_deprecated(VALUE mesg, const char *removal, const char *suggest)
547{
548 rb_str_set_len(mesg, RSTRING_LEN(mesg) - 1);
549 rb_str_cat_cstr(mesg, " is deprecated");
550 if (removal) {
551 rb_str_catf(mesg, " and will be removed in Ruby %s", removal);
552 }
553 if (suggest) rb_str_catf(mesg, "; use %s instead", suggest);
554 rb_str_cat_cstr(mesg, "\n");
555 rb_warn_category(mesg, ID2SYM(id_deprecated));
556}
557
558void
559rb_warn_deprecated(const char *fmt, const char *suggest, ...)
560{
561 if (!deprecation_warning_enabled()) return;
562
563 with_warning_string_from(mesg, 0, fmt, suggest) {
564 warn_deprecated(mesg, NULL, suggest);
565 }
566}
567
568void
569rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...)
570{
571 if (!deprecation_warning_enabled()) return;
572
573 with_warning_string_from(mesg, 0, fmt, suggest) {
574 warn_deprecated(mesg, removal, suggest);
575 }
576}
577
578void
579rb_warn_reserved_name(const char *coming, const char *fmt, ...)
580{
581 if (!deprecation_warning_enabled()) return;
582
583 with_warning_string_from(mesg, 0, fmt, fmt) {
584 rb_str_set_len(mesg, RSTRING_LEN(mesg) - 1);
585 rb_str_catf(mesg, " is reserved for Ruby %s\n", coming);
586 rb_warn_category(mesg, ID2SYM(id_deprecated));
587 }
588}
589
590static inline int
591end_with_asciichar(VALUE str, int c)
592{
593 return RB_TYPE_P(str, T_STRING) &&
594 rb_str_end_with_asciichar(str, c);
595}
596
597/* :nodoc: */
598static VALUE
599warning_write(int argc, VALUE *argv, VALUE buf)
600{
601 while (argc-- > 0) {
602 rb_str_append(buf, *argv++);
603 }
604 return buf;
605}
606
607VALUE rb_ec_backtrace_location_ary(const rb_execution_context_t *ec, long lev, long n, bool skip_internal);
608
609static VALUE
610rb_warn_m(rb_execution_context_t *ec, VALUE exc, VALUE msgs, VALUE uplevel, VALUE category)
611{
612 VALUE location = Qnil;
613 int argc = RARRAY_LENINT(msgs);
614 const VALUE *argv = RARRAY_CONST_PTR(msgs);
615
616 if (!NIL_P(ruby_verbose) && argc > 0) {
617 VALUE str = argv[0];
618 if (!NIL_P(uplevel)) {
619 long lev = NUM2LONG(uplevel);
620 if (lev < 0) {
621 rb_raise(rb_eArgError, "negative level (%ld)", lev);
622 }
623 location = rb_ec_backtrace_location_ary(ec, lev + 1, 1, TRUE);
624 if (!NIL_P(location)) {
625 location = rb_ary_entry(location, 0);
626 }
627 }
628 if (argc > 1 || !NIL_P(uplevel) || !end_with_asciichar(str, '\n')) {
629 VALUE path;
630 if (NIL_P(uplevel)) {
631 str = rb_str_tmp_new(0);
632 }
633 else if (NIL_P(location) ||
634 NIL_P(path = rb_funcall(location, rb_intern("path"), 0))) {
635 str = rb_str_new_cstr("warning: ");
636 }
637 else {
638 str = rb_sprintf("%s:%ld: warning: ",
639 rb_string_value_ptr(&path),
640 NUM2LONG(rb_funcall(location, rb_intern("lineno"), 0)));
641 }
642 RBASIC_SET_CLASS(str, rb_cWarningBuffer);
643 rb_io_puts(argc, argv, str);
644 RBASIC_SET_CLASS(str, rb_cString);
645 }
646
647 if (!NIL_P(category)) {
648 category = rb_to_symbol_type(category);
649 rb_warning_category_from_name(category);
650 }
651
652 if (exc == rb_mWarning) {
654 rb_write_error_str(str);
655 }
656 else {
657 rb_warn_category(str, category);
658 }
659 }
660 return Qnil;
661}
662
663#define MAX_BUG_REPORTERS 0x100
664
665static struct bug_reporters {
666 void (*func)(FILE *out, void *data);
667 void *data;
668} bug_reporters[MAX_BUG_REPORTERS];
669
670static int bug_reporters_size;
671
672int
673rb_bug_reporter_add(void (*func)(FILE *, void *), void *data)
674{
675 struct bug_reporters *reporter;
676 if (bug_reporters_size >= MAX_BUG_REPORTERS) {
677 return 0; /* failed to register */
678 }
679 reporter = &bug_reporters[bug_reporters_size++];
680 reporter->func = func;
681 reporter->data = data;
682
683 return 1;
684}
685
686/* returns true if x can not be used as file name */
687static bool
688path_sep_p(char x)
689{
690#if defined __CYGWIN__ || defined DOSISH
691# define PATH_SEP_ENCODING 1
692 // Assume that "/" is only the first byte in any encoding.
693 if (x == ':') return true; // drive letter or ADS
694 if (x == '\\') return true;
695#endif
696 return x == '/';
697}
698
700 const char *ptr;
701 size_t len;
702};
703
704static const char PATHSEP_REPLACE = '!';
705
706static char *
707append_pathname(char *p, const char *pe, VALUE str)
708{
709#ifdef PATH_SEP_ENCODING
710 rb_encoding *enc = rb_enc_get(str);
711#endif
712 const char *s = RSTRING_PTR(str);
713 const char *const se = s + RSTRING_LEN(str);
714 char c;
715
716 --pe; // for terminator
717
718 while (p < pe && s < se && (c = *s) != '\0') {
719 if (c == '.') {
720 if (s == se || !*s) break; // chomp "." basename
721 if (path_sep_p(s[1])) goto skipsep; // skip "./"
722 }
723 else if (path_sep_p(c)) {
724 // squeeze successive separators
725 *p++ = PATHSEP_REPLACE;
726 skipsep:
727 while (++s < se && path_sep_p(*s));
728 continue;
729 }
730 const char *const ss = s;
731 while (p < pe && s < se && *s && !path_sep_p(*s)) {
732#ifdef PATH_SEP_ENCODING
733 int n = rb_enc_mbclen(s, se, enc);
734#else
735 const int n = 1;
736#endif
737 p += n;
738 s += n;
739 }
740 if (s > ss) memcpy(p - (s - ss), ss, s - ss);
741 }
742
743 return p;
744}
745
746static char *
747append_basename(char *p, const char *pe, struct path_string *path, VALUE str)
748{
749 if (!path->ptr) {
750#ifdef PATH_SEP_ENCODING
751 rb_encoding *enc = rb_enc_get(str);
752#endif
753 const char *const b = RSTRING_PTR(str), *const e = RSTRING_END(str), *p = e;
754
755 while (p > b) {
756 if (path_sep_p(p[-1])) {
757#ifdef PATH_SEP_ENCODING
758 const char *t = rb_enc_prev_char(b, p, e, enc);
759 if (t == p-1) break;
760 p = t;
761#else
762 break;
763#endif
764 }
765 else {
766 --p;
767 }
768 }
769
770 path->ptr = p;
771 path->len = e - p;
772 }
773 size_t n = path->len;
774 if (p + n > pe) n = pe - p;
775 memcpy(p, path->ptr, n);
776 return p + n;
777}
778
779static void
780finish_report(FILE *out, rb_pid_t pid)
781{
782 if (out != stdout && out != stderr) fclose(out);
783#ifdef HAVE_WORKING_FORK
784 if (pid > 0) waitpid(pid, NULL, 0);
785#endif
786}
787
789 struct path_string exe, script;
790 rb_pid_t pid;
791 time_t time;
792};
793
794/*
795 * Open a bug report file to write. The `RUBY_CRASH_REPORT`
796 * environment variable can be set to define a template that is used
797 * to name bug report files. The template can contain % specifiers
798 * which are substituted by the following values when a bug report
799 * file is created:
800 *
801 * %% A single % character.
802 * %e The base name of the executable filename.
803 * %E Pathname of executable, with slashes ('/') replaced by
804 * exclamation marks ('!').
805 * %f Similar to %e with the main script filename.
806 * %F Similar to %E with the main script filename.
807 * %p PID of dumped process in decimal.
808 * %t Time of dump, expressed as seconds since the Epoch,
809 * 1970-01-01 00:00:00 +0000 (UTC).
810 * %NNN Octal char code, upto 3 digits.
811 */
812static char *
813expand_report_argument(const char **input_template, struct report_expansion *values,
814 char *buf, size_t size, bool word)
815{
816 char *p = buf;
817 char *end = buf + size;
818 const char *template = *input_template;
819 bool store = true;
820
821 if (p >= end-1 || !*template) return NULL;
822 do {
823 char c = *template++;
824 if (word && ISSPACE(c)) break;
825 if (!store) continue;
826 if (c == '%') {
827 size_t n;
828 switch (c = *template++) {
829 case 'e':
830 p = append_basename(p, end, &values->exe, rb_argv0);
831 continue;
832 case 'E':
833 p = append_pathname(p, end, rb_argv0);
834 continue;
835 case 'f':
836 p = append_basename(p, end, &values->script, GET_VM()->orig_progname);
837 continue;
838 case 'F':
839 p = append_pathname(p, end, GET_VM()->orig_progname);
840 continue;
841 case 'p':
842 if (!values->pid) values->pid = getpid();
843 snprintf(p, end-p, "%" PRI_PIDT_PREFIX "d", values->pid);
844 p += strlen(p);
845 continue;
846 case 't':
847 if (!values->time) values->time = time(NULL);
848 snprintf(p, end-p, "%" PRI_TIMET_PREFIX "d", values->time);
849 p += strlen(p);
850 continue;
851 default:
852 if (c >= '0' && c <= '7') {
853 c = (unsigned char)ruby_scan_oct(template-1, 3, &n);
854 template += n - 1;
855 if (!c) store = false;
856 }
857 break;
858 }
859 }
860 if (p < end-1) *p++ = c;
861 } while (*template);
862 *input_template = template;
863 *p = '\0';
864 return ++p;
865}
866
867FILE *ruby_popen_writer(char *const *argv, rb_pid_t *pid);
868
869static FILE *
870open_report_path(const char *template, char *buf, size_t size, rb_pid_t *pid)
871{
872 struct report_expansion values = {{0}};
873
874 if (!template) return NULL;
875 if (0) fprintf(stderr, "RUBY_CRASH_REPORT=%s\n", buf);
876 if (*template == '|') {
877 char *argv[16], *bufend = buf + size, *p;
878 int argc;
879 template++;
880 for (argc = 0; argc < numberof(argv) - 1; ++argc) {
881 while (*template && ISSPACE(*template)) template++;
882 p = expand_report_argument(&template, &values, buf, bufend-buf, true);
883 if (!p) break;
884 argv[argc] = buf;
885 buf = p;
886 }
887 argv[argc] = NULL;
888 if (!p) return ruby_popen_writer(argv, pid);
889 }
890 else if (*template) {
891 expand_report_argument(&template, &values, buf, size, false);
892 return fopen(buf, "w");
893 }
894 return NULL;
895}
896
897static const char *crash_report;
898
899/* SIGSEGV handler might have a very small stack. Thus we need to use it carefully. */
900#define REPORT_BUG_BUFSIZ 256
901static FILE *
902bug_report_file(const char *file, int line, rb_pid_t *pid)
903{
904 char buf[REPORT_BUG_BUFSIZ];
905 const char *report = crash_report;
906 if (!report) report = getenv("RUBY_CRASH_REPORT");
907 FILE *out = open_report_path(report, buf, sizeof(buf), pid);
908 int len = err_position_0(buf, sizeof(buf), file, line);
909
910 if (out) {
911 if ((ssize_t)fwrite(buf, 1, len, out) == (ssize_t)len) return out;
912 fclose(out);
913 }
914 if ((ssize_t)fwrite(buf, 1, len, stderr) == (ssize_t)len) {
915 return stderr;
916 }
917 if ((ssize_t)fwrite(buf, 1, len, stdout) == (ssize_t)len) {
918 return stdout;
919 }
920
921 return NULL;
922}
923
924FUNC_MINIMIZED(static void bug_important_message(FILE *out, const char *const msg, size_t len));
925
926static void
927bug_important_message(FILE *out, const char *const msg, size_t len)
928{
929 const char *const endmsg = msg + len;
930 const char *p = msg;
931
932 if (!len) return;
933 if (isatty(fileno(out))) {
934 static const char red[] = "\033[;31;1;7m";
935 static const char green[] = "\033[;32;7m";
936 static const char reset[] = "\033[m";
937 const char *e = strchr(p, '\n');
938 const int w = (int)(e - p);
939 do {
940 int i = (int)(e - p);
941 fputs(*p == ' ' ? green : red, out);
942 fwrite(p, 1, e - p, out);
943 for (; i < w; ++i) fputc(' ', out);
944 fputs(reset, out);
945 fputc('\n', out);
946 } while ((p = e + 1) < endmsg && (e = strchr(p, '\n')) != 0 && e > p + 1);
947 }
948 fwrite(p, 1, endmsg - p, out);
949}
950
951#undef CRASH_REPORTER_MAY_BE_CREATED
952#if defined(__APPLE__) && \
953 (!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 */
954# define CRASH_REPORTER_MAY_BE_CREATED
955#endif
956static void
957preface_dump(FILE *out)
958{
959#if defined __APPLE__
960 static const char msg[] = ""
961 "-- Crash Report log information "
962 "--------------------------------------------\n"
963 " See Crash Report log file in one of the following locations:\n"
964# ifdef CRASH_REPORTER_MAY_BE_CREATED
965 " * ~/Library/Logs/CrashReporter\n"
966 " * /Library/Logs/CrashReporter\n"
967# endif
968 " * ~/Library/Logs/DiagnosticReports\n"
969 " * /Library/Logs/DiagnosticReports\n"
970 " for more details.\n"
971 "Don't forget to include the above Crash Report log file in bug reports.\n"
972 "\n";
973 const size_t msglen = sizeof(msg) - 1;
974#else
975 const char *msg = NULL;
976 const size_t msglen = 0;
977#endif
978 bug_important_message(out, msg, msglen);
979}
980
981static void
982postscript_dump(FILE *out)
983{
984#if defined __APPLE__
985 static const char msg[] = ""
986 "[IMPORTANT]"
987 /*" ------------------------------------------------"*/
988 "\n""Don't forget to include the Crash Report log file under\n"
989# ifdef CRASH_REPORTER_MAY_BE_CREATED
990 "CrashReporter or "
991# endif
992 "DiagnosticReports directory in bug reports.\n"
993 /*"------------------------------------------------------------\n"*/
994 "\n";
995 const size_t msglen = sizeof(msg) - 1;
996#else
997 const char *msg = NULL;
998 const size_t msglen = 0;
999#endif
1000 bug_important_message(out, msg, msglen);
1001}
1002
1003RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
1004static void
1005bug_report_begin_valist(FILE *out, const char *fmt, va_list args)
1006{
1007 char buf[REPORT_BUG_BUFSIZ];
1008
1009 fputs("[BUG] ", out);
1010 vsnprintf(buf, sizeof(buf), fmt, args);
1011 fputs(buf, out);
1012 snprintf(buf, sizeof(buf), "\n%s\n\n", rb_dynamic_description);
1013 fputs(buf, out);
1014 preface_dump(out);
1015}
1016
1017#define bug_report_begin(out, fmt) do { \
1018 va_list args; \
1019 va_start(args, fmt); \
1020 bug_report_begin_valist(out, fmt, args); \
1021 va_end(args); \
1022} while (0)
1023
1024static void
1025bug_report_end(FILE *out, rb_pid_t pid)
1026{
1027 /* call additional bug reporters */
1028 {
1029 int i;
1030 for (i=0; i<bug_reporters_size; i++) {
1031 struct bug_reporters *reporter = &bug_reporters[i];
1032 (*reporter->func)(out, reporter->data);
1033 }
1034 }
1035 postscript_dump(out);
1036 finish_report(out, pid);
1037}
1038
1039#define report_bug(file, line, fmt, ctx) do { \
1040 rb_pid_t pid = -1; \
1041 FILE *out = bug_report_file(file, line, &pid); \
1042 if (out) { \
1043 bug_report_begin(out, fmt); \
1044 rb_vm_bugreport(ctx, out); \
1045 bug_report_end(out, pid); \
1046 } \
1047} while (0) \
1048
1049#define report_bug_valist(file, line, fmt, ctx, args) do { \
1050 rb_pid_t pid = -1; \
1051 FILE *out = bug_report_file(file, line, &pid); \
1052 if (out) { \
1053 bug_report_begin_valist(out, fmt, args); \
1054 rb_vm_bugreport(ctx, out); \
1055 bug_report_end(out, pid); \
1056 } \
1057} while (0) \
1058
1059void
1060ruby_set_crash_report(const char *template)
1061{
1062 crash_report = template;
1063#if RUBY_DEBUG
1064 rb_pid_t pid = -1;
1065 char buf[REPORT_BUG_BUFSIZ];
1066 FILE *out = open_report_path(template, buf, sizeof(buf), &pid);
1067 if (out) {
1068 time_t t = time(NULL);
1069 fprintf(out, "ruby_test_bug_report: %s", ctime(&t));
1070 finish_report(out, pid);
1071 }
1072#endif
1073}
1074
1075NORETURN(static void die(void));
1076static void
1077die(void)
1078{
1079#if defined(_WIN32) && defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 80
1080 /* mingw32 declares in stdlib.h but does not provide. */
1081 _set_abort_behavior( 0, _CALL_REPORTFAULT);
1082#endif
1083
1084 abort();
1085}
1086
1087RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 0)
1088static void
1089rb_bug_without_die_internal(const char *fmt, va_list args)
1090{
1091 const char *file = NULL;
1092 int line = 0;
1093
1094 if (rb_current_execution_context(false)) {
1095 file = rb_source_location_cstr(&line);
1096 }
1097
1098 report_bug_valist(file, line, fmt, NULL, args);
1099}
1100
1101RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 0)
1102void
1103rb_bug_without_die(const char *fmt, ...)
1104{
1105 va_list args;
1106 va_start(args, fmt);
1107 rb_bug_without_die_internal(fmt, args);
1108 va_end(args);
1109}
1110
1111void
1112rb_bug(const char *fmt, ...)
1113{
1114 va_list args;
1115 va_start(args, fmt);
1116 rb_bug_without_die_internal(fmt, args);
1117 va_end(args);
1118 die();
1119}
1120
1121void
1122rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler, int sig, const void *ctx, const char *fmt, ...)
1123{
1124 const char *file = NULL;
1125 int line = 0;
1126
1127 if (rb_current_execution_context(false)) {
1128 file = rb_source_location_cstr(&line);
1129 }
1130
1131 report_bug(file, line, fmt, ctx);
1132
1133 if (default_sighandler) default_sighandler(sig);
1134
1136 die();
1137}
1138
1139
1140void
1141rb_bug_errno(const char *mesg, int errno_arg)
1142{
1143 if (errno_arg == 0)
1144 rb_bug("%s: errno == 0 (NOERROR)", mesg);
1145 else {
1146 const char *errno_str = rb_strerrno(errno_arg);
1147 if (errno_str)
1148 rb_bug("%s: %s (%s)", mesg, strerror(errno_arg), errno_str);
1149 else
1150 rb_bug("%s: %s (%d)", mesg, strerror(errno_arg), errno_arg);
1151 }
1152}
1153
1154/*
1155 * this is safe to call inside signal handler and timer thread
1156 * (which isn't a Ruby Thread object)
1157 */
1158#define write_or_abort(fd, str, len) (write((fd), (str), (len)) < 0 ? abort() : (void)0)
1159#define WRITE_CONST(fd,str) write_or_abort((fd),(str),sizeof(str) - 1)
1160
1161void
1162rb_async_bug_errno(const char *mesg, int errno_arg)
1163{
1164 WRITE_CONST(2, "[ASYNC BUG] ");
1165 write_or_abort(2, mesg, strlen(mesg));
1166 WRITE_CONST(2, "\n");
1167
1168 if (errno_arg == 0) {
1169 WRITE_CONST(2, "errno == 0 (NOERROR)\n");
1170 }
1171 else {
1172 const char *errno_str = rb_strerrno(errno_arg);
1173
1174 if (!errno_str)
1175 errno_str = "undefined errno";
1176 write_or_abort(2, errno_str, strlen(errno_str));
1177 }
1178 WRITE_CONST(2, "\n\n");
1179 write_or_abort(2, rb_dynamic_description, strlen(rb_dynamic_description));
1180 abort();
1181}
1182
1183void
1184rb_report_bug_valist(VALUE file, int line, const char *fmt, va_list args)
1185{
1186 report_bug_valist(RSTRING_PTR(file), line, fmt, NULL, args);
1187}
1188
1189void
1190rb_assert_failure(const char *file, int line, const char *name, const char *expr)
1191{
1192 rb_assert_failure_detail(file, line, name, expr, NULL);
1193}
1194
1195void
1196rb_assert_failure_detail(const char *file, int line, const char *name, const char *expr,
1197 const char *fmt, ...)
1198{
1199 rb_pid_t pid = -1;
1200 FILE *out = bug_report_file(file, line, &pid);
1201 if (out) {
1202 fputs("Assertion Failed: ", out);
1203 if (name) fprintf(out, "%s:", name);
1204 fputs(expr, out);
1205
1206 if (fmt && *fmt) {
1207 va_list args;
1208 va_start(args, fmt);
1209 fputs(": ", out);
1210 vfprintf(out, fmt, args);
1211 va_end(args);
1212 }
1213 fprintf(out, "\n%s\n\n", rb_dynamic_description);
1214
1215 preface_dump(out);
1216 rb_vm_bugreport(NULL, out);
1217 bug_report_end(out, pid);
1218 }
1219
1220 die();
1221}
1222
1223static const char builtin_types[][10] = {
1224 "", /* 0x00, */
1225 "Object",
1226 "Class",
1227 "Module",
1228 "Float",
1229 "String",
1230 "Regexp",
1231 "Array",
1232 "Hash",
1233 "Struct",
1234 "Integer",
1235 "File",
1236 "Data", /* internal use: wrapped C pointers */
1237 "MatchData", /* data of $~ */
1238 "Complex",
1239 "Rational",
1240 "", /* 0x10 */
1241 "nil",
1242 "true",
1243 "false",
1244 "Symbol", /* :symbol */
1245 "Integer",
1246 "undef", /* internal use: #undef; should not happen */
1247 "", /* 0x17 */
1248 "", /* 0x18 */
1249 "", /* 0x19 */
1250 "<Memo>", /* internal use: general memo */
1251 "<Node>", /* internal use: syntax tree node */
1252 "<iClass>", /* internal use: mixed-in module holder */
1253};
1254
1255const char *
1256rb_builtin_type_name(int t)
1257{
1258 const char *name;
1259 if ((unsigned int)t >= numberof(builtin_types)) return 0;
1260 name = builtin_types[t];
1261 if (*name) return name;
1262 return 0;
1263}
1264
1265static VALUE
1266displaying_class_of(VALUE x)
1267{
1268 switch (x) {
1269 case Qfalse: return rb_fstring_cstr("false");
1270 case Qnil: return rb_fstring_cstr("nil");
1271 case Qtrue: return rb_fstring_cstr("true");
1272 default: return rb_obj_class(x);
1273 }
1274}
1275
1276static const char *
1277builtin_class_name(VALUE x)
1278{
1279 const char *etype;
1280
1281 if (NIL_P(x)) {
1282 etype = "nil";
1283 }
1284 else if (FIXNUM_P(x)) {
1285 etype = "Integer";
1286 }
1287 else if (SYMBOL_P(x)) {
1288 etype = "Symbol";
1289 }
1290 else if (RB_TYPE_P(x, T_TRUE)) {
1291 etype = "true";
1292 }
1293 else if (RB_TYPE_P(x, T_FALSE)) {
1294 etype = "false";
1295 }
1296 else {
1297 etype = NULL;
1298 }
1299 return etype;
1300}
1301
1302const char *
1303rb_builtin_class_name(VALUE x)
1304{
1305 const char *etype = builtin_class_name(x);
1306
1307 if (!etype) {
1308 etype = rb_obj_classname(x);
1309 }
1310 return etype;
1311}
1312
1313COLDFUNC NORETURN(static void unexpected_type(VALUE, int, int));
1314#define UNDEF_LEAKED "undef leaked to the Ruby space"
1315
1316static void
1317unexpected_type(VALUE x, int xt, int t)
1318{
1319 const char *tname = rb_builtin_type_name(t);
1320 VALUE mesg, exc = rb_eFatal;
1321
1322 if (tname) {
1323 mesg = rb_sprintf("wrong argument type %"PRIsVALUE" (expected %s)",
1324 displaying_class_of(x), tname);
1325 exc = rb_eTypeError;
1326 }
1327 else if (xt > T_MASK && xt <= 0x3f) {
1328 mesg = rb_sprintf("unknown type 0x%x (0x%x given, probably comes"
1329 " from extension library for ruby 1.8)", t, xt);
1330 }
1331 else {
1332 mesg = rb_sprintf("unknown type 0x%x (0x%x given)", t, xt);
1333 }
1334 rb_exc_raise(rb_exc_new_str(exc, mesg));
1335}
1336
1337void
1339{
1340 int xt;
1341
1342 if (RB_UNLIKELY(UNDEF_P(x))) {
1343 rb_bug(UNDEF_LEAKED);
1344 }
1345
1346 xt = TYPE(x);
1347 if (xt != t || (xt == T_DATA && rbimpl_rtypeddata_p(x))) {
1348 /*
1349 * Typed data is not simple `T_DATA`, but in a sense an
1350 * extension of `struct RVALUE`, which are incompatible with
1351 * each other except when inherited.
1352 *
1353 * So it is not enough to just check `T_DATA`, it must be
1354 * identified by its `type` using `Check_TypedStruct` instead.
1355 */
1356 unexpected_type(x, xt, t);
1357 }
1358}
1359
1360void
1362{
1363 if (RB_UNLIKELY(UNDEF_P(x))) {
1364 rb_bug(UNDEF_LEAKED);
1365 }
1366
1367 unexpected_type(x, TYPE(x), t);
1368}
1369
1370int
1372{
1373 while (child) {
1374 if (child == parent) return 1;
1375 child = child->parent;
1376 }
1377 return 0;
1378}
1379
1380int
1382{
1383 if (!RB_TYPE_P(obj, T_DATA) ||
1384 !RTYPEDDATA_P(obj) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
1385 return 0;
1386 }
1387 return 1;
1388}
1389
1390#undef rb_typeddata_is_instance_of
1391int
1392rb_typeddata_is_instance_of(VALUE obj, const rb_data_type_t *data_type)
1393{
1394 return rb_typeddata_is_instance_of_inline(obj, data_type);
1395}
1396
1397void *
1399{
1400 VALUE actual;
1401
1402 if (!RB_TYPE_P(obj, T_DATA)) {
1403 actual = displaying_class_of(obj);
1404 }
1405 else if (!RTYPEDDATA_P(obj)) {
1406 actual = displaying_class_of(obj);
1407 }
1408 else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
1409 const char *name = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
1410 actual = rb_str_new_cstr(name); /* or rb_fstring_cstr? not sure... */
1411 }
1412 else {
1413 return RTYPEDDATA_GET_DATA(obj);
1414 }
1415
1416 const char *expected = data_type->wrap_struct_name;
1417 rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
1418 actual, expected);
1419 UNREACHABLE_RETURN(NULL);
1420}
1421
1422/* exception classes */
1446
1450
1453static VALUE rb_eNOERROR;
1454
1455ID ruby_static_id_cause;
1456#define id_cause ruby_static_id_cause
1457static ID id_message, id_detailed_message, id_backtrace;
1458static ID id_key, id_matchee, id_args, id_Errno, id_errno, id_i_path;
1459static ID id_receiver, id_recv, id_iseq, id_local_variables;
1460static ID id_private_call_p, id_top, id_bottom;
1461#define id_bt idBt
1462#define id_bt_locations idBt_locations
1463#define id_mesg idMesg
1464#define id_name idName
1465
1466#undef rb_exc_new_cstr
1467
1468VALUE
1469rb_exc_new(VALUE etype, const char *ptr, long len)
1470{
1471 VALUE mesg = rb_str_new(ptr, len);
1472 return rb_class_new_instance(1, &mesg, etype);
1473}
1474
1475VALUE
1476rb_exc_new_cstr(VALUE etype, const char *s)
1477{
1478 return rb_exc_new(etype, s, strlen(s));
1479}
1480
1481VALUE
1483{
1484 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
1485 StringValue(str);
1486 return rb_class_new_instance(1, &str, etype);
1487}
1488
1489static VALUE
1490exc_init(VALUE exc, VALUE mesg)
1491{
1492 rb_ivar_set(exc, id_mesg, mesg);
1493 rb_ivar_set(exc, id_bt, Qnil);
1494
1495 return exc;
1496}
1497
1498/*
1499 * call-seq:
1500 * Exception.new(message = nil) -> exception
1501 *
1502 * Returns a new exception object.
1503 *
1504 * The given +message+ should be
1505 * a {string-convertible object}[rdoc-ref:implicit_conversion.rdoc@String-Convertible+Objects];
1506 * see method #message;
1507 * if not given, the message is the class name of the new instance
1508 * (which may be the name of a subclass):
1509 *
1510 * Examples:
1511 *
1512 * Exception.new # => #<Exception: Exception>
1513 * LoadError.new # => #<LoadError: LoadError> # Subclass of Exception.
1514 * Exception.new('Boom') # => #<Exception: Boom>
1515 *
1516 */
1517
1518static VALUE
1519exc_initialize(int argc, VALUE *argv, VALUE exc)
1520{
1521 VALUE arg;
1522
1523 arg = (!rb_check_arity(argc, 0, 1) ? Qnil : argv[0]);
1524 return exc_init(exc, arg);
1525}
1526
1527/*
1528 * Document-method: exception
1529 *
1530 * call-seq:
1531 * exception(message = nil) -> self or new_exception
1532 *
1533 * Returns an exception object of the same class as +self+;
1534 * useful for creating a similar exception, but with a different message.
1535 *
1536 * With +message+ +nil+, returns +self+:
1537 *
1538 * x0 = StandardError.new('Boom') # => #<StandardError: Boom>
1539 * x1 = x0.exception # => #<StandardError: Boom>
1540 * x0.__id__ == x1.__id__ # => true
1541 *
1542 * With {string-convertible object}[rdoc-ref:implicit_conversion.rdoc@String-Convertible+Objects]
1543 * +message+ (even the same as the original message),
1544 * returns a new exception object whose class is the same as +self+,
1545 * and whose message is the given +message+:
1546 *
1547 * x1 = x0.exception('Boom') # => #<StandardError: Boom>
1548 * x0..equal?(x1) # => false
1549 *
1550 */
1551
1552static VALUE
1553exc_exception(int argc, VALUE *argv, VALUE self)
1554{
1555 VALUE exc;
1556
1557 argc = rb_check_arity(argc, 0, 1);
1558 if (argc == 0) return self;
1559 if (argc == 1 && self == argv[0]) return self;
1560 exc = rb_obj_clone(self);
1561 rb_ivar_set(exc, id_mesg, argv[0]);
1562 return exc;
1563}
1564
1565/*
1566 * call-seq:
1567 * to_s -> string
1568 *
1569 * Returns a string representation of +self+:
1570 *
1571 * x = RuntimeError.new('Boom')
1572 * x.to_s # => "Boom"
1573 * x = RuntimeError.new
1574 * x.to_s # => "RuntimeError"
1575 *
1576 */
1577
1578static VALUE
1579exc_to_s(VALUE exc)
1580{
1581 VALUE mesg = rb_attr_get(exc, idMesg);
1582
1583 if (NIL_P(mesg)) return rb_class_name(CLASS_OF(exc));
1584 return rb_String(mesg);
1585}
1586
1587/* FIXME: Include eval_error.c */
1588void rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE opt, VALUE highlight, VALUE reverse);
1589
1590VALUE
1591rb_get_message(VALUE exc)
1592{
1593 VALUE e = rb_check_funcall(exc, id_message, 0, 0);
1594 if (UNDEF_P(e)) return Qnil;
1595 if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
1596 return e;
1597}
1598
1599VALUE
1600rb_get_detailed_message(VALUE exc, VALUE opt)
1601{
1602 VALUE e;
1603 if (NIL_P(opt)) {
1604 e = rb_check_funcall(exc, id_detailed_message, 0, 0);
1605 }
1606 else {
1607 e = rb_check_funcall_kw(exc, id_detailed_message, 1, &opt, 1);
1608 }
1609 if (UNDEF_P(e)) return Qnil;
1610 if (!RB_TYPE_P(e, T_STRING)) e = rb_check_string_type(e);
1611 return e;
1612}
1613
1614/*
1615 * call-seq:
1616 * Exception.to_tty? -> true or false
1617 *
1618 * Returns +true+ if exception messages will be sent to a terminal device.
1619 */
1620static VALUE
1621exc_s_to_tty_p(VALUE self)
1622{
1623 return RBOOL(rb_stderr_tty_p());
1624}
1625
1626static VALUE
1627check_highlight_keyword(VALUE opt, int auto_tty_detect)
1628{
1629 VALUE highlight = Qnil;
1630
1631 if (!NIL_P(opt)) {
1632 highlight = rb_hash_lookup(opt, sym_highlight);
1633
1634 switch (highlight) {
1635 default:
1636 rb_bool_expected(highlight, "highlight", TRUE);
1638 case Qtrue: case Qfalse: case Qnil: break;
1639 }
1640 }
1641
1642 if (NIL_P(highlight)) {
1643 highlight = RBOOL(auto_tty_detect && rb_stderr_tty_p());
1644 }
1645
1646 return highlight;
1647}
1648
1649static VALUE
1650check_order_keyword(VALUE opt)
1651{
1652 VALUE order = Qnil;
1653
1654 if (!NIL_P(opt)) {
1655 static VALUE kw_order;
1656 if (!kw_order) kw_order = ID2SYM(rb_intern_const("order"));
1657
1658 order = rb_hash_lookup(opt, kw_order);
1659
1660 if (order != Qnil) {
1661 ID id = rb_check_id(&order);
1662 if (id == id_bottom) order = Qtrue;
1663 else if (id == id_top) order = Qfalse;
1664 else {
1665 rb_raise(rb_eArgError, "expected :top or :bottom as "
1666 "order: %+"PRIsVALUE, order);
1667 }
1668 }
1669 }
1670
1671 if (NIL_P(order)) order = Qfalse;
1672
1673 return order;
1674}
1675
1676/*
1677 * call-seq:
1678 * full_message(highlight: true, order: :top) -> string
1679 *
1680 * Returns an enhanced message string:
1681 *
1682 * - Includes the exception class name.
1683 * - If the value of keyword +highlight+ is true (not +nil+ or +false+),
1684 * includes bolding ANSI codes (see below) to enhance the appearance of the message.
1685 * - Includes the {backtrace}[rdoc-ref:exceptions.md@Backtraces]:
1686 *
1687 * - If the value of keyword +order+ is +:top+ (the default),
1688 * lists the error message and the innermost backtrace entry first.
1689 * - If the value of keyword +order+ is +:bottom+,
1690 * lists the error message the innermost entry last.
1691 *
1692 * Example:
1693 *
1694 * def baz
1695 * begin
1696 * 1 / 0
1697 * rescue => x
1698 * pp x.message
1699 * pp x.full_message(highlight: false).split("\n")
1700 * pp x.full_message.split("\n")
1701 * end
1702 * end
1703 * def bar; baz; end
1704 * def foo; bar; end
1705 * foo
1706 *
1707 * Output:
1708 *
1709 * "divided by 0"
1710 * ["t.rb:3:in 'Integer#/': divided by 0 (ZeroDivisionError)",
1711 * "\tfrom t.rb:3:in 'Object#baz'",
1712 * "\tfrom t.rb:10:in 'Object#bar'",
1713 * "\tfrom t.rb:11:in 'Object#foo'",
1714 * "\tfrom t.rb:12:in '<main>'"]
1715 * ["t.rb:3:in 'Integer#/': \e[1mdivided by 0 (\e[1;4mZeroDivisionError\e[m\e[1m)\e[m",
1716 * "\tfrom t.rb:3:in 'Object#baz'",
1717 * "\tfrom t.rb:10:in 'Object#bar'",
1718 * "\tfrom t.rb:11:in 'Object#foo'",
1719 * "\tfrom t.rb:12:in '<main>'"]
1720 *
1721 * An overriding method should be careful with ANSI code enhancements;
1722 * see {Messages}[rdoc-ref:exceptions.md@Messages].
1723 */
1724
1725static VALUE
1726exc_full_message(int argc, VALUE *argv, VALUE exc)
1727{
1728 VALUE opt, str, emesg, errat;
1729 VALUE highlight, order;
1730
1731 rb_scan_args(argc, argv, "0:", &opt);
1732
1733 highlight = check_highlight_keyword(opt, 1);
1734 order = check_order_keyword(opt);
1735
1736 {
1737 if (NIL_P(opt)) opt = rb_hash_new();
1738 rb_hash_aset(opt, sym_highlight, highlight);
1739 }
1740
1741 str = rb_str_new2("");
1742 errat = rb_get_backtrace(exc);
1743 emesg = rb_get_detailed_message(exc, opt);
1744
1745 rb_error_write(exc, emesg, errat, str, opt, highlight, order);
1746 return str;
1747}
1748
1749/*
1750 * call-seq:
1751 * message -> string
1752 *
1753 * Returns #to_s.
1754 *
1755 * See {Messages}[rdoc-ref:exceptions.md@Messages].
1756 */
1757
1758static VALUE
1759exc_message(VALUE exc)
1760{
1761 return rb_funcallv(exc, idTo_s, 0, 0);
1762}
1763
1764/*
1765 * call-seq:
1766 * detailed_message(highlight: false, **kwargs) -> string
1767 *
1768 * Returns the message string with enhancements:
1769 *
1770 * - Includes the exception class name in the first line.
1771 * - If the value of keyword +highlight+ is +true+,
1772 * includes bolding and underlining ANSI codes (see below)
1773 * to enhance the appearance of the message.
1774 *
1775 * Examples:
1776 *
1777 * begin
1778 * 1 / 0
1779 * rescue => x
1780 * p x.message
1781 * p x.detailed_message # Class name added.
1782 * p x.detailed_message(highlight: true) # Class name, bolding, and underlining added.
1783 * end
1784 *
1785 * Output:
1786 *
1787 * "divided by 0"
1788 * "divided by 0 (ZeroDivisionError)"
1789 * "\e[1mdivided by 0 (\e[1;4mZeroDivisionError\e[m\e[1m)\e[m"
1790 *
1791 * This method is overridden by some gems in the Ruby standard library to add information:
1792 *
1793 * - DidYouMean::Correctable#detailed_message.
1794 * - ErrorHighlight::CoreExt#detailed_message.
1795 * - SyntaxSuggest#detailed_message.
1796 *
1797 * An overriding method must be tolerant of passed keyword arguments,
1798 * which may include (but may not be limited to):
1799 *
1800 * - +:highlight+.
1801 * - +:did_you_mean+.
1802 * - +:error_highlight+.
1803 * - +:syntax_suggest+.
1804 *
1805 * An overriding method should also be careful with ANSI code enhancements;
1806 * see {Messages}[rdoc-ref:exceptions.md@Messages].
1807 */
1808
1809static VALUE
1810exc_detailed_message(int argc, VALUE *argv, VALUE exc)
1811{
1812 VALUE opt;
1813
1814 rb_scan_args(argc, argv, "0:", &opt);
1815
1816 VALUE highlight = check_highlight_keyword(opt, 0);
1817
1818 extern VALUE rb_decorate_message(const VALUE eclass, VALUE emesg, int highlight);
1819
1820 return rb_decorate_message(CLASS_OF(exc), rb_get_message(exc), RTEST(highlight));
1821}
1822
1823/*
1824 * call-seq:
1825 * inspect -> string
1826 *
1827 * Returns a string representation of +self+:
1828 *
1829 * x = RuntimeError.new('Boom')
1830 * x.inspect # => "#<RuntimeError: Boom>"
1831 * x = RuntimeError.new
1832 * x.inspect # => "#<RuntimeError: RuntimeError>"
1833 *
1834 */
1835
1836static VALUE
1837exc_inspect(VALUE exc)
1838{
1839 VALUE str, klass;
1840
1841 klass = CLASS_OF(exc);
1842 exc = rb_obj_as_string(exc);
1843 if (RSTRING_LEN(exc) == 0) {
1844 return rb_class_name(klass);
1845 }
1846
1847 str = rb_str_buf_new2("#<");
1848 klass = rb_class_name(klass);
1849 rb_str_buf_append(str, klass);
1850
1851 if (RTEST(rb_str_include(exc, rb_str_new2("\n")))) {
1852 rb_str_catf(str, ":%+"PRIsVALUE, exc);
1853 }
1854 else {
1855 rb_str_buf_cat(str, ": ", 2);
1856 rb_str_buf_append(str, exc);
1857 }
1858
1859 rb_str_buf_cat(str, ">", 1);
1860
1861 return str;
1862}
1863
1864/*
1865 * call-seq:
1866 * backtrace -> array or nil
1867 *
1868 * Returns the backtrace (the list of code locations that led to the exception),
1869 * as an array of strings.
1870 *
1871 * Example (assuming the code is stored in the file named <tt>t.rb</tt>):
1872 *
1873 * def division(numerator, denominator)
1874 * numerator / denominator
1875 * end
1876 *
1877 * begin
1878 * division(1, 0)
1879 * rescue => ex
1880 * p ex.backtrace
1881 * # ["t.rb:2:in 'Integer#/'", "t.rb:2:in 'Object#division'", "t.rb:6:in '<main>'"]
1882 * loc = ex.backtrace.first
1883 * p loc.class
1884 * # String
1885 * end
1886 *
1887 * The value returned by this method might be adjusted when raising (see Kernel#raise),
1888 * or during intermediate handling by #set_backtrace.
1889 *
1890 * See also #backtrace_locations that provide the same value, as structured objects.
1891 * (Note though that two values might not be consistent with each other when
1892 * backtraces are manually adjusted.)
1893 *
1894 * see {Backtraces}[rdoc-ref:exceptions.md@Backtraces].
1895 */
1896
1897static VALUE
1898exc_backtrace(VALUE exc)
1899{
1900 VALUE obj;
1901
1902 obj = rb_attr_get(exc, id_bt);
1903
1904 if (rb_backtrace_p(obj)) {
1905 obj = rb_backtrace_to_str_ary(obj);
1906 /* rb_ivar_set(exc, id_bt, obj); */
1907 }
1908
1909 return obj;
1910}
1911
1912static VALUE rb_check_backtrace(VALUE);
1913
1914VALUE
1915rb_get_backtrace(VALUE exc)
1916{
1917 ID mid = id_backtrace;
1918 VALUE info;
1919 if (rb_method_basic_definition_p(CLASS_OF(exc), id_backtrace)) {
1920 VALUE klass = rb_eException;
1921 rb_execution_context_t *ec = GET_EC();
1922 if (NIL_P(exc))
1923 return Qnil;
1924 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_CALL, exc, mid, mid, klass, Qundef);
1925 info = exc_backtrace(exc);
1926 EXEC_EVENT_HOOK(ec, RUBY_EVENT_C_RETURN, exc, mid, mid, klass, info);
1927 }
1928 else {
1929 info = rb_funcallv(exc, mid, 0, 0);
1930 }
1931 if (NIL_P(info)) return Qnil;
1932 return rb_check_backtrace(info);
1933}
1934
1935/*
1936 * call-seq:
1937 * backtrace_locations -> array or nil
1938 *
1939 * Returns the backtrace (the list of code locations that led to the exception),
1940 * as an array of Thread::Backtrace::Location instances.
1941 *
1942 * Example (assuming the code is stored in the file named <tt>t.rb</tt>):
1943 *
1944 * def division(numerator, denominator)
1945 * numerator / denominator
1946 * end
1947 *
1948 * begin
1949 * division(1, 0)
1950 * rescue => ex
1951 * p ex.backtrace_locations
1952 * # ["t.rb:2:in 'Integer#/'", "t.rb:2:in 'Object#division'", "t.rb:6:in '<main>'"]
1953 * loc = ex.backtrace_locations.first
1954 * p loc.class
1955 * # Thread::Backtrace::Location
1956 * p loc.path
1957 * # "t.rb"
1958 * p loc.lineno
1959 * # 2
1960 * p loc.label
1961 * # "Integer#/"
1962 * end
1963 *
1964 * The value returned by this method might be adjusted when raising (see Kernel#raise),
1965 * or during intermediate handling by #set_backtrace.
1966 *
1967 * See also #backtrace that provide the same value as an array of strings.
1968 * (Note though that two values might not be consistent with each other when
1969 * backtraces are manually adjusted.)
1970 *
1971 * See {Backtraces}[rdoc-ref:exceptions.md@Backtraces].
1972 */
1973static VALUE
1974exc_backtrace_locations(VALUE exc)
1975{
1976 VALUE obj;
1977
1978 obj = rb_attr_get(exc, id_bt_locations);
1979 if (!NIL_P(obj)) {
1980 obj = rb_backtrace_to_location_ary(obj);
1981 }
1982 return obj;
1983}
1984
1985static VALUE
1986rb_check_backtrace(VALUE bt)
1987{
1988 long i;
1989 static const char err[] = "backtrace must be an Array of String or an Array of Thread::Backtrace::Location";
1990
1991 if (!NIL_P(bt)) {
1992 if (RB_TYPE_P(bt, T_STRING)) return rb_ary_new3(1, bt);
1993 if (rb_backtrace_p(bt)) return bt;
1994 if (!RB_TYPE_P(bt, T_ARRAY)) {
1995 rb_raise(rb_eTypeError, err);
1996 }
1997 for (i=0;i<RARRAY_LEN(bt);i++) {
1998 VALUE e = RARRAY_AREF(bt, i);
1999 if (!RB_TYPE_P(e, T_STRING)) {
2000 rb_raise(rb_eTypeError, err);
2001 }
2002 }
2003 }
2004 return bt;
2005}
2006
2007/*
2008 * call-seq:
2009 * set_backtrace(value) -> value
2010 *
2011 * Sets the backtrace value for +self+; returns the given +value+.
2012 *
2013 * The +value+ might be:
2014 *
2015 * - an array of Thread::Backtrace::Location;
2016 * - an array of String instances;
2017 * - a single String instance; or
2018 * - +nil+.
2019 *
2020 * Using array of Thread::Backtrace::Location is the most consistent
2021 * option: it sets both #backtrace and #backtrace_locations. It should be
2022 * preferred when possible. The suitable array of locations can be obtained
2023 * from Kernel#caller_locations, copied from another error, or just set to
2024 * the adjusted result of the current error's #backtrace_locations:
2025 *
2026 * require 'json'
2027 *
2028 * def parse_payload(text)
2029 * JSON.parse(text) # test.rb, line 4
2030 * rescue JSON::ParserError => ex
2031 * ex.set_backtrace(ex.backtrace_locations[2...])
2032 * raise
2033 * end
2034 *
2035 * parse_payload('{"wrong: "json"')
2036 * # test.rb:4:in 'Object#parse_payload': unexpected token at '{"wrong: "json"' (JSON::ParserError)
2037 * #
2038 * # An error points to the body of parse_payload method,
2039 * # hiding the parts of the backtrace related to the internals
2040 * # of the "json" library
2041 *
2042 * # The error has both #backtace and #backtrace_locations set
2043 * # consistently:
2044 * begin
2045 * parse_payload('{"wrong: "json"')
2046 * rescue => ex
2047 * p ex.backtrace
2048 * # ["test.rb:4:in 'Object#parse_payload'", "test.rb:20:in '<main>'"]
2049 * p ex.backtrace_locations
2050 * # ["test.rb:4:in 'Object#parse_payload'", "test.rb:20:in '<main>'"]
2051 * end
2052 *
2053 * When the desired stack of locations is not available and should
2054 * be constructed from scratch, an array of strings or a singular
2055 * string can be used. In this case, only #backtrace is affected:
2056 *
2057 * def parse_payload(text)
2058 * JSON.parse(text)
2059 * rescue JSON::ParserError => ex
2060 * ex.set_backtrace(["dsl.rb:34", "framework.rb:1"])
2061 * # The error have the new value in #backtrace:
2062 * p ex.backtrace
2063 * # ["dsl.rb:34", "framework.rb:1"]
2064 *
2065 * # but the original one in #backtrace_locations
2066 * p ex.backtrace_locations
2067 * # [".../json/common.rb:221:in 'JSON::Ext::Parser.parse'", ...]
2068 * end
2069 *
2070 * parse_payload('{"wrong: "json"')
2071 *
2072 * Calling #set_backtrace with +nil+ clears up #backtrace but doesn't affect
2073 * #backtrace_locations:
2074 *
2075 * def parse_payload(text)
2076 * JSON.parse(text)
2077 * rescue JSON::ParserError => ex
2078 * ex.set_backtrace(nil)
2079 * p ex.backtrace
2080 * # nil
2081 * p ex.backtrace_locations
2082 * # [".../json/common.rb:221:in 'JSON::Ext::Parser.parse'", ...]
2083 * end
2084 *
2085 * parse_payload('{"wrong: "json"')
2086 *
2087 * On reraising of such an exception, both #backtrace and #backtrace_locations
2088 * is set to the place of reraising:
2089 *
2090 * def parse_payload(text)
2091 * JSON.parse(text)
2092 * rescue JSON::ParserError => ex
2093 * ex.set_backtrace(nil)
2094 * raise # test.rb, line 7
2095 * end
2096 *
2097 * begin
2098 * parse_payload('{"wrong: "json"')
2099 * rescue => ex
2100 * p ex.backtrace
2101 * # ["test.rb:7:in 'Object#parse_payload'", "test.rb:11:in '<main>'"]
2102 * p ex.backtrace_locations
2103 * # ["test.rb:7:in 'Object#parse_payload'", "test.rb:11:in '<main>'"]
2104 * end
2105 *
2106 * See {Backtraces}[rdoc-ref:exceptions.md@Backtraces].
2107 */
2108
2109static VALUE
2110exc_set_backtrace(VALUE exc, VALUE bt)
2111{
2112 VALUE btobj = rb_location_ary_to_backtrace(bt);
2113 if (RTEST(btobj)) {
2114 rb_ivar_set(exc, id_bt, btobj);
2115 rb_ivar_set(exc, id_bt_locations, btobj);
2116 return bt;
2117 }
2118 else {
2119 return rb_ivar_set(exc, id_bt, rb_check_backtrace(bt));
2120 }
2121}
2122
2123VALUE
2124rb_exc_set_backtrace(VALUE exc, VALUE bt)
2125{
2126 return exc_set_backtrace(exc, bt);
2127}
2128
2129/*
2130 * call-seq:
2131 * cause -> exception or nil
2132 *
2133 * Returns the previous value of global variable <tt>$!</tt>,
2134 * which may be +nil+
2135 * (see {Global Variables}[rdoc-ref:exceptions.md@Global+Variables]):
2136 *
2137 * begin
2138 * raise('Boom 0')
2139 * rescue => x0
2140 * puts "Exception: #{x0}; $!: #{$!}; cause: #{x0.cause.inspect}."
2141 * begin
2142 * raise('Boom 1')
2143 * rescue => x1
2144 * puts "Exception: #{x1}; $!: #{$!}; cause: #{x1.cause}."
2145 * begin
2146 * raise('Boom 2')
2147 * rescue => x2
2148 * puts "Exception: #{x2}; $!: #{$!}; cause: #{x2.cause}."
2149 * end
2150 * end
2151 * end
2152 *
2153 * Output:
2154 *
2155 * Exception: Boom 0; $!: Boom 0; cause: nil.
2156 * Exception: Boom 1; $!: Boom 1; cause: Boom 0.
2157 * Exception: Boom 2; $!: Boom 2; cause: Boom 1.
2158 *
2159 */
2160
2161static VALUE
2162exc_cause(VALUE exc)
2163{
2164 return rb_attr_get(exc, id_cause);
2165}
2166
2167static VALUE
2168try_convert_to_exception(VALUE obj)
2169{
2170 return rb_check_funcall(obj, idException, 0, 0);
2171}
2172
2173/*
2174 * call-seq:
2175 * self == object -> true or false
2176 *
2177 * Returns whether +object+ is the same class as +self+
2178 * and its #message and #backtrace are equal to those of +self+.
2179 *
2180 */
2181
2182static VALUE
2183exc_equal(VALUE exc, VALUE obj)
2184{
2185 VALUE mesg, backtrace;
2186
2187 if (exc == obj) return Qtrue;
2188
2189 if (rb_obj_class(exc) != rb_obj_class(obj)) {
2190 int state;
2191
2192 obj = rb_protect(try_convert_to_exception, obj, &state);
2193 if (state || UNDEF_P(obj)) {
2194 rb_set_errinfo(Qnil);
2195 return Qfalse;
2196 }
2197 if (rb_obj_class(exc) != rb_obj_class(obj)) return Qfalse;
2198 mesg = rb_check_funcall(obj, id_message, 0, 0);
2199 if (UNDEF_P(mesg)) return Qfalse;
2200 backtrace = rb_check_funcall(obj, id_backtrace, 0, 0);
2201 if (UNDEF_P(backtrace)) return Qfalse;
2202 }
2203 else {
2204 mesg = rb_attr_get(obj, id_mesg);
2205 backtrace = exc_backtrace(obj);
2206 }
2207
2208 if (!rb_equal(rb_attr_get(exc, id_mesg), mesg))
2209 return Qfalse;
2210 return rb_equal(exc_backtrace(exc), backtrace);
2211}
2212
2213/*
2214 * call-seq:
2215 * SystemExit.new -> system_exit
2216 * SystemExit.new(status) -> system_exit
2217 * SystemExit.new(status, msg) -> system_exit
2218 * SystemExit.new(msg) -> system_exit
2219 *
2220 * Create a new +SystemExit+ exception with the given status and message.
2221 * Status is true, false, or an integer.
2222 * If status is not given, true is used.
2223 */
2224
2225static VALUE
2226exit_initialize(int argc, VALUE *argv, VALUE exc)
2227{
2228 VALUE status;
2229 if (argc > 0) {
2230 status = *argv;
2231
2232 switch (status) {
2233 case Qtrue:
2234 status = INT2FIX(EXIT_SUCCESS);
2235 ++argv;
2236 --argc;
2237 break;
2238 case Qfalse:
2239 status = INT2FIX(EXIT_FAILURE);
2240 ++argv;
2241 --argc;
2242 break;
2243 default:
2244 status = rb_check_to_int(status);
2245 if (NIL_P(status)) {
2246 status = INT2FIX(EXIT_SUCCESS);
2247 }
2248 else {
2249#if EXIT_SUCCESS != 0
2250 if (status == INT2FIX(0))
2251 status = INT2FIX(EXIT_SUCCESS);
2252#endif
2253 ++argv;
2254 --argc;
2255 }
2256 break;
2257 }
2258 }
2259 else {
2260 status = INT2FIX(EXIT_SUCCESS);
2261 }
2262 rb_call_super(argc, argv);
2263 rb_ivar_set(exc, id_status, status);
2264 return exc;
2265}
2266
2267
2268/*
2269 * call-seq:
2270 * system_exit.status -> integer
2271 *
2272 * Return the status value associated with this system exit.
2273 */
2274
2275static VALUE
2276exit_status(VALUE exc)
2277{
2278 return rb_attr_get(exc, id_status);
2279}
2280
2281
2282/*
2283 * call-seq:
2284 * system_exit.success? -> true or false
2285 *
2286 * Returns +true+ if exiting successful, +false+ if not.
2287 */
2288
2289static VALUE
2290exit_success_p(VALUE exc)
2291{
2292 VALUE status_val = rb_attr_get(exc, id_status);
2293 int status;
2294
2295 if (NIL_P(status_val))
2296 return Qtrue;
2297 status = NUM2INT(status_val);
2298 return RBOOL(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
2299}
2300
2301static VALUE
2302err_init_recv(VALUE exc, VALUE recv)
2303{
2304 if (!UNDEF_P(recv)) rb_ivar_set(exc, id_recv, recv);
2305 return exc;
2306}
2307
2308/*
2309 * call-seq:
2310 * FrozenError.new(msg=nil, receiver: nil) -> frozen_error
2311 *
2312 * Construct a new FrozenError exception. If given the <i>receiver</i>
2313 * parameter may subsequently be examined using the FrozenError#receiver
2314 * method.
2315 *
2316 * a = [].freeze
2317 * raise FrozenError.new("can't modify frozen array", receiver: a)
2318 */
2319
2320static VALUE
2321frozen_err_initialize(int argc, VALUE *argv, VALUE self)
2322{
2323 ID keywords[1];
2324 VALUE values[numberof(keywords)], options;
2325
2326 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2327 keywords[0] = id_receiver;
2328 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2329 rb_call_super(argc, argv);
2330 err_init_recv(self, values[0]);
2331 return self;
2332}
2333
2334/*
2335 * Document-method: FrozenError#receiver
2336 * call-seq:
2337 * frozen_error.receiver -> object
2338 *
2339 * Return the receiver associated with this FrozenError exception.
2340 */
2341
2342#define frozen_err_receiver name_err_receiver
2343
2344void
2345rb_name_error(ID id, const char *fmt, ...)
2346{
2347 VALUE exc, argv[2];
2348 va_list args;
2349
2350 va_start(args, fmt);
2351 argv[0] = rb_vsprintf(fmt, args);
2352 va_end(args);
2353
2354 argv[1] = ID2SYM(id);
2355 exc = rb_class_new_instance(2, argv, rb_eNameError);
2356 rb_exc_raise(exc);
2357}
2358
2359void
2360rb_name_error_str(VALUE str, const char *fmt, ...)
2361{
2362 VALUE exc, argv[2];
2363 va_list args;
2364
2365 va_start(args, fmt);
2366 argv[0] = rb_vsprintf(fmt, args);
2367 va_end(args);
2368
2369 argv[1] = str;
2370 exc = rb_class_new_instance(2, argv, rb_eNameError);
2371 rb_exc_raise(exc);
2372}
2373
2374static VALUE
2375name_err_init_attr(VALUE exc, VALUE recv, VALUE method)
2376{
2377 const rb_execution_context_t *ec = GET_EC();
2378 rb_control_frame_t *cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(ec->cfp);
2379 cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
2380 rb_ivar_set(exc, id_name, method);
2381 err_init_recv(exc, recv);
2382 if (cfp && VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
2383 rb_ivar_set(exc, id_iseq, rb_iseqw_new(cfp->iseq));
2384 }
2385 return exc;
2386}
2387
2388/*
2389 * call-seq:
2390 * NameError.new(msg=nil, name=nil, receiver: nil) -> name_error
2391 *
2392 * Construct a new NameError exception. If given the <i>name</i>
2393 * parameter may subsequently be examined using the NameError#name
2394 * method. <i>receiver</i> parameter allows to pass object in
2395 * context of which the error happened. Example:
2396 *
2397 * [1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array
2398 * [1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
2399 */
2400
2401static VALUE
2402name_err_initialize(int argc, VALUE *argv, VALUE self)
2403{
2404 ID keywords[1];
2405 VALUE values[numberof(keywords)], name, options;
2406
2407 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2408 keywords[0] = id_receiver;
2409 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2410 name = (argc > 1) ? argv[--argc] : Qnil;
2411 rb_call_super(argc, argv);
2412 name_err_init_attr(self, values[0], name);
2413 return self;
2414}
2415
2416static VALUE rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method);
2417
2418static VALUE
2419name_err_init(VALUE exc, VALUE mesg, VALUE recv, VALUE method)
2420{
2421 exc_init(exc, rb_name_err_mesg_new(mesg, recv, method));
2422 return name_err_init_attr(exc, recv, method);
2423}
2424
2425VALUE
2426rb_name_err_new(VALUE mesg, VALUE recv, VALUE method)
2427{
2429 return name_err_init(exc, mesg, recv, method);
2430}
2431
2432/*
2433 * call-seq:
2434 * name_error.name -> string or nil
2435 *
2436 * Return the name associated with this NameError exception.
2437 */
2438
2439static VALUE
2440name_err_name(VALUE self)
2441{
2442 return rb_attr_get(self, id_name);
2443}
2444
2445/*
2446 * call-seq:
2447 * name_error.local_variables -> array
2448 *
2449 * Return a list of the local variable names defined where this
2450 * NameError exception was raised.
2451 *
2452 * Internal use only.
2453 */
2454
2455static VALUE
2456name_err_local_variables(VALUE self)
2457{
2458 VALUE vars = rb_attr_get(self, id_local_variables);
2459
2460 if (NIL_P(vars)) {
2461 VALUE iseqw = rb_attr_get(self, id_iseq);
2462 if (!NIL_P(iseqw)) vars = rb_iseqw_local_variables(iseqw);
2463 if (NIL_P(vars)) vars = rb_ary_new();
2464 rb_ivar_set(self, id_local_variables, vars);
2465 }
2466 return vars;
2467}
2468
2469static VALUE
2470nometh_err_init_attr(VALUE exc, VALUE args, int priv)
2471{
2472 rb_ivar_set(exc, id_args, args);
2473 rb_ivar_set(exc, id_private_call_p, RBOOL(priv));
2474 return exc;
2475}
2476
2477/*
2478 * call-seq:
2479 * NoMethodError.new(msg=nil, name=nil, args=nil, private=false, receiver: nil) -> no_method_error
2480 *
2481 * Construct a NoMethodError exception for a method of the given name
2482 * called with the given arguments. The name may be accessed using
2483 * the <code>#name</code> method on the resulting object, and the
2484 * arguments using the <code>#args</code> method.
2485 *
2486 * If <i>private</i> argument were passed, it designates method was
2487 * attempted to call in private context, and can be accessed with
2488 * <code>#private_call?</code> method.
2489 *
2490 * <i>receiver</i> argument stores an object whose method was called.
2491 */
2492
2493static VALUE
2494nometh_err_initialize(int argc, VALUE *argv, VALUE self)
2495{
2496 int priv;
2497 VALUE args, options;
2498 argc = rb_scan_args(argc, argv, "*:", NULL, &options);
2499 priv = (argc > 3) && (--argc, RTEST(argv[argc]));
2500 args = (argc > 2) ? argv[--argc] : Qnil;
2501 if (!NIL_P(options)) argv[argc++] = options;
2503 return nometh_err_init_attr(self, args, priv);
2504}
2505
2506VALUE
2507rb_nomethod_err_new(VALUE mesg, VALUE recv, VALUE method, VALUE args, int priv)
2508{
2510 name_err_init(exc, mesg, recv, method);
2511 return nometh_err_init_attr(exc, args, priv);
2512}
2513
2515 VALUE mesg;
2516 VALUE recv;
2517 VALUE name;
2519
2520static void
2521name_err_mesg_mark_and_move(void *p)
2522{
2524 rb_gc_mark_and_move(&ptr->mesg);
2525 rb_gc_mark_and_move(&ptr->recv);
2526 rb_gc_mark_and_move(&ptr->name);
2527}
2528
2529static const rb_data_type_t name_err_mesg_data_type = {
2530 "name_err_mesg",
2531 {
2532 name_err_mesg_mark_and_move,
2534 NULL, // No external memory to report,
2535 name_err_mesg_mark_and_move,
2536 },
2537 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
2538};
2539
2540/* :nodoc: */
2541static VALUE
2542rb_name_err_mesg_init(VALUE klass, VALUE mesg, VALUE recv, VALUE name)
2543{
2544 name_error_message_t *message;
2545 VALUE result = TypedData_Make_Struct(klass, name_error_message_t, &name_err_mesg_data_type, message);
2546 RB_OBJ_WRITE(result, &message->mesg, mesg);
2547 RB_OBJ_WRITE(result, &message->recv, recv);
2548 RB_OBJ_WRITE(result, &message->name, name);
2549 return result;
2550}
2551
2552/* :nodoc: */
2553static VALUE
2554rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method)
2555{
2556 return rb_name_err_mesg_init(rb_cNameErrorMesg, mesg, recv, method);
2557}
2558
2559/* :nodoc: */
2560static VALUE
2561name_err_mesg_alloc(VALUE klass)
2562{
2563 return rb_name_err_mesg_init(klass, Qnil, Qnil, Qnil);
2564}
2565
2566/* :nodoc: */
2567static VALUE
2568name_err_mesg_init_copy(VALUE obj1, VALUE obj2)
2569{
2570 if (obj1 == obj2) return obj1;
2571 rb_obj_init_copy(obj1, obj2);
2572
2573 name_error_message_t *ptr1, *ptr2;
2574 TypedData_Get_Struct(obj1, name_error_message_t, &name_err_mesg_data_type, ptr1);
2575 TypedData_Get_Struct(obj2, name_error_message_t, &name_err_mesg_data_type, ptr2);
2576
2577 RB_OBJ_WRITE(obj1, &ptr1->mesg, ptr2->mesg);
2578 RB_OBJ_WRITE(obj1, &ptr1->recv, ptr2->recv);
2579 RB_OBJ_WRITE(obj1, &ptr1->name, ptr2->name);
2580 return obj1;
2581}
2582
2583/* :nodoc: */
2584static VALUE
2585name_err_mesg_equal(VALUE obj1, VALUE obj2)
2586{
2587 if (obj1 == obj2) return Qtrue;
2588
2589 if (rb_obj_class(obj2) != rb_cNameErrorMesg)
2590 return Qfalse;
2591
2592 name_error_message_t *ptr1, *ptr2;
2593 TypedData_Get_Struct(obj1, name_error_message_t, &name_err_mesg_data_type, ptr1);
2594 TypedData_Get_Struct(obj2, name_error_message_t, &name_err_mesg_data_type, ptr2);
2595
2596 if (!rb_equal(ptr1->mesg, ptr2->mesg)) return Qfalse;
2597 if (!rb_equal(ptr1->recv, ptr2->recv)) return Qfalse;
2598 if (!rb_equal(ptr1->name, ptr2->name)) return Qfalse;
2599 return Qtrue;
2600}
2601
2602/* :nodoc: */
2603static VALUE
2604name_err_mesg_receiver_name(VALUE obj)
2605{
2606 if (RB_SPECIAL_CONST_P(obj)) return Qundef;
2607 if (RB_BUILTIN_TYPE(obj) == T_MODULE || RB_BUILTIN_TYPE(obj) == T_CLASS) {
2608 return rb_check_funcall(obj, rb_intern("name"), 0, 0);
2609 }
2610 return Qundef;
2611}
2612
2613/* :nodoc: */
2614static VALUE
2615name_err_mesg_to_str(VALUE obj)
2616{
2618 TypedData_Get_Struct(obj, name_error_message_t, &name_err_mesg_data_type, ptr);
2619
2620 VALUE mesg = ptr->mesg;
2621 if (NIL_P(mesg)) return Qnil;
2622 else {
2623 struct RString s_str = {RBASIC_INIT}, c_str = {RBASIC_INIT}, d_str = {RBASIC_INIT};
2624 VALUE c, s, d = 0, args[4], c2;
2625 int state = 0;
2626 rb_encoding *usascii = rb_usascii_encoding();
2627
2628#define FAKE_CSTR(v, str) rb_setup_fake_str((v), (str), rb_strlen_lit(str), usascii)
2629 c = s = FAKE_CSTR(&s_str, "");
2630 obj = ptr->recv;
2631 switch (obj) {
2632 case Qnil:
2633 c = d = FAKE_CSTR(&d_str, "nil");
2634 break;
2635 case Qtrue:
2636 c = d = FAKE_CSTR(&d_str, "true");
2637 break;
2638 case Qfalse:
2639 c = d = FAKE_CSTR(&d_str, "false");
2640 break;
2641 default:
2642 if (strstr(RSTRING_PTR(mesg), "%2$s")) {
2643 d = rb_protect(name_err_mesg_receiver_name, obj, &state);
2644 if (state || NIL_OR_UNDEF_P(d))
2645 d = rb_protect(rb_inspect, obj, &state);
2646 if (state) {
2647 rb_set_errinfo(Qnil);
2648 }
2649 d = rb_check_string_type(d);
2650 if (NIL_P(d)) {
2651 d = rb_any_to_s(obj);
2652 }
2653 }
2654
2655 if (!RB_SPECIAL_CONST_P(obj)) {
2656 switch (RB_BUILTIN_TYPE(obj)) {
2657 case T_MODULE:
2658 s = FAKE_CSTR(&s_str, "module ");
2659 c = obj;
2660 break;
2661 case T_CLASS:
2662 s = FAKE_CSTR(&s_str, "class ");
2663 c = obj;
2664 break;
2665 default:
2666 goto object;
2667 }
2668 }
2669 else {
2670 VALUE klass;
2671 object:
2672 klass = CLASS_OF(obj);
2673 if (RB_TYPE_P(klass, T_CLASS) && RCLASS_SINGLETON_P(klass)) {
2674 s = FAKE_CSTR(&s_str, "");
2675 if (obj == rb_vm_top_self()) {
2676 c = FAKE_CSTR(&c_str, "main");
2677 }
2678 else {
2679 c = rb_any_to_s(obj);
2680 }
2681 break;
2682 }
2683 else {
2684 s = FAKE_CSTR(&s_str, "an instance of ");
2685 c = rb_class_real(klass);
2686 }
2687 }
2688 c2 = rb_protect(name_err_mesg_receiver_name, c, &state);
2689 if (state || NIL_OR_UNDEF_P(c2))
2690 c2 = rb_protect(rb_inspect, c, &state);
2691 if (state) {
2692 rb_set_errinfo(Qnil);
2693 }
2694 c2 = rb_check_string_type(c2);
2695 if (NIL_P(c2)) {
2696 c2 = rb_any_to_s(c);
2697 }
2698 c = c2;
2699 break;
2700 }
2701 args[0] = rb_obj_as_string(ptr->name);
2702 args[1] = d;
2703 args[2] = s;
2704 args[3] = c;
2705 mesg = rb_str_format(4, args, mesg);
2706 }
2707 return mesg;
2708}
2709
2710/* :nodoc: */
2711static VALUE
2712name_err_mesg_dump(VALUE obj, VALUE limit)
2713{
2714 return name_err_mesg_to_str(obj);
2715}
2716
2717/* :nodoc: */
2718static VALUE
2719name_err_mesg_load(VALUE klass, VALUE str)
2720{
2721 return str;
2722}
2723
2724/*
2725 * call-seq:
2726 * name_error.receiver -> object
2727 *
2728 * Return the receiver associated with this NameError exception.
2729 */
2730
2731static VALUE
2732name_err_receiver(VALUE self)
2733{
2734 VALUE recv = rb_ivar_lookup(self, id_recv, Qundef);
2735 if (!UNDEF_P(recv)) return recv;
2736
2737 VALUE mesg = rb_attr_get(self, id_mesg);
2738 if (!rb_typeddata_is_kind_of(mesg, &name_err_mesg_data_type)) {
2739 rb_raise(rb_eArgError, "no receiver is available");
2740 }
2741
2743 TypedData_Get_Struct(mesg, name_error_message_t, &name_err_mesg_data_type, ptr);
2744 return ptr->recv;
2745}
2746
2747/*
2748 * call-seq:
2749 * no_method_error.args -> obj
2750 *
2751 * Return the arguments passed in as the third parameter to
2752 * the constructor.
2753 */
2754
2755static VALUE
2756nometh_err_args(VALUE self)
2757{
2758 return rb_attr_get(self, id_args);
2759}
2760
2761/*
2762 * call-seq:
2763 * no_method_error.private_call? -> true or false
2764 *
2765 * Return true if the caused method was called as private.
2766 */
2767
2768static VALUE
2769nometh_err_private_call_p(VALUE self)
2770{
2771 return rb_attr_get(self, id_private_call_p);
2772}
2773
2774void
2775rb_invalid_str(const char *str, const char *type)
2776{
2777 VALUE s = rb_str_new2(str);
2778
2779 rb_raise(rb_eArgError, "invalid value for %s: %+"PRIsVALUE, type, s);
2780}
2781
2782/*
2783 * call-seq:
2784 * key_error.receiver -> object
2785 *
2786 * Return the receiver associated with this KeyError exception.
2787 */
2788
2789static VALUE
2790key_err_receiver(VALUE self)
2791{
2792 VALUE recv;
2793
2794 recv = rb_ivar_lookup(self, id_receiver, Qundef);
2795 if (!UNDEF_P(recv)) return recv;
2796 rb_raise(rb_eArgError, "no receiver is available");
2797}
2798
2799/*
2800 * call-seq:
2801 * key_error.key -> object
2802 *
2803 * Return the key caused this KeyError exception.
2804 */
2805
2806static VALUE
2807key_err_key(VALUE self)
2808{
2809 VALUE key;
2810
2811 key = rb_ivar_lookup(self, id_key, Qundef);
2812 if (!UNDEF_P(key)) return key;
2813 rb_raise(rb_eArgError, "no key is available");
2814}
2815
2816VALUE
2817rb_key_err_new(VALUE mesg, VALUE recv, VALUE key)
2818{
2820 rb_ivar_set(exc, id_mesg, mesg);
2821 rb_ivar_set(exc, id_bt, Qnil);
2822 rb_ivar_set(exc, id_key, key);
2823 rb_ivar_set(exc, id_receiver, recv);
2824 return exc;
2825}
2826
2827/*
2828 * call-seq:
2829 * KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
2830 *
2831 * Construct a new +KeyError+ exception with the given message,
2832 * receiver and key.
2833 */
2834
2835static VALUE
2836key_err_initialize(int argc, VALUE *argv, VALUE self)
2837{
2838 VALUE options;
2839
2840 rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
2841
2842 if (!NIL_P(options)) {
2843 ID keywords[2];
2844 VALUE values[numberof(keywords)];
2845 int i;
2846 keywords[0] = id_receiver;
2847 keywords[1] = id_key;
2848 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2849 for (i = 0; i < numberof(values); ++i) {
2850 if (!UNDEF_P(values[i])) {
2851 rb_ivar_set(self, keywords[i], values[i]);
2852 }
2853 }
2854 }
2855
2856 return self;
2857}
2858
2859/*
2860 * call-seq:
2861 * no_matching_pattern_key_error.matchee -> object
2862 *
2863 * Return the matchee associated with this NoMatchingPatternKeyError exception.
2864 */
2865
2866static VALUE
2867no_matching_pattern_key_err_matchee(VALUE self)
2868{
2869 VALUE matchee;
2870
2871 matchee = rb_ivar_lookup(self, id_matchee, Qundef);
2872 if (!UNDEF_P(matchee)) return matchee;
2873 rb_raise(rb_eArgError, "no matchee is available");
2874}
2875
2876/*
2877 * call-seq:
2878 * no_matching_pattern_key_error.key -> object
2879 *
2880 * Return the key caused this NoMatchingPatternKeyError exception.
2881 */
2882
2883static VALUE
2884no_matching_pattern_key_err_key(VALUE self)
2885{
2886 VALUE key;
2887
2888 key = rb_ivar_lookup(self, id_key, Qundef);
2889 if (!UNDEF_P(key)) return key;
2890 rb_raise(rb_eArgError, "no key is available");
2891}
2892
2893/*
2894 * call-seq:
2895 * NoMatchingPatternKeyError.new(message=nil, matchee: nil, key: nil) -> no_matching_pattern_key_error
2896 *
2897 * Construct a new +NoMatchingPatternKeyError+ exception with the given message,
2898 * matchee and key.
2899 */
2900
2901static VALUE
2902no_matching_pattern_key_err_initialize(int argc, VALUE *argv, VALUE self)
2903{
2904 VALUE options;
2905
2906 rb_call_super(rb_scan_args(argc, argv, "01:", NULL, &options), argv);
2907
2908 if (!NIL_P(options)) {
2909 ID keywords[2];
2910 VALUE values[numberof(keywords)];
2911 int i;
2912 keywords[0] = id_matchee;
2913 keywords[1] = id_key;
2914 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2915 for (i = 0; i < numberof(values); ++i) {
2916 if (!UNDEF_P(values[i])) {
2917 rb_ivar_set(self, keywords[i], values[i]);
2918 }
2919 }
2920 }
2921
2922 return self;
2923}
2924
2925
2926/*
2927 * call-seq:
2928 * SyntaxError.new([msg]) -> syntax_error
2929 *
2930 * Construct a SyntaxError exception.
2931 */
2932
2933static VALUE
2934syntax_error_initialize(int argc, VALUE *argv, VALUE self)
2935{
2936 VALUE mesg;
2937 if (argc == 0) {
2938 mesg = rb_fstring_lit("compile error");
2939 argc = 1;
2940 argv = &mesg;
2941 }
2942 return rb_call_super(argc, argv);
2943}
2944
2945static VALUE
2946syntax_error_with_path(VALUE exc, VALUE path, VALUE *mesg, rb_encoding *enc)
2947{
2948 if (NIL_P(exc)) {
2949 *mesg = rb_enc_str_new(0, 0, enc);
2950 exc = rb_class_new_instance(1, mesg, rb_eSyntaxError);
2951 rb_ivar_set(exc, id_i_path, path);
2952 }
2953 else {
2954 VALUE old_path = rb_attr_get(exc, id_i_path);
2955 if (old_path != path) {
2956 if (rb_str_equal(path, old_path)) {
2957 rb_raise(rb_eArgError, "SyntaxError#path changed: %+"PRIsVALUE" (%p->%p)",
2958 old_path, (void *)old_path, (void *)path);
2959 }
2960 else {
2961 rb_raise(rb_eArgError, "SyntaxError#path changed: %+"PRIsVALUE"(%s%s)->%+"PRIsVALUE"(%s)",
2962 old_path, rb_enc_name(rb_enc_get(old_path)),
2963 (FL_TEST(old_path, RSTRING_FSTR) ? ":FSTR" : ""),
2964 path, rb_enc_name(rb_enc_get(path)));
2965 }
2966 }
2967 VALUE s = *mesg = rb_attr_get(exc, idMesg);
2968 if (RSTRING_LEN(s) > 0 && *(RSTRING_END(s)-1) != '\n')
2969 rb_str_cat_cstr(s, "\n");
2970 }
2971 return exc;
2972}
2973
2974/*
2975 * Document-module: Errno
2976 *
2977 * When an operating system encounters an error,
2978 * it typically reports the error as an integer error code:
2979 *
2980 * $ ls nosuch.txt
2981 * ls: cannot access 'nosuch.txt': No such file or directory
2982 * $ echo $? # Code for last error.
2983 * 2
2984 *
2985 * When the Ruby interpreter interacts with the operating system
2986 * and receives such an error code (e.g., +2+),
2987 * it maps the code to a particular Ruby exception class (e.g., +Errno::ENOENT+):
2988 *
2989 * File.open('nosuch.txt')
2990 * # => No such file or directory @ rb_sysopen - nosuch.txt (Errno::ENOENT)
2991 *
2992 * Each such class is:
2993 *
2994 * - A nested class in this module, +Errno+.
2995 * - A subclass of class SystemCallError.
2996 * - Associated with an error code.
2997 *
2998 * Thus:
2999 *
3000 * Errno::ENOENT.superclass # => SystemCallError
3001 * Errno::ENOENT::Errno # => 2
3002 *
3003 * The names of nested classes are returned by method +Errno.constants+:
3004 *
3005 * Errno.constants.size # => 158
3006 * Errno.constants.sort.take(5) # => [:E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, :EADV]
3007 *
3008 * As seen above, the error code associated with each class
3009 * is available as the value of a constant;
3010 * the value for a particular class may vary among operating systems.
3011 * If the class is not needed for the particular operating system,
3012 * the value is zero:
3013 *
3014 * Errno::ENOENT::Errno # => 2
3015 * Errno::ENOTCAPABLE::Errno # => 0
3016 *
3017 * Each class in Errno can be created with optional messages:
3018 *
3019 * Errno::EPIPE.new # => #<Errno::EPIPE: Broken pipe>
3020 * Errno::EPIPE.new("foo") # => #<Errno::EPIPE: Broken pipe - foo>
3021 * Errno::EPIPE.new("foo", "here") # => #<Errno::EPIPE: Broken pipe @ here - foo>
3022 *
3023 * See SystemCallError.new.
3024 */
3025
3026static st_table *syserr_tbl;
3027
3028void
3029rb_free_warning(void)
3030{
3031 st_free_table(warning_categories.id2enum);
3032 st_free_table(warning_categories.enum2id);
3033 st_free_table(syserr_tbl);
3034}
3035
3036static VALUE
3037setup_syserr(int n, const char *name)
3038{
3040
3041 /* capture nonblock errnos for WaitReadable/WaitWritable subclasses */
3042 switch (n) {
3043 case EAGAIN:
3044 rb_eEAGAIN = error;
3045
3046#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3047 break;
3048 case EWOULDBLOCK:
3049#endif
3050
3051 rb_eEWOULDBLOCK = error;
3052 break;
3053 case EINPROGRESS:
3054 rb_eEINPROGRESS = error;
3055 break;
3056 }
3057
3058 rb_define_const(error, "Errno", INT2NUM(n));
3059 st_add_direct(syserr_tbl, n, (st_data_t)error);
3060 return error;
3061}
3062
3063static VALUE
3064set_syserr(int n, const char *name)
3065{
3066 st_data_t error;
3067
3068 if (!st_lookup(syserr_tbl, n, &error)) {
3069 return setup_syserr(n, name);
3070 }
3071 else {
3072 VALUE errclass = (VALUE)error;
3073 rb_define_const(rb_mErrno, name, errclass);
3074 return errclass;
3075 }
3076}
3077
3078static VALUE
3079get_syserr(int n)
3080{
3081 st_data_t error;
3082
3083 if (!st_lookup(syserr_tbl, n, &error)) {
3084 char name[DECIMAL_SIZE_OF(n) + sizeof("E-")];
3085
3086 snprintf(name, sizeof(name), "E%03d", n);
3087 return setup_syserr(n, name);
3088 }
3089 return (VALUE)error;
3090}
3091
3092/*
3093 * call-seq:
3094 * SystemCallError.new(msg, errno = nil, func = nil) -> system_call_error_subclass
3095 *
3096 * If _errno_ corresponds to a known system error code, constructs the
3097 * appropriate Errno class for that error, otherwise constructs a
3098 * generic SystemCallError object. The error number is subsequently
3099 * available via the #errno method.
3100 *
3101 * If only numeric object is given, it is treated as an Integer _errno_,
3102 * and _msg_ is omitted, otherwise the first argument _msg_ is used as
3103 * the additional error message.
3104 *
3105 * SystemCallError.new(Errno::EPIPE::Errno)
3106 * #=> #<Errno::EPIPE: Broken pipe>
3107 *
3108 * SystemCallError.new("foo")
3109 * #=> #<SystemCallError: unknown error - foo>
3110 *
3111 * SystemCallError.new("foo", Errno::EPIPE::Errno)
3112 * #=> #<Errno::EPIPE: Broken pipe - foo>
3113 *
3114 * If _func_ is not +nil+, it is appended to the message with "<tt> @ </tt>".
3115 *
3116 * SystemCallError.new("foo", Errno::EPIPE::Errno, "here")
3117 * #=> #<Errno::EPIPE: Broken pipe @ here - foo>
3118 *
3119 * A subclass of SystemCallError can also be instantiated via the
3120 * +new+ method of the subclass. See Errno.
3121 */
3122
3123static VALUE
3124syserr_initialize(int argc, VALUE *argv, VALUE self)
3125{
3126 const char *err;
3127 VALUE mesg, error, func, errmsg;
3128 VALUE klass = rb_obj_class(self);
3129
3130 if (klass == rb_eSystemCallError) {
3131 st_data_t data = (st_data_t)klass;
3132 rb_scan_args(argc, argv, "12", &mesg, &error, &func);
3133 if (argc == 1 && FIXNUM_P(mesg)) {
3134 error = mesg; mesg = Qnil;
3135 }
3136 if (!NIL_P(error) && st_lookup(syserr_tbl, NUM2LONG(error), &data)) {
3137 klass = (VALUE)data;
3138 /* change class */
3139 if (!RB_TYPE_P(self, T_OBJECT)) { /* insurance to avoid type crash */
3140 rb_raise(rb_eTypeError, "invalid instance type");
3141 }
3142 RBASIC_SET_CLASS(self, klass);
3143 }
3144 }
3145 else {
3146 rb_scan_args(argc, argv, "02", &mesg, &func);
3147 error = rb_const_get(klass, id_Errno);
3148 }
3149 if (!NIL_P(error)) err = strerror(NUM2INT(error));
3150 else err = "unknown error";
3151
3152 errmsg = rb_enc_str_new_cstr(err, rb_locale_encoding());
3153 if (!NIL_P(mesg)) {
3154 VALUE str = StringValue(mesg);
3155
3156 if (!NIL_P(func)) rb_str_catf(errmsg, " @ %"PRIsVALUE, func);
3157 rb_str_catf(errmsg, " - %"PRIsVALUE, str);
3158 }
3159 mesg = errmsg;
3160
3161 rb_call_super(1, &mesg);
3162 rb_ivar_set(self, id_errno, error);
3163 return self;
3164}
3165
3166/*
3167 * call-seq:
3168 * system_call_error.errno -> integer
3169 *
3170 * Return this SystemCallError's error number.
3171 */
3172
3173static VALUE
3174syserr_errno(VALUE self)
3175{
3176 return rb_attr_get(self, id_errno);
3177}
3178
3179/*
3180 * call-seq:
3181 * system_call_error === other -> true or false
3182 *
3183 * Return +true+ if the receiver is a generic +SystemCallError+, or
3184 * if the error numbers +self+ and _other_ are the same.
3185 */
3186
3187static VALUE
3188syserr_eqq(VALUE self, VALUE exc)
3189{
3190 VALUE num, e;
3191
3193 if (!rb_respond_to(exc, id_errno)) return Qfalse;
3194 }
3195 else if (self == rb_eSystemCallError) return Qtrue;
3196
3197 num = rb_attr_get(exc, id_errno);
3198 if (NIL_P(num)) {
3199 num = rb_funcallv(exc, id_errno, 0, 0);
3200 }
3201 e = rb_const_get(self, id_Errno);
3202 return RBOOL(FIXNUM_P(num) ? num == e : rb_equal(num, e));
3203}
3204
3205
3206/*
3207 * Document-class: StandardError
3208 *
3209 * The most standard error types are subclasses of StandardError. A
3210 * rescue clause without an explicit Exception class will rescue all
3211 * StandardErrors (and only those).
3212 *
3213 * def foo
3214 * raise "Oups"
3215 * end
3216 * foo rescue "Hello" #=> "Hello"
3217 *
3218 * On the other hand:
3219 *
3220 * require 'does/not/exist' rescue "Hi"
3221 *
3222 * <em>raises the exception:</em>
3223 *
3224 * LoadError: no such file to load -- does/not/exist
3225 *
3226 */
3227
3228/*
3229 * Document-class: SystemExit
3230 *
3231 * Raised by +exit+ to initiate the termination of the script.
3232 */
3233
3234/*
3235 * Document-class: SignalException
3236 *
3237 * Raised when a signal is received.
3238 *
3239 * begin
3240 * Process.kill('HUP',Process.pid)
3241 * sleep # wait for receiver to handle signal sent by Process.kill
3242 * rescue SignalException => e
3243 * puts "received Exception #{e}"
3244 * end
3245 *
3246 * <em>produces:</em>
3247 *
3248 * received Exception SIGHUP
3249 */
3250
3251/*
3252 * Document-class: Interrupt
3253 *
3254 * Raised when the interrupt signal is received, typically because the
3255 * user has pressed Control-C (on most posix platforms). As such, it is a
3256 * subclass of +SignalException+.
3257 *
3258 * begin
3259 * puts "Press ctrl-C when you get bored"
3260 * loop {}
3261 * rescue Interrupt => e
3262 * puts "Note: You will typically use Signal.trap instead."
3263 * end
3264 *
3265 * <em>produces:</em>
3266 *
3267 * Press ctrl-C when you get bored
3268 *
3269 * <em>then waits until it is interrupted with Control-C and then prints:</em>
3270 *
3271 * Note: You will typically use Signal.trap instead.
3272 */
3273
3274/*
3275 * Document-class: TypeError
3276 *
3277 * Raised when encountering an object that is not of the expected type.
3278 *
3279 * [1, 2, 3].first("two")
3280 *
3281 * <em>raises the exception:</em>
3282 *
3283 * TypeError: no implicit conversion of String into Integer
3284 *
3285 */
3286
3287/*
3288 * Document-class: ArgumentError
3289 *
3290 * Raised when the arguments are wrong and there isn't a more specific
3291 * Exception class.
3292 *
3293 * Ex: passing the wrong number of arguments
3294 *
3295 * [1, 2, 3].first(4, 5)
3296 *
3297 * <em>raises the exception:</em>
3298 *
3299 * ArgumentError: wrong number of arguments (given 2, expected 1)
3300 *
3301 * Ex: passing an argument that is not acceptable:
3302 *
3303 * [1, 2, 3].first(-4)
3304 *
3305 * <em>raises the exception:</em>
3306 *
3307 * ArgumentError: negative array size
3308 */
3309
3310/*
3311 * Document-class: IndexError
3312 *
3313 * Raised when the given index is invalid.
3314 *
3315 * a = [:foo, :bar]
3316 * a.fetch(0) #=> :foo
3317 * a[4] #=> nil
3318 * a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
3319 *
3320 */
3321
3322/*
3323 * Document-class: KeyError
3324 *
3325 * Raised when the specified key is not found. It is a subclass of
3326 * IndexError.
3327 *
3328 * h = {"foo" => :bar}
3329 * h.fetch("foo") #=> :bar
3330 * h.fetch("baz") #=> KeyError: key not found: "baz"
3331 *
3332 */
3333
3334/*
3335 * Document-class: RangeError
3336 *
3337 * Raised when a given numerical value is out of range.
3338 *
3339 * [1, 2, 3].drop(1 << 100)
3340 *
3341 * <em>raises the exception:</em>
3342 *
3343 * RangeError: bignum too big to convert into `long'
3344 */
3345
3346/*
3347 * Document-class: ScriptError
3348 *
3349 * ScriptError is the superclass for errors raised when a script
3350 * can not be executed because of a +LoadError+,
3351 * +NotImplementedError+ or a +SyntaxError+. Note these type of
3352 * +ScriptErrors+ are not +StandardError+ and will not be
3353 * rescued unless it is specified explicitly (or its ancestor
3354 * +Exception+).
3355 */
3356
3357/*
3358 * Document-class: SyntaxError
3359 *
3360 * Raised when encountering Ruby code with an invalid syntax.
3361 *
3362 * eval("1+1=2")
3363 *
3364 * <em>raises the exception:</em>
3365 *
3366 * SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
3367 */
3368
3369/*
3370 * Document-class: LoadError
3371 *
3372 * Raised when a file required (a Ruby script, extension library, ...)
3373 * fails to load.
3374 *
3375 * require 'this/file/does/not/exist'
3376 *
3377 * <em>raises the exception:</em>
3378 *
3379 * LoadError: no such file to load -- this/file/does/not/exist
3380 */
3381
3382/*
3383 * Document-class: NotImplementedError
3384 *
3385 * Raised when a feature is not implemented on the current platform. For
3386 * example, methods depending on the +fsync+ or +fork+ system calls may
3387 * raise this exception if the underlying operating system or Ruby
3388 * runtime does not support them.
3389 *
3390 * Note that if +fork+ raises a +NotImplementedError+, then
3391 * <code>respond_to?(:fork)</code> returns +false+.
3392 */
3393
3394/*
3395 * Document-class: NameError
3396 *
3397 * Raised when a given name is invalid or undefined.
3398 *
3399 * puts foo
3400 *
3401 * <em>raises the exception:</em>
3402 *
3403 * NameError: undefined local variable or method `foo' for main:Object
3404 *
3405 * Since constant names must start with a capital:
3406 *
3407 * Integer.const_set :answer, 42
3408 *
3409 * <em>raises the exception:</em>
3410 *
3411 * NameError: wrong constant name answer
3412 */
3413
3414/*
3415 * Document-class: NoMethodError
3416 *
3417 * Raised when a method is called on a receiver which doesn't have it
3418 * defined and also fails to respond with +method_missing+.
3419 *
3420 * "hello".to_ary
3421 *
3422 * <em>raises the exception:</em>
3423 *
3424 * NoMethodError: undefined method `to_ary' for an instance of String
3425 */
3426
3427/*
3428 * Document-class: FrozenError
3429 *
3430 * Raised when there is an attempt to modify a frozen object.
3431 *
3432 * [1, 2, 3].freeze << 4
3433 *
3434 * <em>raises the exception:</em>
3435 *
3436 * FrozenError: can't modify frozen Array
3437 */
3438
3439/*
3440 * Document-class: RuntimeError
3441 *
3442 * A generic error class raised when an invalid operation is attempted.
3443 * Kernel#raise will raise a RuntimeError if no Exception class is
3444 * specified.
3445 *
3446 * raise "ouch"
3447 *
3448 * <em>raises the exception:</em>
3449 *
3450 * RuntimeError: ouch
3451 */
3452
3453/*
3454 * Document-class: SecurityError
3455 *
3456 * No longer used by internal code.
3457 */
3458
3459/*
3460 * Document-class: NoMemoryError
3461 *
3462 * Raised when memory allocation fails.
3463 */
3464
3465/*
3466 * Document-class: SystemCallError
3467 *
3468 * SystemCallError is the base class for all low-level
3469 * platform-dependent errors.
3470 *
3471 * The errors available on the current platform are subclasses of
3472 * SystemCallError and are defined in the Errno module.
3473 *
3474 * File.open("does/not/exist")
3475 *
3476 * <em>raises the exception:</em>
3477 *
3478 * Errno::ENOENT: No such file or directory - does/not/exist
3479 */
3480
3481/*
3482 * Document-class: EncodingError
3483 *
3484 * EncodingError is the base class for encoding errors.
3485 */
3486
3487/*
3488 * Document-class: Encoding::CompatibilityError
3489 *
3490 * Raised by Encoding and String methods when the source encoding is
3491 * incompatible with the target encoding.
3492 */
3493
3494/*
3495 * Document-class: NoMatchingPatternError
3496 *
3497 * Raised when matching pattern not found.
3498 */
3499
3500/*
3501 * Document-class: NoMatchingPatternKeyError
3502 *
3503 * Raised when matching key not found.
3504 */
3505
3506/*
3507 * Document-class: fatal
3508 *
3509 * +fatal+ is an Exception that is raised when Ruby has encountered a fatal
3510 * error and must exit.
3511 */
3512
3513/*
3514 * Document-class: NameError::message
3515 * :nodoc:
3516 */
3517
3518/*
3519 * Document-class: Exception
3520 *
3521 * Class +Exception+ and its subclasses are used to indicate that an error
3522 * or other problem has occurred,
3523 * and may need to be handled.
3524 * See {Exceptions}[rdoc-ref:exceptions.md].
3525 *
3526 * An +Exception+ object carries certain information:
3527 *
3528 * - The type (the exception's class),
3529 * commonly StandardError, RuntimeError, or a subclass of one or the other;
3530 * see {Built-In Exception Class Hierarchy}[rdoc-ref:Exception@Built-In+Exception+Class+Hierarchy].
3531 * - An optional descriptive message;
3532 * see methods ::new, #message.
3533 * - Optional backtrace information;
3534 * see methods #backtrace, #backtrace_locations, #set_backtrace.
3535 * - An optional cause;
3536 * see method #cause.
3537 *
3538 * == Built-In \Exception Class Hierarchy
3539 *
3540 * The hierarchy of built-in subclasses of class +Exception+:
3541 *
3542 * * NoMemoryError
3543 * * ScriptError
3544 * * LoadError
3545 * * NotImplementedError
3546 * * SyntaxError
3547 * * SecurityError
3548 * * SignalException
3549 * * Interrupt
3550 * * StandardError
3551 * * ArgumentError
3552 * * UncaughtThrowError
3553 * * EncodingError
3554 * * FiberError
3555 * * IOError
3556 * * EOFError
3557 * * IndexError
3558 * * KeyError
3559 * * StopIteration
3560 * * ClosedQueueError
3561 * * LocalJumpError
3562 * * NameError
3563 * * NoMethodError
3564 * * RangeError
3565 * * FloatDomainError
3566 * * RegexpError
3567 * * RuntimeError
3568 * * FrozenError
3569 * * SystemCallError
3570 * * Errno (and its subclasses, representing system errors)
3571 * * ThreadError
3572 * * TypeError
3573 * * ZeroDivisionError
3574 * * SystemExit
3575 * * SystemStackError
3576 * * {fatal}[rdoc-ref:fatal]
3577 *
3578 */
3579
3580static VALUE
3581exception_alloc(VALUE klass)
3582{
3583 return rb_class_allocate_instance(klass);
3584}
3585
3586static VALUE
3587exception_dumper(VALUE exc)
3588{
3589 // TODO: Currently, the instance variables "bt" and "bt_locations"
3590 // refers to the same object (Array of String). But "bt_locations"
3591 // should have an Array of Thread::Backtrace::Locations.
3592
3593 return exc;
3594}
3595
3596static int
3597ivar_copy_i(ID key, VALUE val, st_data_t exc)
3598{
3599 rb_ivar_set((VALUE)exc, key, val);
3600 return ST_CONTINUE;
3601}
3602
3603void rb_exc_check_circular_cause(VALUE exc);
3604
3605static VALUE
3606exception_loader(VALUE exc, VALUE obj)
3607{
3608 // The loader function of rb_marshal_define_compat seems to be called for two events:
3609 // one is for fixup (r_fixup_compat), the other is for TYPE_USERDEF.
3610 // In the former case, the first argument is an instance of Exception (because
3611 // we pass rb_eException to rb_marshal_define_compat). In the latter case, the first
3612 // argument is a class object (see TYPE_USERDEF case in r_object0).
3613 // We want to copy all instance variables (but "bt_locations") from obj to exc.
3614 // But we do not want to do so in the second case, so the following branch is for that.
3615 if (RB_TYPE_P(exc, T_CLASS)) return obj; // maybe called from Marshal's TYPE_USERDEF
3616
3617 rb_ivar_foreach(obj, ivar_copy_i, exc);
3618
3619 rb_exc_check_circular_cause(exc);
3620
3621 if (rb_attr_get(exc, id_bt) == rb_attr_get(exc, id_bt_locations)) {
3622 rb_ivar_set(exc, id_bt_locations, Qnil);
3623 }
3624
3625 return exc;
3626}
3627
3628void
3629Init_Exception(void)
3630{
3631 rb_eException = rb_define_class("Exception", rb_cObject);
3632 rb_define_alloc_func(rb_eException, exception_alloc);
3633 rb_marshal_define_compat(rb_eException, rb_eException, exception_dumper, exception_loader);
3635 rb_define_singleton_method(rb_eException, "to_tty?", exc_s_to_tty_p, 0);
3636 rb_define_method(rb_eException, "exception", exc_exception, -1);
3637 rb_define_method(rb_eException, "initialize", exc_initialize, -1);
3638 rb_define_method(rb_eException, "==", exc_equal, 1);
3639 rb_define_method(rb_eException, "to_s", exc_to_s, 0);
3640 rb_define_method(rb_eException, "message", exc_message, 0);
3641 rb_define_method(rb_eException, "detailed_message", exc_detailed_message, -1);
3642 rb_define_method(rb_eException, "full_message", exc_full_message, -1);
3643 rb_define_method(rb_eException, "inspect", exc_inspect, 0);
3644 rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
3645 rb_define_method(rb_eException, "backtrace_locations", exc_backtrace_locations, 0);
3646 rb_define_method(rb_eException, "set_backtrace", exc_set_backtrace, 1);
3647 rb_define_method(rb_eException, "cause", exc_cause, 0);
3648
3650 rb_define_method(rb_eSystemExit, "initialize", exit_initialize, -1);
3651 rb_define_method(rb_eSystemExit, "status", exit_status, 0);
3652 rb_define_method(rb_eSystemExit, "success?", exit_success_p, 0);
3653
3655 rb_eSignal = rb_define_class("SignalException", rb_eException);
3657
3663 rb_define_method(rb_eKeyError, "initialize", key_err_initialize, -1);
3664 rb_define_method(rb_eKeyError, "receiver", key_err_receiver, 0);
3665 rb_define_method(rb_eKeyError, "key", key_err_key, 0);
3667
3670 rb_define_method(rb_eSyntaxError, "initialize", syntax_error_initialize, -1);
3671
3672 /* RDoc will use literal name value while parsing rb_attr,
3673 * and will render `idPath` as an attribute name without this trick */
3674 ID path = idPath;
3675
3676 /* the path that failed to parse */
3677 rb_attr(rb_eSyntaxError, path, TRUE, FALSE, FALSE);
3678
3680 /* the path that failed to load */
3681 rb_attr(rb_eLoadError, path, TRUE, FALSE, FALSE);
3682
3683 rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
3684
3686 rb_define_method(rb_eNameError, "initialize", name_err_initialize, -1);
3687 rb_define_method(rb_eNameError, "name", name_err_name, 0);
3688 rb_define_method(rb_eNameError, "receiver", name_err_receiver, 0);
3689 rb_define_method(rb_eNameError, "local_variables", name_err_local_variables, 0);
3690 rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cObject);
3691 rb_define_alloc_func(rb_cNameErrorMesg, name_err_mesg_alloc);
3692 rb_define_method(rb_cNameErrorMesg, "initialize_copy", name_err_mesg_init_copy, 1);
3693 rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1);
3694 rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0);
3695 rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_dump, 1);
3696 rb_define_singleton_method(rb_cNameErrorMesg, "_load", name_err_mesg_load, 1);
3698 rb_define_method(rb_eNoMethodError, "initialize", nometh_err_initialize, -1);
3699 rb_define_method(rb_eNoMethodError, "args", nometh_err_args, 0);
3700 rb_define_method(rb_eNoMethodError, "private_call?", nometh_err_private_call_p, 0);
3701
3704 rb_define_method(rb_eFrozenError, "initialize", frozen_err_initialize, -1);
3705 rb_define_method(rb_eFrozenError, "receiver", frozen_err_receiver, 0);
3707 rb_eNoMemError = rb_define_class("NoMemoryError", rb_eException);
3712 rb_define_method(rb_eNoMatchingPatternKeyError, "initialize", no_matching_pattern_key_err_initialize, -1);
3713 rb_define_method(rb_eNoMatchingPatternKeyError, "matchee", no_matching_pattern_key_err_matchee, 0);
3714 rb_define_method(rb_eNoMatchingPatternKeyError, "key", no_matching_pattern_key_err_key, 0);
3715
3716 syserr_tbl = st_init_numtable();
3718 rb_define_method(rb_eSystemCallError, "initialize", syserr_initialize, -1);
3719 rb_define_method(rb_eSystemCallError, "errno", syserr_errno, 0);
3720 rb_define_singleton_method(rb_eSystemCallError, "===", syserr_eqq, 1);
3721
3722 rb_mErrno = rb_define_module("Errno");
3723
3724 rb_mWarning = rb_define_module("Warning");
3725 rb_define_singleton_method(rb_mWarning, "[]", rb_warning_s_aref, 1);
3726 rb_define_singleton_method(rb_mWarning, "[]=", rb_warning_s_aset, 2);
3727 rb_define_singleton_method(rb_mWarning, "categories", rb_warning_s_categories, 0);
3728 rb_define_method(rb_mWarning, "warn", rb_warning_s_warn, -1);
3729 rb_extend_object(rb_mWarning, rb_mWarning);
3730
3731 /* :nodoc: */
3732 rb_cWarningBuffer = rb_define_class_under(rb_mWarning, "buffer", rb_cString);
3733 rb_define_method(rb_cWarningBuffer, "write", warning_write, -1);
3734
3735 id_cause = rb_intern_const("cause");
3736 id_message = rb_intern_const("message");
3737 id_detailed_message = rb_intern_const("detailed_message");
3738 id_backtrace = rb_intern_const("backtrace");
3739 id_key = rb_intern_const("key");
3740 id_matchee = rb_intern_const("matchee");
3741 id_args = rb_intern_const("args");
3742 id_receiver = rb_intern_const("receiver");
3743 id_private_call_p = rb_intern_const("private_call?");
3744 id_local_variables = rb_intern_const("local_variables");
3745 id_Errno = rb_intern_const("Errno");
3746 id_errno = rb_intern_const("errno");
3747 id_i_path = rb_intern_const("@path");
3748 id_warn = rb_intern_const("warn");
3749 id_category = rb_intern_const("category");
3750 id_deprecated = rb_intern_const("deprecated");
3751 id_experimental = rb_intern_const("experimental");
3752 id_performance = rb_intern_const("performance");
3753 id_strict_unused_block = rb_intern_const("strict_unused_block");
3754 id_top = rb_intern_const("top");
3755 id_bottom = rb_intern_const("bottom");
3756 id_iseq = rb_make_internal_id();
3757 id_recv = rb_make_internal_id();
3758
3759 sym_category = ID2SYM(id_category);
3760 sym_highlight = ID2SYM(rb_intern_const("highlight"));
3761
3762 warning_categories.id2enum = rb_init_identtable();
3763 st_add_direct(warning_categories.id2enum, id_deprecated, RB_WARN_CATEGORY_DEPRECATED);
3764 st_add_direct(warning_categories.id2enum, id_experimental, RB_WARN_CATEGORY_EXPERIMENTAL);
3765 st_add_direct(warning_categories.id2enum, id_performance, RB_WARN_CATEGORY_PERFORMANCE);
3766 st_add_direct(warning_categories.id2enum, id_strict_unused_block, RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK);
3767
3768 warning_categories.enum2id = rb_init_identtable();
3769 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_NONE, 0);
3770 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_DEPRECATED, id_deprecated);
3771 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_EXPERIMENTAL, id_experimental);
3772 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_PERFORMANCE, id_performance);
3773 st_add_direct(warning_categories.enum2id, RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK, id_strict_unused_block);
3774}
3775
3776void
3777rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
3778{
3779 va_list args;
3780 VALUE mesg;
3781
3782 va_start(args, fmt);
3783 mesg = rb_enc_vsprintf(enc, fmt, args);
3784 va_end(args);
3785
3786 rb_exc_raise(rb_exc_new3(exc, mesg));
3787}
3788
3789void
3790rb_vraise(VALUE exc, const char *fmt, va_list ap)
3791{
3792 rb_exc_raise(rb_exc_new3(exc, rb_vsprintf(fmt, ap)));
3793}
3794
3795void
3796rb_raise(VALUE exc_class, const char *fmt, ...)
3797{
3798 va_list args;
3799 va_start(args, fmt);
3800 VALUE exc = rb_exc_new3(exc_class, rb_vsprintf(fmt, args));
3801 va_end(args);
3802 rb_exc_raise(exc);
3803}
3804
3805NORETURN(static void raise_loaderror(VALUE path, VALUE mesg));
3806
3807static void
3808raise_loaderror(VALUE path, VALUE mesg)
3809{
3810 VALUE err = rb_exc_new3(rb_eLoadError, mesg);
3811 rb_ivar_set(err, id_i_path, path);
3812 rb_exc_raise(err);
3813}
3814
3815void
3816rb_loaderror(const char *fmt, ...)
3817{
3818 va_list args;
3819 VALUE mesg;
3820
3821 va_start(args, fmt);
3822 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
3823 va_end(args);
3824 raise_loaderror(Qnil, mesg);
3825}
3826
3827void
3828rb_loaderror_with_path(VALUE path, const char *fmt, ...)
3829{
3830 va_list args;
3831 VALUE mesg;
3832
3833 va_start(args, fmt);
3834 mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
3835 va_end(args);
3836 raise_loaderror(path, mesg);
3837}
3838
3839void
3841{
3842 rb_raise(rb_eNotImpError,
3843 "%"PRIsVALUE"() function is unimplemented on this machine",
3844 rb_id2str(rb_frame_this_func()));
3845}
3846
3847void
3848rb_fatal(const char *fmt, ...)
3849{
3850 va_list args;
3851 VALUE mesg;
3852
3853 if (! ruby_thread_has_gvl_p()) {
3854 /* The thread has no GVL. Object allocation impossible (cant run GC),
3855 * thus no message can be printed out. */
3856 fprintf(stderr, "[FATAL] rb_fatal() outside of GVL\n");
3857 rb_print_backtrace(stderr);
3858 die();
3859 }
3860
3861 va_start(args, fmt);
3862 mesg = rb_vsprintf(fmt, args);
3863 va_end(args);
3864
3866}
3867
3868static VALUE
3869make_errno_exc(const char *mesg)
3870{
3871 int n = errno;
3872
3873 errno = 0;
3874 if (n == 0) {
3875 rb_bug("rb_sys_fail(%s) - errno == 0", mesg ? mesg : "");
3876 }
3877 return rb_syserr_new(n, mesg);
3878}
3879
3880static VALUE
3881make_errno_exc_str(VALUE mesg)
3882{
3883 int n = errno;
3884
3885 errno = 0;
3886 if (!mesg) mesg = Qnil;
3887 if (n == 0) {
3888 const char *s = !NIL_P(mesg) ? RSTRING_PTR(mesg) : "";
3889 rb_bug("rb_sys_fail_str(%s) - errno == 0", s);
3890 }
3891 return rb_syserr_new_str(n, mesg);
3892}
3893
3894VALUE
3895rb_syserr_new(int n, const char *mesg)
3896{
3897 VALUE arg;
3898 arg = mesg ? rb_str_new2(mesg) : Qnil;
3899 return rb_syserr_new_str(n, arg);
3900}
3901
3902VALUE
3904{
3905 return rb_class_new_instance(1, &arg, get_syserr(n));
3906}
3907
3908void
3909rb_syserr_fail(int e, const char *mesg)
3910{
3911 rb_exc_raise(rb_syserr_new(e, mesg));
3912}
3913
3914void
3916{
3918}
3919
3920#undef rb_sys_fail
3921void
3922rb_sys_fail(const char *mesg)
3923{
3924 rb_exc_raise(make_errno_exc(mesg));
3925}
3926
3927#undef rb_sys_fail_str
3928void
3929rb_sys_fail_str(VALUE mesg)
3930{
3931 rb_exc_raise(make_errno_exc_str(mesg));
3932}
3933
3934#ifdef RUBY_FUNCTION_NAME_STRING
3935void
3936rb_sys_fail_path_in(const char *func_name, VALUE path)
3937{
3938 int n = errno;
3939
3940 errno = 0;
3941 rb_syserr_fail_path_in(func_name, n, path);
3942}
3943
3944void
3945rb_syserr_fail_path_in(const char *func_name, int n, VALUE path)
3946{
3947 rb_exc_raise(rb_syserr_new_path_in(func_name, n, path));
3948}
3949
3950VALUE
3951rb_syserr_new_path_in(const char *func_name, int n, VALUE path)
3952{
3953 VALUE args[2];
3954
3955 if (!path) path = Qnil;
3956 if (n == 0) {
3957 const char *s = !NIL_P(path) ? RSTRING_PTR(path) : "";
3958 if (!func_name) func_name = "(null)";
3959 rb_bug("rb_sys_fail_path_in(%s, %s) - errno == 0",
3960 func_name, s);
3961 }
3962 args[0] = path;
3963 args[1] = rb_str_new_cstr(func_name);
3964 return rb_class_new_instance(2, args, get_syserr(n));
3965}
3966#endif
3967
3968NORETURN(static void rb_mod_exc_raise(VALUE exc, VALUE mod));
3969
3970static void
3971rb_mod_exc_raise(VALUE exc, VALUE mod)
3972{
3973 rb_extend_object(exc, mod);
3974 rb_exc_raise(exc);
3975}
3976
3977void
3978rb_mod_sys_fail(VALUE mod, const char *mesg)
3979{
3980 VALUE exc = make_errno_exc(mesg);
3981 rb_mod_exc_raise(exc, mod);
3982}
3983
3984void
3986{
3987 VALUE exc = make_errno_exc_str(mesg);
3988 rb_mod_exc_raise(exc, mod);
3989}
3990
3991void
3992rb_mod_syserr_fail(VALUE mod, int e, const char *mesg)
3993{
3994 VALUE exc = rb_syserr_new(e, mesg);
3995 rb_mod_exc_raise(exc, mod);
3996}
3997
3998void
4000{
4001 VALUE exc = rb_syserr_new_str(e, mesg);
4002 rb_mod_exc_raise(exc, mod);
4003}
4004
4005static void
4006syserr_warning(VALUE mesg, int err)
4007{
4008 rb_str_set_len(mesg, RSTRING_LEN(mesg)-1);
4009 rb_str_catf(mesg, ": %s\n", strerror(err));
4010 rb_write_warning_str(mesg);
4011}
4012
4013#if 0
4014void
4015rb_sys_warn(const char *fmt, ...)
4016{
4017 if (!NIL_P(ruby_verbose)) {
4018 int errno_save = errno;
4019 with_warning_string(mesg, 0, fmt) {
4020 syserr_warning(mesg, errno_save);
4021 }
4022 errno = errno_save;
4023 }
4024}
4025
4026void
4027rb_syserr_warn(int err, const char *fmt, ...)
4028{
4029 if (!NIL_P(ruby_verbose)) {
4030 with_warning_string(mesg, 0, fmt) {
4031 syserr_warning(mesg, err);
4032 }
4033 }
4034}
4035
4036void
4037rb_sys_enc_warn(rb_encoding *enc, const char *fmt, ...)
4038{
4039 if (!NIL_P(ruby_verbose)) {
4040 int errno_save = errno;
4041 with_warning_string(mesg, enc, fmt) {
4042 syserr_warning(mesg, errno_save);
4043 }
4044 errno = errno_save;
4045 }
4046}
4047
4048void
4049rb_syserr_enc_warn(int err, rb_encoding *enc, const char *fmt, ...)
4050{
4051 if (!NIL_P(ruby_verbose)) {
4052 with_warning_string(mesg, enc, fmt) {
4053 syserr_warning(mesg, err);
4054 }
4055 }
4056}
4057#endif
4058
4059void
4060rb_sys_warning(const char *fmt, ...)
4061{
4062 if (RTEST(ruby_verbose)) {
4063 int errno_save = errno;
4064 with_warning_string(mesg, 0, fmt) {
4065 syserr_warning(mesg, errno_save);
4066 }
4067 errno = errno_save;
4068 }
4069}
4070
4071#if 0
4072void
4073rb_syserr_warning(int err, const char *fmt, ...)
4074{
4075 if (RTEST(ruby_verbose)) {
4076 with_warning_string(mesg, 0, fmt) {
4077 syserr_warning(mesg, err);
4078 }
4079 }
4080}
4081#endif
4082
4083void
4084rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...)
4085{
4086 if (RTEST(ruby_verbose)) {
4087 int errno_save = errno;
4088 with_warning_string(mesg, enc, fmt) {
4089 syserr_warning(mesg, errno_save);
4090 }
4091 errno = errno_save;
4092 }
4093}
4094
4095void
4096rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...)
4097{
4098 if (RTEST(ruby_verbose)) {
4099 with_warning_string(mesg, enc, fmt) {
4100 syserr_warning(mesg, err);
4101 }
4102 }
4103}
4104
4105void
4106rb_load_fail(VALUE path, const char *err)
4107{
4108 VALUE mesg = rb_str_buf_new_cstr(err);
4109 rb_str_cat2(mesg, " -- ");
4110 rb_str_append(mesg, path); /* should be ASCII compatible */
4111 raise_loaderror(path, mesg);
4112}
4113
4114void
4115rb_error_frozen(const char *what)
4116{
4117 rb_raise(rb_eFrozenError, "can't modify frozen %s", what);
4118}
4119
4120void
4121rb_frozen_error_raise(VALUE frozen_obj, const char *fmt, ...)
4122{
4123 va_list args;
4124 VALUE exc, mesg;
4125
4126 va_start(args, fmt);
4127 mesg = rb_vsprintf(fmt, args);
4128 va_end(args);
4129 exc = rb_exc_new3(rb_eFrozenError, mesg);
4130 rb_ivar_set(exc, id_recv, frozen_obj);
4131 rb_exc_raise(exc);
4132}
4133
4134static VALUE
4135inspect_frozen_obj(VALUE obj, VALUE mesg, int recur)
4136{
4137 if (recur) {
4138 rb_str_cat_cstr(mesg, " ...");
4139 }
4140 else {
4141 rb_str_append(mesg, rb_inspect(obj));
4142 }
4143 return mesg;
4144}
4145
4146static VALUE
4147get_created_info(VALUE obj, int *pline)
4148{
4149 VALUE info = rb_attr_get(obj, id_debug_created_info);
4150
4151 if (NIL_P(info)) return Qnil;
4152
4153 VALUE path = rb_ary_entry(info, 0);
4154 VALUE line = rb_ary_entry(info, 1);
4155 if (NIL_P(path)) return Qnil;
4156 *pline = NUM2INT(line);
4157 return StringValue(path);
4158}
4159
4160void
4162{
4163 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
4164
4165 VALUE mesg = rb_sprintf("can't modify frozen %"PRIsVALUE": ",
4166 CLASS_OF(frozen_obj));
4168
4169 rb_ivar_set(exc, id_recv, frozen_obj);
4170 rb_exec_recursive(inspect_frozen_obj, frozen_obj, mesg);
4171
4172 int created_line;
4173 VALUE created_path = get_created_info(frozen_obj, &created_line);
4174 if (!NIL_P(created_path)) {
4175 rb_str_catf(mesg, ", created at %"PRIsVALUE":%d", created_path, created_line);
4176 }
4177 rb_exc_raise(exc);
4178}
4179
4180void
4181rb_warn_unchilled_literal(VALUE obj)
4182{
4184 if (!NIL_P(ruby_verbose) && rb_warning_category_enabled_p(category)) {
4185 int line;
4186 VALUE file = rb_source_location(&line);
4187 VALUE mesg = NIL_P(file) ? rb_str_new(0, 0) : rb_str_dup(file);
4188
4189 if (!NIL_P(file)) {
4190 if (line) rb_str_catf(mesg, ":%d", line);
4191 rb_str_cat2(mesg, ": ");
4192 }
4193 rb_str_cat2(mesg, "warning: literal string will be frozen in the future");
4194
4195 VALUE str = obj;
4196 if (STR_SHARED_P(str)) {
4197 str = RSTRING(obj)->as.heap.aux.shared;
4198 }
4199 VALUE created = get_created_info(str, &line);
4200 if (NIL_P(created)) {
4201 rb_str_cat2(mesg, " (run with --debug-frozen-string-literal for more information)\n");
4202 }
4203 else {
4204 rb_str_cat2(mesg, "\n");
4205 rb_str_append(mesg, created);
4206 if (line) rb_str_catf(mesg, ":%d", line);
4207 rb_str_cat2(mesg, ": info: the string was created here\n");
4208 }
4209 rb_warn_category(mesg, rb_warning_category_to_name(category));
4210 }
4211}
4212
4213void
4214rb_warn_unchilled_symbol_to_s(VALUE obj)
4215{
4218 "string returned by :%s.to_s will be frozen in the future", RSTRING_PTR(obj)
4219 );
4220}
4221
4222#undef rb_check_frozen
4223void
4224rb_check_frozen(VALUE obj)
4225{
4227}
4228
4229void
4231{
4232 if (!FL_ABLE(obj)) return;
4233 rb_check_frozen(obj);
4234 if (!FL_ABLE(orig)) return;
4235}
4236
4237void
4238Init_syserr(void)
4239{
4240 rb_eNOERROR = setup_syserr(0, "NOERROR");
4241#if 0
4242 /* No error */
4243 rb_define_const(rb_mErrno, "NOERROR", rb_eNOERROR);
4244#endif
4245#define defined_error(name, num) set_syserr((num), (name));
4246#define undefined_error(name) rb_define_const(rb_mErrno, (name), rb_eNOERROR);
4247#include "known_errors.inc"
4248#undef defined_error
4249#undef undefined_error
4250}
4251
4252#include "warning.rbinc"
4253
#define RUBY_DEBUG
Define this macro when you want assertions.
Definition assert.h:88
#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:1179
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
Definition event.h:43
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:44
#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:1572
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
Definition eval.c:1878
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
Definition class.c:2883
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1603
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition class.c:1685
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:3221
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:3010
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1674
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define ISSPACE
Old name of rb_isspace.
Definition ctype.h:88
#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:1682
#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:1678
#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:205
#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 FL_ABLE
Old name of RB_FL_ABLE.
Definition fl_type.h:121
#define rb_ary_new3
Old name of rb_ary_new_from_args.
Definition array.h:658
#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 FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:130
#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:3840
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:3978
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 unless $VERBOSE is nil.
Definition error.c:476
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:1371
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:508
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:3992
VALUE rb_eNotImpError
NotImplementedError exception.
Definition error.c:1441
VALUE rb_eScriptError
ScriptError exception.
Definition error.c:1447
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:683
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:1381
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
Definition error.c:3909
VALUE rb_eKeyError
KeyError exception.
Definition error.c:1434
VALUE rb_cNameErrorMesg
NameError::Message class.
Definition error.c:1443
VALUE rb_eSystemExit
SystemExit exception.
Definition error.c:1424
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
Definition error.c:2345
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:4060
void rb_check_copyable(VALUE obj, VALUE orig)
Ensures that the passed object can be initialize_copy relationship.
Definition error.c:4230
VALUE rb_eStandardError
StandardError exception.
Definition error.c:1428
VALUE rb_mErrno
Errno module.
Definition error.c:1452
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:3903
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:3999
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:4115
VALUE rb_eFrozenError
FrozenError exception.
Definition error.c:1430
VALUE rb_eNoMemError
NoMemoryError exception.
Definition error.c:1442
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1435
VALUE rb_eLoadError
LoadError exception.
Definition error.c:1449
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:3915
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:475
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1431
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
Definition error.c:1444
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:2360
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Raises an instance of rb_eFrozenError.
Definition error.c:4121
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
Definition error.c:1438
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:439
VALUE rb_eFatal
fatal exception.
Definition error.c:1427
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:2775
VALUE rb_eInterrupt
Interrupt exception.
Definition error.c:1425
VALUE rb_eNameError
NameError exception.
Definition error.c:1436
VALUE rb_eNoMethodError
NoMethodError exception.
Definition error.c:1439
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
Definition eval.c:696
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1429
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:1398
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
Definition error.c:1469
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
Definition error.c:1445
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:4161
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:1482
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1432
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:1141
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:3777
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
Definition error.c:3816
VALUE rb_eException
Mother of all exceptions.
Definition error.c:1423
VALUE rb_eIndexError
IndexError exception.
Definition error.c:1433
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:3828
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1448
VALUE rb_eEncodingError
EncodingError exception.
Definition error.c:1437
VALUE rb_syserr_new(int n, const char *mesg)
Creates an exception object that represents the given C errno.
Definition error.c:3895
VALUE rb_eSecurityError
SecurityError exception.
Definition error.c:1440
void rb_unexpected_type(VALUE x, int t)
Fails with the given object's type incompatibility to the type.
Definition error.c:1361
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:3985
void rb_check_type(VALUE x, int t)
This was the old implementation of Check_Type(), but they diverged.
Definition error.c:1338
VALUE rb_eSystemCallError
SystemCallError exception.
Definition error.c:1451
void rb_warning(const char *fmt,...)
Issues a warning.
Definition error.c:497
VALUE rb_eSignal
SignalException exception.
Definition error.c:1426
@ RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK
Warning is for checking unused block strictly.
Definition error.h:57
@ 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_PERFORMANCE
Warning is for performance issues (not enabled by -w).
Definition error.h:54
@ 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:3294
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition object.c:676
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2190
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2231
VALUE rb_obj_init_copy(VALUE src, VALUE dst)
Default implementation of #initialize_copy, #initialize_dup and #initialize_clone.
Definition object.c:623
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:265
VALUE rb_cEncoding
Encoding class.
Definition encoding.c:60
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:687
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:256
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:177
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
Definition object.c:528
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:924
VALUE rb_String(VALUE val)
This is the logic behind Kernel#String.
Definition object.c:3843
VALUE rb_cString
String class.
Definition string.c:84
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
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:1153
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
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:1084
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:354
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
Definition vm_eval.c:362
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
static void rb_check_frozen_inline(VALUE obj)
Just another name of rb_check_frozen.
Definition error.h:253
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
void ruby_default_signal(int sig)
Pretends as if there was no custom signal handler.
Definition signal.c:411
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:3795
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
Definition string.c:1744
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1497
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1680
#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:1669
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1994
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
Definition string.h:1638
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:3761
VALUE rb_str_equal(VALUE str1, VALUE str2)
Equality of two strings.
Definition string.c:4265
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3385
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2788
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2948
#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:1655
#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:1513
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:1848
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:3439
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:2013
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:500
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:3367
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:2242
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:686
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:680
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:284
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1133
int len
Length of the buffer.
Definition io.h:8
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
Definition util.h:48
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
Definition sprintf.c:215
VALUE rb_vsprintf(const char *fmt, va_list ap)
Identical to rb_sprintf(), except it takes a va_list.
Definition sprintf.c:1211
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
Definition marshal.c:137
#define ALLOCA_N(type, n)
Definition memory.h:292
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 PRI_PIDT_PREFIX
A rb_sprintf() format prefix to be used for a pid_t parameter.
Definition pid_t.h:38
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:281
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
#define RSTRING(obj)
Convenient casting macro.
Definition rstring.h:41
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:2820
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition rtypeddata.h:582
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:80
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:520
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:502
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:605
VALUE rb_argv0
The value of $0 at process bootup.
Definition ruby.c:1870
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:515
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
#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:196
char * ptr
Pointer to the contents of the string.
Definition rstring.h:222
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:202
const rb_data_type_t * parent
Parent of this class.
Definition rtypeddata.h:292
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:209
Definition method.h:55
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:182
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:433
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:376