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