00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "ruby/ruby.h"
00015 #include "ruby/st.h"
00016 #include "ruby/util.h"
00017 #include "ruby/encoding.h"
00018 #include <stdio.h>
00019 #include <errno.h>
00020 #include <ctype.h>
00021 #include <math.h>
00022 #include <float.h>
00023 #include "constant.h"
00024 #include "internal.h"
00025 #include "probes.h"
00026
00027 VALUE rb_cBasicObject;
00028 VALUE rb_mKernel;
00029 VALUE rb_cObject;
00030 VALUE rb_cModule;
00031 VALUE rb_cClass;
00032 VALUE rb_cData;
00033
00034 VALUE rb_cNilClass;
00035 VALUE rb_cTrueClass;
00036 VALUE rb_cFalseClass;
00037
00038 static ID id_eq, id_eql, id_match, id_inspect;
00039 static ID id_init_copy, id_init_clone, id_init_dup;
00040 static ID id_const_missing;
00041
00042 #define CLASS_OR_MODULE_P(obj) \
00043 (!SPECIAL_CONST_P(obj) && \
00044 (BUILTIN_TYPE(obj) == T_CLASS || BUILTIN_TYPE(obj) == T_MODULE))
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 VALUE
00056 rb_equal(VALUE obj1, VALUE obj2)
00057 {
00058 VALUE result;
00059
00060 if (obj1 == obj2) return Qtrue;
00061 result = rb_funcall(obj1, id_eq, 1, obj2);
00062 if (RTEST(result)) return Qtrue;
00063 return Qfalse;
00064 }
00065
00066 int
00067 rb_eql(VALUE obj1, VALUE obj2)
00068 {
00069 return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 VALUE
00109 rb_obj_equal(VALUE obj1, VALUE obj2)
00110 {
00111 if (obj1 == obj2) return Qtrue;
00112 return Qfalse;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 VALUE
00129 rb_obj_hash(VALUE obj)
00130 {
00131 VALUE oid = rb_obj_id(obj);
00132 #if SIZEOF_LONG == SIZEOF_VOIDP
00133 st_index_t index = NUM2LONG(oid);
00134 #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
00135 st_index_t index = NUM2LL(oid);
00136 #else
00137 # error not supported
00138 #endif
00139 st_index_t h = rb_hash_end(rb_hash_start(index));
00140 return LONG2FIX(h);
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150 VALUE
00151 rb_obj_not(VALUE obj)
00152 {
00153 return RTEST(obj) ? Qfalse : Qtrue;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163 VALUE
00164 rb_obj_not_equal(VALUE obj1, VALUE obj2)
00165 {
00166 VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
00167 return RTEST(result) ? Qfalse : Qtrue;
00168 }
00169
00170 VALUE
00171 rb_class_real(VALUE cl)
00172 {
00173 if (cl == 0)
00174 return 0;
00175 while ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS) {
00176 cl = RCLASS_SUPER(cl);
00177 }
00178 return cl;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 VALUE
00194 rb_obj_class(VALUE obj)
00195 {
00196 return rb_class_real(CLASS_OF(obj));
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 static VALUE
00217 rb_obj_singleton_class(VALUE obj)
00218 {
00219 return rb_singleton_class(obj);
00220 }
00221
00222 void
00223 rb_obj_copy_ivar(VALUE dest, VALUE obj)
00224 {
00225 if (!(RBASIC(dest)->flags & ROBJECT_EMBED) && ROBJECT_IVPTR(dest)) {
00226 xfree(ROBJECT_IVPTR(dest));
00227 ROBJECT(dest)->as.heap.ivptr = 0;
00228 ROBJECT(dest)->as.heap.numiv = 0;
00229 ROBJECT(dest)->as.heap.iv_index_tbl = 0;
00230 }
00231 if (RBASIC(obj)->flags & ROBJECT_EMBED) {
00232 MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
00233 RBASIC(dest)->flags |= ROBJECT_EMBED;
00234 }
00235 else {
00236 long len = ROBJECT(obj)->as.heap.numiv;
00237 VALUE *ptr = 0;
00238 if (len > 0) {
00239 ptr = ALLOC_N(VALUE, len);
00240 MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
00241 }
00242 ROBJECT(dest)->as.heap.ivptr = ptr;
00243 ROBJECT(dest)->as.heap.numiv = len;
00244 ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
00245 RBASIC(dest)->flags &= ~ROBJECT_EMBED;
00246 }
00247 }
00248
00249 static void
00250 init_copy(VALUE dest, VALUE obj)
00251 {
00252 if (OBJ_FROZEN(dest)) {
00253 rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
00254 }
00255 RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
00256 RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT|FL_UNTRUSTED);
00257 rb_copy_generic_ivar(dest, obj);
00258 rb_gc_copy_finalizer(dest, obj);
00259 switch (TYPE(obj)) {
00260 case T_OBJECT:
00261 rb_obj_copy_ivar(dest, obj);
00262 break;
00263 case T_CLASS:
00264 case T_MODULE:
00265 if (RCLASS_IV_TBL(dest)) {
00266 st_free_table(RCLASS_IV_TBL(dest));
00267 RCLASS_IV_TBL(dest) = 0;
00268 }
00269 if (RCLASS_CONST_TBL(dest)) {
00270 rb_free_const_table(RCLASS_CONST_TBL(dest));
00271 RCLASS_CONST_TBL(dest) = 0;
00272 }
00273 if (RCLASS_IV_TBL(obj)) {
00274 RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj));
00275 }
00276 break;
00277 }
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 VALUE
00305 rb_obj_clone(VALUE obj)
00306 {
00307 VALUE clone;
00308 VALUE singleton;
00309
00310 if (rb_special_const_p(obj)) {
00311 rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
00312 }
00313 clone = rb_obj_alloc(rb_obj_class(obj));
00314 singleton = rb_singleton_class_clone_and_attach(obj, clone);
00315 RBASIC(clone)->klass = singleton;
00316 if (FL_TEST(singleton, FL_SINGLETON)) {
00317 rb_singleton_class_attached(singleton, clone);
00318 }
00319 RBASIC(clone)->flags &= (FL_TAINT|FL_UNTRUSTED);
00320 RBASIC(clone)->flags |= RBASIC(obj)->flags & ~(FL_FREEZE|FL_FINALIZE);
00321 init_copy(clone, obj);
00322 rb_funcall(clone, id_init_clone, 1, obj);
00323 RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
00324
00325 return clone;
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 VALUE
00347 rb_obj_dup(VALUE obj)
00348 {
00349 VALUE dup;
00350
00351 if (rb_special_const_p(obj)) {
00352 rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
00353 }
00354 dup = rb_obj_alloc(rb_obj_class(obj));
00355 init_copy(dup, obj);
00356 rb_funcall(dup, id_init_dup, 1, obj);
00357
00358 return dup;
00359 }
00360
00361
00362 VALUE
00363 rb_obj_init_copy(VALUE obj, VALUE orig)
00364 {
00365 if (obj == orig) return obj;
00366 rb_check_frozen(obj);
00367 rb_check_trusted(obj);
00368 if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
00369 rb_raise(rb_eTypeError, "initialize_copy should take same class object");
00370 }
00371 return obj;
00372 }
00373
00374
00375 VALUE
00376 rb_obj_init_dup_clone(VALUE obj, VALUE orig)
00377 {
00378 rb_funcall(obj, id_init_copy, 1, orig);
00379 return obj;
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 VALUE
00393 rb_any_to_s(VALUE obj)
00394 {
00395 VALUE str;
00396 VALUE cname = rb_class_name(CLASS_OF(obj));
00397
00398 str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
00399 OBJ_INFECT(str, obj);
00400
00401 return str;
00402 }
00403
00404
00405
00406
00407
00408
00409
00410 VALUE
00411 rb_inspect(VALUE obj)
00412 {
00413 VALUE str = rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
00414 rb_encoding *ext = rb_default_external_encoding();
00415 if (!rb_enc_asciicompat(ext)) {
00416 if (!rb_enc_str_asciionly_p(str))
00417 rb_raise(rb_eEncCompatError, "inspected result must be ASCII only if default external encoding is ASCII incompatible");
00418 return str;
00419 }
00420 if (rb_enc_get(str) != ext && !rb_enc_str_asciionly_p(str))
00421 rb_raise(rb_eEncCompatError, "inspected result must be ASCII only or use the same encoding with default external");
00422 return str;
00423 }
00424
00425 static int
00426 inspect_i(st_data_t k, st_data_t v, st_data_t a)
00427 {
00428 ID id = (ID)k;
00429 VALUE value = (VALUE)v;
00430 VALUE str = (VALUE)a;
00431 VALUE str2;
00432 const char *ivname;
00433
00434
00435 if (CLASS_OF(value) == 0) return ST_CONTINUE;
00436 if (!rb_is_instance_id(id)) return ST_CONTINUE;
00437 if (RSTRING_PTR(str)[0] == '-') {
00438 RSTRING_PTR(str)[0] = '#';
00439 rb_str_cat2(str, " ");
00440 }
00441 else {
00442 rb_str_cat2(str, ", ");
00443 }
00444 ivname = rb_id2name(id);
00445 rb_str_cat2(str, ivname);
00446 rb_str_cat2(str, "=");
00447 str2 = rb_inspect(value);
00448 rb_str_append(str, str2);
00449 OBJ_INFECT(str, str2);
00450
00451 return ST_CONTINUE;
00452 }
00453
00454 static VALUE
00455 inspect_obj(VALUE obj, VALUE str, int recur)
00456 {
00457 if (recur) {
00458 rb_str_cat2(str, " ...");
00459 }
00460 else {
00461 rb_ivar_foreach(obj, inspect_i, str);
00462 }
00463 rb_str_cat2(str, ">");
00464 RSTRING_PTR(str)[0] = '#';
00465 OBJ_INFECT(str, obj);
00466
00467 return str;
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 static VALUE
00505 rb_obj_inspect(VALUE obj)
00506 {
00507 if (rb_ivar_count(obj) > 0) {
00508 VALUE str;
00509 VALUE c = rb_class_name(CLASS_OF(obj));
00510
00511 str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
00512 return rb_exec_recursive(inspect_obj, obj, str);
00513 }
00514 else {
00515 return rb_any_to_s(obj);
00516 }
00517 }
00518
00519 static VALUE
00520 class_or_module_required(VALUE c)
00521 {
00522 if (SPECIAL_CONST_P(c)) goto not_class;
00523 switch (BUILTIN_TYPE(c)) {
00524 case T_MODULE:
00525 case T_CLASS:
00526 case T_ICLASS:
00527 break;
00528
00529 default:
00530 not_class:
00531 rb_raise(rb_eTypeError, "class or module required");
00532 }
00533 return c;
00534 }
00535
00536 static VALUE class_search_ancestor(VALUE cl, VALUE c);
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555 VALUE
00556 rb_obj_is_instance_of(VALUE obj, VALUE c)
00557 {
00558 c = class_or_module_required(c);
00559 if (rb_obj_class(obj) == c) return Qtrue;
00560 return Qfalse;
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592 VALUE
00593 rb_obj_is_kind_of(VALUE obj, VALUE c)
00594 {
00595 VALUE cl = CLASS_OF(obj);
00596
00597 c = class_or_module_required(c);
00598 return class_search_ancestor(cl, RCLASS_ORIGIN(c)) ? Qtrue : Qfalse;
00599 }
00600
00601 static VALUE
00602 class_search_ancestor(VALUE cl, VALUE c)
00603 {
00604 while (cl) {
00605 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
00606 return cl;
00607 cl = RCLASS_SUPER(cl);
00608 }
00609 return 0;
00610 }
00611
00612 VALUE
00613 rb_class_search_ancestor(VALUE cl, VALUE c)
00614 {
00615 cl = class_or_module_required(cl);
00616 c = class_or_module_required(c);
00617 return class_search_ancestor(cl, RCLASS_ORIGIN(c));
00618 }
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635 VALUE
00636 rb_obj_tap(VALUE obj)
00637 {
00638 rb_yield(obj);
00639 return obj;
00640 }
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870 static VALUE
00871 rb_obj_dummy(void)
00872 {
00873 return Qnil;
00874 }
00875
00876
00877
00878
00879
00880
00881
00882
00883 VALUE
00884 rb_obj_tainted(VALUE obj)
00885 {
00886 if (OBJ_TAINTED(obj))
00887 return Qtrue;
00888 return Qfalse;
00889 }
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900 VALUE
00901 rb_obj_taint(VALUE obj)
00902 {
00903 rb_secure(4);
00904 if (!OBJ_TAINTED(obj)) {
00905 rb_check_frozen(obj);
00906 OBJ_TAINT(obj);
00907 }
00908 return obj;
00909 }
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919 VALUE
00920 rb_obj_untaint(VALUE obj)
00921 {
00922 rb_secure(3);
00923 if (OBJ_TAINTED(obj)) {
00924 rb_check_frozen(obj);
00925 FL_UNSET(obj, FL_TAINT);
00926 }
00927 return obj;
00928 }
00929
00930
00931
00932
00933
00934
00935
00936
00937 VALUE
00938 rb_obj_untrusted(VALUE obj)
00939 {
00940 if (OBJ_UNTRUSTED(obj))
00941 return Qtrue;
00942 return Qfalse;
00943 }
00944
00945
00946
00947
00948
00949
00950
00951
00952 VALUE
00953 rb_obj_untrust(VALUE obj)
00954 {
00955 rb_secure(4);
00956 if (!OBJ_UNTRUSTED(obj)) {
00957 rb_check_frozen(obj);
00958 OBJ_UNTRUST(obj);
00959 }
00960 return obj;
00961 }
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971 VALUE
00972 rb_obj_trust(VALUE obj)
00973 {
00974 rb_secure(3);
00975 if (OBJ_UNTRUSTED(obj)) {
00976 rb_check_frozen(obj);
00977 FL_UNSET(obj, FL_UNTRUSTED);
00978 }
00979 return obj;
00980 }
00981
00982 void
00983 rb_obj_infect(VALUE obj1, VALUE obj2)
00984 {
00985 OBJ_INFECT(obj1, obj2);
00986 }
00987
00988 static st_table *immediate_frozen_tbl = 0;
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011 VALUE
01012 rb_obj_freeze(VALUE obj)
01013 {
01014 if (!OBJ_FROZEN(obj)) {
01015 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
01016 rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
01017 }
01018 OBJ_FREEZE(obj);
01019 if (SPECIAL_CONST_P(obj)) {
01020 if (!immediate_frozen_tbl) {
01021 immediate_frozen_tbl = st_init_numtable();
01022 }
01023 st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
01024 }
01025 }
01026 return obj;
01027 }
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040 VALUE
01041 rb_obj_frozen_p(VALUE obj)
01042 {
01043 if (OBJ_FROZEN(obj)) return Qtrue;
01044 if (SPECIAL_CONST_P(obj)) {
01045 if (!immediate_frozen_tbl) return Qfalse;
01046 if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
01047 }
01048 return Qfalse;
01049 }
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068 static VALUE
01069 nil_to_i(VALUE obj)
01070 {
01071 return INT2FIX(0);
01072 }
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083 static VALUE
01084 nil_to_f(VALUE obj)
01085 {
01086 return DBL2NUM(0.0);
01087 }
01088
01089
01090
01091
01092
01093
01094
01095
01096 static VALUE
01097 nil_to_s(VALUE obj)
01098 {
01099 return rb_usascii_str_new(0, 0);
01100 }
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113 static VALUE
01114 nil_to_a(VALUE obj)
01115 {
01116 return rb_ary_new2(0);
01117 }
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130 static VALUE
01131 nil_to_h(VALUE obj)
01132 {
01133 return rb_hash_new();
01134 }
01135
01136
01137
01138
01139
01140
01141
01142
01143 static VALUE
01144 nil_inspect(VALUE obj)
01145 {
01146 return rb_usascii_str_new2("nil");
01147 }
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166 static VALUE
01167 true_to_s(VALUE obj)
01168 {
01169 return rb_usascii_str_new2("true");
01170 }
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181 static VALUE
01182 true_and(VALUE obj, VALUE obj2)
01183 {
01184 return RTEST(obj2)?Qtrue:Qfalse;
01185 }
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203 static VALUE
01204 true_or(VALUE obj, VALUE obj2)
01205 {
01206 return Qtrue;
01207 }
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219 static VALUE
01220 true_xor(VALUE obj, VALUE obj2)
01221 {
01222 return RTEST(obj2)?Qfalse:Qtrue;
01223 }
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243 static VALUE
01244 false_to_s(VALUE obj)
01245 {
01246 return rb_usascii_str_new2("false");
01247 }
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259 static VALUE
01260 false_and(VALUE obj, VALUE obj2)
01261 {
01262 return Qfalse;
01263 }
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275 static VALUE
01276 false_or(VALUE obj, VALUE obj2)
01277 {
01278 return RTEST(obj2)?Qtrue:Qfalse;
01279 }
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294 static VALUE
01295 false_xor(VALUE obj, VALUE obj2)
01296 {
01297 return RTEST(obj2)?Qtrue:Qfalse;
01298 }
01299
01300
01301
01302
01303
01304
01305
01306
01307 static VALUE
01308 rb_true(VALUE obj)
01309 {
01310 return Qtrue;
01311 }
01312
01313
01314
01315
01316
01317
01318
01319
01320
01321
01322 static VALUE
01323 rb_false(VALUE obj)
01324 {
01325 return Qfalse;
01326 }
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338 static VALUE
01339 rb_obj_match(VALUE obj1, VALUE obj2)
01340 {
01341 return Qnil;
01342 }
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352 static VALUE
01353 rb_obj_not_match(VALUE obj1, VALUE obj2)
01354 {
01355 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
01356 return RTEST(result) ? Qfalse : Qtrue;
01357 }
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378 static VALUE
01379 rb_obj_cmp(VALUE obj1, VALUE obj2)
01380 {
01381 if (obj1 == obj2 || rb_equal(obj1, obj2))
01382 return INT2FIX(0);
01383 return Qnil;
01384 }
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423 static VALUE
01424 rb_mod_to_s(VALUE klass)
01425 {
01426 ID id_defined_at;
01427 VALUE refined_class, defined_at;
01428
01429 if (FL_TEST(klass, FL_SINGLETON)) {
01430 VALUE s = rb_usascii_str_new2("#<Class:");
01431 VALUE v = rb_iv_get(klass, "__attached__");
01432
01433 if (CLASS_OR_MODULE_P(v)) {
01434 rb_str_append(s, rb_inspect(v));
01435 }
01436 else {
01437 rb_str_append(s, rb_any_to_s(v));
01438 }
01439 rb_str_cat2(s, ">");
01440
01441 return s;
01442 }
01443 refined_class = rb_refinement_module_get_refined_class(klass);
01444 if (!NIL_P(refined_class)) {
01445 VALUE s = rb_usascii_str_new2("#<refinement:");
01446
01447 rb_str_concat(s, rb_inspect(refined_class));
01448 rb_str_cat2(s, "@");
01449 CONST_ID(id_defined_at, "__defined_at__");
01450 defined_at = rb_attr_get(klass, id_defined_at);
01451 rb_str_concat(s, rb_inspect(defined_at));
01452 rb_str_cat2(s, ">");
01453 return s;
01454 }
01455 return rb_str_dup(rb_class_name(klass));
01456 }
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467 static VALUE
01468 rb_mod_freeze(VALUE mod)
01469 {
01470 rb_class_name(mod);
01471 return rb_obj_freeze(mod);
01472 }
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484 static VALUE
01485 rb_mod_eqq(VALUE mod, VALUE arg)
01486 {
01487 return rb_obj_is_kind_of(arg, mod);
01488 }
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502 VALUE
01503 rb_class_inherited_p(VALUE mod, VALUE arg)
01504 {
01505 VALUE start = mod;
01506
01507 if (mod == arg) return Qtrue;
01508 if (!CLASS_OR_MODULE_P(arg) && !RB_TYPE_P(arg, T_ICLASS)) {
01509 rb_raise(rb_eTypeError, "compared with non class/module");
01510 }
01511 arg = RCLASS_ORIGIN(arg);
01512 if (class_search_ancestor(mod, arg)) {
01513 return Qtrue;
01514 }
01515
01516 if (class_search_ancestor(arg, start)) {
01517 return Qfalse;
01518 }
01519 return Qnil;
01520 }
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533 static VALUE
01534 rb_mod_lt(VALUE mod, VALUE arg)
01535 {
01536 if (mod == arg) return Qfalse;
01537 return rb_class_inherited_p(mod, arg);
01538 }
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553 static VALUE
01554 rb_mod_ge(VALUE mod, VALUE arg)
01555 {
01556 if (!CLASS_OR_MODULE_P(arg)) {
01557 rb_raise(rb_eTypeError, "compared with non class/module");
01558 }
01559
01560 return rb_class_inherited_p(arg, mod);
01561 }
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574 static VALUE
01575 rb_mod_gt(VALUE mod, VALUE arg)
01576 {
01577 if (mod == arg) return Qfalse;
01578 return rb_mod_ge(mod, arg);
01579 }
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593 static VALUE
01594 rb_mod_cmp(VALUE mod, VALUE arg)
01595 {
01596 VALUE cmp;
01597
01598 if (mod == arg) return INT2FIX(0);
01599 if (!CLASS_OR_MODULE_P(arg)) {
01600 return Qnil;
01601 }
01602
01603 cmp = rb_class_inherited_p(mod, arg);
01604 if (NIL_P(cmp)) return Qnil;
01605 if (cmp) {
01606 return INT2FIX(-1);
01607 }
01608 return INT2FIX(1);
01609 }
01610
01611 static VALUE
01612 rb_module_s_alloc(VALUE klass)
01613 {
01614 VALUE mod = rb_module_new();
01615
01616 RBASIC(mod)->klass = klass;
01617 return mod;
01618 }
01619
01620 static VALUE
01621 rb_class_s_alloc(VALUE klass)
01622 {
01623 return rb_class_boot(0);
01624 }
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652 static VALUE
01653 rb_mod_initialize(VALUE module)
01654 {
01655 if (rb_block_given_p()) {
01656 rb_mod_module_exec(1, &module, module);
01657 }
01658 return Qnil;
01659 }
01660
01661
01662
01663
01664
01665
01666
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687
01688
01689
01690
01691 static VALUE
01692 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
01693 {
01694 VALUE super;
01695
01696 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
01697 rb_raise(rb_eTypeError, "already initialized class");
01698 }
01699 if (argc == 0) {
01700 super = rb_cObject;
01701 }
01702 else {
01703 rb_scan_args(argc, argv, "01", &super);
01704 rb_check_inheritable(super);
01705 if (super != rb_cBasicObject && !RCLASS_SUPER(super)) {
01706 rb_raise(rb_eTypeError, "can't inherit uninitialized class");
01707 }
01708 }
01709 RCLASS_SUPER(klass) = super;
01710 rb_make_metaclass(klass, RBASIC(super)->klass);
01711 rb_class_inherited(super, klass);
01712 rb_mod_initialize(klass);
01713
01714 return klass;
01715 }
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739 VALUE
01740 rb_obj_alloc(VALUE klass)
01741 {
01742 VALUE obj;
01743 rb_alloc_func_t allocator;
01744
01745 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
01746 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
01747 }
01748 if (FL_TEST(klass, FL_SINGLETON)) {
01749 rb_raise(rb_eTypeError, "can't create instance of singleton class");
01750 }
01751 allocator = rb_get_alloc_func(klass);
01752 if (!allocator) {
01753 rb_raise(rb_eTypeError, "allocator undefined for %"PRIsVALUE,
01754 klass);
01755 }
01756
01757 #if !defined(DTRACE_PROBES_DISABLED) || !DTRACE_PROBES_DISABLED
01758 if (RUBY_DTRACE_OBJECT_CREATE_ENABLED()) {
01759 const char * file = rb_sourcefile();
01760 RUBY_DTRACE_OBJECT_CREATE(rb_class2name(klass),
01761 file ? file : "",
01762 rb_sourceline());
01763 }
01764 #endif
01765
01766 obj = (*allocator)(klass);
01767
01768 if (rb_obj_class(obj) != rb_class_real(klass)) {
01769 rb_raise(rb_eTypeError, "wrong instance allocation");
01770 }
01771 return obj;
01772 }
01773
01774 static VALUE
01775 rb_class_allocate_instance(VALUE klass)
01776 {
01777 NEWOBJ_OF(obj, struct RObject, klass, T_OBJECT);
01778 return (VALUE)obj;
01779 }
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793 VALUE
01794 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
01795 {
01796 VALUE obj;
01797
01798 obj = rb_obj_alloc(klass);
01799 rb_obj_call_init(obj, argc, argv);
01800
01801 return obj;
01802 }
01803
01804
01805
01806
01807
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823 VALUE
01824 rb_class_superclass(VALUE klass)
01825 {
01826 VALUE super = RCLASS_SUPER(klass);
01827
01828 if (!super) {
01829 if (klass == rb_cBasicObject) return Qnil;
01830 rb_raise(rb_eTypeError, "uninitialized class");
01831 }
01832 while (RB_TYPE_P(super, T_ICLASS)) {
01833 super = RCLASS_SUPER(super);
01834 }
01835 if (!super) {
01836 return Qnil;
01837 }
01838 return super;
01839 }
01840
01841 VALUE
01842 rb_class_get_superclass(VALUE klass)
01843 {
01844 return RCLASS_SUPER(klass);
01845 }
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857 static VALUE
01858 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
01859 {
01860 int i;
01861
01862 for (i=0; i<argc; i++) {
01863 rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
01864 }
01865 return Qnil;
01866 }
01867
01868 VALUE
01869 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
01870 {
01871 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
01872 rb_warning("optional boolean argument is obsoleted");
01873 rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
01874 return Qnil;
01875 }
01876 return rb_mod_attr_reader(argc, argv, klass);
01877 }
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887 static VALUE
01888 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
01889 {
01890 int i;
01891
01892 for (i=0; i<argc; i++) {
01893 rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
01894 }
01895 return Qnil;
01896 }
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913 static VALUE
01914 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
01915 {
01916 int i;
01917
01918 for (i=0; i<argc; i++) {
01919 rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
01920 }
01921 return Qnil;
01922 }
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958 static VALUE
01959 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
01960 {
01961 VALUE name, recur;
01962 rb_encoding *enc;
01963 const char *pbeg, *p, *path, *pend;
01964 ID id;
01965 int nestable = 1;
01966
01967 if (argc == 1) {
01968 name = argv[0];
01969 recur = Qtrue;
01970 }
01971 else {
01972 rb_scan_args(argc, argv, "11", &name, &recur);
01973 }
01974
01975 if (SYMBOL_P(name)) {
01976 name = rb_sym_to_s(name);
01977 nestable = 0;
01978 }
01979
01980 name = rb_check_string_type(name);
01981 Check_Type(name, T_STRING);
01982
01983 enc = rb_enc_get(name);
01984 path = RSTRING_PTR(name);
01985
01986 if (!rb_enc_asciicompat(enc)) {
01987 rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
01988 }
01989
01990 pbeg = p = path;
01991 pend = path + RSTRING_LEN(name);
01992
01993 if (p >= pend || !*p) {
01994 wrong_name:
01995 rb_raise(rb_eNameError, "wrong constant name %"PRIsVALUE,
01996 QUOTE(name));
01997 }
01998
01999 if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
02000 if (!nestable) goto wrong_name;
02001 mod = rb_cObject;
02002 p += 2;
02003 pbeg = p;
02004 }
02005
02006 while (p < pend) {
02007 VALUE part;
02008 long len, beglen;
02009
02010 while (p < pend && *p != ':') p++;
02011
02012 if (pbeg == p) goto wrong_name;
02013
02014 id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
02015 beglen = pbeg-path;
02016
02017 if (p < pend && p[0] == ':') {
02018 if (!nestable) goto wrong_name;
02019 if (p + 2 >= pend || p[1] != ':') goto wrong_name;
02020 p += 2;
02021 pbeg = p;
02022 }
02023
02024 if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
02025 rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
02026 QUOTE(name));
02027 }
02028
02029 if (!id) {
02030 if (!ISUPPER(*pbeg) || !rb_enc_symname2_p(pbeg, len, enc)) {
02031 part = rb_str_subseq(name, beglen, len);
02032 rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
02033 QUOTE(part));
02034 }
02035 else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
02036 id = rb_intern3(pbeg, len, enc);
02037 }
02038 else {
02039 part = rb_str_subseq(name, beglen, len);
02040 rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"%"PRIsVALUE,
02041 rb_str_subseq(name, 0, beglen),
02042 QUOTE(part));
02043 }
02044 }
02045 if (!rb_is_const_id(id)) {
02046 rb_name_error(id, "wrong constant name %"PRIsVALUE,
02047 QUOTE_ID(id));
02048 }
02049 mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
02050 }
02051
02052 return mod;
02053 }
02054
02055
02056
02057
02058
02059
02060
02061
02062
02063
02064
02065
02066
02067 static VALUE
02068 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
02069 {
02070 ID id = rb_to_id(name);
02071
02072 if (!rb_is_const_id(id)) {
02073 rb_name_error(id, "wrong constant name %"PRIsVALUE,
02074 QUOTE_ID(id));
02075 }
02076 rb_const_set(mod, id, value);
02077 return value;
02078 }
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091
02092
02093
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103
02104
02105
02106
02107
02108
02109
02110
02111
02112
02113
02114
02115
02116
02117
02118
02119
02120 static VALUE
02121 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
02122 {
02123 VALUE name, recur;
02124 ID id;
02125
02126 if (argc == 1) {
02127 name = argv[0];
02128 recur = Qtrue;
02129 }
02130 else {
02131 rb_scan_args(argc, argv, "11", &name, &recur);
02132 }
02133 if (!(id = rb_check_id(&name))) {
02134 if (rb_is_const_name(name)) {
02135 return Qfalse;
02136 }
02137 else {
02138 rb_name_error_str(name, "wrong constant name %"PRIsVALUE,
02139 QUOTE(name));
02140 }
02141 }
02142 if (!rb_is_const_id(id)) {
02143 rb_name_error(id, "wrong constant name %"PRIsVALUE,
02144 QUOTE_ID(id));
02145 }
02146 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
02147 }
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169 static VALUE
02170 rb_obj_ivar_get(VALUE obj, VALUE iv)
02171 {
02172 ID id = rb_check_id(&iv);
02173
02174 if (!id) {
02175 if (rb_is_instance_name(iv)) {
02176 return Qnil;
02177 }
02178 else {
02179 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02180 QUOTE(iv));
02181 }
02182 }
02183 if (!rb_is_instance_id(id)) {
02184 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02185 QUOTE_ID(id));
02186 }
02187 return rb_ivar_get(obj, id);
02188 }
02189
02190
02191
02192
02193
02194
02195
02196
02197
02198
02199
02200
02201
02202
02203
02204
02205
02206
02207
02208
02209
02210 static VALUE
02211 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
02212 {
02213 ID id = rb_to_id(iv);
02214
02215 if (!rb_is_instance_id(id)) {
02216 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02217 QUOTE_ID(id));
02218 }
02219 return rb_ivar_set(obj, id, val);
02220 }
02221
02222
02223
02224
02225
02226
02227
02228
02229
02230
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240 static VALUE
02241 rb_obj_ivar_defined(VALUE obj, VALUE iv)
02242 {
02243 ID id = rb_check_id(&iv);
02244
02245 if (!id) {
02246 if (rb_is_instance_name(iv)) {
02247 return Qfalse;
02248 }
02249 else {
02250 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02251 QUOTE(iv));
02252 }
02253 }
02254 if (!rb_is_instance_id(id)) {
02255 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as an instance variable name",
02256 QUOTE_ID(id));
02257 }
02258 return rb_ivar_defined(obj, id);
02259 }
02260
02261
02262
02263
02264
02265
02266
02267
02268
02269
02270
02271
02272
02273
02274
02275 static VALUE
02276 rb_mod_cvar_get(VALUE obj, VALUE iv)
02277 {
02278 ID id = rb_check_id(&iv);
02279
02280 if (!id) {
02281 if (rb_is_class_name(iv)) {
02282 rb_name_error_str(iv, "uninitialized class variable %"PRIsVALUE" in %"PRIsVALUE"",
02283 iv, rb_class_name(obj));
02284 }
02285 else {
02286 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
02287 QUOTE(iv));
02288 }
02289 }
02290 if (!rb_is_class_id(id)) {
02291 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02292 QUOTE_ID(id));
02293 }
02294 return rb_cvar_get(obj, id);
02295 }
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305
02306
02307
02308
02309
02310
02311
02312
02313
02314 static VALUE
02315 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
02316 {
02317 ID id = rb_to_id(iv);
02318
02319 if (!rb_is_class_id(id)) {
02320 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02321 QUOTE_ID(id));
02322 }
02323 rb_cvar_set(obj, id, val);
02324 return val;
02325 }
02326
02327
02328
02329
02330
02331
02332
02333
02334
02335
02336
02337
02338
02339
02340
02341 static VALUE
02342 rb_mod_cvar_defined(VALUE obj, VALUE iv)
02343 {
02344 ID id = rb_check_id(&iv);
02345
02346 if (!id) {
02347 if (rb_is_class_name(iv)) {
02348 return Qfalse;
02349 }
02350 else {
02351 rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as a class variable name",
02352 QUOTE(iv));
02353 }
02354 }
02355 if (!rb_is_class_id(id)) {
02356 rb_name_error(id, "`%"PRIsVALUE"' is not allowed as a class variable name",
02357 QUOTE_ID(id));
02358 }
02359 return rb_cvar_defined(obj, id);
02360 }
02361
02362 static struct conv_method_tbl {
02363 const char *method;
02364 ID id;
02365 } conv_method_names[] = {
02366 {"to_int", 0},
02367 {"to_ary", 0},
02368 {"to_str", 0},
02369 {"to_sym", 0},
02370 {"to_hash", 0},
02371 {"to_proc", 0},
02372 {"to_io", 0},
02373 {"to_a", 0},
02374 {"to_s", 0},
02375 {NULL, 0}
02376 };
02377 #define IMPLICIT_CONVERSIONS 7
02378
02379 static VALUE
02380 convert_type(VALUE val, const char *tname, const char *method, int raise)
02381 {
02382 ID m = 0;
02383 int i;
02384 VALUE r;
02385
02386 for (i=0; conv_method_names[i].method; i++) {
02387 if (conv_method_names[i].method[0] == method[0] &&
02388 strcmp(conv_method_names[i].method, method) == 0) {
02389 m = conv_method_names[i].id;
02390 break;
02391 }
02392 }
02393 if (!m) m = rb_intern(method);
02394 r = rb_check_funcall(val, m, 0, 0);
02395 if (r == Qundef) {
02396 if (raise) {
02397 const char *msg = i < IMPLICIT_CONVERSIONS ?
02398 "no implicit conversion of" : "can't convert";
02399 const char *cname = NIL_P(val) ? "nil" :
02400 val == Qtrue ? "true" :
02401 val == Qfalse ? "false" :
02402 NULL;
02403 if (cname)
02404 rb_raise(rb_eTypeError, "%s %s into %s", msg, cname, tname);
02405 rb_raise(rb_eTypeError, "%s %"PRIsVALUE" into %s", msg,
02406 rb_obj_class(val),
02407 tname);
02408 }
02409 return Qnil;
02410 }
02411 return r;
02412 }
02413
02414 NORETURN(static void conversion_mismatch(VALUE, const char *, const char *, VALUE));
02415 static void
02416 conversion_mismatch(VALUE val, const char *tname, const char *method, VALUE result)
02417 {
02418 VALUE cname = rb_obj_class(val);
02419 rb_raise(rb_eTypeError,
02420 "can't convert %"PRIsVALUE" to %s (%"PRIsVALUE"#%s gives %"PRIsVALUE")",
02421 cname, tname, cname, method, rb_obj_class(result));
02422 }
02423
02424 VALUE
02425 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
02426 {
02427 VALUE v;
02428
02429 if (TYPE(val) == type) return val;
02430 v = convert_type(val, tname, method, TRUE);
02431 if (TYPE(v) != type) {
02432 conversion_mismatch(val, tname, method, v);
02433 }
02434 return v;
02435 }
02436
02437 VALUE
02438 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
02439 {
02440 VALUE v;
02441
02442
02443 if (TYPE(val) == type && type != T_DATA) return val;
02444 v = convert_type(val, tname, method, FALSE);
02445 if (NIL_P(v)) return Qnil;
02446 if (TYPE(v) != type) {
02447 conversion_mismatch(val, tname, method, v);
02448 }
02449 return v;
02450 }
02451
02452
02453 static VALUE
02454 rb_to_integer(VALUE val, const char *method)
02455 {
02456 VALUE v;
02457
02458 if (FIXNUM_P(val)) return val;
02459 if (RB_TYPE_P(val, T_BIGNUM)) return val;
02460 v = convert_type(val, "Integer", method, TRUE);
02461 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02462 conversion_mismatch(val, "Integer", method, v);
02463 }
02464 return v;
02465 }
02466
02467 VALUE
02468 rb_check_to_integer(VALUE val, const char *method)
02469 {
02470 VALUE v;
02471
02472 if (FIXNUM_P(val)) return val;
02473 if (RB_TYPE_P(val, T_BIGNUM)) return val;
02474 v = convert_type(val, "Integer", method, FALSE);
02475 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02476 return Qnil;
02477 }
02478 return v;
02479 }
02480
02481 VALUE
02482 rb_to_int(VALUE val)
02483 {
02484 return rb_to_integer(val, "to_int");
02485 }
02486
02487 VALUE
02488 rb_check_to_int(VALUE val)
02489 {
02490 return rb_check_to_integer(val, "to_int");
02491 }
02492
02493 static VALUE
02494 rb_convert_to_integer(VALUE val, int base)
02495 {
02496 VALUE tmp;
02497
02498 switch (TYPE(val)) {
02499 case T_FLOAT:
02500 if (base != 0) goto arg_error;
02501 if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
02502 && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
02503 break;
02504 }
02505 return rb_dbl2big(RFLOAT_VALUE(val));
02506
02507 case T_FIXNUM:
02508 case T_BIGNUM:
02509 if (base != 0) goto arg_error;
02510 return val;
02511
02512 case T_STRING:
02513 string_conv:
02514 return rb_str_to_inum(val, base, TRUE);
02515
02516 case T_NIL:
02517 if (base != 0) goto arg_error;
02518 rb_raise(rb_eTypeError, "can't convert nil into Integer");
02519 break;
02520
02521 default:
02522 break;
02523 }
02524 if (base != 0) {
02525 tmp = rb_check_string_type(val);
02526 if (!NIL_P(tmp)) goto string_conv;
02527 arg_error:
02528 rb_raise(rb_eArgError, "base specified for non string value");
02529 }
02530 tmp = convert_type(val, "Integer", "to_int", FALSE);
02531 if (NIL_P(tmp)) {
02532 return rb_to_integer(val, "to_i");
02533 }
02534 return tmp;
02535
02536 }
02537
02538 VALUE
02539 rb_Integer(VALUE val)
02540 {
02541 return rb_convert_to_integer(val, 0);
02542 }
02543
02544
02545
02546
02547
02548
02549
02550
02551
02552
02553
02554
02555
02556
02557
02558
02559
02560
02561
02562
02563
02564
02565
02566
02567
02568 static VALUE
02569 rb_f_integer(int argc, VALUE *argv, VALUE obj)
02570 {
02571 VALUE arg = Qnil;
02572 int base = 0;
02573
02574 switch (argc) {
02575 case 2:
02576 base = NUM2INT(argv[1]);
02577 case 1:
02578 arg = argv[0];
02579 break;
02580 default:
02581
02582 rb_scan_args(argc, argv, "11", NULL, NULL);
02583 }
02584 return rb_convert_to_integer(arg, base);
02585 }
02586
02587 double
02588 rb_cstr_to_dbl(const char *p, int badcheck)
02589 {
02590 const char *q;
02591 char *end;
02592 double d;
02593 const char *ellipsis = "";
02594 int w;
02595 enum {max_width = 20};
02596 #define OutOfRange() ((end - p > max_width) ? \
02597 (w = max_width, ellipsis = "...") : \
02598 (w = (int)(end - p), ellipsis = ""))
02599
02600 if (!p) return 0.0;
02601 q = p;
02602 while (ISSPACE(*p)) p++;
02603
02604 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02605 return 0.0;
02606 }
02607
02608 d = strtod(p, &end);
02609 if (errno == ERANGE) {
02610 OutOfRange();
02611 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02612 errno = 0;
02613 }
02614 if (p == end) {
02615 if (badcheck) {
02616 bad:
02617 rb_invalid_str(q, "Float()");
02618 }
02619 return d;
02620 }
02621 if (*end) {
02622 char buf[DBL_DIG * 4 + 10];
02623 char *n = buf;
02624 char *e = buf + sizeof(buf) - 1;
02625 char prev = 0;
02626
02627 while (p < end && n < e) prev = *n++ = *p++;
02628 while (*p) {
02629 if (*p == '_') {
02630
02631 if (badcheck) {
02632 if (n == buf || !ISDIGIT(prev)) goto bad;
02633 ++p;
02634 if (!ISDIGIT(*p)) goto bad;
02635 }
02636 else {
02637 while (*++p == '_');
02638 continue;
02639 }
02640 }
02641 prev = *p++;
02642 if (n < e) *n++ = prev;
02643 }
02644 *n = '\0';
02645 p = buf;
02646
02647 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02648 return 0.0;
02649 }
02650
02651 d = strtod(p, &end);
02652 if (errno == ERANGE) {
02653 OutOfRange();
02654 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02655 errno = 0;
02656 }
02657 if (badcheck) {
02658 if (!end || p == end) goto bad;
02659 while (*end && ISSPACE(*end)) end++;
02660 if (*end) goto bad;
02661 }
02662 }
02663 if (errno == ERANGE) {
02664 errno = 0;
02665 OutOfRange();
02666 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
02667 }
02668 return d;
02669 }
02670
02671 double
02672 rb_str_to_dbl(VALUE str, int badcheck)
02673 {
02674 char *s;
02675 long len;
02676 double ret;
02677 VALUE v = 0;
02678
02679 StringValue(str);
02680 s = RSTRING_PTR(str);
02681 len = RSTRING_LEN(str);
02682 if (s) {
02683 if (badcheck && memchr(s, '\0', len)) {
02684 rb_raise(rb_eArgError, "string for Float contains null byte");
02685 }
02686 if (s[len]) {
02687 char *p = ALLOCV(v, len);
02688 MEMCPY(p, s, char, len);
02689 p[len] = '\0';
02690 s = p;
02691 }
02692 }
02693 ret = rb_cstr_to_dbl(s, badcheck);
02694 if (v)
02695 ALLOCV_END(v);
02696 return ret;
02697 }
02698
02699 VALUE
02700 rb_Float(VALUE val)
02701 {
02702 switch (TYPE(val)) {
02703 case T_FIXNUM:
02704 return DBL2NUM((double)FIX2LONG(val));
02705
02706 case T_FLOAT:
02707 return val;
02708
02709 case T_BIGNUM:
02710 return DBL2NUM(rb_big2dbl(val));
02711
02712 case T_STRING:
02713 return DBL2NUM(rb_str_to_dbl(val, TRUE));
02714
02715 case T_NIL:
02716 rb_raise(rb_eTypeError, "can't convert nil into Float");
02717 break;
02718
02719 default:
02720 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02721 }
02722
02723 UNREACHABLE;
02724 }
02725
02726
02727
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737
02738 static VALUE
02739 rb_f_float(VALUE obj, VALUE arg)
02740 {
02741 return rb_Float(arg);
02742 }
02743
02744 VALUE
02745 rb_to_float(VALUE val)
02746 {
02747 if (RB_TYPE_P(val, T_FLOAT)) return val;
02748 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02749 rb_raise(rb_eTypeError, "can't convert %s into Float",
02750 NIL_P(val) ? "nil" :
02751 val == Qtrue ? "true" :
02752 val == Qfalse ? "false" :
02753 rb_obj_classname(val));
02754 }
02755 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02756 }
02757
02758 VALUE
02759 rb_check_to_float(VALUE val)
02760 {
02761 if (RB_TYPE_P(val, T_FLOAT)) return val;
02762 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02763 return Qnil;
02764 }
02765 return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
02766 }
02767
02768 double
02769 rb_num2dbl(VALUE val)
02770 {
02771 switch (TYPE(val)) {
02772 case T_FLOAT:
02773 return RFLOAT_VALUE(val);
02774
02775 case T_STRING:
02776 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
02777 break;
02778
02779 case T_NIL:
02780 rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
02781 break;
02782
02783 default:
02784 break;
02785 }
02786
02787 return RFLOAT_VALUE(rb_Float(val));
02788 }
02789
02790 VALUE
02791 rb_String(VALUE val)
02792 {
02793 VALUE tmp = rb_check_string_type(val);
02794 if (NIL_P(tmp))
02795 tmp = rb_convert_type(val, T_STRING, "String", "to_s");
02796 return tmp;
02797 }
02798
02799
02800
02801
02802
02803
02804
02805
02806
02807
02808
02809
02810
02811
02812
02813 static VALUE
02814 rb_f_string(VALUE obj, VALUE arg)
02815 {
02816 return rb_String(arg);
02817 }
02818
02819 VALUE
02820 rb_Array(VALUE val)
02821 {
02822 VALUE tmp = rb_check_array_type(val);
02823
02824 if (NIL_P(tmp)) {
02825 tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
02826 if (NIL_P(tmp)) {
02827 return rb_ary_new3(1, val);
02828 }
02829 }
02830 return tmp;
02831 }
02832
02833
02834
02835
02836
02837
02838
02839
02840
02841
02842
02843
02844 static VALUE
02845 rb_f_array(VALUE obj, VALUE arg)
02846 {
02847 return rb_Array(arg);
02848 }
02849
02850 VALUE
02851 rb_Hash(VALUE val)
02852 {
02853 VALUE tmp;
02854
02855 if (NIL_P(val)) return rb_hash_new();
02856 tmp = rb_check_hash_type(val);
02857 if (NIL_P(tmp)) {
02858 if (RB_TYPE_P(val, T_ARRAY) && RARRAY_LEN(val) == 0)
02859 return rb_hash_new();
02860 rb_raise(rb_eTypeError, "can't convert %s into Hash", rb_obj_classname(val));
02861 }
02862 return tmp;
02863 }
02864
02865
02866
02867
02868
02869
02870
02871
02872
02873
02874
02875
02876
02877
02878
02879 static VALUE
02880 rb_f_hash(VALUE obj, VALUE arg)
02881 {
02882 return rb_Hash(arg);
02883 }
02884
02885
02886
02887
02888
02889
02890
02891
02892
02893
02894
02895
02896
02897
02898
02899
02900
02901
02902
02903
02904
02905
02906
02907
02908
02909
02910
02911
02912
02913
02914
02915
02916
02917
02918
02919
02920
02921
02922
02923
02924
02925
02926
02927
02928
02929
02930
02931
02932
02933
02934
02935
02936
02937
02938
02939
02940
02941
02942
02943
02944
02945
02946
02947
02948
02949
02968
02969
02970
02971
02972
02973
02974
02975
02976
02977
02978
02979
02980
02981
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992
02993
02994
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005
03006
03007
03008
03009
03010
03011
03012
03013
03014
03015
03016
03017
03018
03019
03020
03021
03022
03023
03024
03025
03026
03027
03028
03029
03030
03031
03032
03033
03034
03035
03036
03037
03038
03039
03040 void
03041 Init_Object(void)
03042 {
03043 int i;
03044
03045 Init_class_hierarchy();
03046
03047 #if 0
03048
03049 rb_cBasicObject = rb_define_class("BasicObject", Qnil);
03050 rb_cObject = rb_define_class("Object", rb_cBasicObject);
03051 rb_cModule = rb_define_class("Module", rb_cObject);
03052 rb_cClass = rb_define_class("Class", rb_cModule);
03053 #endif
03054
03055 #undef rb_intern
03056 #define rb_intern(str) rb_intern_const(str)
03057
03058 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
03059 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
03060 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
03061 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
03062 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
03063 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
03064
03065 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
03066 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
03067 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
03068
03069
03070
03071
03072
03073
03074
03075
03076
03077
03078
03079
03080
03081 rb_mKernel = rb_define_module("Kernel");
03082 rb_include_module(rb_cObject, rb_mKernel);
03083 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
03084 rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
03085 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
03086 rb_define_private_method(rb_cModule, "prepended", rb_obj_dummy, 1);
03087 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
03088 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
03089 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
03090
03091 rb_define_method(rb_mKernel, "nil?", rb_false, 0);
03092 rb_define_method(rb_mKernel, "===", rb_equal, 1);
03093 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
03094 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
03095 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
03096 rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0);
03097 rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
03098
03099 rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
03100 rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
03101 rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
03102 rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
03103 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
03104 rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
03105 rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
03106
03107 rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
03108 rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
03109 rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
03110 rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
03111 rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
03112 rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0);
03113 rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
03114 rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
03115
03116 rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
03117 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
03118 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1);
03119 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1);
03120 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1);
03121 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1);
03122 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1);
03123 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0);
03124 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
03125 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
03126 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
03127 rb_define_method(rb_mKernel, "remove_instance_variable",
03128 rb_obj_remove_instance_variable, 1);
03129
03130 rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
03131 rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
03132 rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
03133 rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
03134
03135 rb_define_global_function("sprintf", rb_f_sprintf, -1);
03136 rb_define_global_function("format", rb_f_sprintf, -1);
03137
03138 rb_define_global_function("Integer", rb_f_integer, -1);
03139 rb_define_global_function("Float", rb_f_float, 1);
03140
03141 rb_define_global_function("String", rb_f_string, 1);
03142 rb_define_global_function("Array", rb_f_array, 1);
03143 rb_define_global_function("Hash", rb_f_hash, 1);
03144
03145 rb_cNilClass = rb_define_class("NilClass", rb_cObject);
03146 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
03147 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
03148 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
03149 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
03150 rb_define_method(rb_cNilClass, "to_h", nil_to_h, 0);
03151 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
03152 rb_define_method(rb_cNilClass, "&", false_and, 1);
03153 rb_define_method(rb_cNilClass, "|", false_or, 1);
03154 rb_define_method(rb_cNilClass, "^", false_xor, 1);
03155
03156 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
03157 rb_undef_alloc_func(rb_cNilClass);
03158 rb_undef_method(CLASS_OF(rb_cNilClass), "new");
03159
03160
03161
03162 rb_define_global_const("NIL", Qnil);
03163
03164 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
03165 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
03166 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
03167 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
03168 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
03169 rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
03170 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
03171 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
03172 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1);
03173 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
03174 rb_define_alias(rb_cModule, "inspect", "to_s");
03175 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0);
03176 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1);
03177 rb_define_method(rb_cModule, "name", rb_mod_name, 0);
03178 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0);
03179
03180 rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
03181 rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
03182 rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
03183 rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
03184
03185 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
03186 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
03187 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1);
03188 rb_define_method(rb_cModule, "public_instance_methods",
03189 rb_class_public_instance_methods, -1);
03190 rb_define_method(rb_cModule, "protected_instance_methods",
03191 rb_class_protected_instance_methods, -1);
03192 rb_define_method(rb_cModule, "private_instance_methods",
03193 rb_class_private_instance_methods, -1);
03194
03195 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1);
03196 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
03197 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
03198 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
03199 rb_define_private_method(rb_cModule, "remove_const",
03200 rb_mod_remove_const, 1);
03201 rb_define_method(rb_cModule, "const_missing",
03202 rb_mod_const_missing, 1);
03203 rb_define_method(rb_cModule, "class_variables",
03204 rb_mod_class_variables, -1);
03205 rb_define_method(rb_cModule, "remove_class_variable",
03206 rb_mod_remove_cvar, 1);
03207 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
03208 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
03209 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
03210 rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1);
03211 rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1);
03212
03213 rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
03214 rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
03215 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
03216 rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
03217 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
03218 rb_undef_method(rb_cClass, "extend_object");
03219 rb_undef_method(rb_cClass, "append_features");
03220 rb_undef_method(rb_cClass, "prepend_features");
03221
03222
03223
03224
03225
03226
03227
03228 rb_cData = rb_define_class("Data", rb_cObject);
03229 rb_undef_alloc_func(rb_cData);
03230
03231 rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
03232 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
03233 rb_define_alias(rb_cTrueClass, "inspect", "to_s");
03234 rb_define_method(rb_cTrueClass, "&", true_and, 1);
03235 rb_define_method(rb_cTrueClass, "|", true_or, 1);
03236 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
03237 rb_undef_alloc_func(rb_cTrueClass);
03238 rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
03239
03240
03241
03242 rb_define_global_const("TRUE", Qtrue);
03243
03244 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
03245 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
03246 rb_define_alias(rb_cFalseClass, "inspect", "to_s");
03247 rb_define_method(rb_cFalseClass, "&", false_and, 1);
03248 rb_define_method(rb_cFalseClass, "|", false_or, 1);
03249 rb_define_method(rb_cFalseClass, "^", false_xor, 1);
03250 rb_undef_alloc_func(rb_cFalseClass);
03251 rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
03252
03253
03254
03255 rb_define_global_const("FALSE", Qfalse);
03256
03257 id_eq = rb_intern("==");
03258 id_eql = rb_intern("eql?");
03259 id_match = rb_intern("=~");
03260 id_inspect = rb_intern("inspect");
03261 id_init_copy = rb_intern("initialize_copy");
03262 id_init_clone = rb_intern("initialize_clone");
03263 id_init_dup = rb_intern("initialize_dup");
03264 id_const_missing = rb_intern("const_missing");
03265
03266 for (i=0; conv_method_names[i].method; i++) {
03267 conv_method_names[i].id = rb_intern(conv_method_names[i].method);
03268 }
03269 }
03270