Ruby 4.1.0dev (2026-11-17 revision 373d8c72a360f0d6c3be9cd13d1f0c27360e5d90)
object.h
1#ifndef INTERNAL_OBJECT_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_OBJECT_H
11#include "internal/basic_operators.h"
12#include "internal/string.h"
13#include "internal/vm.h"
14#include "ruby/ruby.h" /* for VALUE */
15
16/* object.c */
17
18VALUE rb_class_allocate_instance(VALUE klass);
19VALUE rb_class_allocate_instance_capa(VALUE klass, uint8_t /* attr_index_t */ max_iv_count);
20VALUE rb_class_search_ancestor(VALUE klass, VALUE super);
21NORETURN(void rb_undefined_alloc(VALUE klass));
22double rb_num_to_dbl(VALUE val);
23VALUE rb_obj_dig(int argc, VALUE *argv, VALUE self, VALUE notfound);
24VALUE rb_obj_clone_setup(VALUE obj, VALUE clone, VALUE kwfreeze);
25VALUE rb_obj_dup_setup(VALUE obj, VALUE dup);
26VALUE rb_immutable_obj_clone(int, VALUE *, VALUE);
27VALUE rb_check_convert_type_with_id_slow(VALUE,int,const char*,ID);
28int rb_bool_expected(VALUE, const char *, int raise);
29
30static inline VALUE
31rb_check_convert_type_with_id(VALUE val, int type, const char *tname, ID method)
32{
33 if (RB_TYPE_P(val, type) && type != T_DATA) return val;
34 return rb_check_convert_type_with_id_slow(val, type, tname, method);
35}
36static inline void RBASIC_CLEAR_CLASS(VALUE obj);
37static inline void RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass);
38static inline void RBASIC_SET_CLASS(VALUE obj, VALUE klass);
39
40RUBY_SYMBOL_EXPORT_BEGIN
41/* object.c (export) */
42int rb_opts_exception_p(VALUE opts, int default_value);
43RUBY_SYMBOL_EXPORT_END
44
45CONSTFUNC(VALUE rb_obj_equal(VALUE obj1, VALUE obj2));
46CONSTFUNC(VALUE rb_obj_not(VALUE obj));
47VALUE rb_obj_not_equal(VALUE obj1, VALUE obj2);
48void rb_obj_copy_ivar(VALUE dest, VALUE obj);
49VALUE rb_false(VALUE obj);
50VALUE rb_convert_type_with_id(VALUE v, int t, const char* nam, ID mid);
51VALUE rb_obj_size(VALUE self, VALUE args, VALUE obj);
52VALUE rb_get_freeze_opt(int argc, VALUE *argv);
53
54static inline void
55RBASIC_SET_CLASS_RAW(VALUE obj, VALUE klass)
56{
57 const VALUE *ptr = &RBASIC(obj)->klass;
58 *(VALUE *)ptr = klass;
59}
60
61static inline void
62RBASIC_CLEAR_CLASS(VALUE obj)
63{
64 RBASIC_SET_CLASS_RAW(obj, 0);
65}
66
67static inline void
68RBASIC_SET_CLASS(VALUE obj, VALUE klass)
69{
70 VALUE oldv = RBASIC_CLASS(obj);
71 RBASIC_SET_CLASS_RAW(obj, klass);
72 RB_OBJ_WRITTEN(obj, oldv, klass);
73}
74
75static inline void
76ROBJECT_SET_EXTENDED(VALUE obj, VALUE fields_obj)
77{
79 RUBY_ASSERT(RB_TYPE_P(fields_obj, T_IMEMO));
80 RB_OBJ_WRITE(obj, &ROBJECT(obj)->as.extended, fields_obj);
81}
82
83static inline size_t
84rb_obj_embedded_size(uint32_t fields_count)
85{
86 size_t size = offsetof(struct RObject, as.ary) + (sizeof(VALUE) * fields_count);
87 // Ensure enough room for the heap pointer if this expands
88 if (size < sizeof(struct RObject)) size = sizeof(struct RObject);
89 return size;
90}
91
92static inline VALUE
93opt_equality_specialized_miss(VALUE recv, VALUE obj)
94{
95 RBIMPL_ASSERT_OR_ASSUME(recv != obj);
96 if (BASIC_OP_UNREDEFINED_P(BOP_EQ, ANY_REDEFINED_OP_FLAG)) {
97 if (recv == Qfalse) return Qfalse;
98 if (IMMEDIATE_P(recv)) {
99 // Numeric immediates can be equal to different VALUE via coercion.
100 if (FIXNUM_P(recv)) {
101 if (!SPECIAL_CONST_P(obj) || FLONUM_P(obj)) return Qundef;
102 }
103 else if (FLONUM_P(recv)) {
104 if (!SPECIAL_CONST_P(obj) || FIXNUM_P(obj)) return Qundef;
105
106 }
107 return Qfalse;
108 }
109
110 VALUE klass = RBASIC_CLASS(recv);
111 if (klass == rb_cString) {
112 if (RB_TYPE_P(obj, T_STRING)) {
113 return rb_str_eql_internal(recv, obj);
114 }
115 }
116 else if (klass == rb_cSymbol) {
117 return Qfalse;
118 }
119 }
120 return Qundef;
121}
122
123static inline VALUE
124rb_equal_inline(VALUE obj1, VALUE obj2)
125{
126 if (obj1 == obj2) return Qtrue;
127
128 VALUE result = opt_equality_specialized_miss(obj1, obj2);
129 if (UNDEF_P(result)) {
130 result = rb_equal_opt(obj1, obj2);
131 }
132 return result;
133}
134
135#define rb_equal rb_equal_inline
136
137static inline int
138rb_eql_inline(VALUE obj1, VALUE obj2)
139{
140 if (obj1 == obj2) return TRUE;
141
142 VALUE result = opt_equality_specialized_miss(obj1, obj2);
143 if (UNDEF_P(result)) {
144 result = rb_eql_opt(obj1, obj2);
145 }
146 return RTEST(result);
147}
148
149#define rb_eql rb_eql_inline
150
151#endif /* INTERNAL_OBJECT_H */
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define FLONUM_P
Old name of RB_FLONUM_P.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define IMMEDIATE_P
Old name of RB_IMMEDIATE_P.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
VALUE rb_cSymbol
Symbol class.
Definition string.c:86
VALUE rb_cString
String class.
Definition string.c:85
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:504
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
Definition assert.h:311
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
VALUE type(ANYARGS)
ANYARGS-ed function type.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define ROBJECT(obj)
Convenient casting macro.
Definition robject.h:43
#define RTEST
This is an old name of RB_TEST.
Ruby's ordinal objects.
Definition robject.h:51
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376