14#include "ruby/internal/config.h"
25#include "internal/array.h"
26#include "internal/class.h"
27#include "internal/error.h"
28#include "internal/eval.h"
29#include "internal/inits.h"
30#include "internal/numeric.h"
31#include "internal/object.h"
32#include "internal/struct.h"
33#include "internal/string.h"
34#include "internal/st.h"
35#include "internal/symbol.h"
36#include "internal/variable.h"
70static VALUE rb_cNilClass_to_s;
71static VALUE rb_cTrueClass_to_s;
72static VALUE rb_cFalseClass_to_s;
78#define id_match idEqTilde
79#define id_inspect idInspect
80#define id_init_copy idInitialize_copy
81#define id_init_clone idInitialize_clone
82#define id_init_dup idInitialize_dup
83#define id_const_missing idConst_missing
85static ID id_instance_variables_to_inspect;
87#define CLASS_OR_MODULE_P(obj) \
88 (!SPECIAL_CONST_P(obj) && \
89 (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
96 return offsetof(
struct RObject, as.ary) + (
sizeof(
VALUE) * fields_count);
103 RBASIC_CLEAR_CLASS(obj);
112 RBASIC_SET_CLASS(obj, klass);
118rb_class_allocate_instance(
VALUE klass)
120 uint32_t index_tbl_num_entries = RCLASS_MAX_IV_COUNT(klass);
123 if (!rb_gc_size_allocatable_p(size)) {
130 NEWOBJ_OF_WITH_SHAPE(o,
struct RObject, klass, flags, rb_shape_root(rb_gc_heap_id_for_size(size)), size, 0);
136 size_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
137 for (
size_t i = fields_count; i < ROBJECT_FIELDS_CAPACITY(obj); i++) {
141 rb_bug(
"Expected rb_class_allocate_instance to set the class correctly");
152 RBASIC(obj)->flags = (
type & ~ignored_flags) | (
RBASIC(obj)->flags & ignored_flags);
153 RBASIC_SET_CLASS(obj, klass);
171#define case_equal rb_equal
180 if (obj1 == obj2)
return Qtrue;
181 result = rb_equal_opt(obj1, obj2);
182 if (UNDEF_P(result)) {
185 return RBOOL(
RTEST(result));
193 if (obj1 == obj2)
return TRUE;
194 result = rb_eql_opt(obj1, obj2);
195 if (UNDEF_P(result)) {
198 return RTEST(result);
207 return RBOOL(obj1 == obj2);
219 return RBOOL(!
RTEST(obj));
230 return rb_obj_not(result);
234fake_class_p(
VALUE klass)
248 while (RB_UNLIKELY(fake_class_p(cl))) {
274rb_obj_class_must(
VALUE obj)
297rb_obj_singleton_class(
VALUE obj)
314 shape_id_t src_shape_id = RBASIC_SHAPE_ID(obj);
316 if (rb_shape_too_complex_p(src_shape_id)) {
317 rb_shape_copy_complex_ivars(dest, obj, src_shape_id, ROBJECT_FIELDS_HASH(obj));
321 shape_id_t initial_shape_id = RBASIC_SHAPE_ID(dest);
322 RUBY_ASSERT(RSHAPE_TYPE_P(initial_shape_id, SHAPE_ROOT));
324 shape_id_t dest_shape_id = rb_shape_rebuild(initial_shape_id, src_shape_id);
325 if (UNLIKELY(rb_shape_too_complex_p(dest_shape_id))) {
326 st_table *table = rb_st_init_numtable_with_size(src_num_ivs);
327 rb_obj_copy_ivs_to_hash_table(obj, table);
328 rb_obj_init_too_complex(dest, table);
336 attr_index_t initial_capa = RSHAPE_CAPACITY(initial_shape_id);
337 attr_index_t dest_capa = RSHAPE_CAPACITY(dest_shape_id);
340 if (initial_capa < dest_capa) {
341 rb_ensure_iv_list_size(dest, 0, dest_capa);
345 rb_shape_copy_fields(dest, dest_buf, dest_shape_id, src_buf, src_shape_id);
346 RBASIC_SET_SHAPE_ID(dest, dest_shape_id);
355 RBASIC(dest)->flags &= ~T_MASK;
360 rb_bug(
"Unreachable");
367 rb_obj_copy_ivar(dest, obj);
373 rb_gc_copy_attributes(dest, obj);
378PUREFUNC(
static inline int special_object_p(
VALUE obj));
380special_object_p(
VALUE obj)
397obj_freeze_opt(
VALUE freeze)
405 rb_raise(rb_eArgError,
"unexpected value for freeze: %"PRIsVALUE,
rb_obj_class(freeze));
414 VALUE kwfreeze = obj_freeze_opt(freeze);
415 if (!special_object_p(obj))
416 return mutable_obj_clone(obj, kwfreeze);
417 return immutable_obj_clone(obj, kwfreeze);
422rb_immutable_obj_clone(
int argc,
VALUE *argv,
VALUE obj)
424 VALUE kwfreeze = rb_get_freeze_opt(argc, argv);
425 return immutable_obj_clone(obj, kwfreeze);
429rb_get_freeze_opt(
int argc,
VALUE *argv)
431 static ID keyword_ids[1];
435 if (!keyword_ids[0]) {
441 if (!UNDEF_P(kwfreeze))
442 kwfreeze = obj_freeze_opt(kwfreeze);
451 rb_raise(rb_eArgError,
"can't unfreeze %"PRIsVALUE,
461 VALUE singleton = rb_singleton_class_clone_and_attach(obj, clone);
462 RBASIC_SET_CLASS(clone, singleton);
463 if (RCLASS_SINGLETON_P(singleton)) {
467 init_copy(clone, obj);
479 shape_id_t next_shape_id = rb_shape_transition_frozen(clone);
480 RBASIC_SET_SHAPE_ID(clone, next_shape_id);
484 static VALUE freeze_true_hash;
485 if (!freeze_true_hash) {
486 freeze_true_hash = rb_hash_new();
487 rb_vm_register_global_object(freeze_true_hash);
488 rb_hash_aset(freeze_true_hash,
ID2SYM(idFreeze),
Qtrue);
493 argv[1] = freeze_true_hash;
499 static VALUE freeze_false_hash;
500 if (!freeze_false_hash) {
501 freeze_false_hash = rb_hash_new();
502 rb_vm_register_global_object(freeze_false_hash);
503 rb_hash_aset(freeze_false_hash,
ID2SYM(idFreeze),
Qfalse);
508 argv[1] = freeze_false_hash;
513 rb_bug(
"invalid kwfreeze passed to mutable_obj_clone");
523 return rb_obj_clone_setup(obj, clone, kwfreeze);
529 if (special_object_p(obj))
return obj;
530 return mutable_obj_clone(obj,
Qnil);
586 if (special_object_p(obj)) {
590 return rb_obj_dup_setup(obj, dup);
605rb_obj_itself(
VALUE obj)
624 if (obj == orig)
return obj;
625 rb_check_frozen(obj);
627 rb_raise(
rb_eTypeError,
"initialize_copy should take same class object");
656 if (
rb_scan_args(argc, argv,
"1:", &orig, &opts) < argc) {
658 rb_get_freeze_opt(1, &opts);
680 str = rb_sprintf(
"#<%"PRIsVALUE
":%p>", cname, (
void*)obj);
691 if (enc == NULL) enc = rb_default_external_encoding();
692 if (!rb_enc_asciicompat(enc)) {
694 return rb_str_escape(str);
698 return rb_str_escape(str);
703inspect_i(
ID id,
VALUE value, st_data_t a)
705 VALUE *args = (
VALUE *)a, str = args[0], ivars = args[1];
708 if (
CLASS_OF(value) == 0)
return ST_CONTINUE;
712 for (
long i = 0;
RARRAY_AREF(ivars, i) != name; ) {
713 if (++i >=
RARRAY_LEN(ivars))
return ST_CONTINUE;
716 if (RSTRING_PTR(str)[0] ==
'-') {
717 RSTRING_PTR(str)[0] =
'#';
723 rb_str_catf(str,
"%"PRIsVALUE
"=", rb_id2str(
id));
741 RSTRING_PTR(str)[0] =
'#';
793rb_obj_inspect(
VALUE obj)
797 if (UNDEF_P(ivars) ||
NIL_P(ivars)) {
807 "Expected #instance_variables_to_inspect to return an Array or nil, but it returned %"PRIsVALUE,
815 rb_sprintf(
"-<%"PRIsVALUE
":%p", c, (
void*)obj),
827rb_obj_instance_variables_to_inspect(
VALUE obj)
833class_or_module_required(
VALUE c)
835 switch (OBJ_BUILTIN_TYPE(c)) {
869 c = class_or_module_required(c);
881 size_t c_depth = RCLASS_SUPERCLASS_DEPTH(c);
882 size_t cl_depth = RCLASS_SUPERCLASS_DEPTH(cl);
883 VALUE *classes = RCLASS_SUPERCLASSES(cl);
887 if (cl_depth <= c_depth)
891 return RBOOL(classes[c_depth] == c);
931 if (cl == c)
return Qtrue;
938 return class_search_class_ancestor(cl, c);
943 VALUE includer = RCLASS_INCLUDER(c);
944 if (cl == includer)
return Qtrue;
950 if (
RB_TYPE_P(includer,
T_CLASS) && class_search_class_ancestor(cl, includer))
955 return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
959 return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
972 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
983 cl = class_or_module_required(cl);
984 c = class_or_module_required(c);
985 return class_search_ancestor(cl, RCLASS_ORIGIN(c));
1016#define rb_obj_class_inherited rb_obj_dummy1
1039#define rb_obj_mod_method_added rb_obj_dummy1
1066#define rb_obj_mod_method_removed rb_obj_dummy1
1093#define rb_obj_mod_method_undefined rb_obj_dummy1
1120#define rb_obj_singleton_method_added rb_obj_dummy1
1149#define rb_obj_singleton_method_removed rb_obj_dummy1
1174#define rb_obj_singleton_method_undefined rb_obj_dummy1
1220#define rb_obj_mod_const_added rb_obj_dummy1
1240#define rb_obj_mod_extended rb_obj_dummy1
1263#define rb_obj_mod_included rb_obj_dummy1
1283#define rb_obj_mod_prepended rb_obj_dummy1
1293#define rb_obj_initialize rb_obj_dummy0
1308 return rb_obj_dummy();
1314 return rb_obj_dummy();
1347 rb_bug(
"special consts should be frozen.");
1408rb_nil_to_s(
VALUE obj)
1410 return rb_cNilClass_to_s;
1446 return rb_hash_new();
1460nil_inspect(
VALUE obj)
1517rb_true_to_s(
VALUE obj)
1519 return rb_cTrueClass_to_s;
1538 return RBOOL(
RTEST(obj2));
1582 return rb_obj_not(obj2);
1604rb_false_to_s(
VALUE obj)
1606 return rb_cFalseClass_to_s;
1644#define false_or true_and
1659#define false_xor true_and
1704 return rb_obj_not(result);
1784rb_mod_to_s(
VALUE klass)
1787 VALUE refined_class, defined_at;
1789 if (RCLASS_SINGLETON_P(klass)) {
1791 VALUE v = RCLASS_ATTACHED_OBJECT(klass);
1793 if (CLASS_OR_MODULE_P(v)) {
1803 refined_class = rb_refinement_module_get_refined_class(klass);
1804 if (!
NIL_P(refined_class)) {
1809 CONST_ID(id_defined_at,
"__defined_at__");
1810 defined_at = rb_attr_get(klass, id_defined_at);
1828rb_mod_freeze(
VALUE mod)
1879 if (mod == arg)
return Qtrue;
1883 size_t mod_depth = RCLASS_SUPERCLASS_DEPTH(mod);
1884 size_t arg_depth = RCLASS_SUPERCLASS_DEPTH(arg);
1885 if (arg_depth < mod_depth) {
1887 return RCLASS_SUPERCLASSES(mod)[arg_depth] == arg ?
1891 else if (arg_depth > mod_depth) {
1893 return RCLASS_SUPERCLASSES(arg)[mod_depth] == mod ?
1906 if (class_search_ancestor(mod, RCLASS_ORIGIN(arg))) {
1910 if (class_search_ancestor(arg, mod)) {
1945 if (mod == arg)
return Qfalse;
1980 if (!CLASS_OR_MODULE_P(arg)) {
2015 if (mod == arg)
return Qfalse;
2016 return rb_mod_ge(mod, arg);
2052 if (mod == arg)
return INT2FIX(0);
2053 if (!CLASS_OR_MODULE_P(arg)) {
2065static VALUE rb_mod_initialize_exec(
VALUE module);
2118rb_mod_initialize(
VALUE module)
2120 return rb_mod_initialize_exec(module);
2124rb_mod_initialize_exec(
VALUE module)
2134rb_mod_initialize_clone(
int argc,
VALUE* argv,
VALUE clone)
2136 VALUE ret, orig, opts;
2175rb_class_initialize(
int argc,
VALUE *argv,
VALUE klass)
2188 if (!RCLASS_INITIALIZED_P(super)) {
2189 rb_raise(
rb_eTypeError,
"can't inherit uninitialized class");
2192 rb_class_set_super(klass, super);
2193 rb_make_metaclass(klass,
RBASIC(super)->klass);
2195 rb_mod_initialize_exec(klass);
2202rb_undefined_alloc(
VALUE klass)
2204 rb_raise(
rb_eTypeError,
"allocator undefined for %"PRIsVALUE,
2234rb_class_alloc(
VALUE klass)
2237 return class_call_alloc_func(allocator, klass);
2241class_get_alloc_func(
VALUE klass)
2245 if (!RCLASS_INITIALIZED_P(klass)) {
2246 rb_raise(
rb_eTypeError,
"can't instantiate uninitialized class");
2248 if (RCLASS_SINGLETON_P(klass)) {
2249 rb_raise(
rb_eTypeError,
"can't create instance of singleton class");
2253 rb_undefined_alloc(klass);
2260rb_zjit_class_get_alloc_func(
VALUE klass)
2262 assert(RCLASS_INITIALIZED_P(klass));
2263 assert(!RCLASS_SINGLETON_P(klass));
2274 obj = (*allocator)(klass);
2288 return rb_class_alloc(klass);
2307 obj = rb_class_alloc(klass);
2319 obj = rb_class_alloc(klass);
2320 rb_obj_call_init_kw(obj, argc, argv, kw_splat);
2345 VALUE *superclasses = RCLASS_SUPERCLASSES(klass);
2346 size_t superclasses_depth = RCLASS_SUPERCLASS_DEPTH(klass);
2350 if (!superclasses) {
2355 if (!superclasses_depth) {
2359 VALUE super = superclasses[superclasses_depth - 1];
2371static const char bad_instance_name[] =
"'%1$s' is not allowed as an instance variable name";
2372static const char bad_class_name[] =
"'%1$s' is not allowed as a class variable name";
2373static const char bad_const_name[] =
"wrong constant name %1$s";
2374static const char bad_attr_name[] =
"invalid attribute name '%1$s'";
2375#define wrong_constant_name bad_const_name
2378#define id_for_var(obj, name, type) id_for_setter(obj, name, type, bad_##type##_name)
2380#define id_for_setter(obj, name, type, message) \
2381 check_setter_id(obj, &(name), rb_is_##type##_id, rb_is_##type##_name, message, strlen(message))
2384 int (*valid_id_p)(
ID),
int (*valid_name_p)(
VALUE),
2385 const char *message,
size_t message_len)
2388 VALUE name = *pname;
2390 if (
id ? !valid_id_p(
id) : !valid_name_p(name)) {
2391 rb_name_err_raise_str(rb_fstring_new(message, message_len),
2398rb_is_attr_name(
VALUE name)
2400 return rb_is_local_name(name) || rb_is_const_name(name);
2412 ID id = id_for_var(obj, name, attr);
2413 if (!
id)
id = rb_intern_str(name);
2432rb_mod_attr_reader(
int argc,
VALUE *argv,
VALUE klass)
2437 for (i=0; i<argc; i++) {
2438 ID id = id_for_attr(klass, argv[i]);
2439 rb_attr(klass,
id, TRUE, FALSE, TRUE);
2450rb_mod_attr(
int argc,
VALUE *argv,
VALUE klass)
2452 if (argc == 2 && (argv[1] ==
Qtrue || argv[1] ==
Qfalse)) {
2453 ID id = id_for_attr(klass, argv[0]);
2462 return rb_mod_attr_reader(argc, argv, klass);
2477rb_mod_attr_writer(
int argc,
VALUE *argv,
VALUE klass)
2482 for (i=0; i<argc; i++) {
2483 ID id = id_for_attr(klass, argv[i]);
2484 rb_attr(klass,
id, FALSE, TRUE, TRUE);
2509rb_mod_attr_accessor(
int argc,
VALUE *argv,
VALUE klass)
2514 for (i=0; i<argc; i++) {
2515 ID id = id_for_attr(klass, argv[i]);
2517 rb_attr(klass,
id, TRUE, TRUE, TRUE);
2565rb_mod_const_get(
int argc,
VALUE *argv,
VALUE mod)
2569 const char *pbeg, *p, *path, *pend;
2574 recur = (argc == 1) ?
Qtrue : argv[1];
2577 if (!rb_is_const_sym(name))
goto wrong_name;
2579 if (!
id)
return rb_const_missing(mod, name);
2584 enc = rb_enc_get(name);
2586 if (!rb_enc_asciicompat(enc)) {
2587 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
2591 pend = path + RSTRING_LEN(name);
2593 if (p >= pend || !*p) {
2597 if (p + 2 < pend && p[0] ==
':' && p[1] ==
':') {
2607 while (p < pend && *p !=
':') p++;
2609 if (pbeg == p)
goto wrong_name;
2614 if (p < pend && p[0] ==
':') {
2615 if (p + 2 >= pend || p[1] !=
':')
goto wrong_name;
2621 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
2628 if (!rb_is_const_name(part)) {
2632 else if (!rb_method_basic_definition_p(
CLASS_OF(mod), id_const_missing)) {
2634 mod = rb_const_missing(mod, part);
2638 rb_mod_const_missing(mod, part);
2646 mod = rb_const_get_0(mod,
id, beglen > 0 || !
RTEST(recur),
RTEST(recur), FALSE);
2648 if (!
RTEST(recur)) {
2651 else if (beglen == 0) {
2663 rb_name_err_raise(wrong_constant_name, mod, name);
2689 ID id = id_for_var(mod, name,
const);
2690 if (!
id)
id = rb_intern_str(name);
2738rb_mod_const_defined(
int argc,
VALUE *argv,
VALUE mod)
2742 const char *pbeg, *p, *path, *pend;
2747 recur = (argc == 1) ?
Qtrue : argv[1];
2750 if (!rb_is_const_sym(name))
goto wrong_name;
2757 enc = rb_enc_get(name);
2759 if (!rb_enc_asciicompat(enc)) {
2760 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
2764 pend = path + RSTRING_LEN(name);
2766 if (p >= pend || !*p) {
2770 if (p + 2 < pend && p[0] ==
':' && p[1] ==
':') {
2780 while (p < pend && *p !=
':') p++;
2782 if (pbeg == p)
goto wrong_name;
2787 if (p < pend && p[0] ==
':') {
2788 if (p + 2 >= pend || p[1] !=
':')
goto wrong_name;
2796 if (!rb_is_const_name(part)) {
2810 mod = rb_const_search(mod,
id, beglen > 0 || !
RTEST(recur),
RTEST(recur), FALSE);
2811 if (UNDEF_P(mod))
return Qfalse;
2813 if (!
RTEST(recur)) {
2816 if (p == pend)
return Qtrue;
2819 else if (beglen == 0) {
2822 if (p == pend)
return Qtrue;
2828 if (p == pend)
return Qtrue;
2834 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
2842 rb_name_err_raise(wrong_constant_name, mod, name);
2898rb_mod_const_source_location(
int argc,
VALUE *argv,
VALUE mod)
2902 const char *pbeg, *p, *path, *pend;
2907 recur = (argc == 1) ?
Qtrue : argv[1];
2910 if (!rb_is_const_sym(name))
goto wrong_name;
2912 if (!
id)
return Qnil;
2913 return RTEST(recur) ? rb_const_source_location(mod,
id) : rb_const_source_location_at(mod, id);
2917 enc = rb_enc_get(name);
2919 if (!rb_enc_asciicompat(enc)) {
2920 rb_raise(rb_eArgError,
"invalid class path encoding (non ASCII)");
2924 pend = path + RSTRING_LEN(name);
2926 if (p >= pend || !*p) {
2930 if (p + 2 < pend && p[0] ==
':' && p[1] ==
':') {
2940 while (p < pend && *p !=
':') p++;
2942 if (pbeg == p)
goto wrong_name;
2947 if (p < pend && p[0] ==
':') {
2948 if (p + 2 >= pend || p[1] !=
':')
goto wrong_name;
2956 if (!rb_is_const_name(part)) {
2976 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not refer to class/module",
2982 loc = rb_const_source_location(mod,
id);
2985 loc = rb_const_source_location_at(mod,
id);
2995 rb_name_err_raise(wrong_constant_name, mod, name);
3024 ID id = id_for_var(obj, iv, instance);
3058 ID id = id_for_var(obj, iv, instance);
3059 if (!
id)
id = rb_intern_str(iv);
3086 ID id = id_for_var(obj, iv, instance);
3113 ID id = id_for_var(obj, iv,
class);
3116 rb_name_err_raise(
"uninitialized class variable %1$s in %2$s",
3145 ID id = id_for_var(obj, iv,
class);
3146 if (!
id)
id = rb_intern_str(iv);
3170 ID id = id_for_var(obj, iv,
class);
3192rb_mod_singleton_p(
VALUE klass)
3194 return RBOOL(RCLASS_SINGLETON_P(klass));
3198static const struct conv_method_tbl {
3199 const char method[6];
3201} conv_method_names[] = {
3202#define M(n) {#n, (unsigned short)idTo_##n}
3217#define IMPLICIT_CONVERSIONS 7
3220conv_method_index(
const char *method)
3222 static const char prefix[] =
"to_";
3224 if (strncmp(prefix, method,
sizeof(prefix)-1) == 0) {
3225 const char *
const meth = &method[
sizeof(prefix)-1];
3227 for (i=0; i < numberof(conv_method_names); i++) {
3228 if (conv_method_names[i].method[0] == meth[0] &&
3229 strcmp(conv_method_names[i].method, meth) == 0) {
3234 return numberof(conv_method_names);
3238convert_type_with_id(
VALUE val,
const char *tname,
ID method,
int raise,
int index)
3243 if ((index < 0 ? conv_method_index(rb_id2name(method)) : index) < IMPLICIT_CONVERSIONS) {
3244 rb_no_implicit_conversion(val, tname);
3247 rb_cant_convert(val, tname);
3256convert_type(
VALUE val,
const char *tname,
const char *method,
int raise)
3258 int i = conv_method_index(method);
3259 ID m = i < numberof(conv_method_names) ?
3260 conv_method_names[i].id : rb_intern(method);
3261 return convert_type_with_id(val, tname, m, raise, i);
3270 v = convert_type(val, tname, method, TRUE);
3272 rb_cant_convert_invalid_return(val, tname, method, v);
3279rb_convert_type_with_id(
VALUE val,
int type,
const char *tname,
ID method)
3284 v = convert_type_with_id(val, tname, method, TRUE, -1);
3286 rb_cant_convert_invalid_return(val, tname, rb_id2name(method), v);
3298 v = convert_type(val, tname, method, FALSE);
3301 rb_cant_convert_invalid_return(val, tname, method, v);
3308rb_check_convert_type_with_id(
VALUE val,
int type,
const char *tname,
ID method)
3314 v = convert_type_with_id(val, tname, method, FALSE, -1);
3317 rb_cant_convert_invalid_return(val, tname, rb_id2name(method), v);
3322#define try_to_int(val, mid, raise) \
3323 convert_type_with_id(val, "Integer", mid, raise, -1)
3325ALWAYS_INLINE(
static VALUE rb_to_integer_with_id_exception(
VALUE val,
const char *method,
ID mid,
int raise));
3328rb_to_integer_with_id_exception(
VALUE val,
const char *method,
ID mid,
int raise)
3335 current_cfp = GET_EC()->cfp;
3336 rb_yjit_lazy_push_frame(GET_EC()->cfp->pc);
3337 v = try_to_int(val, mid, raise);
3338 if (!raise &&
NIL_P(v)) {
3339 GET_EC()->cfp = current_cfp;
3343 rb_cant_convert_invalid_return(val,
"Integer", method, v);
3345 GET_EC()->cfp = current_cfp;
3348#define rb_to_integer(val, method, mid) \
3349 rb_to_integer_with_id_exception(val, method, mid, TRUE)
3357 v = convert_type(val,
"Integer", method, FALSE);
3367 return rb_to_integer(val,
"to_int", idTo_int);
3374 val = try_to_int(val, idTo_int, FALSE);
3380rb_check_to_i(
VALUE val)
3383 val = try_to_int(val, idTo_i, FALSE);
3389rb_convert_to_integer(
VALUE val,
int base,
int raise_exception)
3399 else if (! raise_exception) {
3403 rb_raise(rb_eArgError,
"base specified for non string value");
3408 if (!raise_exception && !isfinite(f))
return Qnil;
3410 return rb_dbl2big(f);
3416 return rb_str_convert_to_inum(val, base, TRUE, raise_exception);
3418 else if (
NIL_P(val)) {
3419 if (!raise_exception)
return Qnil;
3420 rb_cant_convert(val,
"Integer");
3425 rb_set_errinfo(
Qnil);
3427 return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception);
3430 if (!raise_exception) {
3431 VALUE result = rb_protect(rb_check_to_i, val, NULL);
3432 rb_set_errinfo(
Qnil);
3436 return rb_to_integer(val,
"to_i", idTo_i);
3442 return rb_convert_to_integer(val, 0, TRUE);
3446rb_check_integer_type(
VALUE val)
3448 return rb_to_integer_with_id_exception(val,
"to_int", idTo_int, FALSE);
3452rb_bool_expected(
VALUE obj,
const char *flagname,
int raise)
3460 static const char message[] =
"expected true or false as %s: %+"PRIsVALUE;
3462 rb_raise(rb_eArgError, message, flagname, obj);
3471rb_opts_exception_p(
VALUE opts,
int default_value)
3473 static const ID kwds[1] = {idException};
3476 return rb_bool_expected(exception,
"exception", TRUE);
3477 return default_value;
3483 return rb_convert_to_integer(arg, 0, TRUE);
3489 int exc = rb_bool_expected(exception,
"exception", TRUE);
3490 return rb_convert_to_integer(arg,
NUM2INT(base), exc);
3494is_digit_char(
unsigned char c,
int base)
3497 return (i >= 0 && i < base);
3501rb_cstr_to_dbl_raise(
const char *p,
rb_encoding *enc,
int badcheck,
int raise,
int *error)
3506 const char *ellipsis =
"";
3508 enum {max_width = 20};
3509#define OutOfRange() ((end - p > max_width) ? \
3510 (w = max_width, ellipsis = "...") : \
3511 (w = (int)(end - p), ellipsis = ""))
3518 if (!badcheck && p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X')) {
3523 if (
errno == ERANGE) {
3525 rb_warning(
"Float %.*s%s out of range", w, p, ellipsis);
3535 char buf[DBL_DIG * 4 + 10];
3537 char *
const init_e = buf + DBL_DIG * 4;
3540 int dot_seen = FALSE;
3542 char exp_letter =
'e';
3544 switch (*p) {
case '+':
case '-': prev = *n++ = *p++;}
3552 if (*++p !=
'0')
break;
3555 while (*++p ==
'0');
3559 while (p < end && n < e) prev = *n++ = *p++;
3564 !is_digit_char(prev, base) ||
3565 !is_digit_char(*++p, base)) {
3566 if (badcheck)
goto bad;
3571 if (e == init_e && (
rb_tolower(prev) == exp_letter)) {
3572 e = buf +
sizeof(buf) - 1;
3574 switch (*p) {
case '+':
case '-': prev = *n++ = *p++;}
3577 while (*++p ==
'0');
3588 if (badcheck)
goto bad;
3592 else if (prev ==
'.' ? dot_seen++ : !is_digit_char(prev, base)) {
3593 if (badcheck)
goto bad;
3596 if (n < e) *n++ = prev;
3601 if (!badcheck && p[0] ==
'0' && (p[1] ==
'x' || p[1] ==
'X')) {
3606 if (
errno == ERANGE) {
3608 rb_warning(
"Float %.*s%s out of range", w, p, ellipsis);
3612 if (!end || p == end)
goto bad;
3613 while (*end &&
ISSPACE(*end)) end++;
3617 if (
errno == ERANGE) {
3620 rb_raise(rb_eArgError,
"Float %.*s%s out of range", w, q, ellipsis);
3627 rb_raise(rb_eArgError,
"invalid value for Float(): %+"PRIsVALUE, s);
3631 if (error) *error = 1;
3639 return rb_cstr_to_dbl_raise(p, NULL, badcheck, TRUE, NULL);
3643rb_str_to_dbl_raise(
VALUE str,
int badcheck,
int raise,
int *error)
3652 s = RSTRING_PTR(str);
3653 len = RSTRING_LEN(str);
3655 if (badcheck && memchr(s,
'\0',
len)) {
3657 rb_raise(rb_eArgError,
"string for Float contains null byte");
3659 if (error) *error = 1;
3670 ret = rb_cstr_to_dbl_raise(s, rb_enc_get(str), badcheck, raise, error);
3683 return rb_str_to_dbl_raise(str, badcheck, TRUE, NULL);
3687#define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
3688#define big2dbl_without_to_f(x) rb_big2dbl(x)
3689#define int2dbl_without_to_f(x) \
3690 (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
3691#define num2dbl_without_to_f(x) \
3692 (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : \
3693 RB_BIGNUM_TYPE_P(x) ? big2dbl_without_to_f(x) : \
3694 (Check_Type(x, T_FLOAT), RFLOAT_VALUE(x)))
3696rat2dbl_without_to_f(
VALUE x)
3700 return num2dbl_without_to_f(num) / num2dbl_without_to_f(den);
3703#define special_const_to_float(val, pre, post) \
3706 rb_raise_static(rb_eTypeError, pre "nil" post); \
3708 rb_raise_static(rb_eTypeError, pre "true" post); \
3710 rb_raise_static(rb_eTypeError, pre "false" post); \
3715to_float(
VALUE *valp,
int raise_exception)
3720 *valp =
DBL2NUM(fix2dbl_without_to_f(val));
3726 else if (raise_exception) {
3727 rb_cant_convert(val,
"Float");
3736 *valp =
DBL2NUM(big2dbl_without_to_f(val));
3739 *valp =
DBL2NUM(rat2dbl_without_to_f(val));
3749convert_type_to_float_protected(
VALUE val)
3751 return rb_convert_type_with_id(val,
T_FLOAT,
"Float", id_to_f);
3755rb_convert_to_float(
VALUE val,
int raise_exception)
3757 switch (to_float(&val, raise_exception)) {
3761 if (!raise_exception) {
3763 double x = rb_str_to_dbl_raise(val, TRUE, raise_exception, &e);
3772 if (!raise_exception) {
3774 VALUE result = rb_protect(convert_type_to_float_protected, val, &state);
3775 if (state) rb_set_errinfo(
Qnil);
3779 return rb_convert_type_with_id(val,
T_FLOAT,
"Float", id_to_f);
3787 return rb_convert_to_float(val, TRUE);
3793 return rb_convert_to_float(arg, TRUE);
3799 int exception = rb_bool_expected(opts,
"exception", TRUE);
3800 return rb_convert_to_float(arg, exception);
3804numeric_to_float(
VALUE val)
3807 rb_cant_convert(val,
"Float");
3809 return rb_convert_type_with_id(val,
T_FLOAT,
"Float", id_to_f);
3815 switch (to_float(&val, TRUE)) {
3819 return numeric_to_float(val);
3829 return rb_check_convert_type_with_id(val,
T_FLOAT,
"Float", id_to_f);
3833basic_to_f_p(
VALUE klass)
3835 return rb_method_basic_definition_p(klass, id_to_f);
3840rb_num_to_dbl(
VALUE val)
3845 return fix2dbl_without_to_f(val);
3848 return rb_float_flonum_value(val);
3851 rb_cant_convert(val,
"Float");
3857 return rb_float_noflonum_value(val);
3860 return big2dbl_without_to_f(val);
3864 return rat2dbl_without_to_f(val);
3870 val = numeric_to_float(val);
3879 return fix2dbl_without_to_f(val);
3882 return rb_float_flonum_value(val);
3885 rb_no_implicit_conversion(val,
"Float");
3891 return rb_float_noflonum_value(val);
3893 return big2dbl_without_to_f(val);
3895 return rat2dbl_without_to_f(val);
3897 rb_no_implicit_conversion(val,
"Float");
3902 val = rb_convert_type_with_id(val,
T_FLOAT,
"Float", id_to_f);
3911 tmp = rb_convert_type_with_id(val,
T_STRING,
"String", idTo_s);
3944 tmp = rb_check_to_array(val);
3986 if (
NIL_P(val))
return rb_hash_new();
3987 tmp = rb_check_hash_type(val);
3990 return rb_hash_new();
3991 rb_cant_convert(val,
"Hash");
4033dig_basic_p(
VALUE obj,
struct dig_method *cache)
4036 if (klass != cache->klass) {
4037 cache->klass = klass;
4038 cache->basic = rb_method_basic_definition_p(klass, id_dig);
4040 return cache->basic;
4044no_dig_method(
int found,
VALUE recv,
ID mid,
int argc,
const VALUE *argv,
VALUE data)
4047 rb_raise(
rb_eTypeError,
"%"PRIsVALUE
" does not have #dig method",
4056 struct dig_method hash = {
Qnil}, ary = {
Qnil}, strt = {
Qnil};
4058 for (; argc > 0; ++argv, --argc) {
4059 if (
NIL_P(obj))
return notfound;
4063 if (dig_basic_p(obj, &hash)) {
4064 obj = rb_hash_aref(obj, *argv);
4069 if (dig_basic_p(obj, &ary)) {
4070 obj = rb_ary_at(obj, *argv);
4075 if (dig_basic_p(obj, &strt)) {
4076 obj = rb_struct_lookup(obj, *argv);
4084 return rb_check_funcall_with_hook_kw(obj, id_dig, argc, argv,
4347 Init_class_hierarchy();
4565 rb_cNilClass_to_s = rb_fstring_enc_lit(
"", rb_usascii_encoding());
4566 rb_vm_register_global_object(rb_cNilClass_to_s);
4614 rb_class_undefined_instance_methods, 0);
4624 rb_mod_const_missing, 1);
4650 rb_cTrueClass_to_s = rb_fstring_enc_lit(
"true", rb_usascii_encoding());
4651 rb_vm_register_global_object(rb_cTrueClass_to_s);
4662 rb_cFalseClass_to_s = rb_fstring_enc_lit(
"false", rb_usascii_encoding());
4663 rb_vm_register_global_object(rb_cFalseClass_to_s);
4674#include "kernel.rbinc"
4675#include "nilclass.rbinc"
4681 id_instance_variables_to_inspect =
rb_intern_const(
"instance_variables_to_inspect");
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
static int rb_tolower(int c)
Our own locale-insensitive version of tolower(3).
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
@ RUBY_FL_PROMOTED
Ruby objects are "generational".
VALUE rb_class_protected_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are protected only.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_class_subclasses(VALUE klass)
Queries the class's direct descendants.
VALUE rb_singleton_class(VALUE obj)
Finds or creates the singleton class of the passed object.
VALUE rb_class_attached_object(VALUE klass)
Returns the attached object for a singleton class.
VALUE rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
Identical to rb_class_instance_methods(), except it returns names of singleton methods instead of ins...
VALUE rb_class_instance_methods(int argc, const VALUE *argv, VALUE mod)
Generates an array of symbols, which are the list of method names defined in the passed class.
void rb_check_inheritable(VALUE super)
Asserts that the given class can derive a child class.
VALUE rb_class_public_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are public only.
VALUE rb_define_module(const char *name)
Defines a top-level module.
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
VALUE rb_mod_included_modules(VALUE mod)
Queries the list of included modules.
VALUE rb_mod_ancestors(VALUE mod)
Queries the module's ancestors.
VALUE rb_class_inherited(VALUE super, VALUE klass)
Calls Class::inherited.
VALUE rb_mod_include_p(VALUE mod, VALUE mod2)
Queries if the passed module is included by the module.
VALUE rb_class_private_instance_methods(int argc, const VALUE *argv, VALUE mod)
Identical to rb_class_instance_methods(), except it returns names of methods that are private only.
VALUE rb_mod_init_copy(VALUE clone, VALUE orig)
The comment that comes with this function says :nodoc:.
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
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_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define ALLOCV
Old name of RB_ALLOCV.
#define ISSPACE
Old name of rb_isspace.
#define RFLOAT_VALUE
Old name of rb_float_value.
#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 OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define T_DATA
Old name of RUBY_T_DATA.
#define CLASS_OF
Old name of rb_class_of.
#define T_NONE
Old name of RUBY_T_NONE.
#define FIXABLE
Old name of RB_FIXABLE.
#define LONG2FIX
Old name of RB_INT2FIX.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define rb_ary_new3
Old name of rb_ary_new_from_args.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#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 FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define DBL2NUM
Old name of rb_float_new.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
void rb_category_warning(rb_warning_category_t category, const char *fmt,...)
Identical to rb_warning(), except it takes additional "category" parameter.
VALUE rb_eTypeError
TypeError exception.
void rb_warning(const char *fmt,...)
Issues a warning.
@ RB_WARN_CATEGORY_DEPRECATED
Warning is for deprecated features.
VALUE rb_cClass
Class class.
VALUE rb_cRational
Rational class.
VALUE rb_class_superclass(VALUE klass)
Returns the superclass of klass.
VALUE rb_class_get_superclass(VALUE klass)
Returns the superclass of a class.
VALUE rb_convert_type(VALUE val, int type, const char *tname, const char *method)
Converts an object into another type.
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
VALUE rb_mKernel
Kernel module.
VALUE rb_check_to_int(VALUE val)
Identical to rb_check_to_integer(), except it uses #to_int for conversion.
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
VALUE rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
Identical to rb_convert_type(), except it returns RUBY_Qnil instead of raising exceptions,...
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_class_new_instance_kw(int argc, const VALUE *argv, VALUE klass, int kw_splat)
Identical to rb_class_new_instance(), except you can specify how to handle the last element of the gi...
VALUE rb_cRefinement
Refinement class.
VALUE rb_cInteger
Module class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_class_new_instance_pass_kw(int argc, const VALUE *argv, VALUE klass)
Identical to rb_class_new_instance(), except it passes the passed keywords if any to the #initialize ...
VALUE rb_check_to_float(VALUE val)
This is complicated.
static VALUE rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
Default implementation of #initialize_clone
VALUE rb_cNilClass
NilClass class.
VALUE rb_Hash(VALUE val)
Equivalent to Kernel#Hash in Ruby.
VALUE rb_obj_frozen_p(VALUE obj)
Just calls RB_OBJ_FROZEN() inside.
VALUE rb_obj_init_copy(VALUE obj, VALUE orig)
Default implementation of #initialize_copy
int rb_eql(VALUE obj1, VALUE obj2)
Checks for equality of the passed objects, in terms of Object#eql?.
double rb_str_to_dbl(VALUE str, int badcheck)
Identical to rb_cstr_to_dbl(), except it accepts a Ruby's string instead of C's.
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
VALUE rb_cFalseClass
FalseClass class.
VALUE rb_cNumeric
Numeric class.
VALUE rb_Array(VALUE val)
This is the logic behind Kernel#Array.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_obj_dup(VALUE obj)
Duplicates the given object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_cModule
Module class.
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
Determines if the given two modules are relatives.
VALUE rb_obj_is_instance_of(VALUE obj, VALUE c)
Queries if the given object is a direct instance of the given class.
VALUE rb_class_real(VALUE cl)
Finds a "real" class.
VALUE rb_obj_init_dup_clone(VALUE obj, VALUE orig)
Default implementation of #initialize_dup
VALUE rb_to_float(VALUE val)
Identical to rb_check_to_float(), except it raises on error.
double rb_num2dbl(VALUE val)
Converts an instance of rb_cNumeric into C's double.
VALUE rb_equal(VALUE obj1, VALUE obj2)
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 c)
Queries if the given object is an instance (of possibly descendants) of the given class.
double rb_cstr_to_dbl(const char *p, int badcheck)
Converts a textual representation of a real number into a numeric, which is the nearest value that th...
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_check_to_integer(VALUE val, const char *method)
Identical to rb_check_convert_type(), except the return value type is fixed to rb_cInteger.
VALUE rb_String(VALUE val)
This is the logic behind Kernel#String.
VALUE rb_cTrueClass
TrueClass class.
size_t rb_obj_embedded_size(uint32_t fields_count)
Internal header for Object.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
VALUE rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
Fills common fields in the object.
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.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id(), except it takes a pointer to a memory region instead of Ruby's 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.
#define RGENGC_WB_PROTECTED_OBJECT
This is a compile-time flag to enable/disable write barrier for struct RObject.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
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.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
int rb_is_instance_id(ID id)
Classifies the given ID, then sees if it is an instance variable.
int rb_is_const_id(ID id)
Classifies the given ID, then sees if it is a constant.
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
VALUE rb_rational_num(VALUE rat)
Queries the numerator of the passed Rational.
VALUE rb_rational_den(VALUE rat)
Queries the denominator of the passed Rational.
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_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
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.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
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_mod_remove_cvar(VALUE mod, VALUE name)
Resembles Module#remove_class_variable.
VALUE rb_obj_instance_variables(VALUE obj)
Resembles Object#instance_variables.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
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_mod_remove_const(VALUE space, VALUE name)
Resembles Module#remove_const.
void rb_cvar_set(VALUE klass, ID name, VALUE val)
Assigns a value to a class variable.
VALUE rb_cvar_get(VALUE klass, ID name)
Obtains a value from a class variable.
VALUE rb_mod_constants(int argc, const VALUE *argv, VALUE recv)
Resembles Module#constants.
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
void rb_const_set(VALUE space, ID name, VALUE val)
Names a constant.
VALUE rb_mod_name(VALUE mod)
Queries the name of a module.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
VALUE rb_obj_remove_instance_variable(VALUE obj, VALUE name)
Resembles Object#remove_instance_variable.
st_index_t rb_ivar_count(VALUE obj)
Number of instance variables defined on an object.
VALUE rb_const_get_from(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
VALUE rb_ivar_defined(VALUE obj, ID name)
Queries if the instance variable is defined at the object.
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
VALUE rb_mod_class_variables(int argc, const VALUE *argv, VALUE recv)
Resembles Module#class_variables.
VALUE rb_cvar_defined(VALUE klass, ID name)
Queries if the given class has the given class variable.
int rb_const_defined_from(VALUE space, ID name)
Identical to rb_const_defined(), except it returns false for private constants.
int rb_const_defined(VALUE space, ID name)
Queries if the constant is defined at the namespace.
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
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.
rb_alloc_func_t rb_get_alloc_func(VALUE klass)
Queries the allocator function of a class.
VALUE rb_mod_module_exec(int argc, const VALUE *argv, VALUE mod)
Identical to rb_obj_instance_exec(), except it evaluates within the context of module.
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.
const signed char ruby_digit36_to_number_table[]
Character to number mapping like ‘'a’->10,'b'->11etc.
#define strtod(s, e)
Just another name of ruby_strtod.
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
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.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RBASIC(obj)
Convenient casting macro.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
const char * rb_class2name(VALUE klass)
Queries the name of the passed class.
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 InitVM(ext)
This macro is for internal use.
#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.
#define RB_NO_KEYWORDS
Do not pass keywords.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
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.