@@ -1255,22 +1255,25 @@ rb_mark_generic_ivar(VALUE obj)
12551255void
12561256rb_free_generic_ivar (VALUE obj )
12571257{
1258- st_data_t key = (st_data_t )obj , value ;
1258+ if (rb_obj_exivar_p (obj )) {
1259+ st_data_t key = (st_data_t )obj , value ;
12591260
1260- bool too_complex = rb_shape_obj_too_complex_p (obj );
1261+ bool too_complex = rb_shape_obj_too_complex_p (obj );
12611262
1262- RB_VM_LOCKING () {
1263- if (st_delete (generic_fields_tbl_no_ractor_check (obj ), & key , & value )) {
1264- struct gen_fields_tbl * fields_tbl = (struct gen_fields_tbl * )value ;
1263+ RB_VM_LOCKING () {
1264+ if (st_delete (generic_fields_tbl_no_ractor_check (obj ), & key , & value )) {
1265+ struct gen_fields_tbl * fields_tbl = (struct gen_fields_tbl * )value ;
12651266
1266- if (UNLIKELY (too_complex )) {
1267- st_free_table (fields_tbl -> as .complex .table );
1268- }
1267+ if (UNLIKELY (too_complex )) {
1268+ st_free_table (fields_tbl -> as .complex .table );
1269+ }
12691270
1270- xfree (fields_tbl );
1271+ xfree (fields_tbl );
1272+ }
12711273 }
1274+ FL_UNSET_RAW (obj , FL_EXIVAR );
1275+ RBASIC_SET_SHAPE_ID (obj , ROOT_SHAPE_ID );
12721276 }
1273- FL_UNSET_RAW (obj , FL_EXIVAR );
12741277}
12751278
12761279size_t
@@ -1327,6 +1330,7 @@ rb_obj_field_get(VALUE obj, shape_id_t target_shape_id)
13271330 break ;
13281331 default :
13291332 RUBY_ASSERT (FL_TEST_RAW (obj , FL_EXIVAR ));
1333+ RUBY_ASSERT (rb_obj_exivar_p (obj ));
13301334 struct gen_fields_tbl * fields_tbl = NULL ;
13311335 rb_ivar_generic_fields_tbl_lookup (obj , & fields_tbl );
13321336 RUBY_ASSERT (fields_tbl );
@@ -1358,6 +1362,7 @@ rb_obj_field_get(VALUE obj, shape_id_t target_shape_id)
13581362 break ;
13591363 default :
13601364 RUBY_ASSERT (FL_TEST_RAW (obj , FL_EXIVAR ));
1365+ RUBY_ASSERT (rb_obj_exivar_p (obj ));
13611366 struct gen_fields_tbl * fields_tbl = NULL ;
13621367 rb_ivar_generic_fields_tbl_lookup (obj , & fields_tbl );
13631368 RUBY_ASSERT (fields_tbl );
@@ -1435,7 +1440,7 @@ rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
14351440 }
14361441 default :
14371442 shape_id = RBASIC_SHAPE_ID (obj );
1438- if (FL_TEST_RAW (obj , FL_EXIVAR )) {
1443+ if (rb_obj_exivar_p (obj )) {
14391444 struct gen_fields_tbl * fields_tbl ;
14401445 rb_gen_fields_tbl_get (obj , id , & fields_tbl );
14411446
@@ -1551,22 +1556,20 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
15511556 size_t trailing_fields = new_fields_count - removed_index ;
15521557
15531558 MEMMOVE (& fields [removed_index ], & fields [removed_index + 1 ], VALUE , trailing_fields );
1554-
1555- if (RB_TYPE_P (obj , T_OBJECT ) &&
1556- !RB_FL_TEST_RAW (obj , ROBJECT_EMBED ) &&
1557- rb_obj_embedded_size (new_fields_count ) <= rb_gc_obj_slot_size (obj )) {
1558- // Re-embed objects when instances become small enough
1559- // This is necessary because YJIT assumes that objects with the same shape
1560- // have the same embeddedness for efficiency (avoid extra checks)
1561- RB_FL_SET_RAW (obj , ROBJECT_EMBED );
1562- MEMCPY (ROBJECT_FIELDS (obj ), fields , VALUE , new_fields_count );
1563- xfree (fields );
1564- }
15651559 }
15661560 else {
1567- if (FL_TEST_RAW (obj , FL_EXIVAR )) {
1568- rb_free_generic_ivar (obj );
1569- }
1561+ rb_free_generic_ivar (obj );
1562+ }
1563+
1564+ if (RB_TYPE_P (obj , T_OBJECT ) &&
1565+ !RB_FL_TEST_RAW (obj , ROBJECT_EMBED ) &&
1566+ rb_obj_embedded_size (new_fields_count ) <= rb_gc_obj_slot_size (obj )) {
1567+ // Re-embed objects when instances become small enough
1568+ // This is necessary because YJIT assumes that objects with the same shape
1569+ // have the same embeddedness for efficiency (avoid extra checks)
1570+ RB_FL_SET_RAW (obj , ROBJECT_EMBED );
1571+ MEMCPY (ROBJECT_FIELDS (obj ), fields , VALUE , new_fields_count );
1572+ xfree (fields );
15701573 }
15711574 rb_obj_set_shape_id (obj , next_shape_id );
15721575
@@ -1844,13 +1847,14 @@ generic_fields_lookup_ensure_size(st_data_t *k, st_data_t *v, st_data_t u, int e
18441847 * v = (st_data_t )fields_tbl ;
18451848 }
18461849
1847- RUBY_ASSERT (FL_TEST ((VALUE )* k , FL_EXIVAR ));
1848-
18491850 fields_lookup -> fields_tbl = fields_tbl ;
18501851 if (fields_lookup -> shape_id ) {
18511852 rb_obj_set_shape_id (fields_lookup -> obj , fields_lookup -> shape_id );
18521853 }
18531854
1855+ RUBY_ASSERT (FL_TEST ((VALUE )* k , FL_EXIVAR ));
1856+ RUBY_ASSERT (rb_obj_exivar_p ((VALUE )* k ));
1857+
18541858 return ST_CONTINUE ;
18551859}
18561860
@@ -2349,8 +2353,8 @@ rb_copy_generic_ivar(VALUE dest, VALUE obj)
23492353
23502354 rb_check_frozen (dest );
23512355
2352- if (!FL_TEST (obj , FL_EXIVAR )) {
2353- goto clear ;
2356+ if (!rb_obj_exivar_p (obj )) {
2357+ return ;
23542358 }
23552359
23562360 unsigned long src_num_ivs = rb_ivar_count (obj );
@@ -2414,11 +2418,7 @@ rb_copy_generic_ivar(VALUE dest, VALUE obj)
24142418 return ;
24152419
24162420 clear :
2417- if (FL_TEST (dest , FL_EXIVAR )) {
2418- rb_free_generic_ivar (dest );
2419- FL_UNSET (dest , FL_EXIVAR );
2420- RBASIC_SET_SHAPE_ID (dest , ROOT_SHAPE_ID );
2421- }
2421+ rb_free_generic_ivar (dest );
24222422}
24232423
24242424void
@@ -2464,7 +2464,7 @@ rb_field_foreach(VALUE obj, rb_ivar_foreach_callback_func *func, st_data_t arg,
24642464 }
24652465 break ;
24662466 default :
2467- if (FL_TEST_RAW (obj , FL_EXIVAR )) {
2467+ if (rb_obj_exivar_p (obj )) {
24682468 gen_fields_each (obj , func , arg , ivar_only );
24692469 }
24702470 break ;
@@ -2500,7 +2500,7 @@ rb_ivar_count(VALUE obj)
25002500 return RBASIC_FIELDS_COUNT (fields_obj );
25012501 }
25022502 default :
2503- if (FL_TEST (obj , FL_EXIVAR )) {
2503+ if (rb_obj_exivar_p (obj )) {
25042504 struct gen_fields_tbl * fields_tbl ;
25052505
25062506 if (rb_gen_fields_tbl_get (obj , 0 , & fields_tbl )) {
0 commit comments