12#include "ruby/internal/config.h"
31#include "internal/class.h"
32#include "internal/error.h"
33#include "internal/eval.h"
34#include "internal/hash.h"
35#include "internal/io.h"
36#include "internal/load.h"
37#include "internal/object.h"
38#include "internal/process.h"
39#include "internal/string.h"
40#include "internal/symbol.h"
41#include "internal/thread.h"
42#include "internal/variable.h"
46#include "internal/vm.h"
47#include "ruby_assert.h"
64#define WIFEXITED(status) 1
68#define WEXITSTATUS(status) (status)
73int rb_str_end_with_asciichar(
VALUE str,
int c);
75long rb_backtrace_length_limit = -1;
79static VALUE rb_mWarning;
80static VALUE rb_cWarningBuffer;
84static ID id_deprecated;
85static ID id_experimental;
86static ID id_performance;
87static ID id_strict_unused_block;
88static VALUE sym_category;
89static VALUE sym_highlight;
94extern const char *rb_dynamic_description;
99#define defined_error(name, num) if (err == (num)) return (name);
100#define undefined_error(name)
101#include "known_errors.inc"
103#undef undefined_error
108err_position_0(
char *buf,
long len,
const char *file,
int line)
113 else if (line == 0) {
114 return snprintf(buf,
len,
"%s: ", file);
117 return snprintf(buf,
len,
"%s:%d: ", file, line);
123err_vcatf(
VALUE str, const
char *pre, const
char *file,
int line,
124 const
char *fmt, va_list args)
128 if (line) rb_str_catf(str,
":%d", line);
132 rb_str_vcatf(str, fmt, args);
139rb_syntax_error_append(
VALUE exc,
VALUE file,
int line,
int column,
142 const char *fn =
NIL_P(file) ? NULL : RSTRING_PTR(file);
144 VALUE mesg = rb_enc_str_new(0, 0, enc);
145 err_vcatf(mesg, NULL, fn, line, fmt, args);
147 rb_write_error_str(mesg);
151 exc = syntax_error_with_path(exc, file, &mesg, enc);
152 err_vcatf(mesg, NULL, fn, line, fmt, args);
158static unsigned int warning_disabled_categories = (
160 ~RB_WARN_CATEGORY_DEFAULT_BITS);
163rb_warning_category_mask(
VALUE category)
165 return 1U << rb_warning_category_from_name(category);
169rb_warning_category_from_name(
VALUE category)
175 !st_lookup(warning_categories.id2enum, cat_id, &cat_value)) {
176 rb_raise(
rb_eArgError,
"unknown category: %"PRIsVALUE, category);
185 if (!st_lookup(warning_categories.enum2id, category, &
id)) {
186 rb_raise(
rb_eArgError,
"invalid category: %d", (
int)category);
192rb_warning_category_update(
unsigned int mask,
unsigned int bits)
194 warning_disabled_categories &= ~mask;
195 warning_disabled_categories |= mask & ~bits;
201 return !(warning_disabled_categories & (1U << category));
229 return RBOOL(rb_warning_category_enabled_p(cat));
243 unsigned int mask = rb_warning_category_mask(category);
244 unsigned int disabled = warning_disabled_categories;
249 warning_disabled_categories = disabled;
261rb_warning_s_categories(
VALUE mod)
263 st_index_t num = warning_categories.id2enum->num_entries;
265 num = st_keys(warning_categories.id2enum, ids, num);
267 for (st_index_t i = 0; i < num; ++i) {
285rb_warning_s_warn(
int argc,
VALUE *argv,
VALUE mod)
296 if (!
NIL_P(category)) {
298 if (!rb_warning_category_enabled_p(cat))
return Qnil;
300 rb_write_error_str(str);
346rb_warning_warn_arity(
void)
349 return me ? rb_method_entry_arity(me) : 1;
356 rb_warning_category_from_name(category);
359 if (rb_warning_warn_arity() == 1) {
360 return rb_warning_warn(rb_mWarning, str);
365 args[1] = rb_hash_new_capa(1);
366 rb_hash_aset(args[1], sym_category, category);
372rb_write_warning_str(
VALUE str)
374 rb_warning_warn(rb_mWarning, str);
379warn_vsprintf(
rb_encoding *enc, const
char *file,
int line, const
char *fmt, va_list args)
381 VALUE str = rb_enc_str_new(0, 0, enc);
383 err_vcatf(str,
"warning: ", file, line, fmt, args);
387#define with_warn_vsprintf(enc, file, line, fmt) \
390 va_start(args, fmt); \
391 str = warn_vsprintf(enc, file, line, fmt, args); \
395rb_compile_warn(
const char *file,
int line,
const char *fmt, ...)
398 with_warn_vsprintf(NULL, file, line, fmt) {
399 rb_write_warning_str(str);
405rb_enc_compile_warn(
rb_encoding *enc,
const char *file,
int line,
const char *fmt, ...)
408 with_warn_vsprintf(enc, file, line, fmt) {
409 rb_write_warning_str(str);
416rb_compile_warning(
const char *file,
int line,
const char *fmt, ...)
419 with_warn_vsprintf(NULL, file, line, fmt) {
420 rb_write_warning_str(str);
427rb_enc_compile_warning(
rb_encoding *enc,
const char *file,
int line,
const char *fmt, ...)
430 with_warn_vsprintf(enc, file, line, fmt) {
431 rb_write_warning_str(str);
440 with_warn_vsprintf(NULL, file, line, fmt) {
441 rb_warn_category(str, rb_warning_category_to_name(category));
448warning_string(
rb_encoding *enc, const
char *fmt, va_list args)
451 const char *file = rb_source_location_cstr(&line);
452 return warn_vsprintf(enc, file, line, fmt, args);
455#define with_warning_string(mesg, enc, fmt) \
456 with_warning_string_from(mesg, enc, fmt, fmt)
457#define with_warning_string_from(mesg, enc, fmt, last_arg) \
459 va_list args; va_start(args, last_arg); \
460 mesg = warning_string(enc, fmt, args); \
467 with_warning_string(mesg, 0, fmt) {
468 rb_write_warning_str(mesg);
477 with_warning_string(mesg, 0, fmt) {
478 rb_warn_category(mesg, rb_warning_category_to_name(category));
487 with_warning_string(mesg, enc, fmt) {
488 rb_write_warning_str(mesg);
498 with_warning_string(mesg, 0, fmt) {
499 rb_write_warning_str(mesg);
509 with_warning_string(mesg, 0, fmt) {
510 rb_warn_category(mesg, rb_warning_category_to_name(category));
516rb_warning_string(
const char *fmt, ...)
518 with_warning_string(mesg, 0, fmt) {
525rb_enc_warning(
rb_encoding *enc,
const char *fmt, ...)
528 with_warning_string(mesg, enc, fmt) {
529 rb_write_warning_str(mesg);
536deprecation_warning_enabled(
void)
544warn_deprecated(
VALUE mesg,
const char *removal,
const char *suggest,
VALUE category)
549 rb_str_catf(mesg,
" and will be removed in Ruby %s", removal);
551 if (suggest) rb_str_catf(mesg,
"; use %s instead", suggest);
553 rb_warn_category(mesg, category);
557rb_warn_deprecated(
const char *fmt,
const char *suggest, ...)
559 if (!deprecation_warning_enabled())
return;
561 with_warning_string_from(mesg, 0, fmt, suggest) {
562 warn_deprecated(mesg, NULL, suggest,
ID2SYM(id_deprecated));
567rb_warn_deprecated_to_remove(
const char *removal,
const char *fmt,
const char *suggest, ...)
569 if (!deprecation_warning_enabled())
return;
571 with_warning_string_from(mesg, 0, fmt, suggest) {
572 warn_deprecated(mesg, removal, suggest,
ID2SYM(id_deprecated));
577rb_warn_to_remove(
const char *removal,
const char *fmt,
const char *suggest, ...)
581 with_warning_string_from(mesg, 0, fmt, suggest) {
582 warn_deprecated(mesg, removal, suggest,
Qnil);
587rb_warn_reserved_name(
const char *coming,
const char *fmt, ...)
589 if (!deprecation_warning_enabled())
return;
591 with_warning_string_from(mesg, 0, fmt, fmt) {
593 rb_str_catf(mesg,
" is reserved for Ruby %s\n", coming);
594 rb_warn_category(mesg,
ID2SYM(id_deprecated));
599end_with_asciichar(
VALUE str,
int c)
602 rb_str_end_with_asciichar(str, c);
607warning_write(
int argc,
VALUE *argv,
VALUE buf)
626 if (!
NIL_P(uplevel)) {
631 location = rb_ec_backtrace_location_ary(ec, lev + 1, 1, TRUE);
632 if (!
NIL_P(location)) {
636 if (argc > 1 || !
NIL_P(uplevel) || !end_with_asciichar(str,
'\n')) {
638 if (
NIL_P(uplevel)) {
641 else if (
NIL_P(location) ||
646 str = rb_sprintf(
"%s:%ld: warning: ",
650 RBASIC_SET_CLASS(str, rb_cWarningBuffer);
651 rb_io_puts(argc, argv, str);
655 if (!
NIL_P(category)) {
656 category = rb_to_symbol_type(category);
657 rb_warning_category_from_name(category);
660 if (exc == rb_mWarning) {
662 rb_write_error_str(str);
665 rb_warn_category(str, category);
671#define MAX_BUG_REPORTERS 0x100
674 void (*func)(
FILE *out,
void *data);
678static int bug_reporters_size;
681rb_bug_reporter_add(
void (*func)(
FILE *,
void *),
void *data)
684 if (bug_reporters_size >= MAX_BUG_REPORTERS) {
688 reporter->func = func;
689 reporter->data = data;
698#if defined __CYGWIN__ || defined DOSISH
699# define PATH_SEP_ENCODING 1
701 if (x ==
':')
return true;
702 if (x ==
'\\')
return true;
712static const char PATHSEP_REPLACE =
'!';
715append_pathname(
char *p,
const char *pe,
VALUE str)
717#ifdef PATH_SEP_ENCODING
720 const char *s = RSTRING_PTR(str);
721 const char *
const se = s + RSTRING_LEN(str);
726 while (p < pe && s < se && (c = *s) !=
'\0') {
728 if (s == se || !*s)
break;
729 if (path_sep_p(s[1]))
goto skipsep;
731 else if (path_sep_p(c)) {
733 *p++ = PATHSEP_REPLACE;
735 while (++s < se && path_sep_p(*s));
738 const char *
const ss = s;
739 while (p < pe && s < se && *s && !path_sep_p(*s)) {
740#ifdef PATH_SEP_ENCODING
741 int n = rb_enc_mbclen(s, se, enc);
748 if (s > ss) memcpy(p - (s - ss), ss, s - ss);
755append_basename(
char *p,
const char *pe,
struct path_string *path,
VALUE str)
758#ifdef PATH_SEP_ENCODING
761 const char *
const b = RSTRING_PTR(str), *
const e =
RSTRING_END(str), *p = e;
764 if (path_sep_p(p[-1])) {
765#ifdef PATH_SEP_ENCODING
766 const char *t = rb_enc_prev_char(b, p, e, enc);
781 size_t n = path->len;
782 if (p + n > pe) n = pe - p;
783 memcpy(p, path->ptr, n);
788finish_report(
FILE *out, rb_pid_t pid)
790 if (out != stdout && out != stderr) fclose(out);
791#ifdef HAVE_WORKING_FORK
792 if (pid > 0) waitpid(pid, NULL, 0);
821expand_report_argument(
const char **input_template,
struct report_expansion *values,
822 char *buf,
size_t size,
bool word)
825 char *end = buf + size;
826 const char *
template = *input_template;
829 if (p >= end-1 || !*
template)
return NULL;
831 char c = *
template++;
833 if (!store)
continue;
836 switch (c = *
template++) {
838 p = append_basename(p, end, &values->exe,
rb_argv0);
841 p = append_pathname(p, end,
rb_argv0);
844 p = append_basename(p, end, &values->script, GET_VM()->orig_progname);
847 p = append_pathname(p, end, GET_VM()->orig_progname);
850 if (!values->pid) values->pid = getpid();
855 if (!values->time) values->time = time(NULL);
856 snprintf(p, end-p,
"%" PRI_TIMET_PREFIX
"d", values->time);
860 if (c >=
'0' && c <=
'7') {
861 c = (
unsigned char)ruby_scan_oct(
template-1, 3, &n);
863 if (!c) store =
false;
868 if (p < end-1) *p++ = c;
870 *input_template =
template;
875FILE *ruby_popen_writer(
char *
const *argv, rb_pid_t *pid);
878open_report_path(
const char *
template,
char *buf,
size_t size, rb_pid_t *pid)
882 if (!
template)
return NULL;
883 if (0) fprintf(stderr,
"RUBY_CRASH_REPORT=%s\n", buf);
884 if (*
template ==
'|') {
885 char *argv[16], *bufend = buf + size, *p;
888 for (argc = 0; argc < numberof(argv) - 1; ++argc) {
889 while (*
template &&
ISSPACE(*
template))
template++;
890 p = expand_report_argument(&
template, &values, buf, bufend-buf,
true);
896 if (!p)
return ruby_popen_writer(argv, pid);
898 else if (*
template) {
899 expand_report_argument(&
template, &values, buf, size,
false);
900 return fopen(buf,
"w");
905static const char *crash_report;
908#define REPORT_BUG_BUFSIZ 256
910bug_report_file(
const char *file,
int line, rb_pid_t *pid)
912 char buf[REPORT_BUG_BUFSIZ];
913 const char *report = crash_report;
914 if (!report) report = getenv(
"RUBY_CRASH_REPORT");
915 FILE *out = open_report_path(report, buf,
sizeof(buf), pid);
916 int len = err_position_0(buf,
sizeof(buf), file, line);
922 setvbuf(out, NULL, _IONBF, 0);
923 if ((ssize_t)fwrite(buf, 1,
len, out) == (ssize_t)
len)
return out;
926 if ((ssize_t)fwrite(buf, 1,
len, stderr) == (ssize_t)
len) {
929 if ((ssize_t)fwrite(buf, 1,
len, stdout) == (ssize_t)
len) {
936FUNC_MINIMIZED(
static void bug_important_message(
FILE *out,
const char *
const msg,
size_t len));
939bug_important_message(
FILE *out,
const char *
const msg,
size_t len)
941 const char *
const endmsg = msg +
len;
945 if (isatty(fileno(out))) {
946 static const char red[] =
"\033[;31;1;7m";
947 static const char green[] =
"\033[;32;7m";
948 static const char reset[] =
"\033[m";
949 const char *e = strchr(p,
'\n');
950 const int w = (int)(e - p);
952 int i = (int)(e - p);
953 fputs(*p ==
' ' ? green : red, out);
954 fwrite(p, 1, e - p, out);
955 for (; i < w; ++i) fputc(
' ', out);
958 }
while ((p = e + 1) < endmsg && (e = strchr(p,
'\n')) != 0 && e > p + 1);
960 fwrite(p, 1, endmsg - p, out);
964preface_dump(
FILE *out)
967 static const char msg[] =
""
968 "-- Crash Report log information "
969 "--------------------------------------------\n"
970 " See Crash Report log file in one of the following locations:\n"
971 " * ~/Library/Logs/DiagnosticReports\n"
972 " * /Library/Logs/DiagnosticReports\n"
973 " for more details.\n"
974 "Don't forget to include the above Crash Report log file in bug reports.\n"
976 const size_t msglen =
sizeof(msg) - 1;
978 const char *msg = NULL;
979 const size_t msglen = 0;
981 bug_important_message(out, msg, msglen);
985postscript_dump(
FILE *out)
988 static const char msg[] =
""
991 "\n""Don't forget to include the Crash Report log file under\n"
992 "DiagnosticReports directory in bug reports.\n"
995 const size_t msglen =
sizeof(msg) - 1;
997 const char *msg = NULL;
998 const size_t msglen = 0;
1000 bug_important_message(out, msg, msglen);
1005bug_report_begin_valist(
FILE *out, const
char *fmt, va_list args)
1007 char buf[REPORT_BUG_BUFSIZ];
1009 fputs(
"[BUG] ", out);
1010 vsnprintf(buf,
sizeof(buf), fmt, args);
1012 snprintf(buf,
sizeof(buf),
"\n%s\n\n", rb_dynamic_description);
1017#define bug_report_begin(out, fmt) do { \
1019 va_start(args, fmt); \
1020 bug_report_begin_valist(out, fmt, args); \
1025bug_report_end(
FILE *out, rb_pid_t pid)
1030 for (i=0; i<bug_reporters_size; i++) {
1032 (*reporter->func)(out, reporter->data);
1035 postscript_dump(out);
1036 finish_report(out, pid);
1047bug_report_claim(
void)
1052 bug_reporter_ec = ec;
1055 if (ec != NULL && ec == bug_reporter_ec) {
1060 for (
int i = 0; i < 100; i++) {
1064 struct timespec ts = { 0, 100 * 1000 * 1000 };
1065 nanosleep(&ts, NULL);
1071#define report_bug(file, line, fmt, ctx) do { \
1072 rb_pid_t pid = -1; \
1073 FILE *out = bug_report_claim() ? bug_report_file(file, line, &pid) : NULL; \
1075 bug_report_begin(out, fmt); \
1076 rb_vm_bugreport(ctx, out); \
1077 bug_report_end(out, pid); \
1081#define report_bug_valist(file, line, fmt, ctx, args) do { \
1082 rb_pid_t pid = -1; \
1083 FILE *out = bug_report_claim() ? bug_report_file(file, line, &pid) : NULL; \
1085 bug_report_begin_valist(out, fmt, args); \
1086 rb_vm_bugreport(ctx, out); \
1087 bug_report_end(out, pid); \
1092ruby_set_crash_report(
const char *
template)
1094 crash_report =
template;
1097 char buf[REPORT_BUG_BUFSIZ];
1098 FILE *out = open_report_path(
template, buf,
sizeof(buf), &pid);
1100 time_t t = time(NULL);
1101 fprintf(out,
"ruby_test_bug_report: %s", ctime(&t));
1102 finish_report(out, pid);
1107NORETURN(
static void die(
void));
1111#if defined(_WIN32) && defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 80
1113 _set_abort_behavior( 0, _CALL_REPORTFAULT);
1119 signal(SIGABRT, SIG_DFL);
1125rb_bug_without_die_internal(const
char *fmt, va_list args)
1127 const char *file = NULL;
1130 if (rb_current_execution_context(
false)) {
1131 file = rb_source_location_cstr(&line);
1134 report_bug_valist(file, line, fmt, NULL, args);
1139rb_bug_without_die(const
char *fmt, ...)
1142 va_start(args, fmt);
1143 rb_bug_without_die_internal(fmt, args);
1148rb_bug(
const char *fmt, ...)
1151 va_start(args, fmt);
1152 rb_bug_without_die_internal(fmt, args);
1158rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler,
int sig,
const void *ctx,
const char *fmt, ...)
1160 const char *file = NULL;
1163 if (rb_current_execution_context(
false)) {
1164 file = rb_source_location_cstr(&line);
1167 report_bug(file, line, fmt, ctx);
1169 if (default_sighandler) default_sighandler(sig);
1180 rb_bug(
"%s: errno == 0 (NOERROR)", mesg);
1182 const char *errno_str = rb_strerrno(errno_arg);
1184 rb_bug(
"%s: %s (%s)", mesg, strerror(errno_arg), errno_str);
1186 rb_bug(
"%s: %s (%d)", mesg, strerror(errno_arg), errno_arg);
1194#define write_or_abort(fd, str, len) (write((fd), (str), (len)) < 0 ? abort() : (void)0)
1195#define WRITE_CONST(fd,str) write_or_abort((fd),(str),sizeof(str) - 1)
1198rb_async_bug_errno(
const char *mesg,
int errno_arg)
1200 WRITE_CONST(2,
"[ASYNC BUG] ");
1201 write_or_abort(2, mesg, strlen(mesg));
1202 WRITE_CONST(2,
"\n");
1204 if (errno_arg == 0) {
1205 WRITE_CONST(2,
"errno == 0 (NOERROR)\n");
1208 const char *errno_str = rb_strerrno(errno_arg);
1211 errno_str =
"undefined errno";
1212 write_or_abort(2, errno_str, strlen(errno_str));
1214 WRITE_CONST(2,
"\n\n");
1215 write_or_abort(2, rb_dynamic_description, strlen(rb_dynamic_description));
1220rb_report_bug_valist(
VALUE file,
int line,
const char *fmt, va_list args)
1222 report_bug_valist(RSTRING_PTR(file), line, fmt, NULL, args);
1226rb_assert_failure(
const char *file,
int line,
const char *name,
const char *expr)
1228 rb_assert_failure_detail(file, line, name, expr, NULL);
1232rb_assert_failure_detail(
const char *file,
int line,
const char *name,
const char *expr,
1233 const char *fmt, ...)
1236 FILE *out = bug_report_claim() ? bug_report_file(file, line, &pid) : NULL;
1238 fputs(
"Assertion Failed: ", out);
1239 if (name) fprintf(out,
"%s:", name);
1244 va_start(args, fmt);
1246 vfprintf(out, fmt, args);
1249 fprintf(out,
"\n%s\n\n", rb_dynamic_description);
1252 rb_vm_bugreport(NULL, out);
1253 bug_report_end(out, pid);
1259static const char builtin_types[][10] = {
1292rb_builtin_type_name(
int t)
1295 if ((
unsigned int)t >= numberof(builtin_types))
return 0;
1296 name = builtin_types[t];
1297 if (*name)
return name;
1302displaying_class_of(
VALUE x)
1305 case Qfalse:
return rb_fstring_cstr(
"false");
1306 case Qnil:
return rb_fstring_cstr(
"nil");
1307 case Qtrue:
return rb_fstring_cstr(
"true");
1313builtin_class_name(
VALUE x)
1339rb_builtin_class_name(
VALUE x)
1341 const char *etype = builtin_class_name(x);
1349COLDFUNC NORETURN(
static void unexpected_type(
VALUE,
int,
int));
1350#define UNDEF_LEAKED "undef leaked to the Ruby space"
1355 rb_raise(
rb_eTypeError,
"wrong argument type %s (expected %s)",
1362 rb_raise(
rb_eTypeError,
"wrong argument type %"PRIsVALUE
" (expected %s)",
1363 displaying_class_of(obj), expected);
1367unexpected_type(
VALUE x,
int xt,
int t)
1369 const char *tname = rb_builtin_type_name(t);
1375 else if (xt >
T_MASK && xt <= 0x3f) {
1376 mesg = rb_sprintf(
"unknown type 0x%x (0x%x given, probably comes"
1377 " from extension library for ruby 1.8)", t, xt);
1380 mesg = rb_sprintf(
"unknown type 0x%x (0x%x given)", t, xt);
1390 if (RB_UNLIKELY(UNDEF_P(x))) {
1391 rb_bug(UNDEF_LEAKED);
1407 unexpected_type(x, xt, t);
1414 if (RB_UNLIKELY(UNDEF_P(x))) {
1415 rb_bug(UNDEF_LEAKED);
1418 unexpected_type(x,
TYPE(x), t);
1421#undef rb_typeddata_inherited_p
1425 return rbimpl_typeddata_inherited_p_inline(child, parent);
1428#undef rb_typeddata_is_kind_of
1432 return rbimpl_typeddata_is_kind_of_inline(obj, data_type);
1435#undef rb_typeddata_is_instance_of
1439 return rb_typeddata_is_instance_of_inline(obj, data_type);
1445 return rbimpl_check_typeddata(obj, data_type);
1479static VALUE rb_eNOERROR;
1481ID ruby_static_id_cause;
1482#define id_cause ruby_static_id_cause
1483static ID id_message, id_detailed_message, id_backtrace;
1484static ID id_key, id_matchee, id_args, id_Errno, id_errno, id_i_path;
1485static ID id_receiver, id_recv, id_iseq, id_local_variables;
1486static ID id_private_call_p, id_top, id_bottom;
1488#define id_bt_locations idBt_locations
1489#define id_mesg idMesg
1490#define id_name idName
1492#undef rb_exc_new_cstr
1510 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
1545exc_initialize(
int argc,
VALUE *argv,
VALUE exc)
1550 return exc_init(exc, arg);
1579exc_exception(
int argc,
VALUE *argv,
VALUE self)
1584 if (argc == 0)
return self;
1585 if (argc == 1 && self == argv[0])
return self;
1607 VALUE mesg = rb_attr_get(exc, idMesg);
1617rb_get_message(
VALUE exc)
1620 if (UNDEF_P(e))
return Qnil;
1635 if (UNDEF_P(e))
return Qnil;
1647exc_s_to_tty_p(
VALUE self)
1649 return RBOOL(rb_stderr_tty_p());
1653check_highlight_keyword(
VALUE opt,
int auto_tty_detect)
1658 highlight = rb_hash_lookup(opt, sym_highlight);
1660 switch (highlight) {
1662 rb_bool_expected(highlight,
"highlight", TRUE);
1668 if (
NIL_P(highlight)) {
1669 highlight = RBOOL(auto_tty_detect && rb_stderr_tty_p());
1676check_order_keyword(
VALUE opt)
1681 static VALUE kw_order;
1684 order = rb_hash_lookup(opt, kw_order);
1686 if (order !=
Qnil) {
1688 if (
id == id_bottom) order =
Qtrue;
1689 else if (
id == id_top) order =
Qfalse;
1692 "order: %+"PRIsVALUE, order);
1752exc_full_message(
int argc,
VALUE *argv,
VALUE exc)
1754 VALUE opt, str, emesg, errat;
1755 VALUE highlight, order;
1759 highlight = check_highlight_keyword(opt, 1);
1760 order = check_order_keyword(opt);
1763 if (
NIL_P(opt)) opt = rb_hash_new_capa(1);
1764 rb_hash_aset(opt, sym_highlight, highlight);
1768 errat = rb_get_backtrace(exc);
1769 emesg = rb_get_detailed_message(exc, opt);
1771 rb_error_write(exc, emesg, errat, str, opt, highlight, order);
1785exc_message(
VALUE exc)
1796load_decoration_gem(
VALUE feature)
1807require_decoration_gems(
void)
1813 VALUE saved_errinfo = rb_errinfo();
1814 static const char *
const gems[] = {
"ErrorHighlight",
"DidYouMean",
"SyntaxSuggest"};
1815 for (
size_t i = 0; i < numberof(gems); i++) {
1817 if (
NIL_P(feature))
continue;
1819 rb_protect(load_decoration_gem, feature, &state);
1822 rb_set_errinfo(saved_errinfo);
1831lazy_load_decoration_gems(
VALUE exc)
1833 if (ATOMIC_EXCHANGE(decoration_gems_loaded, 1))
return false;
1840 require_decoration_gems();
1848rb_eager_load_detailed_message_extension(
void)
1850 if (ATOMIC_EXCHANGE(decoration_gems_loaded, 1))
return;
1852 require_decoration_gems();
1901exc_detailed_message(
int argc,
VALUE *argv,
VALUE exc)
1903 if (lazy_load_decoration_gems(exc)) {
1911 VALUE highlight = check_highlight_keyword(opt, 0);
1913 extern VALUE rb_decorate_message(
const VALUE eclass,
VALUE emesg,
int highlight);
1915 return rb_decorate_message(
CLASS_OF(exc), rb_get_message(exc),
RTEST(highlight));
1932exc_inspect(
VALUE exc)
1938 if (RSTRING_LEN(exc) == 0) {
1947 rb_str_catf(str,
":%+"PRIsVALUE, exc);
1993exc_backtrace(
VALUE exc)
1997 obj = rb_attr_get(exc, id_bt);
1999 if (rb_backtrace_p(obj)) {
2000 obj = rb_backtrace_to_str_ary(obj);
2010rb_get_backtrace(
VALUE exc)
2012 ID mid = id_backtrace;
2020 info = exc_backtrace(exc);
2027 return rb_check_backtrace(info);
2069exc_backtrace_locations(
VALUE exc)
2073 obj = rb_attr_get(exc, id_bt_locations);
2075 obj = rb_backtrace_to_location_ary(obj);
2081rb_check_backtrace(
VALUE bt)
2084 static const char err[] =
"backtrace must be an Array of String or an Array of Thread::Backtrace::Location";
2088 if (rb_backtrace_p(bt))
return bt;
2207 VALUE btobj = rb_location_ary_to_backtrace(bt);
2214 return rb_ivar_set(exc, id_bt, rb_check_backtrace(bt));
2221 return exc_set_backtrace(exc, bt);
2259 return rb_attr_get(exc, id_cause);
2263try_convert_to_exception(
VALUE obj)
2280 VALUE mesg, backtrace;
2282 if (exc == obj)
return Qtrue;
2287 obj = rb_protect(try_convert_to_exception, obj, &state);
2288 if (state || UNDEF_P(obj)) {
2289 rb_set_errinfo(
Qnil);
2294 if (UNDEF_P(mesg))
return Qfalse;
2296 if (UNDEF_P(backtrace))
return Qfalse;
2299 mesg = rb_attr_get(obj, id_mesg);
2300 backtrace = exc_backtrace(obj);
2303 if (!
rb_equal(rb_attr_get(exc, id_mesg), mesg))
2305 return rb_equal(exc_backtrace(exc), backtrace);
2321exit_initialize(
int argc,
VALUE *argv,
VALUE exc)
2329 status =
INT2FIX(EXIT_SUCCESS);
2334 status =
INT2FIX(EXIT_FAILURE);
2340 if (
NIL_P(status)) {
2341 status =
INT2FIX(EXIT_SUCCESS);
2344#if EXIT_SUCCESS != 0
2346 status =
INT2FIX(EXIT_SUCCESS);
2355 status =
INT2FIX(EXIT_SUCCESS);
2371exit_status(
VALUE exc)
2373 return rb_attr_get(exc, id_status);
2385exit_success_p(
VALUE exc)
2387 VALUE status_val = rb_attr_get(exc, id_status);
2390 if (
NIL_P(status_val))
2393 return RBOOL(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
2399 if (!UNDEF_P(recv))
rb_ivar_set(exc, id_recv, recv);
2416frozen_err_initialize(
int argc,
VALUE *argv,
VALUE self)
2419 VALUE values[numberof(keywords)], options;
2422 keywords[0] = id_receiver;
2423 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2425 err_init_recv(self, values[0]);
2437#define frozen_err_receiver name_err_receiver
2445 va_start(args, fmt);
2460 va_start(args, fmt);
2474 cfp = rb_vm_get_ruby_level_next_cfp(ec, cfp);
2476 err_init_recv(exc, recv);
2477 if (cfp && VM_FRAME_TYPE(cfp) != VM_FRAME_MAGIC_DUMMY) {
2478 rb_ivar_set(exc, id_iseq, rb_iseqw_new(CFP_ISEQ(cfp)));
2497name_err_initialize(
int argc,
VALUE *argv,
VALUE self)
2500 VALUE values[numberof(keywords)], name, options;
2503 keywords[0] = id_receiver;
2504 rb_get_kwargs(options, keywords, 0, numberof(values), values);
2505 name = (argc > 1) ? argv[--argc] :
Qnil;
2507 name_err_init_attr(self, values[0], name);
2516 exc_init(exc, rb_name_err_mesg_new(mesg, recv, method));
2517 return name_err_init_attr(exc, recv, method);
2524 return name_err_init(exc, mesg, recv, method);
2535name_err_name(
VALUE self)
2537 return rb_attr_get(self, id_name);
2551name_err_local_variables(
VALUE self)
2553 VALUE vars = rb_attr_get(self, id_local_variables);
2556 VALUE iseqw = rb_attr_get(self, id_iseq);
2557 if (!
NIL_P(iseqw)) vars = rb_iseqw_local_variables(iseqw);
2565nometh_err_init_attr(
VALUE exc,
VALUE args,
int priv)
2589nometh_err_initialize(
int argc,
VALUE *argv,
VALUE self)
2592 VALUE args, options;
2594 priv = (argc > 3) && (--argc,
RTEST(argv[argc]));
2595 args = (argc > 2) ? argv[--argc] :
Qnil;
2596 if (!
NIL_P(options)) argv[argc++] = options;
2598 return nometh_err_init_attr(self, args, priv);
2605 name_err_init(exc, mesg, recv, method);
2606 return nometh_err_init_attr(exc, args, priv);
2616name_err_mesg_mark_and_move(
void *p)
2619 rb_gc_mark_and_move(&ptr->mesg);
2620 rb_gc_mark_and_move(&ptr->recv);
2621 rb_gc_mark_and_move(&ptr->name);
2627 name_err_mesg_mark_and_move,
2630 name_err_mesg_mark_and_move,
2632 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
2656name_err_mesg_alloc(
VALUE klass)
2663name_err_mesg_init_copy(
VALUE obj1,
VALUE obj2)
2665 if (obj1 == obj2)
return obj1;
2682 if (obj1 == obj2)
return Qtrue;
2699name_err_mesg_receiver_name(
VALUE obj)
2710name_err_mesg_to_str(
VALUE obj)
2715 VALUE mesg = ptr->mesg;
2718 struct RString s_str = {RBASIC_INIT}, c_str = {RBASIC_INIT}, d_str = {RBASIC_INIT};
2719 VALUE c, s, d = 0, args[4], c2;
2723#define rb_memsearch_lit(str, v) \
2724 rb_memsearch((str), rb_strlen_lit(str), RSTRING_PTR(v), RSTRING_LEN(v), rb_enc_get(v))
2725#define FAKE_CSTR(v, str) rb_setup_fake_str((v), (str), rb_strlen_lit(str), usascii)
2726 c = s = FAKE_CSTR(&s_str,
"");
2730 c = d = FAKE_CSTR(&d_str,
"nil");
2733 c = d = FAKE_CSTR(&d_str,
"true");
2736 c = d = FAKE_CSTR(&d_str,
"false");
2739 if (rb_memsearch_lit(
"%2$s", mesg) >= 0) {
2740 d = rb_protect(name_err_mesg_receiver_name, obj, &state);
2741 if (state || NIL_OR_UNDEF_P(d))
2744 rb_set_errinfo(
Qnil);
2755 s = FAKE_CSTR(&s_str,
"module ");
2759 s = FAKE_CSTR(&s_str,
"class ");
2771 s = FAKE_CSTR(&s_str,
"");
2772 if (obj == rb_vm_top_self()) {
2773 c = FAKE_CSTR(&c_str,
"main");
2781 s = FAKE_CSTR(&s_str,
"an instance of ");
2785 c2 = rb_protect(name_err_mesg_receiver_name, c, &state);
2786 if (state || NIL_OR_UNDEF_P(c2))
2789 rb_set_errinfo(
Qnil);
2811 return name_err_mesg_to_str(obj);
2829name_err_receiver(
VALUE self)
2831 VALUE recv = rb_ivar_lookup(self, id_recv,
Qundef);
2832 if (!UNDEF_P(recv))
return recv;
2834 VALUE mesg = rb_attr_get(self, id_mesg);
2835 if (!rb_typeddata_is_kind_of(mesg, &name_err_mesg_data_type)) {
2853nometh_err_args(
VALUE self)
2855 return rb_attr_get(self, id_args);
2866nometh_err_private_call_p(
VALUE self)
2868 return rb_attr_get(self, id_private_call_p);
2872type_err_cname(
VALUE val)
2877 else if (val ==
Qtrue) {
2880 else if (val ==
Qfalse) {
2886NORETURN(
static void type_err_raise(
VALUE val,
const char *tname,
const char *msg));
2888type_err_raise(
VALUE val,
const char *tname,
const char *msg)
2890 const char *cname = type_err_cname(val);
2898NORETURN(
void rb_no_implicit_conversion(
VALUE val,
const char *tname));
2900rb_no_implicit_conversion(
VALUE val,
const char *tname)
2902 type_err_raise(val, tname,
"no implicit conversion of");
2905NORETURN(
void rb_cant_convert(
VALUE val,
const char *tname));
2907rb_cant_convert(
VALUE val,
const char *tname)
2909 type_err_raise(val, tname,
"can't convert");
2912NORETURN(
void rb_cant_convert_invalid_return(
VALUE val,
const char *tname,
const char *method_name,
VALUE ret));
2914rb_cant_convert_invalid_return(
VALUE val,
const char *tname,
const char *method_name,
VALUE ret)
2916 const char *cname = type_err_cname(val);
2920 enc,
rb_eTypeError,
"can't convert %s into %s (%s#%s gives %s)",
2921 cname, tname, cname, method_name, type_err_cname(ret));
2924 const char *retname = type_err_cname(ret);
2929 enc,
rb_eTypeError,
"can't convert %"PRIsVALUE
" into %s (%"PRIsVALUE
"#%s gives %s)",
2930 klass, tname, klass, method_name, retname);
2949key_err_receiver(
VALUE self)
2953 recv = rb_ivar_lookup(self, id_receiver,
Qundef);
2954 if (!UNDEF_P(recv))
return recv;
2966key_err_key(
VALUE self)
2970 key = rb_ivar_lookup(self, id_key,
Qundef);
2971 if (!UNDEF_P(key))
return key;
2995key_err_initialize(
int argc,
VALUE *argv,
VALUE self)
3001 if (!
NIL_P(options)) {
3003 VALUE values[numberof(keywords)];
3005 keywords[0] = id_receiver;
3006 keywords[1] = id_key;
3007 rb_get_kwargs(options, keywords, 0, numberof(values), values);
3008 for (i = 0; i < numberof(values); ++i) {
3009 if (!UNDEF_P(values[i])) {
3026no_matching_pattern_key_err_matchee(
VALUE self)
3030 matchee = rb_ivar_lookup(self, id_matchee,
Qundef);
3031 if (!UNDEF_P(matchee))
return matchee;
3043no_matching_pattern_key_err_key(
VALUE self)
3047 key = rb_ivar_lookup(self, id_key,
Qundef);
3048 if (!UNDEF_P(key))
return key;
3061no_matching_pattern_key_err_initialize(
int argc,
VALUE *argv,
VALUE self)
3067 if (!
NIL_P(options)) {
3069 VALUE values[numberof(keywords)];
3071 keywords[0] = id_matchee;
3072 keywords[1] = id_key;
3073 rb_get_kwargs(options, keywords, 0, numberof(values), values);
3074 for (i = 0; i < numberof(values); ++i) {
3075 if (!UNDEF_P(values[i])) {
3093syntax_error_initialize(
int argc,
VALUE *argv,
VALUE self)
3097 mesg = rb_fstring_lit(
"compile error");
3108 *mesg = rb_enc_str_new(0, 0, enc);
3113 VALUE old_path = rb_attr_get(exc, id_i_path);
3114 if (old_path != path) {
3116 rb_raise(
rb_eArgError,
"SyntaxError#path changed: %+"PRIsVALUE
" (%p->%p)",
3117 old_path, (
void *)old_path, (
void *)path);
3120 rb_raise(
rb_eArgError,
"SyntaxError#path changed: %+"PRIsVALUE
"(%s%s)->%+"PRIsVALUE
"(%s)",
3121 old_path, rb_enc_name(rb_enc_get(old_path)),
3122 (
FL_TEST(old_path, RSTRING_FSTR) ?
":FSTR" :
""),
3123 path, rb_enc_name(rb_enc_get(path)));
3126 VALUE s = *mesg = rb_attr_get(exc, idMesg);
3127 if (RSTRING_LEN(s) > 0 && *(
RSTRING_END(s)-1) !=
'\n')
3188rb_free_warning(
void)
3190 st_free_table(warning_categories.id2enum);
3191 st_free_table(warning_categories.enum2id);
3192 st_free_table(syserr_tbl);
3196setup_syserr(
int n,
const char *name)
3205#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
3210 rb_eEWOULDBLOCK = error;
3213 rb_eEINPROGRESS = error;
3217 rb_define_const(error,
"Errno",
INT2NUM(n));
3218 st_add_direct(syserr_tbl, n, (st_data_t)error);
3223set_syserr(
int n,
const char *name)
3227 if (!st_lookup(syserr_tbl, n, &error)) {
3228 return setup_syserr(n, name);
3232 rb_define_const(
rb_mErrno, name, errclass);
3242 if (!st_lookup(syserr_tbl, n, &error)) {
3245 snprintf(name,
sizeof(name),
"E%03d", n);
3246 return setup_syserr(n, name);
3248 return (
VALUE)error;
3283syserr_initialize(
int argc,
VALUE *argv,
VALUE self)
3286 VALUE mesg, error, func, errmsg;
3290 st_data_t data = (st_data_t)klass;
3293 error = mesg; mesg =
Qnil;
3295 if (!
NIL_P(error) && st_lookup(syserr_tbl,
NUM2LONG(error), &data)) {
3296 klass = (
VALUE)data;
3301 RBASIC_SET_CLASS(self, klass);
3309 else err =
"unknown error";
3315 if (!
NIL_P(func)) rb_str_catf(errmsg,
" @ %"PRIsVALUE, func);
3316 rb_str_catf(errmsg,
" - %"PRIsVALUE, str);
3333syserr_errno(
VALUE self)
3335 return rb_attr_get(self, id_errno);
3356 num = rb_attr_get(exc, id_errno);
3740exception_alloc(
VALUE klass)
3742 return rb_class_allocate_instance(klass);
3746exception_dumper(
VALUE exc)
3756ivar_copy_i(
ID key,
VALUE val, st_data_t exc)
3762void rb_exc_check_circular_cause(
VALUE exc);
3778 rb_exc_check_circular_cause(exc);
3780 if (rb_attr_get(exc, id_bt) == rb_attr_get(exc, id_bt_locations)) {
3875 syserr_tbl = st_init_numtable();
3883 rb_mWarning = rb_define_module(
"Warning");
3891 rb_cWarningBuffer = rb_define_class_under(rb_mWarning,
"buffer",
rb_cString);
3915 id_iseq = rb_make_internal_id();
3916 id_recv = rb_make_internal_id();
3918 sym_category =
ID2SYM(id_category);
3921 warning_categories.id2enum = rb_init_identtable();
3927 warning_categories.enum2id = rb_init_identtable();
3941 va_start(args, fmt);
3949rb_vraise(
VALUE exc,
const char *fmt, va_list ap)
3955rb_raise(
VALUE exc_class,
const char *fmt, ...)
3958 va_start(args, fmt);
3964NORETURN(
static void raise_loaderror(
VALUE path,
VALUE mesg));
3980 va_start(args, fmt);
3983 raise_loaderror(
Qnil, mesg);
3992 va_start(args, fmt);
3995 raise_loaderror(path, mesg);
4002 "%"PRIsVALUE
"() function is unimplemented on this machine",
4003 rb_id2str(rb_frame_this_func()));
4007rb_fatal(
const char *fmt, ...)
4012 if (! ruby_thread_has_gvl_p()) {
4015 fprintf(stderr,
"[FATAL] rb_fatal() outside of GVL\n");
4016 rb_print_backtrace(stderr);
4020 va_start(args, fmt);
4028make_errno_exc(
const char *mesg)
4034 rb_bug(
"rb_sys_fail(%s) - errno == 0", mesg ? mesg :
"");
4040make_errno_exc_str(
VALUE mesg)
4045 if (!mesg) mesg =
Qnil;
4047 const char *s = !
NIL_P(mesg) ? RSTRING_PTR(mesg) :
"";
4048 rb_bug(
"rb_sys_fail_str(%s) - errno == 0", s);
4081rb_sys_fail(
const char *mesg)
4086#undef rb_sys_fail_str
4093#ifdef RUBY_FUNCTION_NAME_STRING
4095rb_sys_fail_path_in(
const char *func_name,
VALUE path)
4100 rb_syserr_fail_path_in(func_name, n, path);
4104rb_syserr_fail_path_in(
const char *func_name,
int n,
VALUE path)
4106 rb_exc_raise(rb_syserr_new_path_in(func_name, n, path));
4110rb_syserr_new_path_in(
const char *func_name,
int n,
VALUE path)
4114 if (!path) path =
Qnil;
4116 const char *s = !
NIL_P(path) ? RSTRING_PTR(path) :
"";
4117 if (!func_name) func_name =
"(null)";
4118 rb_bug(
"rb_sys_fail_path_in(%s, %s) - errno == 0",
4127NORETURN(
static void rb_mod_exc_raise(
VALUE exc,
VALUE mod));
4139 VALUE exc = make_errno_exc(mesg);
4140 rb_mod_exc_raise(exc, mod);
4146 VALUE exc = make_errno_exc_str(mesg);
4147 rb_mod_exc_raise(exc, mod);
4154 rb_mod_exc_raise(exc, mod);
4161 rb_mod_exc_raise(exc, mod);
4165syserr_warning(
VALUE mesg,
int err)
4168 rb_str_catf(mesg,
": %s\n", strerror(err));
4169 rb_write_warning_str(mesg);
4174rb_sys_warn(
const char *fmt, ...)
4177 int errno_save =
errno;
4178 with_warning_string(mesg, 0, fmt) {
4179 syserr_warning(mesg, errno_save);
4186rb_syserr_warn(
int err,
const char *fmt, ...)
4189 with_warning_string(mesg, 0, fmt) {
4190 syserr_warning(mesg, err);
4196rb_sys_enc_warn(
rb_encoding *enc,
const char *fmt, ...)
4199 int errno_save =
errno;
4200 with_warning_string(mesg, enc, fmt) {
4201 syserr_warning(mesg, errno_save);
4208rb_syserr_enc_warn(
int err,
rb_encoding *enc,
const char *fmt, ...)
4211 with_warning_string(mesg, enc, fmt) {
4212 syserr_warning(mesg, err);
4222 int errno_save =
errno;
4223 with_warning_string(mesg, 0, fmt) {
4224 syserr_warning(mesg, errno_save);
4232rb_syserr_warning(
int err,
const char *fmt, ...)
4235 with_warning_string(mesg, 0, fmt) {
4236 syserr_warning(mesg, err);
4243rb_sys_enc_warning(
rb_encoding *enc,
const char *fmt, ...)
4246 int errno_save =
errno;
4247 with_warning_string(mesg, enc, fmt) {
4248 syserr_warning(mesg, errno_save);
4255rb_syserr_enc_warning(
int err,
rb_encoding *enc,
const char *fmt, ...)
4258 with_warning_string(mesg, enc, fmt) {
4259 syserr_warning(mesg, err);
4265rb_load_fail(
VALUE path,
const char *err)
4270 raise_loaderror(path, mesg);
4285 va_start(args, fmt);
4294inspect_frozen_obj(
VALUE obj,
VALUE mesg,
int recur)
4306get_created_info(
VALUE obj,
int *pline)
4308 VALUE info = rb_attr_get(obj, id_debug_created_info);
4322 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
4324 VALUE mesg = rb_sprintf(
"can't modify frozen %"PRIsVALUE
": ",
4332 VALUE created_path = get_created_info(frozen_obj, &created_line);
4333 if (!
NIL_P(created_path)) {
4334 rb_str_catf(mesg,
", created at %"PRIsVALUE
":%d", created_path, created_line);
4340rb_warn_unchilled_literal(
VALUE obj)
4345 VALUE file = rb_source_location(&line);
4349 if (line) rb_str_catf(mesg,
":%d", line);
4352 rb_str_cat2(mesg,
"warning: literal string will be frozen in the future");
4355 if (STR_SHARED_P(str)) {
4356 str =
RSTRING(obj)->as.heap.aux.shared;
4358 VALUE created = get_created_info(str, &line);
4359 if (
NIL_P(created)) {
4360 rb_str_cat2(mesg,
" (run with --debug-frozen-string-literal for more information)\n");
4365 if (line) rb_str_catf(mesg,
":%d", line);
4366 rb_str_cat2(mesg,
": info: the string was created here\n");
4368 rb_warn_category(mesg, rb_warning_category_to_name(category));
4372#undef rb_check_frozen
4383 rb_check_frozen(obj);
4390 rb_eNOERROR = setup_syserr(0,
"NOERROR");
4393 rb_define_const(
rb_mErrno,
"NOERROR", rb_eNOERROR);
4395#define defined_error(name, num) set_syserr((num), (name));
4396#define undefined_error(name) rb_define_const(rb_mErrno, (name), rb_eNOERROR);
4397#include "known_errors.inc"
4399#undef undefined_error
4402#include "warning.rbinc"
#define RUBY_DEBUG
Define this macro when you want assertions.
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#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.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
void rb_extend_object(VALUE obj, VALUE module)
Extend the object with the module.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
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.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define TYPE(_)
Old name of rb_type.
#define ISSPACE
Old name of rb_isspace.
#define T_STRING
Old name of RUBY_T_STRING.
#define T_MASK
Old name of RUBY_T_MASK.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define UNREACHABLE
Old name of RBIMPL_UNREACHABLE.
#define ID2SYM
Old name of RB_ID2SYM.
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define T_DATA
Old name of RUBY_T_DATA.
#define CLASS_OF
Old name of rb_class_of.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_TRUE
Old name of RUBY_T_TRUE.
#define FL_ABLE
Old name of RB_FL_ABLE.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define rb_exc_new3
Old name of rb_exc_new_str.
#define T_FALSE
Old name of RUBY_T_FALSE.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_notimplement(void)
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...
rb_warning_category_t
Warning categories.
void rb_category_warn(rb_warning_category_t category, const char *fmt,...)
Identical to rb_category_warning(), except it reports unless $VERBOSE is nil.
void rb_category_warning(rb_warning_category_t category, const char *fmt,...)
Identical to rb_warning(), except it takes additional "category" parameter.
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.
VALUE rb_eNotImpError
NotImplementedError exception.
VALUE rb_eScriptError
ScriptError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
VALUE rb_eKeyError
KeyError exception.
VALUE rb_cNameErrorMesg
NameError::Message class.
VALUE rb_eSystemExit
SystemExit exception.
void rb_name_error(ID id, const char *fmt,...)
Raises an instance of rb_eNameError.
void rb_sys_warning(const char *fmt,...)
Identical to rb_sys_fail(), except it does not raise an exception to render a warning instead.
void rb_check_copyable(VALUE obj, VALUE orig)
Ensures that the passed object can be initialize_copy relationship.
VALUE rb_eStandardError
StandardError exception.
VALUE rb_mErrno
Errno module.
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.
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.
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...
VALUE rb_eFrozenError
FrozenError exception.
VALUE rb_eNoMemError
NoMemoryError exception.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eLoadError
LoadError exception.
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.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eTypeError
TypeError exception.
void rb_unexpected_object_type(VALUE obj, const char *expected)
Fails with the given object's type incompatibility to the type.
VALUE rb_eNoMatchingPatternError
NoMatchingPatternError exception.
void rb_name_error_str(VALUE str, const char *fmt,...)
Identical to rb_name_error(), except it takes a VALUE instead of ID.
void rb_frozen_error_raise(VALUE frozen_obj, const char *fmt,...)
Raises an instance of rb_eFrozenError.
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
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.
VALUE rb_eFatal
fatal exception.
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.
VALUE rb_eInterrupt
Interrupt exception.
VALUE rb_eNameError
NameError exception.
VALUE rb_eNoMethodError
NoMethodError exception.
void rb_exc_fatal(VALUE mesg)
Raises a fatal error in the current thread.
VALUE rb_eRuntimeError
RuntimeError exception.
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.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_eNoMatchingPatternKeyError
NoMatchingPatternKeyError exception.
void rb_error_frozen_object(VALUE frozen_obj)
Identical to rb_error_frozen(), except it takes arbitrary Ruby object instead of C's string.
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.
VALUE rb_eArgError
ArgumentError exception.
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...
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
VALUE rb_eException
Mother of all exceptions.
VALUE rb_exc_new(VALUE etype, const char *ptr, rb_len_t len)
Creates an instance of the passed exception class.
VALUE rb_eIndexError
IndexError exception.
void rb_loaderror_with_path(VALUE path, const char *fmt,...)
Identical to rb_loaderror(), except it additionally takes which file is unable to load.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_eEncodingError
EncodingError exception.
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.
VALUE rb_syserr_new(int n, const char *mesg)
Creates an exception object that represents the given C errno.
VALUE rb_eSecurityError
SecurityError exception.
void rb_unexpected_type(VALUE x, int t)
Fails with the given object's type incompatibility to the type.
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.
void rb_check_type(VALUE x, int t)
This was the old implementation of Check_Type(), but they diverged.
VALUE rb_eSystemCallError
SystemCallError exception.
void rb_warning(const char *fmt,...)
Issues a warning.
VALUE rb_eSignal
SignalException exception.
@ RB_WARN_CATEGORY_STRICT_UNUSED_BLOCK
Warning is for checking unused block strictly.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
@ RB_WARN_CATEGORY_EXPERIMENTAL
Warning is for experimental features.
@ RB_WARN_CATEGORY_PERFORMANCE
Warning is for performance issues (not enabled by -w).
@ RB_WARN_CATEGORY_NONE
Category unspecified.
VALUE rb_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
VALUE rb_cObject
Object class.
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_obj_init_copy(VALUE src, VALUE dst)
Default implementation of #initialize_copy, #initialize_dup and #initialize_clone.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cEncoding
Encoding class.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
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.
VALUE rb_String(VALUE val)
This is the logic behind Kernel#String.
VALUE rb_cString
String class.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
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.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
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.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
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...
VALUE rb_call_super(int argc, const VALUE *argv)
This resembles ruby's super.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_new(void)
Allocates a new, empty array.
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.
static void rb_check_frozen_inline(VALUE obj)
Just another name of rb_check_frozen.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_require_string(VALUE feature)
Finds and loads the given feature, if absent.
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
void ruby_default_signal(int sig)
Pretends as if there was no custom signal handler.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(rb_len_t len)
Allocates a "temporary" string.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_str_buf_cat
Just another name of rb_str_cat.
#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.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
#define rb_str_buf_new_cstr(str)
Identical to rb_str_new_cstr, except done differently.
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.
VALUE rb_str_equal(VALUE str1, VALUE str2)
Equality of two strings.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
#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.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
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.
VALUE rb_autoload_p(VALUE space, ID name)
Queries if an autoload is defined at a point.
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.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
int rb_method_basic_definition_p(VALUE klass, ID mid)
Well... Let us hesitate from describing what a "basic definition" is.
void rb_attr(VALUE klass, ID name, int need_reader, int need_writer, int honour_visibility)
This function resembles now-deprecated Module#attr.
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.
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...
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().
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
int len
Length of the buffer.
#define DECIMAL_SIZE_OF(expr)
An approximation of decimal representation size.
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
VALUE rb_vsprintf(const char *fmt, va_list ap)
Identical to rb_sprintf(), except it takes a va_list.
#define ALLOCA_N(type, n)
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.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#define RSTRING(obj)
Convenient casting macro.
char * rb_string_value_ptr(volatile VALUE *ptr)
Identical to rb_str_to_str(), except it returns the converted string's backend memory region.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#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...
VALUE rb_argv0
The value of $0 at process bootup.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define errno
Ractor-aware version of errno.
#define RB_PASS_KEYWORDS
Pass keywords, final argument must be a hash of keywords.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
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.
char * ptr
Pointer to the contents of the string.
This is the struct that holds necessary info for a struct.
const char * wrap_struct_name
Name of structs of this kind.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.