-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathclass.c
More file actions
3283 lines (2857 loc) · 93.3 KB
/
class.c
File metadata and controls
3283 lines (2857 loc) · 93.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
class.c -
$Author$
created at: Tue Aug 10 15:05:44 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
/*!
* \addtogroup class
* \{
*/
#include "ruby/internal/config.h"
#include <ctype.h>
#include "constant.h"
#include "debug_counter.h"
#include "id_table.h"
#include "internal.h"
#include "internal/box.h"
#include "internal/class.h"
#include "internal/eval.h"
#include "internal/hash.h"
#include "internal/object.h"
#include "internal/string.h"
#include "internal/variable.h"
#include "ruby/st.h"
#include "vm_core.h"
#include "ruby/ractor.h"
#include "yjit.h"
#include "zjit.h"
/* Flags of T_CLASS
*
* 0: RCLASS_IS_ROOT
* The class has been added to the VM roots. Will always be marked and pinned.
* This is done for classes defined from C to allow storing them in global variables.
* 1: RUBY_FL_SINGLETON
* This class is a singleton class.
* 2: RCLASS_PRIME_CLASSEXT_WRITABLE
* This class's prime classext is the only classext and writable from any boxes.
* If unset, the prime classext is writable only from the root box.
* 3: RCLASS_IS_INITIALIZED
* Class has been initialized.
* 4: RCLASS_BOXABLE
* Is a builtin class that may be boxed. It larger than a normal class.
*/
/* Flags of T_ICLASS
*
* 2: RCLASS_PRIME_CLASSEXT_WRITABLE
* This module's prime classext is the only classext and writable from any boxes.
* If unset, the prime classext is writable only from the root box.
* 4: RCLASS_BOXABLE
* Is a builtin class that may be boxed. It larger than a normal class.
*/
/* Flags of T_MODULE
*
* 0: RCLASS_IS_ROOT
* The class has been added to the VM roots. Will always be marked and pinned.
* This is done for classes defined from C to allow storing them in global variables.
* 1: <reserved>
* Ensures that RUBY_FL_SINGLETON is never set on a T_MODULE. See `rb_class_real`.
* 2: RCLASS_PRIME_CLASSEXT_WRITABLE
* This module's prime classext is the only classext and writable from any boxes.
* If unset, the prime classext is writable only from the root box.
* 3: RCLASS_IS_INITIALIZED
* Module has been initialized.
* 4: RCLASS_BOXABLE
* Is a builtin class that may be boxed. It larger than a normal class.
* 5: RMODULE_IS_REFINEMENT
* Module is used for refinements.
*/
#define METACLASS_OF(k) RBASIC(k)->klass
#define SET_METACLASS_OF(k, cls) RBASIC_SET_CLASS(k, cls)
static enum rb_id_table_iterator_result
cvar_table_free_i(VALUE value, void *ctx)
{
struct rb_cvar_class_tbl_entry *entry = (struct rb_cvar_class_tbl_entry *)value;
SIZED_FREE(entry);
return ID_TABLE_CONTINUE;
}
rb_classext_t *
rb_class_unlink_classext(VALUE klass, const rb_box_t *box)
{
st_data_t ext;
st_data_t key = (st_data_t)box->box_object;
VALUE obj_id = rb_obj_id(klass);
st_delete(box->classext_cow_classes, &obj_id, 0);
st_delete(RCLASS_CLASSEXT_TBL(klass), &key, &ext);
return (rb_classext_t *)ext;
}
void
rb_class_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime)
{
struct rb_id_table *tbl;
rb_id_table_free(RCLASSEXT_M_TBL(ext));
if (!RCLASSEXT_SHARED_CONST_TBL(ext) && (tbl = RCLASSEXT_CONST_TBL(ext)) != NULL) {
rb_free_const_table(tbl);
}
if ((tbl = RCLASSEXT_CVC_TBL(ext)) != NULL) {
rb_id_table_foreach_values(tbl, cvar_table_free_i, NULL);
rb_id_table_free(tbl);
}
rb_class_classext_free_subclasses(ext, klass, false);
if (RCLASSEXT_SUPERCLASSES_WITH_SELF(ext)) {
RUBY_ASSERT(is_prime); // superclasses should only be used on prime
size_t depth = RCLASSEXT_SUPERCLASS_DEPTH(ext);
if (depth != RCLASS_MAX_SUPERCLASS_DEPTH) {
depth++;
}
SIZED_FREE_N(RCLASSEXT_SUPERCLASSES(ext), depth);
}
if (!is_prime) { // the prime classext will be freed with RClass
SIZED_FREE(ext);
}
}
void
rb_iclass_classext_free(VALUE klass, rb_classext_t *ext, bool is_prime)
{
if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
/* Method table is not shared for origin iclasses of classes */
rb_id_table_free(RCLASSEXT_M_TBL(ext));
}
if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
}
rb_class_classext_free_subclasses(ext, klass, false);
if (!is_prime) { // the prime classext will be freed with RClass
SIZED_FREE(ext);
}
}
static void
iclass_free_orphan_classext(VALUE klass, rb_classext_t *ext)
{
if (RCLASSEXT_ICLASS_IS_ORIGIN(ext) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext)) {
/* Method table is not shared for origin iclasses of classes */
rb_id_table_free(RCLASSEXT_M_TBL(ext));
}
if (RCLASSEXT_CALLABLE_M_TBL(ext) != NULL) {
rb_id_table_free(RCLASSEXT_CALLABLE_M_TBL(ext));
}
rb_class_classext_free_subclasses(ext, klass, true); // replacing this classext with a newer one
SIZED_FREE(ext);
}
struct rb_class_set_box_classext_args {
VALUE obj;
rb_classext_t *ext;
};
static int
set_box_classext_update(st_data_t *key_ptr, st_data_t *val_ptr, st_data_t a, int existing)
{
struct rb_class_set_box_classext_args *args = (struct rb_class_set_box_classext_args *)a;
if (existing) {
if (LIKELY(BUILTIN_TYPE(args->obj) == T_ICLASS)) {
iclass_free_orphan_classext(args->obj, (rb_classext_t *)*val_ptr);
}
else {
rb_bug("Updating existing classext for non-iclass never happen");
}
}
*val_ptr = (st_data_t)args->ext;
return ST_CONTINUE;
}
void
rb_class_set_box_classext(VALUE obj, const rb_box_t *box, rb_classext_t *ext)
{
struct rb_class_set_box_classext_args args = {
.obj = obj,
.ext = ext,
};
VM_ASSERT(BOX_USER_P(box));
st_update(RCLASS_CLASSEXT_TBL(obj), (st_data_t)box->box_object, set_box_classext_update, (st_data_t)&args);
st_insert(box->classext_cow_classes, (st_data_t)rb_obj_id(obj), obj);
// FIXME: This is done here because this is the first time the objects in
// the classext are exposed via this class. It's likely that if GC
// compaction occurred between the VALUEs being copied in and this
// writebarrier trigger the values will be stale.
rb_gc_writebarrier_remember(obj);
}
RUBY_EXTERN rb_serial_t ruby_vm_global_cvar_state;
struct duplicate_id_tbl_data {
struct rb_id_table *tbl;
VALUE klass;
};
static enum rb_id_table_iterator_result
duplicate_classext_id_table_i(ID key, VALUE value, void *data)
{
struct rb_id_table *tbl = (struct rb_id_table *)data;
rb_id_table_insert(tbl, key, value);
return ID_TABLE_CONTINUE;
}
static enum rb_id_table_iterator_result
duplicate_classext_m_tbl_i(ID key, VALUE value, void *data)
{
struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
rb_method_entry_t *me = (rb_method_entry_t *)value;
rb_method_table_insert0(arg->klass, arg->tbl, key, me, false);
return ID_TABLE_CONTINUE;
}
static struct rb_id_table *
duplicate_classext_m_tbl(struct rb_id_table *orig, VALUE klass, bool init_missing)
{
struct rb_id_table *tbl;
if (!orig) {
if (init_missing)
return rb_id_table_create(0);
else
return NULL;
}
tbl = rb_id_table_create(rb_id_table_size(orig));
struct duplicate_id_tbl_data data = {
.tbl = tbl,
.klass = klass,
};
rb_id_table_foreach(orig, duplicate_classext_m_tbl_i, &data);
return tbl;
}
static struct rb_id_table *
duplicate_classext_id_table(struct rb_id_table *orig, bool init_missing)
{
struct rb_id_table *tbl;
if (!orig) {
if (init_missing)
return rb_id_table_create(0);
else
return NULL;
}
tbl = rb_id_table_create(rb_id_table_size(orig));
rb_id_table_foreach(orig, duplicate_classext_id_table_i, tbl);
return tbl;
}
static rb_const_entry_t *
duplicate_classext_const_entry(rb_const_entry_t *src, VALUE klass)
{
// See also: setup_const_entry (variable.c)
rb_const_entry_t *dst = ZALLOC(rb_const_entry_t);
dst->flag = src->flag;
dst->line = src->line;
RB_OBJ_WRITE(klass, &dst->value, src->value);
RB_OBJ_WRITE(klass, &dst->file, src->file);
return dst;
}
static enum rb_id_table_iterator_result
duplicate_classext_const_tbl_i(ID key, VALUE value, void *data)
{
struct duplicate_id_tbl_data *arg = (struct duplicate_id_tbl_data *)data;
rb_const_entry_t *entry = duplicate_classext_const_entry((rb_const_entry_t *)value, arg->klass);
rb_id_table_insert(arg->tbl, key, (VALUE)entry);
return ID_TABLE_CONTINUE;
}
static struct rb_id_table *
duplicate_classext_const_tbl(struct rb_id_table *src, VALUE klass)
{
struct rb_id_table *dst;
if (!src)
return NULL;
dst = rb_id_table_create(rb_id_table_size(src));
struct duplicate_id_tbl_data data = {
.tbl = dst,
.klass = klass,
};
rb_id_table_foreach(src, duplicate_classext_const_tbl_i, (void *)&data);
return dst;
}
static VALUE
box_subclasses_tbl_key(const rb_box_t *box)
{
if (!box){
return 0;
}
return (VALUE)box->box_id;
}
static void
duplicate_classext_subclasses(rb_classext_t *orig, rb_classext_t *copy)
{
rb_subclass_anchor_t *anchor, *orig_anchor;
rb_subclass_entry_t *head, *cur, *cdr, *entry, *first = NULL;
rb_box_subclasses_t *box_subclasses;
struct st_table *tbl;
if (RCLASSEXT_SUBCLASSES(orig)) {
orig_anchor = RCLASSEXT_SUBCLASSES(orig);
box_subclasses = orig_anchor->box_subclasses;
tbl = ((rb_box_subclasses_t *)box_subclasses)->tbl;
anchor = ZALLOC(rb_subclass_anchor_t);
anchor->box_subclasses = rb_box_subclasses_ref_inc(box_subclasses);
head = ZALLOC(rb_subclass_entry_t);
anchor->head = head;
RCLASSEXT_SUBCLASSES(copy) = anchor;
cur = head;
entry = orig_anchor->head;
RUBY_ASSERT(!entry->klass);
// The head entry has NULL klass always. See rb_class_foreach_subclass().
entry = entry->next;
while (entry) {
if (rb_objspace_garbage_object_p(entry->klass)) {
entry = entry->next;
continue;
}
cdr = ZALLOC(rb_subclass_entry_t);
cdr->klass = entry->klass;
cdr->prev = cur;
cur->next = cdr;
if (!first) {
VALUE box_id = box_subclasses_tbl_key(RCLASSEXT_BOX(copy));
first = cdr;
st_insert(tbl, box_id, (st_data_t)first);
}
cur = cdr;
entry = entry->next;
}
}
if (RCLASSEXT_BOX_SUPER_SUBCLASSES(orig))
RCLASSEXT_BOX_SUPER_SUBCLASSES(copy) = rb_box_subclasses_ref_inc(RCLASSEXT_BOX_SUPER_SUBCLASSES(orig));
if (RCLASSEXT_BOX_MODULE_SUBCLASSES(orig))
RCLASSEXT_BOX_MODULE_SUBCLASSES(copy) = rb_box_subclasses_ref_inc(RCLASSEXT_BOX_MODULE_SUBCLASSES(orig));
}
static void
class_duplicate_iclass_classext(VALUE iclass, rb_classext_t *mod_ext, const rb_box_t *box)
{
RUBY_ASSERT(RB_TYPE_P(iclass, T_ICLASS));
rb_classext_t *src = RCLASS_EXT_PRIME(iclass);
rb_classext_t *ext = RCLASS_EXT_TABLE_LOOKUP_INTERNAL(iclass, box);
int first_set = 0;
if (ext) {
// iclass classext for the ns is only for cc/callable_m_tbl if it's created earlier than module's one
rb_invalidate_method_caches(RCLASSEXT_CALLABLE_M_TBL(ext), RCLASSEXT_CC_TBL(ext));
}
ext = ZALLOC(rb_classext_t);
RCLASSEXT_BOX(ext) = box;
RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(src);
// See also: rb_include_class_new()
if (RCLASSEXT_ICLASS_IS_ORIGIN(src) && !RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src)) {
RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(src), iclass, true);
}
else {
RCLASSEXT_M_TBL(ext) = RCLASSEXT_M_TBL(mod_ext);
}
RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(mod_ext);
RCLASSEXT_CVC_TBL(ext) = RCLASSEXT_CVC_TBL(mod_ext);
// Those are cache and should be recreated when methods are called
// RCLASSEXT_CALLABLE_M_TBL(ext) = NULL;
// RCLASSEXT_CC_TBL(ext) = NULL;
// subclasses, box_super_subclasses_tbl, box_module_subclasses_tbl
duplicate_classext_subclasses(src, ext);
RCLASSEXT_SET_ORIGIN(ext, iclass, RCLASSEXT_ORIGIN(src));
RCLASSEXT_ICLASS_IS_ORIGIN(ext) = RCLASSEXT_ICLASS_IS_ORIGIN(src);
RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(src);
RCLASSEXT_SET_INCLUDER(ext, iclass, RCLASSEXT_INCLUDER(src));
VM_ASSERT(FL_TEST_RAW(iclass, RCLASS_BOXABLE));
first_set = RCLASS_SET_BOX_CLASSEXT(iclass, box, ext);
if (first_set) {
RCLASS_SET_PRIME_CLASSEXT_WRITABLE(iclass, false);
}
}
rb_classext_t *
rb_class_duplicate_classext(rb_classext_t *orig, VALUE klass, const rb_box_t *box)
{
VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
rb_classext_t *ext = ZALLOC(rb_classext_t);
bool dup_iclass = RB_TYPE_P(klass, T_MODULE) ? true : false;
RCLASSEXT_BOX(ext) = box;
RCLASSEXT_SUPER(ext) = RCLASSEXT_SUPER(orig);
RCLASSEXT_M_TBL(ext) = duplicate_classext_m_tbl(RCLASSEXT_M_TBL(orig), klass, dup_iclass);
RCLASSEXT_ICLASS_IS_ORIGIN(ext) = true;
RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) = false;
if (orig->fields_obj) {
RB_OBJ_WRITE(klass, &ext->fields_obj, rb_imemo_fields_clone(orig->fields_obj));
}
if (RCLASSEXT_SHARED_CONST_TBL(orig)) {
RCLASSEXT_CONST_TBL(ext) = RCLASSEXT_CONST_TBL(orig);
RCLASSEXT_SHARED_CONST_TBL(ext) = true;
}
else {
RCLASSEXT_CONST_TBL(ext) = duplicate_classext_const_tbl(RCLASSEXT_CONST_TBL(orig), klass);
RCLASSEXT_SHARED_CONST_TBL(ext) = false;
}
/*
* callable_m_tbl is for `super` chain, and entries will be created when the super chain is called.
* so initially, it can be NULL and let it be created lazily.
* RCLASSEXT_CALLABLE_M_TBL(ext) = NULL;
*
* cc_tbl is for method inline cache, and method calls from different boxes never occur on
* the same code, so the copied classext should have a different cc_tbl from the prime one.
* RCLASSEXT_CC_TBL(copy) = NULL
*/
RCLASSEXT_CVC_TBL(ext) = duplicate_classext_id_table(RCLASSEXT_CVC_TBL(orig), dup_iclass);
// subclasses, subclasses_index
duplicate_classext_subclasses(orig, ext);
RCLASSEXT_SET_ORIGIN(ext, klass, RCLASSEXT_ORIGIN(orig));
/*
* Members not copied to box's classext values
* * refined_class
* * as.class.allocator / as.singleton_class.attached_object
* * includer
* * max IV count
* * variation count
*/
RCLASSEXT_PERMANENT_CLASSPATH(ext) = RCLASSEXT_PERMANENT_CLASSPATH(orig);
RCLASSEXT_CLONED(ext) = RCLASSEXT_CLONED(orig);
RCLASSEXT_CLASSPATH(ext) = RCLASSEXT_CLASSPATH(orig);
/* For the usual T_CLASS/T_MODULE, iclass flags are always false */
if (dup_iclass) {
VALUE iclass;
/*
* ICLASS has the same m_tbl/const_tbl/cvc_tbl with the included module.
* So the module's classext is copied, its tables should be also referred
* by the ICLASS's classext for the box.
*/
rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
rb_subclass_entry_t *subclass_entry = anchor->head;
while (subclass_entry) {
if (subclass_entry->klass && RB_TYPE_P(subclass_entry->klass, T_ICLASS)) {
iclass = subclass_entry->klass;
VM_ASSERT(RB_TYPE_P(iclass, T_ICLASS));
if (RBASIC_CLASS(iclass) == klass) {
// Is the subclass an ICLASS including this module into another class
// If so we need to re-associate it under our box with the new ext
VM_ASSERT(FL_TEST_RAW(iclass, RCLASS_BOXABLE));
class_duplicate_iclass_classext(iclass, ext, box);
}
}
subclass_entry = subclass_entry->next;
}
}
return ext;
}
void
rb_class_ensure_writable(VALUE klass)
{
VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_MODULE) || RB_TYPE_P(klass, T_ICLASS));
RCLASS_EXT_WRITABLE(klass);
}
struct class_classext_foreach_arg {
rb_class_classext_foreach_callback_func *func;
void * callback_arg;
};
static int
class_classext_foreach_i(st_data_t key, st_data_t value, st_data_t arg)
{
struct class_classext_foreach_arg *foreach_arg = (struct class_classext_foreach_arg *)arg;
rb_class_classext_foreach_callback_func *func = foreach_arg->func;
func((rb_classext_t *)value, false, (VALUE)key, foreach_arg->callback_arg);
return ST_CONTINUE;
}
void
rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *func, void *arg)
{
st_table *tbl = RCLASS_CLASSEXT_TBL(klass);
struct class_classext_foreach_arg foreach_arg;
if (tbl) {
foreach_arg.func = func;
foreach_arg.callback_arg = arg;
rb_st_foreach(tbl, class_classext_foreach_i, (st_data_t)&foreach_arg);
}
func(RCLASS_EXT_PRIME(klass), true, (VALUE)NULL, arg);
}
VALUE
rb_class_super_of(VALUE klass)
{
return RCLASS_SUPER(klass);
}
VALUE
rb_class_singleton_p(VALUE klass)
{
return RCLASS_SINGLETON_P(klass);
}
unsigned char
rb_class_variation_count(VALUE klass)
{
return RCLASS_VARIATION_COUNT(klass);
}
static void
push_subclass_entry_to_list(VALUE super, VALUE klass, bool is_module)
{
rb_subclass_entry_t *entry, *head;
rb_subclass_anchor_t *anchor;
rb_box_subclasses_t *box_subclasses;
struct st_table *tbl;
const rb_box_t *box = rb_current_box();
entry = ZALLOC(rb_subclass_entry_t);
entry->klass = klass;
RB_VM_LOCKING() {
anchor = RCLASS_WRITABLE_SUBCLASSES(super);
VM_ASSERT(anchor);
box_subclasses = (rb_box_subclasses_t *)anchor->box_subclasses;
VM_ASSERT(box_subclasses);
tbl = box_subclasses->tbl;
VM_ASSERT(tbl);
head = anchor->head;
if (head->next) {
head->next->prev = entry;
entry->next = head->next;
}
head->next = entry;
entry->prev = head;
st_insert(tbl, box_subclasses_tbl_key(box), (st_data_t)entry);
}
if (is_module) {
RCLASS_WRITE_BOX_MODULE_SUBCLASSES(klass, anchor->box_subclasses);
}
else {
RCLASS_WRITE_BOX_SUPER_SUBCLASSES(klass, anchor->box_subclasses);
}
}
void
rb_class_subclass_add(VALUE super, VALUE klass)
{
if (super && !UNDEF_P(super)) {
push_subclass_entry_to_list(super, klass, false);
}
}
static void
rb_module_add_to_subclasses_list(VALUE module, VALUE iclass)
{
if (module && !UNDEF_P(module)) {
push_subclass_entry_to_list(module, iclass, true);
}
}
static struct rb_subclass_entry *
class_get_subclasses_for_ns(struct st_table *tbl, VALUE box_id)
{
st_data_t value;
if (st_lookup(tbl, (st_data_t)box_id, &value)) {
return (struct rb_subclass_entry *)value;
}
return NULL;
}
static int
remove_class_from_subclasses_replace_first_entry(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
{
*value = arg;
return ST_CONTINUE;
}
static void
remove_class_from_subclasses(struct st_table *tbl, VALUE box_id, VALUE klass)
{
rb_subclass_entry_t *entry = class_get_subclasses_for_ns(tbl, box_id);
bool first_entry = true;
while (entry) {
if (entry->klass == klass) {
rb_subclass_entry_t *prev = entry->prev, *next = entry->next;
if (prev) {
prev->next = next;
}
if (next) {
next->prev = prev;
}
if (first_entry) {
if (next) {
st_update(tbl, box_id, remove_class_from_subclasses_replace_first_entry, (st_data_t)next);
}
else {
// no subclass entries in this ns after the deletion
st_delete(tbl, &box_id, NULL);
}
}
SIZED_FREE(entry);
break;
}
else if (first_entry) {
first_entry = false;
}
entry = entry->next;
}
}
void
rb_class_remove_from_super_subclasses(VALUE klass)
{
rb_classext_t *ext = RCLASS_EXT_WRITABLE(klass);
rb_box_subclasses_t *box_subclasses = RCLASSEXT_BOX_SUPER_SUBCLASSES(ext);
if (!box_subclasses) return;
remove_class_from_subclasses(box_subclasses->tbl, box_subclasses_tbl_key(RCLASSEXT_BOX(ext)), klass);
rb_box_subclasses_ref_dec(box_subclasses);
RCLASSEXT_BOX_SUPER_SUBCLASSES(ext) = 0;
}
void
rb_class_classext_free_subclasses(rb_classext_t *ext, VALUE klass, bool replacing)
{
rb_subclass_anchor_t *anchor = RCLASSEXT_SUBCLASSES(ext);
struct st_table *tbl = anchor->box_subclasses->tbl;
VALUE box_id = box_subclasses_tbl_key(RCLASSEXT_BOX(ext));
rb_subclass_entry_t *next, *entry = anchor->head;
while (entry) {
next = entry->next;
SIZED_FREE(entry);
entry = next;
}
VM_ASSERT(
rb_box_subclasses_ref_count(anchor->box_subclasses) > 0,
"box_subclasses refcount (%p) %ld", anchor->box_subclasses, rb_box_subclasses_ref_count(anchor->box_subclasses));
st_delete(tbl, &box_id, NULL);
rb_box_subclasses_ref_dec(anchor->box_subclasses);
SIZED_FREE(anchor);
if (RCLASSEXT_BOX_SUPER_SUBCLASSES(ext)) {
rb_box_subclasses_t *box_sub = RCLASSEXT_BOX_SUPER_SUBCLASSES(ext);
if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
rb_box_subclasses_ref_dec(box_sub);
}
if (RCLASSEXT_BOX_MODULE_SUBCLASSES(ext)) {
rb_box_subclasses_t *box_sub = RCLASSEXT_BOX_MODULE_SUBCLASSES(ext);
if (!replacing) remove_class_from_subclasses(box_sub->tbl, box_id, klass);
rb_box_subclasses_ref_dec(box_sub);
}
}
void
rb_class_foreach_subclass(VALUE klass, void (*f)(VALUE, VALUE), VALUE arg)
{
rb_subclass_entry_t *tmp;
rb_subclass_entry_t *cur = RCLASS_SUBCLASSES_FIRST(klass);
/* do not be tempted to simplify this loop into a for loop, the order of
operations is important here if `f` modifies the linked list */
while (cur) {
VALUE curklass = cur->klass;
tmp = cur->next;
// do not trigger GC during f, otherwise the cur will become
// a dangling pointer if the subclass is collected
f(curklass, arg);
cur = tmp;
}
}
static void
class_detach_subclasses(VALUE klass, VALUE arg)
{
rb_class_remove_from_super_subclasses(klass);
}
static void
class_switch_superclass(VALUE super, VALUE klass)
{
RB_VM_LOCKING() {
class_detach_subclasses(klass, Qnil);
rb_class_subclass_add(super, klass);
}
}
/**
* Allocates a struct RClass for a new class, iclass, or module.
*
* @param type The type of the RClass (T_CLASS, T_ICLASS, or T_MODULE)
* @param klass value for basic.klass of the returned object.
* @return an uninitialized Class/IClass/Module object.
* @pre `klass` must refer to a class or module
*
* @note this function is not Class#allocate.
*/
static VALUE
class_alloc0(enum ruby_value_type type, VALUE klass, bool boxable)
{
rb_box_subclasses_t *box_subclasses;
rb_subclass_anchor_t *anchor;
const rb_box_t *box = rb_current_box();
if (!ruby_box_init_done) {
boxable = true;
}
size_t alloc_size = sizeof(struct RClass_and_rb_classext_t);
if (boxable) {
alloc_size = sizeof(struct RClass_boxable);
}
// class_alloc is supposed to return a new object that is not promoted yet.
// So, we need to avoid GC after NEWOBJ_OF.
// To achieve that, we allocate subclass lists before NEWOBJ_OF.
//
// TODO: Note that this could cause memory leak.
// If NEWOBJ_OF fails with out of memory, these buffers will leak.
box_subclasses = ZALLOC(rb_box_subclasses_t);
box_subclasses->refcount = 1;
box_subclasses->tbl = st_init_numtable();
anchor = ZALLOC(rb_subclass_anchor_t);
anchor->box_subclasses = box_subclasses;
anchor->head = ZALLOC(rb_subclass_entry_t);
RUBY_ASSERT(type == T_CLASS || type == T_ICLASS || type == T_MODULE);
VALUE flags = type | FL_SHAREABLE;
if (RGENGC_WB_PROTECTED_CLASS) flags |= FL_WB_PROTECTED;
if (boxable) flags |= RCLASS_BOXABLE;
NEWOBJ_OF(obj, struct RClass, klass, flags, alloc_size, 0);
obj->object_id = 0;
memset(RCLASS_EXT_PRIME(obj), 0, sizeof(rb_classext_t));
/* ZALLOC
RCLASS_CONST_TBL(obj) = 0;
RCLASS_M_TBL(obj) = 0;
RCLASS_FIELDS(obj) = 0;
RCLASS_SET_SUPER((VALUE)obj, 0);
*/
if (boxable) {
((struct RClass_boxable *)obj)->box_classext_tbl = NULL;
}
RCLASS_PRIME_BOX((VALUE)obj) = box;
// Classes/Modules defined in user boxes are
// writable directly because it exists only in a box.
RCLASS_SET_PRIME_CLASSEXT_WRITABLE((VALUE)obj, !boxable || BOX_USER_P(box));
RCLASS_SET_ORIGIN((VALUE)obj, (VALUE)obj);
RCLASS_SET_REFINED_CLASS((VALUE)obj, Qnil);
RCLASS_SET_SUBCLASSES((VALUE)obj, anchor);
return (VALUE)obj;
}
static VALUE
class_alloc(enum ruby_value_type type, VALUE klass)
{
bool boxable = rb_box_available() && BOX_ROOT_P(rb_current_box());
return class_alloc0(type, klass, boxable);
}
static VALUE
class_associate_super(VALUE klass, VALUE super, bool init)
{
if (super && !UNDEF_P(super)) {
class_switch_superclass(super, klass);
}
if (init) {
RCLASS_SET_SUPER(klass, super);
}
else {
RCLASS_WRITE_SUPER(klass, super);
}
rb_class_update_superclasses(klass);
return super;
}
VALUE
rb_class_set_super(VALUE klass, VALUE super)
{
return class_associate_super(klass, super, false);
}
static void
class_initialize_method_table(VALUE c)
{
// initialize the prime classext m_tbl
RCLASS_SET_M_TBL(c, rb_id_table_create(0));
}
static void
class_clear_method_table(VALUE c)
{
RCLASS_WRITE_M_TBL(c, rb_id_table_create(0));
}
static VALUE
class_boot_boxable(VALUE super, bool boxable)
{
VALUE klass = class_alloc0(T_CLASS, rb_cClass, boxable);
// initialize method table prior to class_associate_super()
// because class_associate_super() may cause GC and promote klass
class_initialize_method_table(klass);
class_associate_super(klass, super, true);
if (super && !UNDEF_P(super)) {
rb_class_set_initialized(klass);
}
return (VALUE)klass;
}
/**
* A utility function that wraps class_alloc.
*
* allocates a class and initializes safely.
* @param super a class from which the new class derives.
* @return a class object.
* @pre `super` must be a class.
* @post the metaclass of the new class is Class.
*/
VALUE
rb_class_boot(VALUE super)
{
return class_boot_boxable(super, false);
}
static VALUE *
class_superclasses_including_self(VALUE klass)
{
if (RCLASS_SUPERCLASSES_WITH_SELF_P(klass))
return RCLASS_SUPERCLASSES(klass);
size_t depth = RCLASS_SUPERCLASS_DEPTH(klass);
VALUE *superclasses = xmalloc(sizeof(VALUE) * (depth + 1));
if (depth > 0)
memcpy(superclasses, RCLASS_SUPERCLASSES(klass), sizeof(VALUE) * depth);
superclasses[depth] = klass;
return superclasses;
}
void
rb_class_update_superclasses(VALUE klass)
{
VALUE *superclasses;
size_t super_depth;
VALUE super = RCLASS_SUPER(klass);
if (!RB_TYPE_P(klass, T_CLASS)) return;
if (UNDEF_P(super)) return;
// If the superclass array is already built
if (RCLASS_SUPERCLASSES(klass))
return;
// find the proper superclass
while (super != Qfalse && !RB_TYPE_P(super, T_CLASS)) {
super = RCLASS_SUPER(super);
}
// For BasicObject and uninitialized classes, depth=0 and ary=NULL
if (super == Qfalse)
return;
// Sometimes superclasses are set before the full ancestry tree is built
// This happens during metaclass construction
if (super != rb_cBasicObject && !RCLASS_SUPERCLASS_DEPTH(super)) {
rb_class_update_superclasses(super);
// If it is still unset we need to try later
if (!RCLASS_SUPERCLASS_DEPTH(super))
return;
}
super_depth = RCLASS_SUPERCLASS_DEPTH(super);
if (RCLASS_SUPERCLASSES_WITH_SELF_P(super)) {
superclasses = RCLASS_SUPERCLASSES(super);
}
else {
superclasses = class_superclasses_including_self(super);
RCLASS_WRITE_SUPERCLASSES(super, super_depth, superclasses, true);
}
size_t depth = super_depth == RCLASS_MAX_SUPERCLASS_DEPTH ? super_depth : super_depth + 1;
RCLASS_WRITE_SUPERCLASSES(klass, depth, superclasses, false);
}
void
rb_check_inheritable(VALUE super)
{
if (!RB_TYPE_P(super, T_CLASS)) {
rb_raise(rb_eTypeError, "superclass must be an instance of Class (given an instance of %"PRIsVALUE")",
rb_obj_class(super));
}
if (RCLASS_SINGLETON_P(super)) {
rb_raise(rb_eTypeError, "can't make subclass of singleton class");
}
if (super == rb_cClass) {
rb_raise(rb_eTypeError, "can't make subclass of Class");
}
}
VALUE
rb_class_new(VALUE super)
{
Check_Type(super, T_CLASS);
rb_check_inheritable(super);
VALUE klass = rb_class_boot(super);
if (super != rb_cObject && super != rb_cBasicObject) {
RCLASS_SET_MAX_IV_COUNT(klass, RCLASS_MAX_IV_COUNT(super));
}
RUBY_ASSERT(getenv("RUBY_BOX") || RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass));
return klass;
}
VALUE
rb_class_s_alloc(VALUE klass)
{
return rb_class_boot(0);
}
static void
clone_method(VALUE old_klass, VALUE new_klass, ID mid, const rb_method_entry_t *me)
{
if (me->def->type == VM_METHOD_TYPE_ISEQ) {