00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "ruby.h"
00016 #include "st.h"
00017 #include "util.h"
00018 #include <stdio.h>
00019 #include <errno.h>
00020 #include <ctype.h>
00021 #include <math.h>
00022
00023 VALUE rb_mKernel;
00024 VALUE rb_cObject;
00025 VALUE rb_cModule;
00026 VALUE rb_cClass;
00027 VALUE rb_cData;
00028
00029 VALUE rb_cNilClass;
00030 VALUE rb_cTrueClass;
00031 VALUE rb_cFalseClass;
00032 VALUE rb_cSymbol;
00033
00034 static ID id_eq, id_eql, id_inspect, id_init_copy;
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 VALUE
00046 rb_equal(obj1, obj2)
00047 VALUE obj1, obj2;
00048 {
00049 VALUE result;
00050
00051 if (obj1 == obj2) return Qtrue;
00052 result = rb_funcall(obj1, id_eq, 1, obj2);
00053 if (RTEST(result)) return Qtrue;
00054 return Qfalse;
00055 }
00056
00057 int
00058 rb_eql(obj1, obj2)
00059 VALUE obj1, obj2;
00060 {
00061 return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 static VALUE
00094 rb_obj_equal(obj1, obj2)
00095 VALUE obj1, obj2;
00096 {
00097 if (obj1 == obj2) return Qtrue;
00098 return Qfalse;
00099 }
00100
00101
00102
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 VALUE
00132 rb_obj_id(obj)
00133 VALUE obj;
00134 {
00135 if (SPECIAL_CONST_P(obj)) {
00136 return LONG2NUM((long)obj);
00137 }
00138 return (VALUE)((long)obj|FIXNUM_FLAG);
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 VALUE
00149 rb_obj_id_obsolete(obj)
00150 VALUE obj;
00151 {
00152 rb_warn("Object#id will be deprecated; use Object#object_id");
00153 return rb_obj_id(obj);
00154 }
00155
00156 VALUE
00157 rb_class_real(cl)
00158 VALUE cl;
00159 {
00160 while (FL_TEST(cl, FL_SINGLETON) || TYPE(cl) == T_ICLASS) {
00161 cl = RCLASS(cl)->super;
00162 }
00163 return cl;
00164 }
00165
00166
00167
00168
00169
00170
00171
00172
00173 VALUE
00174 rb_obj_type(obj)
00175 VALUE obj;
00176 {
00177 rb_warn("Object#type is deprecated; use Object#class");
00178 return rb_class_real(CLASS_OF(obj));
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 VALUE
00197 rb_obj_class(obj)
00198 VALUE obj;
00199 {
00200 return rb_class_real(CLASS_OF(obj));
00201 }
00202
00203 static void
00204 init_copy(dest, obj)
00205 VALUE dest, obj;
00206 {
00207 if (OBJ_FROZEN(dest)) {
00208 rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
00209 }
00210 RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
00211 RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT);
00212 if (FL_TEST(obj, FL_EXIVAR)) {
00213 rb_copy_generic_ivar(dest, obj);
00214 }
00215 rb_gc_copy_finalizer(dest, obj);
00216 switch (TYPE(obj)) {
00217 case T_OBJECT:
00218 case T_CLASS:
00219 case T_MODULE:
00220 if (ROBJECT(dest)->iv_tbl) {
00221 st_free_table(ROBJECT(dest)->iv_tbl);
00222 ROBJECT(dest)->iv_tbl = 0;
00223 }
00224 if (ROBJECT(obj)->iv_tbl) {
00225 ROBJECT(dest)->iv_tbl = st_copy(ROBJECT(obj)->iv_tbl);
00226 }
00227 }
00228 rb_funcall(dest, id_init_copy, 1, obj);
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 VALUE
00256 rb_obj_clone(obj)
00257 VALUE obj;
00258 {
00259 VALUE clone;
00260
00261 if (rb_special_const_p(obj)) {
00262 rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
00263 }
00264 clone = rb_obj_alloc(rb_obj_class(obj));
00265 RBASIC(clone)->klass = rb_singleton_class_clone(obj);
00266 RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT)) & ~(FL_FREEZE|FL_FINALIZE);
00267 init_copy(clone, obj);
00268 RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
00269
00270 return clone;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291 VALUE
00292 rb_obj_dup(obj)
00293 VALUE obj;
00294 {
00295 VALUE dup;
00296
00297 if (rb_special_const_p(obj)) {
00298 rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
00299 }
00300 dup = rb_obj_alloc(rb_obj_class(obj));
00301 init_copy(dup, obj);
00302
00303 return dup;
00304 }
00305
00306
00307 VALUE
00308 rb_obj_init_copy(obj, orig)
00309 VALUE obj, orig;
00310 {
00311 if (obj == orig) return obj;
00312 rb_check_frozen(obj);
00313 if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
00314 rb_raise(rb_eTypeError, "initialize_copy should take same class object");
00315 }
00316 return obj;
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 static VALUE
00335 rb_any_to_a(obj)
00336 VALUE obj;
00337 {
00338 rb_warn("default `to_a' will be obsolete");
00339 return rb_ary_new3(1, obj);
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 VALUE
00354 rb_any_to_s(obj)
00355 VALUE obj;
00356 {
00357 char *cname = rb_obj_classname(obj);
00358 size_t len;
00359 VALUE str;
00360
00361 len = strlen(cname)+6+16;
00362 str = rb_str_new(0, len);
00363 snprintf(RSTRING(str)->ptr, len+1, "#<%s:0x%lx>", cname, obj);
00364 RSTRING(str)->len = strlen(RSTRING(str)->ptr);
00365 if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
00366
00367 return str;
00368 }
00369
00370 VALUE
00371 rb_inspect(obj)
00372 VALUE obj;
00373 {
00374 return rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
00375 }
00376
00377 static int
00378 inspect_i(id, value, str)
00379 ID id;
00380 VALUE value;
00381 VALUE str;
00382 {
00383 VALUE str2;
00384 char *ivname;
00385
00386
00387 if (CLASS_OF(value) == 0) return ST_CONTINUE;
00388 if (!rb_is_instance_id(id)) return ST_CONTINUE;
00389 if (RSTRING(str)->ptr[0] == '-') {
00390 RSTRING(str)->ptr[0] = '#';
00391 rb_str_cat2(str, " ");
00392 }
00393 else {
00394 rb_str_cat2(str, ", ");
00395 }
00396 ivname = rb_id2name(id);
00397 rb_str_cat2(str, ivname);
00398 rb_str_cat2(str, "=");
00399 str2 = rb_inspect(value);
00400 rb_str_append(str, str2);
00401 OBJ_INFECT(str, str2);
00402
00403 return ST_CONTINUE;
00404 }
00405
00406 static VALUE
00407 inspect_obj(obj, str)
00408 VALUE obj, str;
00409 {
00410 st_foreach_safe(ROBJECT(obj)->iv_tbl, inspect_i, str);
00411 rb_str_cat2(str, ">");
00412 RSTRING(str)->ptr[0] = '#';
00413 OBJ_INFECT(str, obj);
00414
00415 return str;
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 static VALUE
00432 rb_obj_inspect(obj)
00433 VALUE obj;
00434 {
00435 if (TYPE(obj) == T_OBJECT
00436 && ROBJECT(obj)->iv_tbl
00437 && ROBJECT(obj)->iv_tbl->num_entries > 0) {
00438 VALUE str;
00439 size_t len;
00440 char *c;
00441
00442 c = rb_obj_classname(obj);
00443 if (rb_inspecting_p(obj)) {
00444 len = strlen(c)+10+16+1;
00445 str = rb_str_new(0, len);
00446 snprintf(RSTRING(str)->ptr, len, "#<%s:0x%lx ...>", c, obj);
00447 RSTRING(str)->len = strlen(RSTRING(str)->ptr);
00448 return str;
00449 }
00450 len = strlen(c)+6+16+1;
00451 str = rb_str_new(0, len);
00452 snprintf(RSTRING(str)->ptr, len, "-<%s:0x%lx", c, obj);
00453 RSTRING(str)->len = strlen(RSTRING(str)->ptr);
00454 return rb_protect_inspect(inspect_obj, obj, str);
00455 }
00456 return rb_funcall(obj, rb_intern("to_s"), 0, 0);
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 VALUE
00469 rb_obj_is_instance_of(obj, c)
00470 VALUE obj, c;
00471 {
00472 switch (TYPE(c)) {
00473 case T_MODULE:
00474 case T_CLASS:
00475 case T_ICLASS:
00476 break;
00477 default:
00478 rb_raise(rb_eTypeError, "class or module required");
00479 }
00480
00481 if (rb_obj_class(obj) == c) return Qtrue;
00482 return Qfalse;
00483 }
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 VALUE
00513 rb_obj_is_kind_of(obj, c)
00514 VALUE obj, c;
00515 {
00516 VALUE cl = CLASS_OF(obj);
00517
00518 switch (TYPE(c)) {
00519 case T_MODULE:
00520 case T_CLASS:
00521 case T_ICLASS:
00522 break;
00523
00524 default:
00525 rb_raise(rb_eTypeError, "class or module required");
00526 }
00527
00528 while (cl) {
00529 if (cl == c || RCLASS(cl)->m_tbl == RCLASS(c)->m_tbl)
00530 return Qtrue;
00531 cl = RCLASS(cl)->super;
00532 }
00533 return Qfalse;
00534 }
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
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
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
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 static VALUE
00643 rb_obj_dummy()
00644 {
00645 return Qnil;
00646 }
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 VALUE
00657 rb_obj_tainted(obj)
00658 VALUE obj;
00659 {
00660 if (OBJ_TAINTED(obj))
00661 return Qtrue;
00662 return Qfalse;
00663 }
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674 VALUE
00675 rb_obj_taint(obj)
00676 VALUE obj;
00677 {
00678 rb_secure(4);
00679 if (!OBJ_TAINTED(obj)) {
00680 if (OBJ_FROZEN(obj)) {
00681 rb_error_frozen("object");
00682 }
00683 OBJ_TAINT(obj);
00684 }
00685 return obj;
00686 }
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696 VALUE
00697 rb_obj_untaint(obj)
00698 VALUE obj;
00699 {
00700 rb_secure(3);
00701 if (OBJ_TAINTED(obj)) {
00702 if (OBJ_FROZEN(obj)) {
00703 rb_error_frozen("object");
00704 }
00705 FL_UNSET(obj, FL_TAINT);
00706 }
00707 return obj;
00708 }
00709
00710 void
00711 rb_obj_infect(obj1, obj2)
00712 VALUE obj1, obj2;
00713 {
00714 OBJ_INFECT(obj1, obj2);
00715 }
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 VALUE
00738 rb_obj_freeze(obj)
00739 VALUE obj;
00740 {
00741 if (!OBJ_FROZEN(obj)) {
00742 if (rb_safe_level() >= 4 && !OBJ_TAINTED(obj)) {
00743 rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
00744 }
00745 OBJ_FREEZE(obj);
00746 }
00747 return obj;
00748 }
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761 static VALUE
00762 rb_obj_frozen_p(obj)
00763 VALUE obj;
00764 {
00765 if (OBJ_FROZEN(obj)) return Qtrue;
00766 return Qfalse;
00767 }
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786 static VALUE
00787 nil_to_i(obj)
00788 VALUE obj;
00789 {
00790 return INT2FIX(0);
00791 }
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802 static VALUE
00803 nil_to_f(obj)
00804 VALUE obj;
00805 {
00806 return rb_float_new(0.0);
00807 }
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818 static VALUE
00819 nil_to_s(obj)
00820 VALUE obj;
00821 {
00822 return rb_str_new2("");
00823 }
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834 static VALUE
00835 nil_to_a(obj)
00836 VALUE obj;
00837 {
00838 return rb_ary_new2(0);
00839 }
00840
00841
00842
00843
00844
00845
00846
00847
00848 static VALUE
00849 nil_inspect(obj)
00850 VALUE obj;
00851 {
00852 return rb_str_new2("nil");
00853 }
00854
00855 #ifdef NIL_PLUS
00856 static VALUE
00857 nil_plus(x, y)
00858 VALUE x, y;
00859 {
00860 switch (TYPE(y)) {
00861 case T_NIL:
00862 case T_FIXNUM:
00863 case T_FLOAT:
00864 case T_BIGNUM:
00865 case T_STRING:
00866 case T_ARRAY:
00867 return y;
00868 default:
00869 rb_raise(rb_eTypeError, "tried to add %s(%s) to nil",
00870 RSTRING(rb_inspect(y))->ptr,
00871 rb_obj_classname(y));
00872 }
00873
00874 }
00875 #endif
00876
00877 static VALUE
00878 main_to_s(obj)
00879 VALUE obj;
00880 {
00881 return rb_str_new2("main");
00882 }
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902 static VALUE
00903 true_to_s(obj)
00904 VALUE obj;
00905 {
00906 return rb_str_new2("true");
00907 }
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918 static VALUE
00919 true_and(obj, obj2)
00920 VALUE obj, obj2;
00921 {
00922 return RTEST(obj2)?Qtrue:Qfalse;
00923 }
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941 static VALUE
00942 true_or(obj, obj2)
00943 VALUE obj, obj2;
00944 {
00945 return Qtrue;
00946 }
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958 static VALUE
00959 true_xor(obj, obj2)
00960 VALUE obj, obj2;
00961 {
00962 return RTEST(obj2)?Qfalse:Qtrue;
00963 }
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983 static VALUE
00984 false_to_s(obj)
00985 VALUE obj;
00986 {
00987 return rb_str_new2("false");
00988 }
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000 static VALUE
01001 false_and(obj, obj2)
01002 VALUE obj, obj2;
01003 {
01004 return Qfalse;
01005 }
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017 static VALUE
01018 false_or(obj, obj2)
01019 VALUE obj, obj2;
01020 {
01021 return RTEST(obj2)?Qtrue:Qfalse;
01022 }
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037 static VALUE
01038 false_xor(obj, obj2)
01039 VALUE obj, obj2;
01040 {
01041 return RTEST(obj2)?Qtrue:Qfalse;
01042 }
01043
01044
01045
01046
01047
01048
01049
01050
01051 static VALUE
01052 rb_true(obj)
01053 VALUE obj;
01054 {
01055 return Qtrue;
01056 }
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067 static VALUE
01068 rb_false(obj)
01069 VALUE obj;
01070 {
01071 return Qfalse;
01072 }
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084 static VALUE
01085 rb_obj_pattern_match(obj1, obj2)
01086 VALUE obj1, obj2;
01087 {
01088 return Qfalse;
01089 }
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135 static VALUE
01136 sym_to_i(sym)
01137 VALUE sym;
01138 {
01139 ID id = SYM2ID(sym);
01140
01141 return LONG2FIX(id);
01142 }
01143
01144
01145
01146
01147 static VALUE
01148 sym_to_int(sym)
01149 VALUE sym;
01150 {
01151 rb_warning("treating Symbol as an integer");
01152 return sym_to_i(sym);
01153 }
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165 static VALUE
01166 sym_inspect(sym)
01167 VALUE sym;
01168 {
01169 VALUE str;
01170 char *name;
01171 ID id = SYM2ID(sym);
01172
01173 name = rb_id2name(id);
01174 str = rb_str_new(0, strlen(name)+1);
01175 RSTRING(str)->ptr[0] = ':';
01176 strcpy(RSTRING(str)->ptr+1, name);
01177 if (!rb_symname_p(name)) {
01178 str = rb_str_dump(str);
01179 strncpy(RSTRING(str)->ptr, ":\"", 2);
01180 }
01181 return str;
01182 }
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196 static VALUE
01197 sym_to_s(sym)
01198 VALUE sym;
01199 {
01200 return rb_str_new2(rb_id2name(SYM2ID(sym)));
01201 }
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213 static VALUE
01214 sym_to_sym(sym)
01215 VALUE sym;
01216 {
01217 return sym;
01218 }
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258 static VALUE
01259 rb_mod_to_s(klass)
01260 VALUE klass;
01261
01262 {
01263 if (FL_TEST(klass, FL_SINGLETON)) {
01264 VALUE s = rb_str_new2("#<");
01265 VALUE v = rb_iv_get(klass, "__attached__");
01266
01267 rb_str_cat2(s, "Class:");
01268 switch (TYPE(v)) {
01269 case T_CLASS: case T_MODULE:
01270 rb_str_append(s, rb_inspect(v));
01271 break;
01272 default:
01273 rb_str_append(s, rb_any_to_s(v));
01274 break;
01275 }
01276 rb_str_cat2(s, ">");
01277
01278 return s;
01279 }
01280 return rb_str_dup(rb_class_name(klass));
01281 }
01282
01283
01284
01285
01286
01287
01288
01289
01290 static VALUE
01291 rb_mod_freeze(mod)
01292 VALUE mod;
01293 {
01294 rb_mod_to_s(mod);
01295 return rb_obj_freeze(mod);
01296 }
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308 static VALUE
01309 rb_mod_eqq(mod, arg)
01310 VALUE mod, arg;
01311 {
01312 return rb_obj_is_kind_of(arg, mod);
01313 }
01314
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327 VALUE
01328 rb_class_inherited_p(mod, arg)
01329 VALUE mod, arg;
01330 {
01331 VALUE start = mod;
01332
01333 if (mod == arg) return Qtrue;
01334 switch (TYPE(arg)) {
01335 case T_MODULE:
01336 case T_CLASS:
01337 break;
01338 default:
01339 rb_raise(rb_eTypeError, "compared with non class/module");
01340 }
01341
01342 if (FL_TEST(mod, FL_SINGLETON)) {
01343 if (RCLASS(mod)->m_tbl == RCLASS(arg)->m_tbl)
01344 return Qtrue;
01345 mod = RBASIC(mod)->klass;
01346 }
01347 while (mod) {
01348 if (RCLASS(mod)->m_tbl == RCLASS(arg)->m_tbl)
01349 return Qtrue;
01350 mod = RCLASS(mod)->super;
01351 }
01352
01353 while (arg) {
01354 if (RCLASS(arg)->m_tbl == RCLASS(start)->m_tbl)
01355 return Qfalse;
01356 arg = RCLASS(arg)->super;
01357 }
01358 return Qnil;
01359 }
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372 static VALUE
01373 rb_mod_lt(mod, arg)
01374 VALUE mod, arg;
01375 {
01376 if (mod == arg) return Qfalse;
01377 return rb_class_inherited_p(mod, arg);
01378 }
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393 static VALUE
01394 rb_mod_ge(mod, arg)
01395 VALUE mod, arg;
01396 {
01397 switch (TYPE(arg)) {
01398 case T_MODULE:
01399 case T_CLASS:
01400 break;
01401 default:
01402 rb_raise(rb_eTypeError, "compared with non class/module");
01403 }
01404
01405 return rb_class_inherited_p(arg, mod);
01406 }
01407
01408
01409
01410
01411
01412
01413
01414
01415
01416
01417
01418
01419 static VALUE
01420 rb_mod_gt(mod, arg)
01421 VALUE mod, arg;
01422 {
01423 if (mod == arg) return Qfalse;
01424 return rb_mod_ge(mod, arg);
01425 }
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438 static VALUE
01439 rb_mod_cmp(mod, arg)
01440 VALUE mod, arg;
01441 {
01442 VALUE cmp;
01443
01444 if (mod == arg) return INT2FIX(0);
01445 switch (TYPE(arg)) {
01446 case T_MODULE:
01447 case T_CLASS:
01448 break;
01449 default:
01450 return Qnil;
01451 }
01452
01453 cmp = rb_class_inherited_p(mod, arg);
01454 if (NIL_P(cmp)) return Qnil;
01455 if (cmp) {
01456 return INT2FIX(-1);
01457 }
01458 return INT2FIX(1);
01459 }
01460
01461 static VALUE rb_module_s_alloc (VALUE);
01462 static VALUE
01463 rb_module_s_alloc(klass)
01464 VALUE klass;
01465 {
01466 VALUE mod = rb_module_new();
01467
01468 RBASIC(mod)->klass = klass;
01469 return mod;
01470 }
01471
01472 static VALUE rb_class_s_alloc (VALUE);
01473 static VALUE
01474 rb_class_s_alloc(klass)
01475 VALUE klass;
01476 {
01477 return rb_class_boot(0);
01478 }
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503 static VALUE
01504 rb_mod_initialize(module)
01505 VALUE module;
01506 {
01507 if (rb_block_given_p()) {
01508 rb_mod_module_eval(0, 0, module);
01509 }
01510 return Qnil;
01511 }
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523 static VALUE
01524 rb_class_initialize(argc, argv, klass)
01525 int argc;
01526 VALUE *argv;
01527 VALUE klass;
01528 {
01529 VALUE super;
01530
01531 if (RCLASS(klass)->super != 0) {
01532 rb_raise(rb_eTypeError, "already initialized class");
01533 }
01534 if (rb_scan_args(argc, argv, "01", &super) == 0) {
01535 super = rb_cObject;
01536 }
01537 else {
01538 rb_check_inheritable(super);
01539 }
01540 RCLASS(klass)->super = super;
01541 rb_make_metaclass(klass, RBASIC(super)->klass);
01542 rb_mod_initialize(klass);
01543 rb_class_inherited(super, klass);
01544
01545 return klass;
01546 }
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557 VALUE
01558 rb_obj_alloc(klass)
01559 VALUE klass;
01560 {
01561 VALUE obj;
01562
01563 if (RCLASS(klass)->super == 0) {
01564 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
01565 }
01566 if (FL_TEST(klass, FL_SINGLETON)) {
01567 rb_raise(rb_eTypeError, "can't create instance of virtual class");
01568 }
01569 obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0);
01570 if (rb_obj_class(obj) != rb_class_real(klass)) {
01571 rb_raise(rb_eTypeError, "wrong instance allocation");
01572 }
01573 return obj;
01574 }
01575
01576 static VALUE rb_class_allocate_instance (VALUE);
01577 static VALUE
01578 rb_class_allocate_instance(klass)
01579 VALUE klass;
01580 {
01581 NEWOBJ(obj, struct RObject);
01582 OBJSETUP(obj, klass, T_OBJECT);
01583 return (VALUE)obj;
01584 }
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598 VALUE
01599 rb_class_new_instance(argc, argv, klass)
01600 int argc;
01601 VALUE *argv;
01602 VALUE klass;
01603 {
01604 VALUE obj;
01605
01606 obj = rb_obj_alloc(klass);
01607 rb_obj_call_init(obj, argc, argv);
01608
01609 return obj;
01610 }
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624 static VALUE
01625 rb_class_superclass(klass)
01626 VALUE klass;
01627 {
01628 VALUE super = RCLASS(klass)->super;
01629
01630 if (!super) {
01631 rb_raise(rb_eTypeError, "uninitialized class");
01632 }
01633 if (FL_TEST(klass, FL_SINGLETON)) {
01634 super = RBASIC(klass)->klass;
01635 }
01636 while (TYPE(super) == T_ICLASS) {
01637 super = RCLASS(super)->super;
01638 }
01639 if (!super) {
01640 return Qnil;
01641 }
01642 return super;
01643 }
01644
01645 static ID
01646 str_to_id(str)
01647 VALUE str;
01648 {
01649 if (!RSTRING(str)->ptr || RSTRING(str)->len == 0) {
01650 rb_raise(rb_eArgError, "empty symbol string");
01651 }
01652 if (RSTRING(str)->len != strlen(RSTRING(str)->ptr)) {
01653 rb_warn("Symbols should not contain NUL (\\0)");
01654 }
01655 return rb_intern(RSTRING(str)->ptr);
01656 }
01657
01658 ID
01659 rb_to_id(name)
01660 VALUE name;
01661 {
01662 VALUE tmp;
01663 ID id;
01664
01665 switch (TYPE(name)) {
01666 case T_STRING:
01667 return str_to_id(name);
01668 case T_FIXNUM:
01669 rb_warn("do not use Fixnums as Symbols");
01670 id = FIX2LONG(name);
01671 if (!rb_id2name(id)) {
01672 rb_raise(rb_eArgError, "%ld is not a symbol", id);
01673 }
01674 break;
01675 case T_SYMBOL:
01676 id = SYM2ID(name);
01677 break;
01678 default:
01679 tmp = rb_check_string_type(name);
01680 if (!NIL_P(tmp)) {
01681 return str_to_id(tmp);
01682 }
01683 rb_raise(rb_eTypeError, "%s is not a symbol", RSTRING(rb_inspect(name))->ptr);
01684 }
01685 return id;
01686 }
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697
01698
01699
01700
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714 static VALUE
01715 rb_mod_attr(argc, argv, klass)
01716 int argc;
01717 VALUE *argv;
01718 VALUE klass;
01719 {
01720 VALUE name, pub;
01721
01722 rb_scan_args(argc, argv, "11", &name, &pub);
01723 rb_attr(klass, rb_to_id(name), 1, RTEST(pub), Qtrue);
01724 return Qnil;
01725 }
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736 static VALUE
01737 rb_mod_attr_reader(argc, argv, klass)
01738 int argc;
01739 VALUE *argv;
01740 VALUE klass;
01741 {
01742 int i;
01743
01744 for (i=0; i<argc; i++) {
01745 rb_attr(klass, rb_to_id(argv[i]), 1, 0, Qtrue);
01746 }
01747 return Qnil;
01748 }
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758 static VALUE
01759 rb_mod_attr_writer(argc, argv, klass)
01760 int argc;
01761 VALUE *argv;
01762 VALUE klass;
01763 {
01764 int i;
01765
01766 for (i=0; i<argc; i++) {
01767 rb_attr(klass, rb_to_id(argv[i]), 0, 1, Qtrue);
01768 }
01769 return Qnil;
01770 }
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781
01782
01783
01784
01785 static VALUE
01786 rb_mod_attr_accessor(argc, argv, klass)
01787 int argc;
01788 VALUE *argv;
01789 VALUE klass;
01790 {
01791 int i;
01792
01793 for (i=0; i<argc; i++) {
01794 rb_attr(klass, rb_to_id(argv[i]), 1, 1, Qtrue);
01795 }
01796 return Qnil;
01797 }
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807
01808 static VALUE
01809 rb_mod_const_get(mod, name)
01810 VALUE mod, name;
01811 {
01812 ID id = rb_to_id(name);
01813
01814 if (!rb_is_const_id(id)) {
01815 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
01816 }
01817 return rb_const_get(mod, id);
01818 }
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830
01831
01832 static VALUE
01833 rb_mod_const_set(mod, name, value)
01834 VALUE mod, name, value;
01835 {
01836 ID id = rb_to_id(name);
01837
01838 if (!rb_is_const_id(id)) {
01