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 <stdio.h>
00018 #include <errno.h>
00019 #include <ctype.h>
00020 #include <math.h>
00021 #include <float.h>
00022 #include "constant.h"
00023 #include "internal.h"
00024
00025 VALUE rb_cBasicObject;
00026 VALUE rb_mKernel;
00027 VALUE rb_cObject;
00028 VALUE rb_cModule;
00029 VALUE rb_cClass;
00030 VALUE rb_cData;
00031
00032 VALUE rb_cNilClass;
00033 VALUE rb_cTrueClass;
00034 VALUE rb_cFalseClass;
00035
00036 static ID id_eq, id_eql, id_match, id_inspect;
00037 static ID id_init_copy, id_init_clone, id_init_dup;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 VALUE
00049 rb_equal(VALUE obj1, VALUE obj2)
00050 {
00051 VALUE result;
00052
00053 if (obj1 == obj2) return Qtrue;
00054 result = rb_funcall(obj1, id_eq, 1, obj2);
00055 if (RTEST(result)) return Qtrue;
00056 return Qfalse;
00057 }
00058
00059 int
00060 rb_eql(VALUE obj1, VALUE obj2)
00061 {
00062 return RTEST(rb_funcall(obj1, id_eql, 1, obj2));
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
00094 VALUE
00095 rb_obj_equal(VALUE obj1, VALUE obj2)
00096 {
00097 if (obj1 == obj2) return Qtrue;
00098 return Qfalse;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 VALUE
00112 rb_obj_hash(VALUE obj)
00113 {
00114 VALUE oid = rb_obj_id(obj);
00115 st_index_t h = rb_hash_end(rb_hash_start(NUM2LONG(oid)));
00116 return LONG2FIX(h);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126 VALUE
00127 rb_obj_not(VALUE obj)
00128 {
00129 return RTEST(obj) ? Qfalse : Qtrue;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 VALUE
00140 rb_obj_not_equal(VALUE obj1, VALUE obj2)
00141 {
00142 VALUE result = rb_funcall(obj1, id_eq, 1, obj2);
00143 return RTEST(result) ? Qfalse : Qtrue;
00144 }
00145
00146 VALUE
00147 rb_class_real(VALUE cl)
00148 {
00149 if (cl == 0)
00150 return 0;
00151 while ((RBASIC(cl)->flags & FL_SINGLETON) || BUILTIN_TYPE(cl) == T_ICLASS) {
00152 cl = RCLASS_SUPER(cl);
00153 }
00154 return cl;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 VALUE
00170 rb_obj_class(VALUE obj)
00171 {
00172 return rb_class_real(CLASS_OF(obj));
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 static VALUE
00193 rb_obj_singleton_class(VALUE obj)
00194 {
00195 return rb_singleton_class(obj);
00196 }
00197
00198 static void
00199 init_copy(VALUE dest, VALUE obj)
00200 {
00201 if (OBJ_FROZEN(dest)) {
00202 rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_obj_classname(dest));
00203 }
00204 RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
00205 RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT|FL_UNTRUSTED);
00206 rb_copy_generic_ivar(dest, obj);
00207 rb_gc_copy_finalizer(dest, obj);
00208 switch (TYPE(obj)) {
00209 case T_OBJECT:
00210 if (!(RBASIC(dest)->flags & ROBJECT_EMBED) && ROBJECT_IVPTR(dest)) {
00211 xfree(ROBJECT_IVPTR(dest));
00212 ROBJECT(dest)->as.heap.ivptr = 0;
00213 ROBJECT(dest)->as.heap.numiv = 0;
00214 ROBJECT(dest)->as.heap.iv_index_tbl = 0;
00215 }
00216 if (RBASIC(obj)->flags & ROBJECT_EMBED) {
00217 MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
00218 RBASIC(dest)->flags |= ROBJECT_EMBED;
00219 }
00220 else {
00221 long len = ROBJECT(obj)->as.heap.numiv;
00222 VALUE *ptr = ALLOC_N(VALUE, len);
00223 MEMCPY(ptr, ROBJECT(obj)->as.heap.ivptr, VALUE, len);
00224 ROBJECT(dest)->as.heap.ivptr = ptr;
00225 ROBJECT(dest)->as.heap.numiv = len;
00226 ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
00227 RBASIC(dest)->flags &= ~ROBJECT_EMBED;
00228 }
00229 break;
00230 case T_CLASS:
00231 case T_MODULE:
00232 if (RCLASS_IV_TBL(dest)) {
00233 st_free_table(RCLASS_IV_TBL(dest));
00234 RCLASS_IV_TBL(dest) = 0;
00235 }
00236 if (RCLASS_CONST_TBL(dest)) {
00237 rb_free_const_table(RCLASS_CONST_TBL(dest));
00238 RCLASS_CONST_TBL(dest) = 0;
00239 }
00240 if (RCLASS_IV_TBL(obj)) {
00241 RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj));
00242 }
00243 break;
00244 }
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 VALUE
00272 rb_obj_clone(VALUE obj)
00273 {
00274 VALUE clone;
00275
00276 if (rb_special_const_p(obj)) {
00277 rb_raise(rb_eTypeError, "can't clone %s", rb_obj_classname(obj));
00278 }
00279 clone = rb_obj_alloc(rb_obj_class(obj));
00280 RBASIC(clone)->klass = rb_singleton_class_clone(obj);
00281 RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT) | FL_TEST(clone, FL_UNTRUSTED)) & ~(FL_FREEZE|FL_FINALIZE|FL_MARK);
00282 init_copy(clone, obj);
00283 rb_funcall(clone, id_init_clone, 1, obj);
00284 RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
00285
00286 return clone;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 VALUE
00308 rb_obj_dup(VALUE obj)
00309 {
00310 VALUE dup;
00311
00312 if (rb_special_const_p(obj)) {
00313 rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
00314 }
00315 dup = rb_obj_alloc(rb_obj_class(obj));
00316 init_copy(dup, obj);
00317 rb_funcall(dup, id_init_dup, 1, obj);
00318
00319 return dup;
00320 }
00321
00322
00323 VALUE
00324 rb_obj_init_copy(VALUE obj, VALUE orig)
00325 {
00326 if (obj == orig) return obj;
00327 rb_check_frozen(obj);
00328 if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
00329 rb_raise(rb_eTypeError, "initialize_copy should take same class object");
00330 }
00331 return obj;
00332 }
00333
00334
00335 VALUE
00336 rb_obj_init_dup_clone(VALUE obj, VALUE orig)
00337 {
00338 rb_funcall(obj, id_init_copy, 1, orig);
00339 return obj;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 VALUE
00353 rb_any_to_s(VALUE obj)
00354 {
00355 const char *cname = rb_obj_classname(obj);
00356 VALUE str;
00357
00358 str = rb_sprintf("#<%s:%p>", cname, (void*)obj);
00359 OBJ_INFECT(str, obj);
00360
00361 return str;
00362 }
00363
00364 VALUE
00365 rb_inspect(VALUE obj)
00366 {
00367 return rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
00368 }
00369
00370 static int
00371 inspect_i(ID id, VALUE value, VALUE str)
00372 {
00373 VALUE str2;
00374 const char *ivname;
00375
00376
00377 if (CLASS_OF(value) == 0) return ST_CONTINUE;
00378 if (!rb_is_instance_id(id)) return ST_CONTINUE;
00379 if (RSTRING_PTR(str)[0] == '-') {
00380 RSTRING_PTR(str)[0] = '#';
00381 rb_str_cat2(str, " ");
00382 }
00383 else {
00384 rb_str_cat2(str, ", ");
00385 }
00386 ivname = rb_id2name(id);
00387 rb_str_cat2(str, ivname);
00388 rb_str_cat2(str, "=");
00389 str2 = rb_inspect(value);
00390 rb_str_append(str, str2);
00391 OBJ_INFECT(str, str2);
00392
00393 return ST_CONTINUE;
00394 }
00395
00396 static VALUE
00397 inspect_obj(VALUE obj, VALUE str, int recur)
00398 {
00399 if (recur) {
00400 rb_str_cat2(str, " ...");
00401 }
00402 else {
00403 rb_ivar_foreach(obj, inspect_i, str);
00404 }
00405 rb_str_cat2(str, ">");
00406 RSTRING_PTR(str)[0] = '#';
00407 OBJ_INFECT(str, obj);
00408
00409 return str;
00410 }
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 static VALUE
00427 rb_obj_inspect(VALUE obj)
00428 {
00429 if (TYPE(obj) == T_OBJECT && rb_obj_basic_to_s_p(obj)) {
00430 int has_ivar = 0;
00431 VALUE *ptr = ROBJECT_IVPTR(obj);
00432 long len = ROBJECT_NUMIV(obj);
00433 long i;
00434
00435 for (i = 0; i < len; i++) {
00436 if (ptr[i] != Qundef) {
00437 has_ivar = 1;
00438 break;
00439 }
00440 }
00441
00442 if (has_ivar) {
00443 VALUE str;
00444 const char *c = rb_obj_classname(obj);
00445
00446 str = rb_sprintf("-<%s:%p", c, (void*)obj);
00447 return rb_exec_recursive(inspect_obj, obj, str);
00448 }
00449 return rb_any_to_s(obj);
00450 }
00451 return rb_funcall(obj, rb_intern("to_s"), 0, 0);
00452 }
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472 VALUE
00473 rb_obj_is_instance_of(VALUE obj, VALUE c)
00474 {
00475 switch (TYPE(c)) {
00476 case T_MODULE:
00477 case T_CLASS:
00478 case T_ICLASS:
00479 break;
00480 default:
00481 rb_raise(rb_eTypeError, "class or module required");
00482 }
00483
00484 if (rb_obj_class(obj) == c) return Qtrue;
00485 return Qfalse;
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
00513
00514
00515
00516
00517 VALUE
00518 rb_obj_is_kind_of(VALUE obj, VALUE c)
00519 {
00520 VALUE cl = CLASS_OF(obj);
00521
00522 switch (TYPE(c)) {
00523 case T_MODULE:
00524 case T_CLASS:
00525 case T_ICLASS:
00526 break;
00527
00528 default:
00529 rb_raise(rb_eTypeError, "class or module required");
00530 }
00531
00532 while (cl) {
00533 if (cl == c || RCLASS_M_TBL(cl) == RCLASS_M_TBL(c))
00534 return Qtrue;
00535 cl = RCLASS_SUPER(cl);
00536 }
00537 return Qfalse;
00538 }
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 VALUE
00557 rb_obj_tap(VALUE obj)
00558 {
00559 rb_yield(obj);
00560 return obj;
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
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 static VALUE
00754 rb_obj_dummy(void)
00755 {
00756 return Qnil;
00757 }
00758
00759
00760
00761
00762
00763
00764
00765
00766 VALUE
00767 rb_obj_tainted(VALUE obj)
00768 {
00769 if (OBJ_TAINTED(obj))
00770 return Qtrue;
00771 return Qfalse;
00772 }
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783 VALUE
00784 rb_obj_taint(VALUE obj)
00785 {
00786 rb_secure(4);
00787 if (!OBJ_TAINTED(obj)) {
00788 rb_check_frozen(obj);
00789 OBJ_TAINT(obj);
00790 }
00791 return obj;
00792 }
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802 VALUE
00803 rb_obj_untaint(VALUE obj)
00804 {
00805 rb_secure(3);
00806 if (OBJ_TAINTED(obj)) {
00807 rb_check_frozen(obj);
00808 FL_UNSET(obj, FL_TAINT);
00809 }
00810 return obj;
00811 }
00812
00813
00814
00815
00816
00817
00818
00819
00820 VALUE
00821 rb_obj_untrusted(VALUE obj)
00822 {
00823 if (OBJ_UNTRUSTED(obj))
00824 return Qtrue;
00825 return Qfalse;
00826 }
00827
00828
00829
00830
00831
00832
00833
00834
00835 VALUE
00836 rb_obj_untrust(VALUE obj)
00837 {
00838 rb_secure(4);
00839 if (!OBJ_UNTRUSTED(obj)) {
00840 rb_check_frozen(obj);
00841 OBJ_UNTRUST(obj);
00842 }
00843 return obj;
00844 }
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854 VALUE
00855 rb_obj_trust(VALUE obj)
00856 {
00857 rb_secure(3);
00858 if (OBJ_UNTRUSTED(obj)) {
00859 rb_check_frozen(obj);
00860 FL_UNSET(obj, FL_UNTRUSTED);
00861 }
00862 return obj;
00863 }
00864
00865 void
00866 rb_obj_infect(VALUE obj1, VALUE obj2)
00867 {
00868 OBJ_INFECT(obj1, obj2);
00869 }
00870
00871 static st_table *immediate_frozen_tbl = 0;
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894 VALUE
00895 rb_obj_freeze(VALUE obj)
00896 {
00897 if (!OBJ_FROZEN(obj)) {
00898 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(obj)) {
00899 rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
00900 }
00901 OBJ_FREEZE(obj);
00902 if (SPECIAL_CONST_P(obj)) {
00903 if (!immediate_frozen_tbl) {
00904 immediate_frozen_tbl = st_init_numtable();
00905 }
00906 st_insert(immediate_frozen_tbl, obj, (st_data_t)Qtrue);
00907 }
00908 }
00909 return obj;
00910 }
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923 VALUE
00924 rb_obj_frozen_p(VALUE obj)
00925 {
00926 if (OBJ_FROZEN(obj)) return Qtrue;
00927 if (SPECIAL_CONST_P(obj)) {
00928 if (!immediate_frozen_tbl) return Qfalse;
00929 if (st_lookup(immediate_frozen_tbl, obj, 0)) return Qtrue;
00930 }
00931 return Qfalse;
00932 }
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951 static VALUE
00952 nil_to_i(VALUE obj)
00953 {
00954 return INT2FIX(0);
00955 }
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966 static VALUE
00967 nil_to_f(VALUE obj)
00968 {
00969 return DBL2NUM(0.0);
00970 }
00971
00972
00973
00974
00975
00976
00977
00978
00979 static VALUE
00980 nil_to_s(VALUE obj)
00981 {
00982 return rb_usascii_str_new(0, 0);
00983 }
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996 static VALUE
00997 nil_to_a(VALUE obj)
00998 {
00999 return rb_ary_new2(0);
01000 }
01001
01002
01003
01004
01005
01006
01007
01008
01009 static VALUE
01010 nil_inspect(VALUE obj)
01011 {
01012 return rb_usascii_str_new2("nil");
01013 }
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032 static VALUE
01033 true_to_s(VALUE obj)
01034 {
01035 return rb_usascii_str_new2("true");
01036 }
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047 static VALUE
01048 true_and(VALUE obj, VALUE obj2)
01049 {
01050 return RTEST(obj2)?Qtrue:Qfalse;
01051 }
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069 static VALUE
01070 true_or(VALUE obj, VALUE obj2)
01071 {
01072 return Qtrue;
01073 }
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085 static VALUE
01086 true_xor(VALUE obj, VALUE obj2)
01087 {
01088 return RTEST(obj2)?Qfalse:Qtrue;
01089 }
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109 static VALUE
01110 false_to_s(VALUE obj)
01111 {
01112 return rb_usascii_str_new2("false");
01113 }
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125 static VALUE
01126 false_and(VALUE obj, VALUE obj2)
01127 {
01128 return Qfalse;
01129 }
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141 static VALUE
01142 false_or(VALUE obj, VALUE obj2)
01143 {
01144 return RTEST(obj2)?Qtrue:Qfalse;
01145 }
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160 static VALUE
01161 false_xor(VALUE obj, VALUE obj2)
01162 {
01163 return RTEST(obj2)?Qtrue:Qfalse;
01164 }
01165
01166
01167
01168
01169
01170
01171
01172
01173 static VALUE
01174 rb_true(VALUE obj)
01175 {
01176 return Qtrue;
01177 }
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188 static VALUE
01189 rb_false(VALUE obj)
01190 {
01191 return Qfalse;
01192 }
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204 static VALUE
01205 rb_obj_match(VALUE obj1, VALUE obj2)
01206 {
01207 return Qnil;
01208 }
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218 static VALUE
01219 rb_obj_not_match(VALUE obj1, VALUE obj2)
01220 {
01221 VALUE result = rb_funcall(obj1, id_match, 1, obj2);
01222 return RTEST(result) ? Qfalse : Qtrue;
01223 }
01224
01225
01226
01227
01228
01229
01230
01231
01232 static VALUE
01233 rb_obj_cmp(VALUE obj1, VALUE obj2)
01234 {
01235 if (obj1 == obj2 || rb_equal(obj1, obj2))
01236 return INT2FIX(0);
01237 return Qnil;
01238 }
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277 static VALUE
01278 rb_mod_to_s(VALUE klass)
01279 {
01280 if (FL_TEST(klass, FL_SINGLETON)) {
01281 VALUE s = rb_usascii_str_new2("#<");
01282 VALUE v = rb_iv_get(klass, "__attached__");
01283
01284 rb_str_cat2(s, "Class:");
01285 switch (TYPE(v)) {
01286 case T_CLASS: case T_MODULE:
01287 rb_str_append(s, rb_inspect(v));
01288 break;
01289 default:
01290 rb_str_append(s, rb_any_to_s(v));
01291 break;
01292 }
01293 rb_str_cat2(s, ">");
01294
01295 return s;
01296 }
01297 return rb_str_dup(rb_class_name(klass));
01298 }
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309 static VALUE
01310 rb_mod_freeze(VALUE mod)
01311 {
01312 rb_class_name(mod);
01313 return rb_obj_freeze(mod);
01314 }
01315
01316
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326 static VALUE
01327 rb_mod_eqq(VALUE mod, VALUE arg)
01328 {
01329 return rb_obj_is_kind_of(arg, mod);
01330 }
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344 VALUE
01345 rb_class_inherited_p(VALUE mod, VALUE arg)
01346 {
01347 VALUE start = mod;
01348
01349 if (mod == arg) return Qtrue;
01350 switch (TYPE(arg)) {
01351 case T_MODULE:
01352 case T_CLASS:
01353 break;
01354 default:
01355 rb_raise(rb_eTypeError, "compared with non class/module");
01356 }
01357 while (mod) {
01358 if (RCLASS_M_TBL(mod) == RCLASS_M_TBL(arg))
01359 return Qtrue;
01360 mod = RCLASS_SUPER(mod);
01361 }
01362
01363 while (arg) {
01364 if (RCLASS_M_TBL(arg) == RCLASS_M_TBL(start))
01365 return Qfalse;
01366 arg = RCLASS_SUPER(arg);
01367 }
01368 return Qnil;
01369 }
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01382 static VALUE
01383 rb_mod_lt(VALUE mod, VALUE arg)
01384 {
01385 if (mod == arg) return Qfalse;
01386 return rb_class_inherited_p(mod, arg);
01387 }
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402 static VALUE
01403 rb_mod_ge(VALUE mod, VALUE arg)
01404 {
01405 switch (TYPE(arg)) {
01406 case T_MODULE:
01407 case T_CLASS:
01408 break;
01409 default:
01410 rb_raise(rb_eTypeError, "compared with non class/module");
01411 }
01412
01413 return rb_class_inherited_p(arg, mod);
01414 }
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427 static VALUE
01428 rb_mod_gt(VALUE mod, VALUE arg)
01429 {
01430 if (mod == arg) return Qfalse;
01431 return rb_mod_ge(mod, arg);
01432 }
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445 static VALUE
01446 rb_mod_cmp(VALUE mod, VALUE arg)
01447 {
01448 VALUE cmp;
01449
01450 if (mod == arg) return INT2FIX(0);
01451 switch (TYPE(arg)) {
01452 case T_MODULE:
01453 case T_CLASS:
01454 break;
01455 default:
01456 return Qnil;
01457 }
01458
01459 cmp = rb_class_inherited_p(mod, arg);
01460 if (NIL_P(cmp)) return Qnil;
01461 if (cmp) {
01462 return INT2FIX(-1);
01463 }
01464 return INT2FIX(1);
01465 }
01466
01467 static VALUE
01468 rb_module_s_alloc(VALUE klass)
01469 {
01470 VALUE mod = rb_module_new();
01471
01472 RBASIC(mod)->klass = klass;
01473 return mod;
01474 }
01475
01476 static VALUE
01477 rb_class_s_alloc(VALUE klass)
01478 {
01479 return rb_class_boot(0);
01480 }
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508 static VALUE
01509 rb_mod_initialize(VALUE module)
01510 {
01511 if (rb_block_given_p()) {
01512 rb_mod_module_exec(1, &module, module);
01513 }
01514 return Qnil;
01515 }
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547 static VALUE
01548 rb_class_initialize(int argc, VALUE *argv, VALUE klass)
01549 {
01550 VALUE super;
01551
01552 if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) {
01553 rb_raise(rb_eTypeError, "already initialized class");
01554 }
01555 if (argc == 0) {
01556 super = rb_cObject;
01557 }
01558 else {
01559 rb_scan_args(argc, argv, "01", &super);
01560 rb_check_inheritable(super);
01561 }
01562 RCLASS_SUPER(klass) = super;
01563 rb_make_metaclass(klass, RBASIC(super)->klass);
01564 rb_class_inherited(super, klass);
01565 rb_mod_initialize(klass);
01566
01567 return klass;
01568 }
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592 VALUE
01593 rb_obj_alloc(VALUE klass)
01594 {
01595 VALUE obj;
01596
01597 if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
01598 rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
01599 }
01600 if (FL_TEST(klass, FL_SINGLETON)) {
01601 rb_raise(rb_eTypeError, "can't create instance of singleton class");
01602 }
01603 obj = rb_funcall(klass, ID_ALLOCATOR, 0, 0);
01604 if (rb_obj_class(obj) != rb_class_real(klass)) {
01605 rb_raise(rb_eTypeError, "wrong instance allocation");
01606 }
01607 return obj;
01608 }
01609
01610 static VALUE
01611 rb_class_allocate_instance(VALUE klass)
01612 {
01613 NEWOBJ(obj, struct RObject);
01614 OBJSETUP(obj, klass, T_OBJECT);
01615 return (VALUE)obj;
01616 }
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630 VALUE
01631 rb_class_new_instance(int argc, VALUE *argv, VALUE klass)
01632 {
01633 VALUE obj;
01634
01635 obj = rb_obj_alloc(klass);
01636 rb_obj_call_init(obj, argc, argv);
01637
01638 return obj;
01639 }
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660 VALUE
01661 rb_class_superclass(VALUE klass)
01662 {
01663 VALUE super = RCLASS_SUPER(klass);
01664
01665 if (!super) {
01666 if (klass == rb_cBasicObject) return Qnil;
01667 rb_raise(rb_eTypeError, "uninitialized class");
01668 }
01669 while (TYPE(super) == T_ICLASS) {
01670 super = RCLASS_SUPER(super);
01671 }
01672 if (!super) {
01673 return Qnil;
01674 }
01675 return super;
01676 }
01677
01678 VALUE
01679 rb_class_get_superclass(VALUE klass)
01680 {
01681 return RCLASS_SUPER(klass);
01682 }
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694 static VALUE
01695 rb_mod_attr_reader(int argc, VALUE *argv, VALUE klass)
01696 {
01697 int i;
01698
01699 for (i=0; i<argc; i++) {
01700 rb_attr(klass, rb_to_id(argv[i]), TRUE, FALSE, TRUE);
01701 }
01702 return Qnil;
01703 }
01704
01705 VALUE
01706 rb_mod_attr(int argc, VALUE *argv, VALUE klass)
01707 {
01708 if (argc == 2 && (argv[1] == Qtrue || argv[1] == Qfalse)) {
01709 rb_warning("optional boolean argument is obsoleted");
01710 rb_attr(klass, rb_to_id(argv[0]), 1, RTEST(argv[1]), TRUE);
01711 return Qnil;
01712 }
01713 return rb_mod_attr_reader(argc, argv, klass);
01714 }
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724 static VALUE
01725 rb_mod_attr_writer(int argc, VALUE *argv, VALUE klass)
01726 {
01727 int i;
01728
01729 for (i=0; i<argc; i++) {
01730 rb_attr(klass, rb_to_id(argv[i]), FALSE, TRUE, TRUE);
01731 }
01732 return Qnil;
01733 }
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750 static VALUE
01751 rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
01752 {
01753 int i;
01754
01755 for (i=0; i<argc; i++) {
01756 rb_attr(klass, rb_to_id(argv[i]), TRUE, TRUE, TRUE);
01757 }
01758 return Qnil;
01759 }
01760
01761
01762
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775 static VALUE
01776 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
01777 {
01778 VALUE name, recur;
01779 ID id;
01780
01781 if (argc == 1) {
01782 name = argv[0];
01783 recur = Qtrue;
01784 }
01785 else {
01786 rb_scan_args(argc, argv, "11", &name, &recur);
01787 }
01788 id = rb_to_id(name);
01789 if (!rb_is_const_id(id)) {
01790 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
01791 }
01792 return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
01793 }
01794
01795
01796
01797
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807 static VALUE
01808 rb_mod_const_set(VALUE mod, VALUE name, VALUE value)
01809 {
01810 ID id = rb_to_id(name);
01811
01812 if (!rb_is_const_id(id)) {
01813 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
01814 }
01815 rb_const_set(mod, id, value);
01816 return value;
01817 }
01818
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830
01831
01832
01833
01834 static VALUE
01835 rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
01836 {
01837 VALUE name, recur;
01838 ID id;
01839
01840 if (argc == 1) {
01841 name = argv[0];
01842 recur = Qtrue;
01843 }
01844 else {
01845 rb_scan_args(argc, argv, "11", &name, &recur);
01846 }
01847 id = rb_to_id(name);
01848 if (!rb_is_const_id(id)) {
01849 rb_name_error(id, "wrong constant name %s", rb_id2name(id));
01850 }
01851 return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
01852 }
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874 static VALUE
01875 rb_obj_ivar_get(VALUE obj, VALUE iv)
01876 {
01877 ID id = rb_to_id(iv);
01878
01879 if (!rb_is_instance_id(id)) {
01880 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
01881 }
01882 return rb_ivar_get(obj, id);
01883 }
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905 static VALUE
01906 rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
01907 {
01908 ID id = rb_to_id(iv);
01909
01910 if (!rb_is_instance_id(id)) {
01911 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
01912 }
01913 return rb_ivar_set(obj, id, val);
01914 }
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934 static VALUE
01935 rb_obj_ivar_defined(VALUE obj, VALUE iv)
01936 {
01937 ID id = rb_to_id(iv);
01938
01939 if (!rb_is_instance_id(id)) {
01940 rb_name_error(id, "`%s' is not allowed as an instance variable name", rb_id2name(id));
01941 }
01942 return rb_ivar_defined(obj, id);
01943 }
01944
01945
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959 static VALUE
01960 rb_mod_cvar_get(VALUE obj, VALUE iv)
01961 {
01962 ID id = rb_to_id(iv);
01963
01964 if (!rb_is_class_id(id)) {
01965 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
01966 }
01967 return rb_cvar_get(obj, id);
01968 }
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987 static VALUE
01988 rb_mod_cvar_set(VALUE obj, VALUE iv, VALUE val)
01989 {
01990 ID id = rb_to_id(iv);
01991
01992 if (!rb_is_class_id(id)) {
01993 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
01994 }
01995 rb_cvar_set(obj, id, val);
01996 return val;
01997 }
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013 static VALUE
02014 rb_mod_cvar_defined(VALUE obj, VALUE iv)
02015 {
02016 ID id = rb_to_id(iv);
02017
02018 if (!rb_is_class_id(id)) {
02019 rb_name_error(id, "`%s' is not allowed as a class variable name", rb_id2name(id));
02020 }
02021 return rb_cvar_defined(obj, id);
02022 }
02023
02024 static struct conv_method_tbl {
02025 const char *method;
02026 ID id;
02027 } conv_method_names[] = {
02028 {"to_int", 0},
02029 {"to_ary", 0},
02030 {"to_str", 0},
02031 {"to_sym", 0},
02032 {"to_hash", 0},
02033 {"to_proc", 0},
02034 {"to_io", 0},
02035 {"to_a", 0},
02036 {"to_s", 0},
02037 {NULL, 0}
02038 };
02039
02040 static VALUE
02041 convert_type(VALUE val, const char *tname, const char *method, int raise)
02042 {
02043 ID m = 0;
02044 int i;
02045 VALUE r;
02046
02047 for (i=0; conv_method_names[i].method; i++) {
02048 if (conv_method_names[i].method[0] == method[0] &&
02049 strcmp(conv_method_names[i].method, method) == 0) {
02050 m = conv_method_names[i].id;
02051 break;
02052 }
02053 }
02054 if (!m) m = rb_intern(method);
02055 r = rb_check_funcall(val, m, 0, 0);
02056 if (r == Qundef) {
02057 if (raise) {
02058 rb_raise(rb_eTypeError, "can't convert %s into %s",
02059 NIL_P(val) ? "nil" :
02060 val == Qtrue ? "true" :
02061 val == Qfalse ? "false" :
02062 rb_obj_classname(val),
02063 tname);
02064 }
02065 return Qnil;
02066 }
02067 return r;
02068 }
02069
02070 VALUE
02071 rb_convert_type(VALUE val, int type, const char *tname, const char *method)
02072 {
02073 VALUE v;
02074
02075 if (TYPE(val) == type) return val;
02076 v = convert_type(val, tname, method, TRUE);
02077 if (TYPE(v) != type) {
02078 const char *cname = rb_obj_classname(val);
02079 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
02080 cname, tname, cname, method, rb_obj_classname(v));
02081 }
02082 return v;
02083 }
02084
02085 VALUE
02086 rb_check_convert_type(VALUE val, int type, const char *tname, const char *method)
02087 {
02088 VALUE v;
02089
02090
02091 if (TYPE(val) == type && type != T_DATA) return val;
02092 v = convert_type(val, tname, method, FALSE);
02093 if (NIL_P(v)) return Qnil;
02094 if (TYPE(v) != type) {
02095 const char *cname = rb_obj_classname(val);
02096 rb_raise(rb_eTypeError, "can't convert %s to %s (%s#%s gives %s)",
02097 cname, tname, cname, method, rb_obj_classname(v));
02098 }
02099 return v;
02100 }
02101
02102
02103 static VALUE
02104 rb_to_integer(VALUE val, const char *method)
02105 {
02106 VALUE v;
02107
02108 if (FIXNUM_P(val)) return val;
02109 if (TYPE(val) == T_BIGNUM) return val;
02110 v = convert_type(val, "Integer", method, TRUE);
02111 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02112 const char *cname = rb_obj_classname(val);
02113 rb_raise(rb_eTypeError, "can't convert %s to Integer (%s#%s gives %s)",
02114 cname, cname, method, rb_obj_classname(v));
02115 }
02116 return v;
02117 }
02118
02119 VALUE
02120 rb_check_to_integer(VALUE val, const char *method)
02121 {
02122 VALUE v;
02123
02124 if (FIXNUM_P(val)) return val;
02125 if (TYPE(val) == T_BIGNUM) return val;
02126 v = convert_type(val, "Integer", method, FALSE);
02127 if (!rb_obj_is_kind_of(v, rb_cInteger)) {
02128 return Qnil;
02129 }
02130 return v;
02131 }
02132
02133 VALUE
02134 rb_to_int(VALUE val)
02135 {
02136 return rb_to_integer(val, "to_int");
02137 }
02138
02139 static VALUE
02140 rb_convert_to_integer(VALUE val, int base)
02141 {
02142 VALUE tmp;
02143
02144 switch (TYPE(val)) {
02145 case T_FLOAT:
02146 if (base != 0) goto arg_error;
02147 if (RFLOAT_VALUE(val) <= (double)FIXNUM_MAX
02148 && RFLOAT_VALUE(val) >= (double)FIXNUM_MIN) {
02149 break;
02150 }
02151 return rb_dbl2big(RFLOAT_VALUE(val));
02152
02153 case T_FIXNUM:
02154 case T_BIGNUM:
02155 if (base != 0) goto arg_error;
02156 return val;
02157
02158 case T_STRING:
02159 string_conv:
02160 return rb_str_to_inum(val, base, TRUE);
02161
02162 case T_NIL:
02163 if (base != 0) goto arg_error;
02164 rb_raise(rb_eTypeError, "can't convert nil into Integer");
02165 break;
02166
02167 default:
02168 break;
02169 }
02170 if (base != 0) {
02171 tmp = rb_check_string_type(val);
02172 if (!NIL_P(tmp)) goto string_conv;
02173 arg_error:
02174 rb_raise(rb_eArgError, "base specified for non string value");
02175 }
02176 tmp = convert_type(val, "Integer", "to_int", FALSE);
02177 if (NIL_P(tmp)) {
02178 return rb_to_integer(val, "to_i");
02179 }
02180 return tmp;
02181
02182 }
02183
02184 VALUE
02185 rb_Integer(VALUE val)
02186 {
02187 return rb_convert_to_integer(val, 0);
02188 }
02189
02190
02191
02192
02193
02194
02195
02196
02197
02198
02199
02200
02201
02202
02203
02204
02205
02206
02207
02208
02209
02210
02211
02212 static VALUE
02213 rb_f_integer(int argc, VALUE *argv, VALUE obj)
02214 {
02215 VALUE arg = Qnil;
02216 int base = 0;
02217
02218 switch (argc) {
02219 case 2:
02220 base = NUM2INT(argv[1]);
02221 case 1:
02222 arg = argv[0];
02223 break;
02224 default:
02225
02226 rb_scan_args(argc, argv, "11", NULL, NULL);
02227 }
02228 return rb_convert_to_integer(arg, base);
02229 }
02230
02231 double
02232 rb_cstr_to_dbl(const char *p, int badcheck)
02233 {
02234 const char *q;
02235 char *end;
02236 double d;
02237 const char *ellipsis = "";
02238 int w;
02239 enum {max_width = 20};
02240 #define OutOfRange() ((end - p > max_width) ? \
02241 (w = max_width, ellipsis = "...") : \
02242 (w = (int)(end - p), ellipsis = ""))
02243
02244 if (!p) return 0.0;
02245 q = p;
02246 while (ISSPACE(*p)) p++;
02247
02248 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02249 return 0.0;
02250 }
02251
02252 d = strtod(p, &end);
02253 if (errno == ERANGE) {
02254 OutOfRange();
02255 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02256 errno = 0;
02257 }
02258 if (p == end) {
02259 if (badcheck) {
02260 bad:
02261 rb_invalid_str(q, "Float()");
02262 }
02263 return d;
02264 }
02265 if (*end) {
02266 char buf[DBL_DIG * 4 + 10];
02267 char *n = buf;
02268 char *e = buf + sizeof(buf) - 1;
02269 char prev = 0;
02270
02271 while (p < end && n < e) prev = *n++ = *p++;
02272 while (*p) {
02273 if (*p == '_') {
02274
02275 if (badcheck) {
02276 if (n == buf || !ISDIGIT(prev)) goto bad;
02277 ++p;
02278 if (!ISDIGIT(*p)) goto bad;
02279 }
02280 else {
02281 while (*++p == '_');
02282 continue;
02283 }
02284 }
02285 prev = *p++;
02286 if (n < e) *n++ = prev;
02287 }
02288 *n = '\0';
02289 p = buf;
02290
02291 if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
02292 return 0.0;
02293 }
02294
02295 d = strtod(p, &end);
02296 if (errno == ERANGE) {
02297 OutOfRange();
02298 rb_warning("Float %.*s%s out of range", w, p, ellipsis);
02299 errno = 0;
02300 }
02301 if (badcheck) {
02302 if (!end || p == end) goto bad;
02303 while (*end && ISSPACE(*end)) end++;
02304 if (*end) goto bad;
02305 }
02306 }
02307 if (errno == ERANGE) {
02308 errno = 0;
02309 OutOfRange();
02310 rb_raise(rb_eArgError, "Float %.*s%s out of range", w, q, ellipsis);
02311 }
02312 return d;
02313 }
02314
02315 double
02316 rb_str_to_dbl(VALUE str, int badcheck)
02317 {
02318 char *s;
02319 long len;
02320 double ret;
02321 VALUE v = 0;
02322
02323 StringValue(str);
02324 s = RSTRING_PTR(str);
02325 len = RSTRING_LEN(str);
02326 if (s) {
02327 if (badcheck && memchr(s, '\0', len)) {
02328 rb_raise(rb_eArgError, "string for Float contains null byte");
02329 }
02330 if (s[len]) {
02331 char *p = ALLOCV(v, len);
02332 MEMCPY(p, s, char, len);
02333 p[len] = '\0';
02334 s = p;
02335 }
02336 }
02337 ret = rb_cstr_to_dbl(s, badcheck);
02338 if (v)
02339 ALLOCV_END(v);
02340 return ret;
02341 }
02342
02343 VALUE
02344 rb_Float(VALUE val)
02345 {
02346 switch (TYPE(val)) {
02347 case T_FIXNUM:
02348 return DBL2NUM((double)FIX2LONG(val));
02349
02350 case T_FLOAT:
02351 return val;
02352
02353 case T_BIGNUM:
02354 return DBL2NUM(rb_big2dbl(val));
02355
02356 case T_STRING:
02357 return DBL2NUM(rb_str_to_dbl(val, TRUE));
02358
02359 case T_NIL:
02360 rb_raise(rb_eTypeError, "can't convert nil into Float");
02361 break;
02362
02363 default:
02364 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02365 }
02366 }
02367
02368
02369
02370
02371
02372
02373
02374
02375
02376
02377
02378
02379
02380 static VALUE
02381 rb_f_float(VALUE obj, VALUE arg)
02382 {
02383 return rb_Float(arg);
02384 }
02385
02386 VALUE
02387 rb_to_float(VALUE val)
02388 {
02389 if (TYPE(val) == T_FLOAT) return val;
02390 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02391 rb_raise(rb_eTypeError, "can't convert %s into Float",
02392 NIL_P(val) ? "nil" :
02393 val == Qtrue ? "true" :
02394 val == Qfalse ? "false" :
02395 rb_obj_classname(val));
02396 }
02397 return rb_convert_type(val, T_FLOAT, "Float", "to_f");
02398 }
02399
02400 VALUE
02401 rb_check_to_float(VALUE val)
02402 {
02403 if (TYPE(val) == T_FLOAT) return val;
02404 if (!rb_obj_is_kind_of(val, rb_cNumeric)) {
02405 return Qnil;
02406 }
02407 return rb_check_convert_type(val, T_FLOAT, "Float", "to_f");
02408 }
02409
02410 double
02411 rb_num2dbl(VALUE val)
02412 {
02413 switch (TYPE(val)) {
02414 case T_FLOAT:
02415 return RFLOAT_VALUE(val);
02416
02417 case T_STRING:
02418 rb_raise(rb_eTypeError, "no implicit conversion to float from string");
02419 break;
02420
02421 case T_NIL:
02422 rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
02423 break;
02424
02425 default:
02426 break;
02427 }
02428
02429 return RFLOAT_VALUE(rb_Float(val));
02430 }
02431
02432 VALUE
02433 rb_String(VALUE val)
02434 {
02435 VALUE tmp = rb_check_string_type(val);
02436 if (NIL_P(tmp))
02437 tmp = rb_convert_type(val, T_STRING, "String", "to_s");
02438 return tmp;
02439 }
02440
02441
02442
02443
02444
02445
02446
02447
02448
02449
02450
02451
02452
02453
02454 static VALUE
02455 rb_f_string(VALUE obj, VALUE arg)
02456 {
02457 return rb_String(arg);
02458 }
02459
02460 VALUE
02461 rb_Array(VALUE val)
02462 {
02463 VALUE tmp = rb_check_array_type(val);
02464
02465 if (NIL_P(tmp)) {
02466 tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a");
02467 if (NIL_P(tmp)) {
02468 return rb_ary_new3(1, val);
02469 }
02470 }
02471 return tmp;
02472 }
02473
02474
02475
02476
02477
02478
02479
02480
02481
02482
02483
02484 static VALUE
02485 rb_f_array(VALUE obj, VALUE arg)
02486 {
02487 return rb_Array(arg);
02488 }
02489
02490
02491
02492
02493
02494
02495
02496
02497
02498
02499
02500
02501
02502
02503
02504
02505
02506
02507
02508
02509
02510
02511
02512
02513
02514
02515
02516
02517
02518
02519
02520
02521
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531
02532
02533
02534
02535
02536
02537
02538
02539
02540
02541
02542
02543
02544
02545
02546
02547
02548
02567
02568
02569
02570
02571
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
02591
02592
02593
02594
02595
02596
02597
02598
02599
02600
02601
02602
02603
02604
02605
02606
02607
02608
02609
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622
02623
02624
02625
02626
02627
02628
02629
02630
02631
02632
02633
02634 void
02635 Init_Object(void)
02636 {
02637 int i;
02638
02639 Init_class_hierarchy();
02640
02641 #if 0
02642
02643 rb_cBasicObject = rb_define_class("BasicObject", Qnil);
02644 rb_cObject = rb_define_class("Object", rb_cBasicObject);
02645 rb_cModule = rb_define_class("Module", rb_cObject);
02646 rb_cClass = rb_define_class("Class", rb_cModule);
02647 #endif
02648
02649 #undef rb_intern
02650 #define rb_intern(str) rb_intern_const(str)
02651
02652 rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
02653 rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
02654 rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
02655 rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
02656 rb_define_method(rb_cBasicObject, "!", rb_obj_not, 0);
02657 rb_define_method(rb_cBasicObject, "!=", rb_obj_not_equal, 1);
02658
02659 rb_define_private_method(rb_cBasicObject, "singleton_method_added", rb_obj_dummy, 1);
02660 rb_define_private_method(rb_cBasicObject, "singleton_method_removed", rb_obj_dummy, 1);
02661 rb_define_private_method(rb_cBasicObject, "singleton_method_undefined", rb_obj_dummy, 1);
02662
02663 rb_mKernel = rb_define_module("Kernel");
02664 rb_include_module(rb_cObject, rb_mKernel);
02665 rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
02666 rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
02667 rb_define_private_method(rb_cModule, "extended", rb_obj_dummy, 1);
02668 rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
02669 rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
02670 rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
02671
02672 rb_define_method(rb_mKernel, "nil?", rb_false, 0);
02673 rb_define_method(rb_mKernel, "===", rb_equal, 1);
02674 rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
02675 rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
02676 rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
02677 rb_define_method(rb_mKernel, "hash", rb_obj_hash, 0);
02678 rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
02679
02680 rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
02681 rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
02682 rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
02683 rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
02684 rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
02685 rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
02686 rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
02687
02688 rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
02689 rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
02690 rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
02691 rb_define_method(rb_mKernel, "untrust", rb_obj_untrust, 0);
02692 rb_define_method(rb_mKernel, "untrusted?", rb_obj_untrusted, 0);
02693 rb_define_method(rb_mKernel, "trust", rb_obj_trust, 0);
02694 rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
02695 rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
02696
02697 rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
02698 rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
02699 rb_define_method(rb_mKernel, "methods", rb_obj_methods, -1);
02700 rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1);
02701 rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, -1);
02702 rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, -1);
02703 rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, -1);
02704 rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0);
02705 rb_define_method(rb_mKernel, "instance_variable_get", rb_obj_ivar_get, 1);
02706 rb_define_method(rb_mKernel, "instance_variable_set", rb_obj_ivar_set, 2);
02707 rb_define_method(rb_mKernel, "instance_variable_defined?", rb_obj_ivar_defined, 1);
02708 rb_define_private_method(rb_mKernel, "remove_instance_variable",
02709 rb_obj_remove_instance_variable, 1);
02710
02711 rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
02712 rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
02713 rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
02714 rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
02715
02716 rb_define_global_function("sprintf", rb_f_sprintf, -1);
02717 rb_define_global_function("format", rb_f_sprintf, -1);
02718
02719 rb_define_global_function("Integer", rb_f_integer, -1);
02720 rb_define_global_function("Float", rb_f_float, 1);
02721
02722 rb_define_global_function("String", rb_f_string, 1);
02723 rb_define_global_function("Array", rb_f_array, 1);
02724
02725 rb_cNilClass = rb_define_class("NilClass", rb_cObject);
02726 rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
02727 rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
02728 rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
02729 rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
02730 rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
02731 rb_define_method(rb_cNilClass, "&", false_and, 1);
02732 rb_define_method(rb_cNilClass, "|", false_or, 1);
02733 rb_define_method(rb_cNilClass, "^", false_xor, 1);
02734
02735 rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
02736 rb_undef_alloc_func(rb_cNilClass);
02737 rb_undef_method(CLASS_OF(rb_cNilClass), "new");
02738
02739
02740
02741 rb_define_global_const("NIL", Qnil);
02742
02743 rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
02744 rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
02745 rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
02746 rb_define_method(rb_cModule, "<=>", rb_mod_cmp, 1);
02747 rb_define_method(rb_cModule, "<", rb_mod_lt, 1);
02748 rb_define_method(rb_cModule, "<=", rb_class_inherited_p, 1);
02749 rb_define_method(rb_cModule, ">", rb_mod_gt, 1);
02750 rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
02751 rb_define_method(rb_cModule, "initialize_copy", rb_mod_init_copy, 1);
02752 rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
02753 rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0);
02754 rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1);
02755 rb_define_method(rb_cModule, "name", rb_mod_name, 0);
02756 rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0);
02757
02758 rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
02759 rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
02760 rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
02761 rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
02762
02763 rb_define_alloc_func(rb_cModule, rb_module_s_alloc);
02764 rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
02765 rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1);
02766 rb_define_method(rb_cModule, "public_instance_methods",
02767 rb_class_public_instance_methods, -1);
02768 rb_define_method(rb_cModule, "protected_instance_methods",
02769 rb_class_protected_instance_methods, -1);
02770 rb_define_method(rb_cModule, "private_instance_methods",
02771 rb_class_private_instance_methods, -1);
02772
02773 rb_define_method(rb_cModule, "constants", rb_mod_constants, -1);
02774 rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
02775 rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
02776 rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1);
02777 rb_define_private_method(rb_cModule, "remove_const",
02778 rb_mod_remove_const, 1);
02779 rb_define_method(rb_cModule, "const_missing",
02780 rb_mod_const_missing, 1);
02781 rb_define_method(rb_cModule, "class_variables",
02782 rb_mod_class_variables, 0);
02783 rb_define_method(rb_cModule, "remove_class_variable",
02784 rb_mod_remove_cvar, 1);
02785 rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
02786 rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
02787 rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
02788 rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1);
02789 rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1);
02790
02791 rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
02792 rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
02793 rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
02794 rb_define_method(rb_cClass, "initialize_copy", rb_class_init_copy, 1);
02795 rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
02796 rb_define_alloc_func(rb_cClass, rb_class_s_alloc);
02797 rb_undef_method(rb_cClass, "extend_object");
02798 rb_undef_method(rb_cClass, "append_features");
02799
02800 rb_cData = rb_define_class("Data", rb_cObject);
02801 rb_undef_alloc_func(rb_cData);
02802
02803 rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
02804 rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
02805 rb_define_method(rb_cTrueClass, "&", true_and, 1);
02806 rb_define_method(rb_cTrueClass, "|", true_or, 1);
02807 rb_define_method(rb_cTrueClass, "^", true_xor, 1);
02808 rb_undef_alloc_func(rb_cTrueClass);
02809 rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
02810
02811
02812
02813 rb_define_global_const("TRUE", Qtrue);
02814
02815 rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
02816 rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
02817 rb_define_method(rb_cFalseClass, "&", false_and, 1);
02818 rb_define_method(rb_cFalseClass, "|", false_or, 1);
02819 rb_define_method(rb_cFalseClass, "^", false_xor, 1);
02820 rb_undef_alloc_func(rb_cFalseClass);
02821 rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
02822
02823
02824
02825 rb_define_global_const("FALSE", Qfalse);
02826
02827 id_eq = rb_intern("==");
02828 id_eql = rb_intern("eql?");
02829 id_match = rb_intern("=~");
02830 id_inspect = rb_intern("inspect");
02831 id_init_copy = rb_intern("initialize_copy");
02832 id_init_clone = rb_intern("initialize_clone");
02833 id_init_dup = rb_intern("initialize_dup");
02834
02835 for (i=0; conv_method_names[i].method; i++) {
02836 conv_method_names[i].id = rb_intern(conv_method_names[i].method);
02837 }
02838 }
02839