1 #ifndef INTERNAL_CLASS_H
2 #define INTERNAL_CLASS_H
13 #include "internal/serial.h"
14 #include "internal/static_assert.h"
15 #include "internal/variable.h"
20 #include "ruby_assert.h"
38 rb_serial_t global_cvar_state;
49 size_t superclass_depth;
60 const VALUE refined_class;
66 VALUE attached_object;
70 attr_index_t max_iv_count;
71 unsigned char variation_count;
72 bool permanent_classpath : 1;
78 STATIC_ASSERT(shape_max_variations, SHAPE_MAX_VARIATIONS < (1 << (
sizeof(((
rb_classext_t *)0)->variation_count) * CHAR_BIT)));
87 STATIC_ASSERT(sizeof_rb_classext_t,
sizeof(
struct RClass) +
sizeof(
rb_classext_t) <= 4 * RVALUE_SIZE);
94 #define RCLASS_EXT(c) (&((struct RClass_and_rb_classext_t*)(c))->classext)
95 #define RCLASS_CONST_TBL(c) (RCLASS_EXT(c)->const_tbl)
96 #define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
97 #define RCLASS_IVPTR(c) (RCLASS_EXT(c)->iv_ptr)
98 #define RCLASS_CALLABLE_M_TBL(c) (RCLASS_EXT(c)->callable_m_tbl)
99 #define RCLASS_CC_TBL(c) (RCLASS_EXT(c)->cc_tbl)
100 #define RCLASS_CVC_TBL(c) (RCLASS_EXT(c)->cvc_tbl)
101 #define RCLASS_ORIGIN(c) (RCLASS_EXT(c)->origin_)
102 #define RCLASS_REFINED_CLASS(c) (RCLASS_EXT(c)->refined_class)
103 #define RCLASS_INCLUDER(c) (RCLASS_EXT(c)->includer)
104 #define RCLASS_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->subclass_entry)
105 #define RCLASS_MODULE_SUBCLASS_ENTRY(c) (RCLASS_EXT(c)->module_subclass_entry)
106 #define RCLASS_SUBCLASSES(c) (RCLASS_EXT(c)->subclasses)
107 #define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT(c)->superclass_depth)
108 #define RCLASS_SUPERCLASSES(c) (RCLASS_EXT(c)->superclasses)
109 #define RCLASS_ATTACHED_OBJECT(c) (RCLASS_EXT(c)->as.singleton_class.attached_object)
111 #define RCLASS_IS_ROOT FL_USER0
112 #define RICLASS_IS_ORIGIN FL_USER0
113 #define RCLASS_SUPERCLASSES_INCLUDE_SELF FL_USER2
114 #define RICLASS_ORIGIN_SHARED_MTBL FL_USER3
117 RCLASS_IV_HASH(
VALUE obj)
121 return (
st_table *)RCLASS_IVPTR(obj);
129 RCLASS_IVPTR(obj) = (
VALUE *)tbl;
132 static inline uint32_t
133 RCLASS_IV_COUNT(
VALUE obj)
136 if (rb_shape_obj_too_complex(obj)) {
143 count = (uint32_t)rb_st_table_size(RCLASS_IV_HASH(obj));
150 return rb_shape_get_shape_by_id(RCLASS_SHAPE_ID(obj))->next_iv_index;
158 RCLASS_M_TBL(klass) = table;
162 void rb_class_subclass_add(
VALUE super,
VALUE klass);
163 void rb_class_remove_from_super_subclasses(
VALUE);
164 void rb_class_update_superclasses(
VALUE);
165 size_t rb_class_superclasses_memsize(
VALUE);
166 void rb_class_remove_subclass_head(
VALUE);
167 int rb_singleton_class_internal_p(
VALUE sklass);
171 void rb_module_set_initialized(
VALUE module);
172 void rb_module_check_initializable(
VALUE module);
176 void rb_class_detach_subclasses(
VALUE);
177 void rb_class_detach_module_subclasses(
VALUE);
178 void rb_class_remove_from_module_subclasses(
VALUE);
181 VALUE rb_obj_protected_methods(
int argc,
const VALUE *argv,
VALUE obj);
184 VALUE rb_class_undefined_instance_methods(
VALUE mod);
188 void rb_undef_methods_from(
VALUE klass,
VALUE super);
190 static inline void RCLASS_SET_ORIGIN(
VALUE klass,
VALUE origin);
191 static inline void RICLASS_SET_ORIGIN_SHARED_MTBL(
VALUE iclass);
194 static inline void RCLASS_SET_INCLUDER(
VALUE iclass,
VALUE klass);
197 VALUE rb_keyword_error_new(
const char *,
VALUE);
200 RCLASS_SINGLETON_P(
VALUE klass)
206 RCLASS_ALLOCATOR(
VALUE klass)
208 if (RCLASS_SINGLETON_P(klass)) {
211 return RCLASS_EXT(klass)->as.class.allocator;
217 assert(!RCLASS_SINGLETON_P(klass));
218 RCLASS_EXT(klass)->as.class.allocator = allocator;
225 if (klass != origin)
FL_SET(origin, RICLASS_IS_ORIGIN);
229 RICLASS_SET_ORIGIN_SHARED_MTBL(
VALUE iclass)
231 FL_SET(iclass, RICLASS_ORIGIN_SHARED_MTBL);
235 RICLASS_OWNS_M_TBL_P(
VALUE iclass)
237 return FL_TEST_RAW(iclass, RICLASS_IS_ORIGIN | RICLASS_ORIGIN_SHARED_MTBL) == RICLASS_IS_ORIGIN;
241 RCLASS_SET_INCLUDER(
VALUE iclass,
VALUE klass)
249 return RCLASS(klass)->super;
256 rb_class_remove_from_super_subclasses(klass);
257 rb_class_subclass_add(super, klass);
260 rb_class_update_superclasses(klass);
265 RCLASS_SET_CLASSPATH(
VALUE klass,
VALUE classpath,
bool permanent)
270 RB_OBJ_WRITE(klass, &(RCLASS_EXT(klass)->classpath), classpath);
271 RCLASS_EXT(klass)->permanent_classpath = permanent;
275 RCLASS_SET_ATTACHED_OBJECT(
VALUE klass,
VALUE attached_object)
277 assert(RCLASS_SINGLETON_P(klass));
279 RB_OBJ_WRITE(klass, &RCLASS_EXT(klass)->as.singleton_class.attached_object, attached_object);
280 return attached_object;
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_class_boot(VALUE)
A utility function that wraps class_alloc.
VALUE rb_class_inherited(VALUE, VALUE)
Calls Class::inherited.
VALUE rb_singleton_class_get(VALUE obj)
Returns the singleton class of obj, or nil if obj is not a singleton object.
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
#define T_STRING
Old name of RUBY_T_STRING.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_SET
Old name of RB_FL_SET.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
static bool RB_OBJ_PROMOTED(VALUE obj)
Tests if the object is "promoted" – that is, whether the object experienced one or more GC marks.
VALUE(* rb_alloc_func_t)(VALUE klass)
This is the type of functions that ruby calls when trying to allocate an object.
#define RCLASS_SUPER
Just another name of rb_class_get_superclass.
#define RCLASS(obj)
Convenient casting macro.
Ruby object's base components.
struct rb_subclass_entry * module_subclass_entry
In the case that this is an ICLASS, module_subclasses points to the link in the module's subclasses l...
Internal header for Class.
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_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.