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