-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.c
4581 lines (4131 loc) · 124 KB
/
object.c
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
/**********************************************************************
object.c -
$Author$
created at: Thu Jul 15 12:01:24 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#include "ruby/internal/config.h"
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include "constant.h"
#include "id.h"
#include "internal.h"
#include "internal/array.h"
#include "internal/class.h"
#include "internal/error.h"
#include "internal/eval.h"
#include "internal/inits.h"
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/struct.h"
#include "internal/string.h"
#include "internal/st.h"
#include "internal/symbol.h"
#include "internal/variable.h"
#include "variable.h"
#include "probes.h"
#include "ruby/encoding.h"
#include "ruby/st.h"
#include "ruby/util.h"
#include "ruby/assert.h"
#include "builtin.h"
#include "shape.h"
#include "yjit.h"
/* Flags of RObject
*
* 1: ROBJECT_EMBED
* The object has its instance variables embedded (the array of
* instance variables directly follow the object, rather than being
* on a separately allocated buffer).
* if !SHAPE_IN_BASIC_FLAGS
* 4-19: SHAPE_FLAG_MASK
* Shape ID for the object.
* endif
*/
/*!
* \addtogroup object
* \{
*/
VALUE rb_cBasicObject;
VALUE rb_mKernel;
VALUE rb_cObject;
VALUE rb_cModule;
VALUE rb_cClass;
VALUE rb_cRefinement;
VALUE rb_cNilClass;
VALUE rb_cTrueClass;
VALUE rb_cFalseClass;
static VALUE rb_cNilClass_to_s;
static VALUE rb_cTrueClass_to_s;
static VALUE rb_cFalseClass_to_s;
/*! \cond INTERNAL_MACRO */
#define id_eq idEq
#define id_eql idEqlP
#define id_match idEqTilde
#define id_inspect idInspect
#define id_init_copy idInitialize_copy
#define id_init_clone idInitialize_clone
#define id_init_dup idInitialize_dup
#define id_const_missing idConst_missing
#define id_to_f idTo_f
#define CLASS_OR_MODULE_P(obj) \
(!SPECIAL_CONST_P(obj) && \
(BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
/*! \endcond */
size_t
rb_obj_embedded_size(uint32_t numiv)
{
return offsetof(struct RObject, as.ary) + (sizeof(VALUE) * numiv);
}
VALUE
rb_obj_hide(VALUE obj)
{
if (!SPECIAL_CONST_P(obj)) {
RBASIC_CLEAR_CLASS(obj);
}
return obj;
}
VALUE
rb_obj_reveal(VALUE obj, VALUE klass)
{
if (!SPECIAL_CONST_P(obj)) {
RBASIC_SET_CLASS(obj, klass);
}
return obj;
}
VALUE
rb_class_allocate_instance(VALUE klass)
{
uint32_t index_tbl_num_entries = RCLASS_EXT(klass)->max_iv_count;
size_t size = rb_obj_embedded_size(index_tbl_num_entries);
if (!rb_gc_size_allocatable_p(size)) {
size = sizeof(struct RObject);
}
NEWOBJ_OF(o, struct RObject, klass,
T_OBJECT | ROBJECT_EMBED | (RGENGC_WB_PROTECTED_OBJECT ? FL_WB_PROTECTED : 0), size, 0);
VALUE obj = (VALUE)o;
RUBY_ASSERT(rb_shape_get_shape(obj)->type == SHAPE_ROOT);
// Set the shape to the specific T_OBJECT shape.
ROBJECT_SET_SHAPE_ID(obj, (shape_id_t)(rb_gc_size_pool_id_for_size(size) + FIRST_T_OBJECT_SHAPE_ID));
#if RUBY_DEBUG
RUBY_ASSERT(!rb_shape_obj_too_complex(obj));
VALUE *ptr = ROBJECT_IVPTR(obj);
for (size_t i = 0; i < ROBJECT_IV_CAPACITY(obj); i++) {
ptr[i] = Qundef;
}
#endif
return obj;
}
VALUE
rb_obj_setup(VALUE obj, VALUE klass, VALUE type)
{
VALUE ignored_flags = RUBY_FL_PROMOTED | RUBY_FL_SEEN_OBJ_ID;
RBASIC(obj)->flags = (type & ~ignored_flags) | (RBASIC(obj)->flags & ignored_flags);
RBASIC_SET_CLASS(obj, klass);
return obj;
}
/*
* call-seq:
* true === other -> true or false
* false === other -> true or false
* nil === other -> true or false
*
* Returns +true+ or +false+.
*
* Like Object#==, if +object+ is an instance of Object
* (and not an instance of one of its many subclasses).
*
* This method is commonly overridden by those subclasses,
* to provide meaningful semantics in +case+ statements.
*/
#define case_equal rb_equal
/* The default implementation of #=== is
* to call #== with the rb_equal() optimization. */
VALUE
rb_equal(VALUE obj1, VALUE obj2)
{
VALUE result;
if (obj1 == obj2) return Qtrue;
result = rb_equal_opt(obj1, obj2);
if (UNDEF_P(result)) {
result = rb_funcall(obj1, id_eq, 1, obj2);
}
return RBOOL(RTEST(result));
}
int
rb_eql(VALUE obj1, VALUE obj2)
{
VALUE result;
if (obj1 == obj2) return TRUE;
result = rb_eql_opt(obj1, obj2);
if (UNDEF_P(result)) {
result = rb_funcall(obj1, id_eql, 1, obj2);
}
return RTEST(result);
}
/**
* call-seq:
* obj == other -> true or false
* obj.equal?(other) -> true or false
* obj.eql?(other) -> true or false
*
* Equality --- At the Object level, #== returns <code>true</code>
* only if +obj+ and +other+ are the same object. Typically, this
* method is overridden in descendant classes to provide
* class-specific meaning.
*
* Unlike #==, the #equal? method should never be overridden by
* subclasses as it is used to determine object identity (that is,
* <code>a.equal?(b)</code> if and only if <code>a</code> is the same
* object as <code>b</code>):
*
* obj = "a"
* other = obj.dup
*
* obj == other #=> true
* obj.equal? other #=> false
* obj.equal? obj #=> true
*
* The #eql? method returns <code>true</code> if +obj+ and +other+
* refer to the same hash key. This is used by Hash to test members
* for equality. For any pair of objects where #eql? returns +true+,
* the #hash value of both objects must be equal. So any subclass
* that overrides #eql? should also override #hash appropriately.
*
* For objects of class Object, #eql? is synonymous
* with #==. Subclasses normally continue this tradition by aliasing
* #eql? to their overridden #== method, but there are exceptions.
* Numeric types, for example, perform type conversion across #==,
* but not across #eql?, so:
*
* 1 == 1.0 #=> true
* 1.eql? 1.0 #=> false
*--
* \private
*++
*/
VALUE
rb_obj_equal(VALUE obj1, VALUE obj2)
{
return RBOOL(obj1 == obj2);
}
VALUE rb_obj_hash(VALUE obj);
/**
* call-seq:
* !obj -> true or false
*
* Boolean negate.
*--
* \private
*++
*/
VALUE
rb_obj_not(VALUE obj)
{
return RBOOL(!RTEST(obj));
}
/**
* call-seq:
* obj != other -> true or false
*
* Returns true if two objects are not-equal, otherwise false.
*--
* \private
*++
*/
VALUE
rb_obj_not_equal(VALUE obj1, VALUE obj2)
{
VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
return rb_obj_not(result);
}
VALUE
rb_class_real(VALUE cl)
{
while (cl &&
(RCLASS_SINGLETON_P(cl) || BUILTIN_TYPE(cl) == T_ICLASS)) {
cl = RCLASS_SUPER(cl);
}
return cl;
}
VALUE
rb_obj_class(VALUE obj)
{
return rb_class_real(CLASS_OF(obj));
}
/*
* call-seq:
* obj.singleton_class -> class
*
* Returns the singleton class of <i>obj</i>. This method creates
* a new singleton class if <i>obj</i> does not have one.
*
* If <i>obj</i> is <code>nil</code>, <code>true</code>, or
* <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
* respectively.
* If <i>obj</i> is an Integer, a Float or a Symbol, it raises a TypeError.
*
* Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>>
* String.singleton_class #=> #<Class:String>
* nil.singleton_class #=> NilClass
*/
static VALUE
rb_obj_singleton_class(VALUE obj)
{
return rb_singleton_class(obj);
}
/*! \private */
void
rb_obj_copy_ivar(VALUE dest, VALUE obj)
{
RUBY_ASSERT(!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE));
RUBY_ASSERT(BUILTIN_TYPE(dest) == BUILTIN_TYPE(obj));
rb_shape_t * src_shape = rb_shape_get_shape(obj);
if (rb_shape_obj_too_complex(obj)) {
// obj is TOO_COMPLEX so we can copy its iv_hash
st_table *table = st_copy(ROBJECT_IV_HASH(obj));
rb_obj_convert_to_too_complex(dest, table);
return;
}
uint32_t src_num_ivs = RBASIC_IV_COUNT(obj);
rb_shape_t * shape_to_set_on_dest = src_shape;
VALUE * src_buf;
VALUE * dest_buf;
if (!src_num_ivs) {
return;
}
// The copy should be mutable, so we don't want the frozen shape
if (rb_shape_frozen_shape_p(src_shape)) {
shape_to_set_on_dest = rb_shape_get_parent(src_shape);
}
src_buf = ROBJECT_IVPTR(obj);
dest_buf = ROBJECT_IVPTR(dest);
rb_shape_t * initial_shape = rb_shape_get_shape(dest);
if (initial_shape->size_pool_index != src_shape->size_pool_index) {
RUBY_ASSERT(initial_shape->type == SHAPE_T_OBJECT);
shape_to_set_on_dest = rb_shape_rebuild_shape(initial_shape, src_shape);
if (UNLIKELY(rb_shape_id(shape_to_set_on_dest) == OBJ_TOO_COMPLEX_SHAPE_ID)) {
st_table * table = rb_st_init_numtable_with_size(src_num_ivs);
rb_obj_copy_ivs_to_hash_table(obj, table);
rb_obj_convert_to_too_complex(dest, table);
return;
}
}
RUBY_ASSERT(src_num_ivs <= shape_to_set_on_dest->capacity || rb_shape_id(shape_to_set_on_dest) == OBJ_TOO_COMPLEX_SHAPE_ID);
if (initial_shape->capacity < shape_to_set_on_dest->capacity) {
rb_ensure_iv_list_size(dest, initial_shape->capacity, shape_to_set_on_dest->capacity);
dest_buf = ROBJECT_IVPTR(dest);
}
MEMCPY(dest_buf, src_buf, VALUE, src_num_ivs);
// Fire write barriers
for (uint32_t i = 0; i < src_num_ivs; i++) {
RB_OBJ_WRITTEN(dest, Qundef, dest_buf[i]);
}
rb_shape_set_shape(dest, shape_to_set_on_dest);
}
static void
init_copy(VALUE dest, VALUE obj)
{
if (OBJ_FROZEN(dest)) {
rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
}
RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
// Copies the shape id from obj to dest
RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR);
rb_gc_copy_attributes(dest, obj);
rb_copy_generic_ivar(dest, obj);
if (RB_TYPE_P(obj, T_OBJECT)) {
rb_obj_copy_ivar(dest, obj);
}
}
static VALUE immutable_obj_clone(VALUE obj, VALUE kwfreeze);
static VALUE mutable_obj_clone(VALUE obj, VALUE kwfreeze);
PUREFUNC(static inline int special_object_p(VALUE obj)); /*!< \private */
static inline int
special_object_p(VALUE obj)
{
if (SPECIAL_CONST_P(obj)) return TRUE;
switch (BUILTIN_TYPE(obj)) {
case T_BIGNUM:
case T_FLOAT:
case T_SYMBOL:
case T_RATIONAL:
case T_COMPLEX:
/* not a comprehensive list */
return TRUE;
default:
return FALSE;
}
}
static VALUE
obj_freeze_opt(VALUE freeze)
{
switch (freeze) {
case Qfalse:
case Qtrue:
case Qnil:
break;
default:
rb_raise(rb_eArgError, "unexpected value for freeze: %"PRIsVALUE, rb_obj_class(freeze));
}
return freeze;
}
static VALUE
rb_obj_clone2(rb_execution_context_t *ec, VALUE obj, VALUE freeze)
{
VALUE kwfreeze = obj_freeze_opt(freeze);
if (!special_object_p(obj))
return mutable_obj_clone(obj, kwfreeze);
return immutable_obj_clone(obj, kwfreeze);
}
/*! \private */
VALUE
rb_immutable_obj_clone(int argc, VALUE *argv, VALUE obj)
{
VALUE kwfreeze = rb_get_freeze_opt(argc, argv);
return immutable_obj_clone(obj, kwfreeze);
}
VALUE
rb_get_freeze_opt(int argc, VALUE *argv)
{
static ID keyword_ids[1];
VALUE opt;
VALUE kwfreeze = Qnil;
if (!keyword_ids[0]) {
CONST_ID(keyword_ids[0], "freeze");
}
rb_scan_args(argc, argv, "0:", &opt);
if (!NIL_P(opt)) {
rb_get_kwargs(opt, keyword_ids, 0, 1, &kwfreeze);
if (!UNDEF_P(kwfreeze))
kwfreeze = obj_freeze_opt(kwfreeze);
}
return kwfreeze;
}
static VALUE
immutable_obj_clone(VALUE obj, VALUE kwfreeze)
{
if (kwfreeze == Qfalse)
rb_raise(rb_eArgError, "can't unfreeze %"PRIsVALUE,
rb_obj_class(obj));
return obj;
}
VALUE
rb_obj_clone_setup(VALUE obj, VALUE clone, VALUE kwfreeze)
{
VALUE argv[2];
VALUE singleton = rb_singleton_class_clone_and_attach(obj, clone);
RBASIC_SET_CLASS(clone, singleton);
if (RCLASS_SINGLETON_P(singleton)) {
rb_singleton_class_attached(singleton, clone);
}
init_copy(clone, obj);
switch (kwfreeze) {
case Qnil:
rb_funcall(clone, id_init_clone, 1, obj);
RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
if (CHILLED_STRING_P(obj)) {
STR_CHILL_RAW(clone);
}
else if (RB_OBJ_FROZEN(obj)) {
rb_shape_t * next_shape = rb_shape_transition_shape_frozen(clone);
if (!rb_shape_obj_too_complex(clone) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
rb_evict_ivars_to_hash(clone);
}
else {
rb_shape_set_shape(clone, next_shape);
}
}
break;
case Qtrue: {
static VALUE freeze_true_hash;
if (!freeze_true_hash) {
freeze_true_hash = rb_hash_new();
rb_vm_register_global_object(freeze_true_hash);
rb_hash_aset(freeze_true_hash, ID2SYM(idFreeze), Qtrue);
rb_obj_freeze(freeze_true_hash);
}
argv[0] = obj;
argv[1] = freeze_true_hash;
rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
RBASIC(clone)->flags |= FL_FREEZE;
rb_shape_t * next_shape = rb_shape_transition_shape_frozen(clone);
// If we're out of shapes, but we want to freeze, then we need to
// evacuate this clone to a hash
if (!rb_shape_obj_too_complex(clone) && next_shape->type == SHAPE_OBJ_TOO_COMPLEX) {
rb_evict_ivars_to_hash(clone);
}
else {
rb_shape_set_shape(clone, next_shape);
}
break;
}
case Qfalse: {
static VALUE freeze_false_hash;
if (!freeze_false_hash) {
freeze_false_hash = rb_hash_new();
rb_vm_register_global_object(freeze_false_hash);
rb_hash_aset(freeze_false_hash, ID2SYM(idFreeze), Qfalse);
rb_obj_freeze(freeze_false_hash);
}
argv[0] = obj;
argv[1] = freeze_false_hash;
rb_funcallv_kw(clone, id_init_clone, 2, argv, RB_PASS_KEYWORDS);
break;
}
default:
rb_bug("invalid kwfreeze passed to mutable_obj_clone");
}
return clone;
}
static VALUE
mutable_obj_clone(VALUE obj, VALUE kwfreeze)
{
VALUE clone = rb_obj_alloc(rb_obj_class(obj));
return rb_obj_clone_setup(obj, clone, kwfreeze);
}
VALUE
rb_obj_clone(VALUE obj)
{
if (special_object_p(obj)) return obj;
return mutable_obj_clone(obj, Qnil);
}
VALUE
rb_obj_dup_setup(VALUE obj, VALUE dup)
{
init_copy(dup, obj);
rb_funcall(dup, id_init_dup, 1, obj);
return dup;
}
/*
* call-seq:
* obj.dup -> an_object
*
* Produces a shallow copy of <i>obj</i>---the instance variables of
* <i>obj</i> are copied, but not the objects they reference.
*
* This method may have class-specific behavior. If so, that
* behavior will be documented under the #+initialize_copy+ method of
* the class.
*
* === on dup vs clone
*
* In general, #clone and #dup may have different semantics in
* descendant classes. While #clone is used to duplicate an object,
* including its internal state, #dup typically uses the class of the
* descendant object to create the new instance.
*
* When using #dup, any modules that the object has been extended with will not
* be copied.
*
* class Klass
* attr_accessor :str
* end
*
* module Foo
* def foo; 'foo'; end
* end
*
* s1 = Klass.new #=> #<Klass:0x401b3a38>
* s1.extend(Foo) #=> #<Klass:0x401b3a38>
* s1.foo #=> "foo"
*
* s2 = s1.clone #=> #<Klass:0x401be280>
* s2.foo #=> "foo"
*
* s3 = s1.dup #=> #<Klass:0x401c1084>
* s3.foo #=> NoMethodError: undefined method `foo' for #<Klass:0x401c1084>
*/
VALUE
rb_obj_dup(VALUE obj)
{
VALUE dup;
if (special_object_p(obj)) {
return obj;
}
dup = rb_obj_alloc(rb_obj_class(obj));
return rb_obj_dup_setup(obj, dup);
}
/*
* call-seq:
* obj.itself -> obj
*
* Returns the receiver.
*
* string = "my string"
* string.itself.object_id == string.object_id #=> true
*
*/
static VALUE
rb_obj_itself(VALUE obj)
{
return obj;
}
VALUE
rb_obj_size(VALUE self, VALUE args, VALUE obj)
{
return LONG2FIX(1);
}
/**
* :nodoc:
*--
* Default implementation of `#initialize_copy`
* @param[in,out] obj the receiver being initialized
* @param[in] orig the object to be copied from.
*++
*/
VALUE
rb_obj_init_copy(VALUE obj, VALUE orig)
{
if (obj == orig) return obj;
rb_check_frozen(obj);
if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
rb_raise(rb_eTypeError, "initialize_copy should take same class object");
}
return obj;
}
/**
* :nodoc:
*--
* Default implementation of `#initialize_dup`
*
* @param[in,out] obj the receiver being initialized
* @param[in] orig the object to be dup from.
*++
**/
VALUE
rb_obj_init_dup_clone(VALUE obj, VALUE orig)
{
rb_funcall(obj, id_init_copy, 1, orig);
return obj;
}
/**
* :nodoc:
*--
* Default implementation of `#initialize_clone`
*
* @param[in] The number of arguments
* @param[in] The array of arguments
* @param[in] obj the receiver being initialized
*++
**/
static VALUE
rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
{
VALUE orig, opts;
if (rb_scan_args(argc, argv, "1:", &orig, &opts) < argc) {
/* Ignore a freeze keyword */
rb_get_freeze_opt(1, &opts);
}
rb_funcall(obj, id_init_copy, 1, orig);
return obj;
}
/*
* call-seq:
* obj.to_s -> string
*
* Returns a string representing <i>obj</i>. The default #to_s prints
* the object's class and an encoding of the object id. As a special
* case, the top-level object that is the initial execution context
* of Ruby programs returns ``main''.
*
*/
VALUE
rb_any_to_s(VALUE obj)
{
VALUE str;
VALUE cname = rb_class_name(CLASS_OF(obj));
str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
return str;
}
VALUE
rb_inspect(VALUE obj)
{
VALUE str = rb_obj_as_string(rb_funcallv(obj, id_inspect, 0, 0));
rb_encoding *enc = rb_default_internal_encoding();
if (enc == NULL) enc = rb_default_external_encoding();
if (!rb_enc_asciicompat(enc)) {
if (!rb_enc_str_asciionly_p(str))
return rb_str_escape(str);
return str;
}
if (rb_enc_get(str) != enc && !rb_enc_str_asciionly_p(str))
return rb_str_escape(str);
return str;
}
static int
inspect_i(ID id, VALUE value, st_data_t a)
{
VALUE str = (VALUE)a;
/* need not to show internal data */
if (CLASS_OF(value) == 0) return ST_CONTINUE;
if (!rb_is_instance_id(id)) return ST_CONTINUE;
if (RSTRING_PTR(str)[0] == '-') { /* first element */
RSTRING_PTR(str)[0] = '#';
rb_str_cat2(str, " ");
}
else {
rb_str_cat2(str, ", ");
}
rb_str_catf(str, "%"PRIsVALUE"=", rb_id2str(id));
rb_str_buf_append(str, rb_inspect(value));
return ST_CONTINUE;
}
static VALUE
inspect_obj(VALUE obj, VALUE str, int recur)
{
if (recur) {
rb_str_cat2(str, " ...");
}
else {
rb_ivar_foreach(obj, inspect_i, str);
}
rb_str_cat2(str, ">");
RSTRING_PTR(str)[0] = '#';
return str;
}
/*
* call-seq:
* obj.inspect -> string
*
* Returns a string containing a human-readable representation of <i>obj</i>.
* The default #inspect shows the object's class name, an encoding of
* its memory address, and a list of the instance variables and their
* values (by calling #inspect on each of them). User defined classes
* should override this method to provide a better representation of
* <i>obj</i>. When overriding this method, it should return a string
* whose encoding is compatible with the default external encoding.
*
* [ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
* Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
*
* class Foo
* end
* Foo.new.inspect #=> "#<Foo:0x0300c868>"
*
* class Bar
* def initialize
* @bar = 1
* end
* end
* Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
*/
static VALUE
rb_obj_inspect(VALUE obj)
{
if (rb_ivar_count(obj) > 0) {
VALUE str;
VALUE c = rb_class_name(CLASS_OF(obj));
str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
return rb_exec_recursive(inspect_obj, obj, str);
}
else {
return rb_any_to_s(obj);
}
}
static VALUE
class_or_module_required(VALUE c)
{
switch (OBJ_BUILTIN_TYPE(c)) {
case T_MODULE:
case T_CLASS:
case T_ICLASS:
break;
default:
rb_raise(rb_eTypeError, "class or module required");
}
return c;
}
static VALUE class_search_ancestor(VALUE cl, VALUE c);
/*
* call-seq:
* obj.instance_of?(class) -> true or false
*
* Returns <code>true</code> if <i>obj</i> is an instance of the given
* class. See also Object#kind_of?.
*
* class A; end
* class B < A; end
* class C < B; end
*
* b = B.new
* b.instance_of? A #=> false
* b.instance_of? B #=> true
* b.instance_of? C #=> false
*/
VALUE
rb_obj_is_instance_of(VALUE obj, VALUE c)
{
c = class_or_module_required(c);
return RBOOL(rb_obj_class(obj) == c);
}
// Returns whether c is a proper (c != cl) superclass of cl
// Both c and cl must be T_CLASS
static VALUE
class_search_class_ancestor(VALUE cl, VALUE c)
{
RUBY_ASSERT(RB_TYPE_P(c, T_CLASS));
RUBY_ASSERT(RB_TYPE_P(cl, T_CLASS));
size_t c_depth = RCLASS_SUPERCLASS_DEPTH(c);
size_t cl_depth = RCLASS_SUPERCLASS_DEPTH(cl);
VALUE *classes = RCLASS_SUPERCLASSES(cl);
// If c's inheritance chain is longer, it cannot be an ancestor
// We are checking for a proper superclass so don't check if they are equal
if (cl_depth <= c_depth)
return Qfalse;
// Otherwise check that c is in cl's inheritance chain
return RBOOL(classes[c_depth] == c);
}
/*
* call-seq:
* obj.is_a?(class) -> true or false
* obj.kind_of?(class) -> true or false
*
* Returns <code>true</code> if <i>class</i> is the class of
* <i>obj</i>, or if <i>class</i> is one of the superclasses of
* <i>obj</i> or modules included in <i>obj</i>.
*
* module M; end
* class A
* include M
* end
* class B < A; end
* class C < B; end
*
* b = B.new
* b.is_a? A #=> true
* b.is_a? B #=> true
* b.is_a? C #=> false
* b.is_a? M #=> true
*
* b.kind_of? A #=> true
* b.kind_of? B #=> true
* b.kind_of? C #=> false
* b.kind_of? M #=> true
*/
VALUE
rb_obj_is_kind_of(VALUE obj, VALUE c)
{
VALUE cl = CLASS_OF(obj);
RUBY_ASSERT(RB_TYPE_P(cl, T_CLASS));
// Fastest path: If the object's class is an exact match we know `c` is a
// class without checking type and can return immediately.
if (cl == c) return Qtrue;
// Note: YJIT needs this function to never allocate and never raise when
// `c` is a class or a module.
if (LIKELY(RB_TYPE_P(c, T_CLASS))) {
// Fast path: Both are T_CLASS
return class_search_class_ancestor(cl, c);
}
else if (RB_TYPE_P(c, T_ICLASS)) {
// First check if we inherit the includer
// If we do we can return true immediately
VALUE includer = RCLASS_INCLUDER(c);
if (cl == includer) return Qtrue;
// Usually includer is a T_CLASS here, except when including into an
// already included Module.
// If it is a class, attempt the fast class-to-class check and return
// true if there is a match.
if (RB_TYPE_P(includer, T_CLASS) && class_search_class_ancestor(cl, includer))
return Qtrue;
// We don't include the ICLASS directly, so must check if we inherit
// the module via another include
return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
}
else if (RB_TYPE_P(c, T_MODULE)) {
// Slow path: check each ancestor in the linked list and its method table
return RBOOL(class_search_ancestor(cl, RCLASS_ORIGIN(c)));
}
else {
rb_raise(rb_eTypeError, "class or module required");
UNREACHABLE_RETURN(Qfalse);
}
}
static VALUE
class_search_ancestor(VALUE cl, VALUE c)
{
while (cl) {
if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
return cl;
cl = RCLASS_SUPER(cl);
}
return 0;
}
/*! \private */
VALUE
rb_class_search_ancestor(VALUE cl, VALUE c)
{
cl = class_or_module_required(cl);
c = class_or_module_required(c);
return class_search_ancestor(cl, RCLASS_ORIGIN(c));
}
/*
* Document-method: inherited
*
* call-seq:
* inherited(subclass)
*
* Callback invoked whenever a subclass of the current class is created.
*
* Example:
*
* class Foo
* def self.inherited(subclass)
* puts "New subclass: #{subclass}"