object.c


DEFINITIONS

This source file includes following functions.
  1. rb_equal
  2. rb_eql
  3. rb_obj_equal
  4. rb_obj_id
  5. rb_class_real
  6. rb_obj_class
  7. copy_object
  8. rb_obj_clone
  9. rb_obj_dup
  10. rb_obj_become
  11. rb_any_to_a
  12. rb_any_to_s
  13. rb_inspect
  14. inspect_i
  15. inspect_obj
  16. rb_obj_inspect
  17. rb_obj_is_instance_of
  18. rb_obj_is_kind_of
  19. rb_obj_dummy
  20. rb_obj_tainted
  21. rb_obj_taint
  22. rb_obj_untaint
  23. rb_obj_freeze
  24. rb_obj_frozen_p
  25. nil_to_i
  26. nil_to_f
  27. nil_to_s
  28. nil_to_a
  29. nil_inspect
  30. nil_plus
  31. main_to_s
  32. true_to_s
  33. true_and
  34. true_or
  35. true_xor
  36. false_to_s
  37. false_and
  38. false_or
  39. false_xor
  40. rb_true
  41. rb_false
  42. sym_to_i
  43. sym_inspect
  44. sym_to_s
  45. sym_intern
  46. rb_mod_to_s
  47. rb_mod_eqq
  48. rb_mod_le
  49. rb_mod_lt
  50. rb_mod_ge
  51. rb_mod_gt
  52. rb_mod_cmp
  53. rb_mod_initialize
  54. rb_class_initialize
  55. rb_module_s_alloc
  56. rb_class_s_new
  57. rb_obj_alloc
  58. rb_class_allocate_instance
  59. rb_class_new_instance
  60. rb_class_superclass
  61. rb_to_id
  62. rb_mod_attr
  63. rb_mod_attr_reader
  64. rb_mod_attr_writer
  65. rb_mod_attr_accessor
  66. rb_mod_const_get
  67. rb_mod_const_set
  68. rb_mod_const_defined
  69. rb_obj_methods
  70. rb_obj_protected_methods
  71. rb_obj_private_methods
  72. convert_type
  73. rb_convert_type
  74. rb_check_convert_type
  75. rb_to_integer
  76. rb_to_int
  77. rb_Integer
  78. rb_f_integer
  79. rb_cstr_to_dbl
  80. rb_str_to_dbl
  81. rb_Float
  82. rb_f_float
  83. rb_num2dbl
  84. rb_str2cstr
  85. rb_String
  86. rb_f_string
  87. rb_Array
  88. rb_f_array
  89. boot_defclass
  90. Init_Object


   1  /**********************************************************************
   2  
   3    object.c -
   4  
   5    $Author: matz $
   6    $Date: 2002/09/04 06:37:35 $
   7    created at: Thu Jul 15 12:01:24 JST 1993
   8  
   9    Copyright (C) 1993-2002 Yukihiro Matsumoto
  10    Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
  11    Copyright (C) 2000  Information-technology Promotion Agency, Japan
  12  
  13  **********************************************************************/
  14  
  15  #include "ruby.h"
  16  #include "st.h"
  17  #include "util.h"
  18  #include <stdio.h>
  19  #include <errno.h>
  20  #include <ctype.h>
  21  #include <math.h>
  22  
  23  VALUE rb_mKernel;
  24  VALUE rb_cObject;
  25  VALUE rb_cModule;
  26  VALUE rb_cClass;
  27  VALUE rb_cData;
  28  
  29  VALUE rb_cNilClass;
  30  VALUE rb_cTrueClass;
  31  VALUE rb_cFalseClass;
  32  VALUE rb_cSymbol;
  33  
  34  static ID eq, eql;
  35  static ID inspect;
  36  static ID become;
  37  static ID alloc;
  38  
  39  VALUE
  40  rb_equal(obj1, obj2)
  41      VALUE obj1, obj2;
  42  {
  43      VALUE result;
  44  
  45      if (obj1 == obj2) return Qtrue;
  46      result = rb_funcall(obj1, eq, 1, obj2);
  47      if (RTEST(result)) return Qtrue;
  48      return Qfalse;
  49  }
  50  
  51  int
  52  rb_eql(obj1, obj2)
  53      VALUE obj1, obj2;
  54  {
  55      return RTEST(rb_funcall(obj1, eql, 1, obj2));
  56  }
  57  
  58  static VALUE
  59  rb_obj_equal(obj1, obj2)
  60      VALUE obj1, obj2;
  61  {
  62      if (obj1 == obj2) return Qtrue;
  63      return Qfalse;
  64  }
  65  
  66  VALUE
  67  rb_obj_id(obj)
  68      VALUE obj;
  69  {
  70      if (SPECIAL_CONST_P(obj)) {
  71          return LONG2NUM((long)obj);
  72      }
  73      return (VALUE)((long)obj|FIXNUM_FLAG);
  74  }
  75  
  76  VALUE
  77  rb_class_real(cl)
  78      VALUE cl;
  79  {
  80      while (FL_TEST(cl, FL_SINGLETON) || TYPE(cl) == T_ICLASS) {
  81          cl = RCLASS(cl)->super;
  82      }
  83      return cl;
  84  }
  85  
  86  VALUE
  87  rb_obj_class(obj)
  88      VALUE obj;
  89  {
  90      return rb_class_real(CLASS_OF(obj));
  91  }
  92  
  93  static void
  94  copy_object(dest, obj)
  95      VALUE dest, obj;
  96  {
  97      if (OBJ_FROZEN(dest)) {
  98          rb_raise(rb_eTypeError, "[bug] frozen object (%s) allocated", rb_class2name(CLASS_OF(dest)));
  99      }
 100      RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
 101      RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR|FL_TAINT);
 102      rb_funcall(dest, become, 1, obj);
 103      if (FL_TEST(obj, FL_EXIVAR)) {
 104          rb_copy_generic_ivar(dest, obj);
 105      }
 106      switch (TYPE(obj)) {
 107        case T_OBJECT:
 108        case T_CLASS:
 109        case T_MODULE:
 110          if (ROBJECT(dest)->iv_tbl) {
 111              st_free_table(ROBJECT(dest)->iv_tbl);
 112              ROBJECT(dest)->iv_tbl = 0;
 113          }
 114          if (ROBJECT(obj)->iv_tbl) {
 115              ROBJECT(dest)->iv_tbl = st_copy(ROBJECT(obj)->iv_tbl);
 116          }
 117      }
 118  }
 119  
 120  VALUE
 121  rb_obj_clone(obj)
 122      VALUE obj;
 123  {
 124      VALUE clone;
 125  
 126      if (rb_special_const_p(obj)) {
 127          rb_raise(rb_eTypeError, "can't clone %s", rb_class2name(CLASS_OF(obj)));
 128      }
 129      clone = rb_obj_alloc(rb_obj_class(obj));
 130      copy_object(clone, obj);
 131      RBASIC(clone)->klass = rb_singleton_class_clone(obj);
 132      RBASIC(clone)->flags = RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT);
 133  
 134      return clone;
 135  }
 136  
 137  VALUE
 138  rb_obj_dup(obj)
 139      VALUE obj;
 140  {
 141      VALUE dup;
 142  
 143      if (rb_special_const_p(obj)) {
 144          rb_raise(rb_eTypeError, "can't dup %s", rb_class2name(CLASS_OF(obj)));
 145      }
 146      dup = rb_obj_alloc(rb_obj_class(obj));
 147      copy_object(dup, obj);
 148  
 149      return dup;
 150  }
 151  
 152  VALUE
 153  rb_obj_become(obj, orig)
 154      VALUE obj, orig;
 155  {
 156      if (obj == orig) return obj;
 157      rb_check_frozen(obj);
 158      if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
 159          rb_raise(rb_eTypeError, "become should take same class object");
 160      }
 161      return obj;
 162  }
 163  
 164  static VALUE
 165  rb_any_to_a(obj)
 166      VALUE obj;
 167  {
 168      rb_warn("default `to_a' will be obsolete");
 169      return rb_ary_new3(1, obj);
 170  }
 171  
 172  VALUE
 173  rb_any_to_s(obj)
 174      VALUE obj;
 175  {
 176      char *cname = rb_class2name(CLASS_OF(obj));
 177      VALUE str;
 178  
 179      str = rb_str_new(0, strlen(cname)+6+16+1); /* 6:tags 16:addr 1:nul */
 180      sprintf(RSTRING(str)->ptr, "#<%s:0x%lx>", cname, obj);
 181      RSTRING(str)->len = strlen(RSTRING(str)->ptr);
 182      if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
 183  
 184      return str;
 185  }
 186  
 187  VALUE
 188  rb_inspect(obj)
 189      VALUE obj;
 190  {
 191      return rb_obj_as_string(rb_funcall(obj, inspect, 0, 0));
 192  }
 193  
 194  static int
 195  inspect_i(id, value, str)
 196      ID id;
 197      VALUE value;
 198      VALUE str;
 199  {
 200      VALUE str2;
 201      char *ivname;
 202  
 203      /* need not to show internal data */
 204      if (CLASS_OF(value) == 0) return ST_CONTINUE;
 205      if (!rb_is_instance_id(id)) return ST_CONTINUE;
 206      if (RSTRING(str)->ptr[0] == '-') { /* first element */
 207          RSTRING(str)->ptr[0] = '#';
 208          rb_str_cat2(str, " ");
 209      }
 210      else {
 211          rb_str_cat2(str, ", ");
 212      }
 213      ivname = rb_id2name(id);
 214      rb_str_cat2(str, ivname);
 215      rb_str_cat2(str, "=");
 216      str2 = rb_inspect(value);
 217      rb_str_append(str, str2);
 218      OBJ_INFECT(str, str2);
 219  
 220      return ST_CONTINUE;
 221  }
 222  
 223  static VALUE
 224  inspect_obj(obj, str)
 225      VALUE obj, str;
 226  {
 227      st_foreach(ROBJECT(obj)->iv_tbl, inspect_i, str);
 228      rb_str_cat2(str, ">");
 229      RSTRING(str)->ptr[0] = '#';
 230      OBJ_INFECT(str, obj);
 231  
 232      return str;
 233  }
 234  
 235  static VALUE
 236  rb_obj_inspect(obj)
 237      VALUE obj;
 238  {
 239      if (TYPE(obj) == T_OBJECT
 240          && ROBJECT(obj)->iv_tbl
 241          && ROBJECT(obj)->iv_tbl->num_entries > 0) {
 242          VALUE str;
 243          char *c;
 244  
 245          c = rb_class2name(CLASS_OF(obj));
 246          if (rb_inspecting_p(obj)) {
 247              str = rb_str_new(0, strlen(c)+10+16+1); /* 10:tags 16:addr 1:nul */
 248              sprintf(RSTRING(str)->ptr, "#<%s:0x%lx ...>", c, obj);
 249              RSTRING(str)->len = strlen(RSTRING(str)->ptr);
 250              return str;
 251          }
 252          str = rb_str_new(0, strlen(c)+6+16+1); /* 6:tags 16:addr 1:nul */
 253          sprintf(RSTRING(str)->ptr, "-<%s:0x%lx", c, obj);
 254          RSTRING(str)->len = strlen(RSTRING(str)->ptr);
 255          return rb_protect_inspect(inspect_obj, obj, str);
 256      }
 257      return rb_funcall(obj, rb_intern("to_s"), 0, 0);
 258  }
 259  
 260  VALUE
 261  rb_obj_is_instance_of(obj, c)
 262      VALUE obj, c;
 263  {
 264      switch (TYPE(c)) {
 265        case T_MODULE:
 266        case T_CLASS:
 267        case T_ICLASS:
 268          break;
 269        default:
 270          rb_raise(rb_eTypeError, "class or module required");
 271      }
 272  
 273      if (rb_obj_class(obj) == c) return Qtrue;
 274      return Qfalse;
 275  }
 276  
 277  VALUE
 278  rb_obj_is_kind_of(obj, c)
 279      VALUE obj, c;
 280  {
 281      VALUE cl = CLASS_OF(obj);
 282  
 283      switch (TYPE(c)) {
 284        case T_MODULE:
 285        case T_CLASS:
 286        case T_ICLASS:
 287          break;
 288  
 289        default:
 290          rb_raise(rb_eTypeError, "class or module required");
 291      }
 292  
 293      while (cl) {
 294          if (cl == c || RCLASS(cl)->m_tbl == RCLASS(c)->m_tbl)
 295              return Qtrue;
 296          cl = RCLASS(cl)->super;
 297      }
 298      return Qfalse;
 299  }
 300  
 301  static VALUE
 302  rb_obj_dummy()
 303  {
 304      return Qnil;
 305  }
 306  
 307  VALUE
 308  rb_obj_tainted(obj)
 309      VALUE obj;
 310  {
 311      if (OBJ_TAINTED(obj))
 312          return Qtrue;
 313      return Qfalse;
 314  }
 315  
 316  VALUE
 317  rb_obj_taint(obj)
 318      VALUE obj;
 319  {
 320      rb_secure(4);
 321      if (!OBJ_TAINTED(obj)) {
 322          if (OBJ_FROZEN(obj)) {
 323              rb_error_frozen("object");
 324          }
 325          OBJ_TAINT(obj);
 326      }
 327      return obj;
 328  }
 329  
 330  VALUE
 331  rb_obj_untaint(obj)
 332      VALUE obj;
 333  {
 334      rb_secure(3);
 335      if (OBJ_TAINTED(obj)) {
 336          if (OBJ_FROZEN(obj)) {
 337              rb_error_frozen("object");
 338          }
 339          FL_UNSET(obj, FL_TAINT);
 340      }
 341      return obj;
 342  }
 343  
 344  VALUE
 345  rb_obj_freeze(obj)
 346      VALUE obj;
 347  {
 348      if (!OBJ_FROZEN(obj)) {
 349          if (rb_safe_level() >= 4 && !OBJ_TAINTED(obj)) {
 350              rb_raise(rb_eSecurityError, "Insecure: can't freeze object");
 351          }
 352          OBJ_FREEZE(obj);
 353      }
 354      return obj;
 355  }
 356  
 357  static VALUE
 358  rb_obj_frozen_p(obj)
 359      VALUE obj;
 360  {
 361      if (OBJ_FROZEN(obj)) return Qtrue;
 362      return Qfalse;
 363  }
 364  
 365  static VALUE
 366  nil_to_i(obj)
 367      VALUE obj;
 368  {
 369      return INT2FIX(0);
 370  }
 371  
 372  static VALUE
 373  nil_to_f(obj)
 374      VALUE obj;
 375  {
 376      return rb_float_new(0.0);
 377  }
 378  
 379  static VALUE
 380  nil_to_s(obj)
 381      VALUE obj;
 382  {
 383      return rb_str_new2("");
 384  }
 385  
 386  static VALUE
 387  nil_to_a(obj)
 388      VALUE obj;
 389  {
 390      return rb_ary_new2(0);
 391  }
 392  
 393  static VALUE
 394  nil_inspect(obj)
 395      VALUE obj;
 396  {
 397      return rb_str_new2("nil");
 398  }
 399  
 400  #ifdef NIL_PLUS
 401  static VALUE
 402  nil_plus(x, y)
 403      VALUE x, y;
 404  {
 405      switch (TYPE(y)) {
 406        case T_NIL:
 407        case T_FIXNUM:
 408        case T_FLOAT:
 409        case T_BIGNUM:
 410        case T_STRING:
 411        case T_ARRAY:
 412          return y;
 413        default:
 414          rb_raise(rb_eTypeError, "tried to add %s(%s) to nil",
 415                   RSTRING(rb_inspect(y))->ptr,
 416                   rb_class2name(CLASS_OF(y)));
 417      }
 418      /* not reached */
 419  }
 420  #endif
 421  
 422  static VALUE
 423  main_to_s(obj)
 424      VALUE obj;
 425  {
 426      return rb_str_new2("main");
 427  }
 428  
 429  static VALUE
 430  true_to_s(obj)
 431      VALUE obj;
 432  {
 433      return rb_str_new2("true");
 434  }
 435  
 436  static VALUE
 437  true_and(obj, obj2)
 438      VALUE obj, obj2;
 439  {
 440      return RTEST(obj2)?Qtrue:Qfalse;
 441  }
 442  
 443  static VALUE
 444  true_or(obj, obj2)
 445      VALUE obj, obj2;
 446  {
 447      return Qtrue;
 448  }
 449  
 450  static VALUE
 451  true_xor(obj, obj2)
 452      VALUE obj, obj2;
 453  {
 454      return RTEST(obj2)?Qfalse:Qtrue;
 455  }
 456  
 457  static VALUE
 458  false_to_s(obj)
 459      VALUE obj;
 460  {
 461      return rb_str_new2("false");
 462  }
 463  
 464  static VALUE
 465  false_and(obj, obj2)
 466      VALUE obj, obj2;
 467  {
 468      return Qfalse;
 469  }
 470  
 471  static VALUE
 472  false_or(obj, obj2)
 473      VALUE obj, obj2;
 474  {
 475      return RTEST(obj2)?Qtrue:Qfalse;
 476  }
 477  
 478  static VALUE
 479  false_xor(obj, obj2)
 480      VALUE obj, obj2;
 481  {
 482      return RTEST(obj2)?Qtrue:Qfalse;
 483  }
 484  
 485  static VALUE
 486  rb_true(obj)
 487      VALUE obj;
 488  {
 489      return Qtrue;
 490  }
 491  
 492  static VALUE
 493  rb_false(obj)
 494      VALUE obj;
 495  {
 496      return Qfalse;
 497  }
 498  
 499  static VALUE
 500  sym_to_i(sym)
 501      VALUE sym;
 502  {
 503      ID id = SYM2ID(sym);
 504  
 505      return LONG2FIX(id);
 506  }
 507  
 508  static VALUE
 509  sym_inspect(sym)
 510      VALUE sym;
 511  {
 512      VALUE str;
 513      char *name;
 514  
 515      name = rb_id2name(SYM2ID(sym));
 516      str = rb_str_new(0, strlen(name)+1);
 517      RSTRING(str)->ptr[0] = ':';
 518      strcpy(RSTRING(str)->ptr+1, name);
 519      return str;
 520  }
 521  
 522  static VALUE
 523  sym_to_s(sym)
 524      VALUE sym;
 525  {
 526      return rb_str_new2(rb_id2name(SYM2ID(sym)));
 527  }
 528  
 529  static VALUE
 530  sym_intern(sym)
 531      VALUE sym;
 532  {
 533      return sym;
 534  }
 535  
 536  static VALUE
 537  rb_mod_to_s(klass)
 538      VALUE klass;
 539  
 540  {
 541      if (FL_TEST(klass, FL_SINGLETON)) {
 542          VALUE s = rb_str_new2("#<");
 543          VALUE v = rb_iv_get(klass, "__attached__");
 544  
 545          rb_str_cat2(s, "Class:");
 546          switch (TYPE(v)) {
 547            case T_CLASS: case T_MODULE:
 548              rb_str_append(s, rb_inspect(v));
 549              break;
 550            default:
 551              rb_str_append(s, rb_any_to_s(v));
 552              break;
 553          }
 554          rb_str_cat2(s, ">");
 555  
 556          return s;
 557      }
 558      return rb_str_dup(rb_class_path(klass));
 559  }
 560  
 561  static VALUE
 562  rb_mod_eqq(mod, arg)
 563      VALUE mod, arg;
 564  {
 565      return rb_obj_is_kind_of(arg, mod);
 566  }
 567  
 568  static VALUE
 569  rb_mod_le(mod, arg)
 570      VALUE mod, arg;
 571  {
 572      if (mod == arg) return Qtrue;
 573      switch (TYPE(arg)) {
 574        case T_MODULE:
 575        case T_CLASS:
 576          break;
 577        default:
 578          rb_raise(rb_eTypeError, "compared with non class/module");
 579      }
 580  
 581      while (mod) {
 582          if (RCLASS(mod)->m_tbl == RCLASS(arg)->m_tbl)
 583              return Qtrue;
 584          mod = RCLASS(mod)->super;
 585      }
 586      return Qfalse;
 587  }
 588  
 589  static VALUE
 590  rb_mod_lt(mod, arg)
 591      VALUE mod, arg;
 592  {
 593      if (mod == arg) return Qfalse;
 594      return rb_mod_le(mod, arg);
 595  }
 596  
 597  static VALUE
 598  rb_mod_ge(mod, arg)
 599      VALUE mod, arg;
 600  {
 601      switch (TYPE(arg)) {
 602        case T_MODULE:
 603        case T_CLASS:
 604          break;
 605        default:
 606          rb_raise(rb_eTypeError, "compared with non class/module");
 607      }
 608  
 609      return rb_mod_le(arg, mod);
 610  }
 611  
 612  static VALUE
 613  rb_mod_gt(mod, arg)
 614      VALUE mod, arg;
 615  {
 616      if (mod == arg) return Qfalse;
 617      return rb_mod_ge(mod, arg);
 618  }
 619  
 620  static VALUE
 621  rb_mod_cmp(mod, arg)
 622      VALUE mod, arg;
 623  {
 624      VALUE start = mod;
 625  
 626      if (mod == arg) return INT2FIX(0);
 627      switch (TYPE(arg)) {
 628        case T_MODULE:
 629        case T_CLASS:
 630          break;
 631        default:
 632          rb_raise(rb_eTypeError, "<=> requires Class or Module (%s given)",
 633                   rb_class2name(CLASS_OF(arg)));
 634          break;
 635      }
 636  
 637      if (rb_mod_le(mod, arg)) {
 638          return INT2FIX(-1);
 639      }
 640  
 641      while (arg) {
 642          if (RCLASS(arg)->m_tbl == RCLASS(start)->m_tbl)
 643              return INT2FIX(1);
 644          arg = RCLASS(arg)->super;
 645      }
 646      return Qnil;
 647  }
 648  
 649  static VALUE
 650  rb_mod_initialize(module)
 651      VALUE module;
 652  {
 653      if (rb_block_given_p()) {
 654          rb_mod_module_eval(0, 0, module);
 655      }
 656      return Qnil;
 657  }
 658  
 659  static VALUE
 660  rb_class_initialize(argc, argv, klass)
 661      int argc;
 662      VALUE *argv;
 663      VALUE klass;
 664  {
 665      return rb_mod_initialize(klass);
 666  }
 667  
 668  static VALUE
 669  rb_module_s_alloc(klass)
 670      VALUE klass;
 671  {
 672      VALUE mod = rb_module_new();
 673  
 674      RBASIC(mod)->klass = klass;
 675      return mod;
 676  }
 677  
 678  static VALUE
 679  rb_class_s_new(argc, argv)
 680      int argc;
 681      VALUE *argv;
 682  {
 683      VALUE super, klass;
 684  
 685      if (rb_scan_args(argc, argv, "01", &super) == 0) {
 686          super = rb_cObject;
 687      }
 688      klass = rb_class_new(super);
 689      rb_make_metaclass(klass, RBASIC(super)->klass);
 690      rb_obj_call_init(klass, argc, argv);
 691      rb_class_inherited(super, klass);
 692  
 693      return klass;
 694  }
 695  
 696  VALUE
 697  rb_obj_alloc(klass)
 698      VALUE klass;
 699  {
 700      VALUE obj = rb_funcall(klass, alloc, 0, 0);
 701  
 702      if (rb_obj_class(obj) != rb_class_real(klass)) {
 703          rb_raise(rb_eTypeError, "wrong instance allocation");
 704      }
 705      return obj;
 706  }
 707  
 708  static VALUE
 709  rb_class_allocate_instance(klass)
 710      VALUE klass;
 711  {
 712      if (FL_TEST(klass, FL_SINGLETON)) {
 713          rb_raise(rb_eTypeError, "can't create instance of virtual class");
 714      }
 715      if (rb_frame_last_func() != alloc) {
 716          return rb_obj_alloc(klass);
 717      }
 718      else {
 719          NEWOBJ(obj, struct RObject);
 720          OBJSETUP(obj, klass, T_OBJECT);
 721          return (VALUE)obj;
 722      }
 723  }
 724  
 725  VALUE
 726  rb_class_new_instance(argc, argv, klass)
 727      int argc;
 728      VALUE *argv;
 729      VALUE klass;
 730  {
 731      VALUE obj;
 732  
 733      obj = rb_obj_alloc(klass);
 734      rb_obj_call_init(obj, argc, argv);
 735  
 736      return obj;
 737  }
 738  
 739  static VALUE
 740  rb_class_superclass(klass)
 741      VALUE klass;
 742  {
 743      VALUE super = RCLASS(klass)->super;
 744  
 745      while (TYPE(super) == T_ICLASS) {
 746          super = RCLASS(super)->super;
 747      }
 748      if (!super) {
 749          return Qnil;
 750      }
 751      return super;
 752  }
 753  
 754  ID
 755  rb_to_id(name)
 756      VALUE name;
 757  {
 758      ID id;
 759  
 760      switch (TYPE(name)) {
 761        case T_STRING:
 762          return rb_intern(RSTRING(name)->ptr);
 763        case T_FIXNUM:
 764          id = FIX2LONG(name);
 765          if (!rb_id2name(id)) {
 766              rb_raise(rb_eArgError, "%d is not a symbol", id);
 767          }
 768          break;
 769        case T_SYMBOL:
 770          id = SYM2ID(name);
 771          break;
 772        default:
 773          rb_raise(rb_eTypeError, "%s is not a symbol", RSTRING(rb_inspect(name))->ptr);
 774      }
 775      return id;
 776  }
 777  
 778  static VALUE
 779  rb_mod_attr(argc, argv, klass)
 780      int argc;
 781      VALUE *argv;
 782      VALUE klass;
 783  {
 784      VALUE name, pub;
 785  
 786      rb_scan_args(argc, argv, "11", &name, &pub);
 787      rb_attr(klass, rb_to_id(name), 1, RTEST(pub), Qtrue);
 788      return Qnil;
 789  }
 790  
 791  static VALUE
 792  rb_mod_attr_reader(argc, argv, klass)
 793      int argc;
 794      VALUE *argv;
 795      VALUE klass;
 796  {
 797      int i;
 798  
 799      for (i=0; i<argc; i++) {
 800          rb_attr(klass, rb_to_id(argv[i]), 1, 0, Qtrue);
 801      }
 802      return Qnil;
 803  }
 804  
 805  static VALUE
 806  rb_mod_attr_writer(argc, argv, klass)
 807      int argc;
 808      VALUE *argv;
 809      VALUE klass;
 810  {
 811      int i;
 812  
 813      for (i=0; i<argc; i++) {
 814          rb_attr(klass, rb_to_id(argv[i]), 0, 1, Qtrue);
 815      }
 816      return Qnil;
 817  }
 818  
 819  static VALUE
 820  rb_mod_attr_accessor(argc, argv, klass)
 821      int argc;
 822      VALUE *argv;
 823      VALUE klass;
 824  {
 825      int i;
 826  
 827      for (i=0; i<argc; i++) {
 828          rb_attr(klass, rb_to_id(argv[i]), 1, 1, Qtrue);
 829      }
 830      return Qnil;
 831  }
 832  
 833  static VALUE
 834  rb_mod_const_get(mod, name)
 835      VALUE mod, name;
 836  {
 837      ID id = rb_to_id(name);
 838  
 839      if (!rb_is_const_id(id)) {
 840          rb_name_error(id, "wrong constant name %s", rb_id2name(id));
 841      }
 842      return rb_const_get(mod, id);
 843  }
 844  
 845  static VALUE
 846  rb_mod_const_set(mod, name, value)
 847      VALUE mod, name, value;
 848  {
 849      ID id = rb_to_id(name);
 850  
 851      if (!rb_is_const_id(id)) {
 852          rb_name_error(id, "wrong constant name %s", rb_id2name(id));
 853      }
 854      rb_const_set(mod, id, value);
 855      return value;
 856  }
 857  
 858  static VALUE
 859  rb_mod_const_defined(mod, name)
 860      VALUE mod, name;
 861  {
 862      ID id = rb_to_id(name);
 863  
 864      if (!rb_is_const_id(id)) {
 865          rb_name_error(id, "wrong constant name %s", rb_id2name(id));
 866      }
 867      return rb_const_defined_at(mod, id);
 868  }
 869  
 870  static VALUE
 871  rb_obj_methods(obj)
 872      VALUE obj;
 873  {
 874      VALUE argv[1];
 875  
 876      argv[0] = Qtrue;
 877      return rb_class_instance_methods(1, argv, CLASS_OF(obj));
 878  }
 879  
 880  static VALUE
 881  rb_obj_protected_methods(obj)
 882      VALUE obj;
 883  {
 884      VALUE argv[1];
 885  
 886      argv[0] = Qtrue;
 887      return rb_class_protected_instance_methods(1, argv, CLASS_OF(obj));
 888  }
 889  
 890  static VALUE
 891  rb_obj_private_methods(obj)
 892      VALUE obj;
 893  {
 894      VALUE argv[1];
 895  
 896      argv[0] = Qtrue;
 897      return rb_class_private_instance_methods(1, argv, CLASS_OF(obj));
 898  }
 899  
 900  static VALUE
 901  convert_type(val, tname, method, raise)
 902      VALUE val;
 903      const char *tname, *method;
 904      int raise;
 905  {
 906      ID m;
 907  
 908      m = rb_intern(method);
 909      if (!rb_respond_to(val, m)) {
 910          if (raise) {
 911              rb_raise(rb_eTypeError, "cannot convert %s into %s",
 912                       NIL_P(val) ? "nil" :
 913                       val == Qtrue ? "true" :
 914                       val == Qfalse ? "false" :
 915                       rb_class2name(CLASS_OF(val)), 
 916                       tname);
 917          }
 918          else {
 919              return Qnil;
 920          }
 921      }
 922      return rb_funcall(val, m, 0);
 923  }
 924  
 925  VALUE
 926  rb_convert_type(val, type, tname, method)
 927      VALUE val;
 928      int type;
 929      const char *tname, *method;
 930  {
 931      VALUE v;
 932  
 933      if (TYPE(val) == type) return val;
 934      v = convert_type(val, tname, method, Qtrue);
 935      if (TYPE(v) != type) {
 936          rb_raise(rb_eTypeError, "%s#%s should return %s",
 937                   rb_class2name(CLASS_OF(val)), method, tname);
 938      }
 939      return v;
 940  }
 941  
 942  VALUE
 943  rb_check_convert_type(val, type, tname, method)
 944      VALUE val;
 945      int type;
 946      const char *tname, *method;
 947  {
 948      VALUE v;
 949  
 950      /* always convert T_DATA */
 951      if (TYPE(val) == type && type != T_DATA) return val;
 952      v = convert_type(val, tname, method, Qfalse);
 953      if (NIL_P(v)) return Qnil;
 954      if (TYPE(v) != type) {
 955          rb_raise(rb_eTypeError, "%s#%s should return %s",
 956                   rb_class2name(CLASS_OF(val)), method, tname);
 957      }
 958      return v;
 959  }
 960  
 961  
 962  static VALUE
 963  rb_to_integer(val, method)
 964      VALUE val;
 965      char *method;
 966  {
 967      VALUE v = convert_type(val, "Integer", method, Qtrue);
 968      if (!rb_obj_is_kind_of(v, rb_cInteger)) {
 969          rb_raise(rb_eTypeError, "%s#%s should return Integer",
 970                   rb_class2name(CLASS_OF(val)), method);
 971      }
 972      return v;
 973  }
 974  
 975  VALUE
 976  rb_to_int(val)
 977      VALUE val;
 978  {
 979      return rb_to_integer(val, "to_int");
 980  }
 981  
 982  VALUE
 983  rb_Integer(val)
 984      VALUE val;
 985  {
 986      switch (TYPE(val)) {
 987        case T_FLOAT:
 988          if (RFLOAT(val)->value <= (double)FIXNUM_MAX
 989              && RFLOAT(val)->value >= (double)FIXNUM_MIN) {
 990              break;
 991          }
 992          return rb_dbl2big(RFLOAT(val)->value);
 993  
 994        case T_FIXNUM:
 995        case T_BIGNUM:
 996          return val;
 997  
 998        case T_STRING:
 999          return rb_str_to_inum(val, 0, Qtrue);
1000  
1001        default:
1002          break;
1003      }
1004      if (rb_respond_to(val, rb_intern("to_int"))) {
1005          return rb_to_integer(val, "to_int");
1006      }
1007      return rb_to_integer(val, "to_i");
1008  }
1009  
1010  static VALUE
1011  rb_f_integer(obj, arg)
1012      VALUE obj, arg;
1013  {
1014      return rb_Integer(arg);
1015  }
1016  
1017  double
1018  rb_cstr_to_dbl(p, badcheck)
1019      const char *p;
1020      int badcheck;
1021  {
1022      const char *q;
1023      char *end;
1024      double d;
1025  
1026      q = p;
1027      if (badcheck) {
1028          while (ISSPACE(*p)) p++;
1029      }
1030      else {
1031          while (ISSPACE(*p) || *p == '_') p++;
1032      }
1033      d = strtod(p, &end);
1034      if (p == end) {
1035          if (badcheck) {
1036            bad:
1037              rb_invalid_str(q, "Float()");
1038          }
1039          return d;
1040      }
1041      if (*end) {
1042          char *buf = ALLOCA_N(char, strlen(p)+1);
1043          char *n = buf;
1044  
1045          while (p < end) *n++ = *p++;
1046          while (*p) {
1047              if (*p == '_') {
1048                  /* remove underscores between digits */
1049                  if (badcheck) {
1050                      if (n == buf || !ISDIGIT(n[-1])) goto bad;
1051                      ++p;
1052                      if (!ISDIGIT(*p)) goto bad;
1053                  }
1054                  else {
1055                      while (*++p == '_');
1056                      continue;
1057                  }
1058              }
1059              *n++ = *p++;
1060          }
1061          *n = '\0';
1062          p = buf;
1063          d = strtod(p, &end);
1064          if (badcheck) {
1065              if (p == end) goto bad;
1066              while (*end && ISSPACE(*end)) end++;
1067              if (*end) goto bad;
1068          }
1069      }
1070      if (errno == ERANGE) {
1071          errno = 0;
1072          rb_raise(rb_eArgError, "Float %s out of range", q);
1073      }
1074      return d;
1075  }
1076  
1077  double
1078  rb_str_to_dbl(str, badcheck)
1079      VALUE str;
1080      int badcheck;
1081  {
1082      char *s;
1083      long len;
1084  
1085      StringValue(str);
1086      s = RSTRING(str)->ptr;
1087      len = RSTRING(str)->len;
1088      if (s[len]) {               /* no sentinel somehow */
1089          char *p = ALLOCA_N(char, len+1);
1090  
1091          MEMCPY(p, s, char, len);
1092          p[len] = '\0';
1093          s = p;
1094      }
1095      if (badcheck && len != strlen(s)) {
1096          rb_raise(rb_eArgError, "string for Float contains null byte");
1097      }
1098      return rb_cstr_to_dbl(s, badcheck);
1099  }
1100  
1101  VALUE
1102  rb_Float(val)
1103      VALUE val;
1104  {
1105      switch (TYPE(val)) {
1106        case T_FIXNUM:
1107          return rb_float_new((double)FIX2LONG(val));
1108  
1109        case T_FLOAT:
1110          return val;
1111  
1112        case T_BIGNUM:
1113          return rb_float_new(rb_big2dbl(val));
1114  
1115        case T_STRING:
1116          return rb_float_new(rb_str_to_dbl(val, Qtrue));
1117  
1118        case T_NIL:
1119          rb_raise(rb_eTypeError, "cannot convert nil into Float");
1120          break;
1121  
1122        default:
1123        {
1124            VALUE f = rb_convert_type(val, T_FLOAT, "Float", "to_f");
1125            if (isnan(RFLOAT(f)->value)) {
1126                rb_raise(rb_eArgError, "invalid value for Float()");
1127            }
1128            return f;
1129        }
1130      }
1131  }
1132  
1133  static VALUE
1134  rb_f_float(obj, arg)
1135      VALUE obj, arg;
1136  {
1137      return rb_Float(arg);
1138  }
1139  
1140  double
1141  rb_num2dbl(val)
1142      VALUE val;
1143  {
1144      switch (TYPE(val)) {
1145        case T_FLOAT:
1146          return RFLOAT(val)->value;
1147  
1148        case T_STRING:
1149          rb_raise(rb_eTypeError, "no implicit conversion to float from string");
1150          break;
1151  
1152        case T_NIL:
1153          rb_raise(rb_eTypeError, "no implicit conversion to float from nil");
1154          break;
1155  
1156        default:
1157          break;
1158      }
1159  
1160      return RFLOAT(rb_Float(val))->value;
1161  }
1162  
1163  char*
1164  rb_str2cstr(str, len)
1165      VALUE str;
1166      long *len;
1167  {
1168      StringValue(str);
1169      if (len) *len = RSTRING(str)->len;
1170      else if (RTEST(ruby_verbose) && RSTRING(str)->len != strlen(RSTRING(str)->ptr)) {
1171          rb_warn("string contains \\0 character");
1172      }
1173      return RSTRING(str)->ptr;
1174  }
1175  
1176  VALUE
1177  rb_String(val)
1178      VALUE val;
1179  {
1180      return rb_convert_type(val, T_STRING, "String", "to_s");
1181  }
1182  
1183  static VALUE
1184  rb_f_string(obj, arg)
1185      VALUE obj, arg;
1186  {
1187      return rb_String(arg);
1188  }
1189  
1190  VALUE
1191  rb_Array(val)
1192      VALUE val;
1193  {
1194      ID to_ary;
1195  
1196      if (NIL_P(val)) {
1197          rb_raise(rb_eTypeError, "cannot convert nil into Array");
1198      }
1199      if (TYPE(val) == T_ARRAY) return val;
1200      to_ary = rb_intern("to_ary");
1201      if (rb_respond_to(val, to_ary)) {
1202          val = rb_funcall(val, to_ary, 0);
1203      }
1204      else {
1205          val = rb_funcall(val, rb_intern("to_a"), 0);
1206      }
1207      if (TYPE(val) != T_ARRAY) {
1208          rb_raise(rb_eTypeError, "`to_a' did not return Array");
1209      }
1210      return val;
1211  }
1212  
1213  static VALUE
1214  rb_f_array(obj, arg)
1215      VALUE obj, arg;
1216  {
1217      return rb_Array(arg);
1218  }
1219  
1220  static VALUE
1221  boot_defclass(name, super)
1222      char *name;
1223      VALUE super;
1224  {
1225      extern st_table *rb_class_tbl;
1226      VALUE obj = rb_class_boot(super);
1227      ID id = rb_intern(name);
1228  
1229      rb_name_class(obj, id);
1230      st_add_direct(rb_class_tbl, id, obj);
1231      return obj;
1232  }
1233  
1234  VALUE ruby_top_self;
1235  
1236  void
1237  Init_Object()
1238  {
1239      VALUE metaclass;
1240  
1241      alloc = rb_intern("allocate");
1242  
1243      rb_cObject = boot_defclass("Object", 0);
1244      rb_cModule = boot_defclass("Module", rb_cObject);
1245      rb_cClass =  boot_defclass("Class",  rb_cModule);
1246  
1247      metaclass = rb_make_metaclass(rb_cObject, rb_cClass);
1248      metaclass = rb_make_metaclass(rb_cModule, metaclass);
1249      metaclass = rb_make_metaclass(rb_cClass, metaclass);
1250  
1251      rb_mKernel = rb_define_module("Kernel");
1252      rb_include_module(rb_cObject, rb_mKernel);
1253      rb_define_private_method(rb_cObject, "initialize", rb_obj_dummy, 0);
1254      rb_define_private_method(rb_cClass, "inherited", rb_obj_dummy, 1);
1255      rb_define_private_method(rb_cModule, "included", rb_obj_dummy, 1);
1256      rb_define_private_method(rb_cModule, "method_added", rb_obj_dummy, 1);
1257      rb_define_private_method(rb_cModule, "method_removed", rb_obj_dummy, 1);
1258      rb_define_private_method(rb_cModule, "method_undefined", rb_obj_dummy, 1);
1259  
1260      /*
1261       * Ruby's Class Hierarchy Chart
1262       *
1263       *                           +------------------+
1264       *                           |                  |
1265       *             Object---->(Object)              |
1266       *              ^  ^        ^  ^                |
1267       *              |  |        |  |                |
1268       *              |  |  +-----+  +---------+      |
1269       *              |  |  |                  |      |
1270       *              |  +-----------+         |      |
1271       *              |     |        |         |      |
1272       *       +------+     |     Module--->(Module)  |
1273       *       |            |        ^         ^      |
1274       *  OtherClass-->(OtherClass)  |         |      |
1275       *                             |         |      |
1276       *                           Class---->(Class)  |
1277       *                             ^                |
1278       *                             |                |
1279       *                             +----------------+
1280       *
1281       *   + All metaclasses are instances of the class `Class'.
1282       */
1283  
1284      rb_define_method(rb_mKernel, "nil?", rb_false, 0);
1285      rb_define_method(rb_mKernel, "==", rb_obj_equal, 1);
1286      rb_define_method(rb_mKernel, "equal?", rb_obj_equal, 1);
1287      rb_define_method(rb_mKernel, "===", rb_obj_equal, 1); 
1288      rb_define_method(rb_mKernel, "=~", rb_false, 1);
1289  
1290      rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
1291  
1292      rb_define_method(rb_mKernel, "hash", rb_obj_id, 0);
1293      rb_define_method(rb_mKernel, "id", rb_obj_id, 0);
1294      rb_define_method(rb_mKernel, "__id__", rb_obj_id, 0);
1295      rb_define_method(rb_mKernel, "type", rb_obj_class, 0);
1296      rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
1297  
1298      rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
1299      rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
1300      rb_define_method(rb_mKernel, "become", rb_obj_become, 1);
1301  
1302      rb_define_method(rb_mKernel, "taint", rb_obj_taint, 0);
1303      rb_define_method(rb_mKernel, "tainted?", rb_obj_tainted, 0);
1304      rb_define_method(rb_mKernel, "untaint", rb_obj_untaint, 0);
1305      rb_define_method(rb_mKernel, "freeze", rb_obj_freeze, 0);
1306      rb_define_method(rb_mKernel, "frozen?", rb_obj_frozen_p, 0);
1307  
1308      rb_define_method(rb_mKernel, "to_a", rb_any_to_a, 0); /* to be removed */
1309      rb_define_method(rb_mKernel, "to_s", rb_any_to_s, 0);
1310      rb_define_method(rb_mKernel, "inspect", rb_obj_inspect, 0);
1311      rb_define_method(rb_mKernel, "methods", rb_obj_methods, 0);
1312      rb_define_method(rb_mKernel, "public_methods", rb_obj_methods, 0);
1313      rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1);
1314      rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, 0);
1315      rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, 0);
1316      rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0);
1317      rb_define_private_method(rb_mKernel, "remove_instance_variable",
1318                               rb_obj_remove_instance_variable, 1);
1319  
1320      rb_define_method(rb_mKernel, "instance_of?", rb_obj_is_instance_of, 1);
1321      rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
1322      rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
1323  
1324      rb_define_global_function("singleton_method_added", rb_obj_dummy, 1);
1325      rb_define_global_function("singleton_method_removed", rb_obj_dummy, 1);
1326      rb_define_global_function("singleton_method_undefined", rb_obj_dummy, 1);
1327  
1328      rb_define_global_function("sprintf", rb_f_sprintf, -1);
1329      rb_define_global_function("format", rb_f_sprintf, -1);
1330  
1331      rb_define_global_function("Integer", rb_f_integer, 1);
1332      rb_define_global_function("Float", rb_f_float, 1);
1333  
1334      rb_define_global_function("String", rb_f_string, 1);
1335      rb_define_global_function("Array", rb_f_array, 1);
1336  
1337      rb_cNilClass = rb_define_class("NilClass", rb_cObject);
1338      rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0);
1339      rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0);
1340      rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0);
1341      rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0);
1342      rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0);
1343      rb_define_method(rb_cNilClass, "&", false_and, 1);
1344      rb_define_method(rb_cNilClass, "|", false_or, 1);
1345      rb_define_method(rb_cNilClass, "^", false_xor, 1);
1346  
1347      rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
1348      rb_undef_method(CLASS_OF(rb_cNilClass), "allocate");
1349      rb_undef_method(CLASS_OF(rb_cNilClass), "new");
1350      rb_define_global_const("NIL", Qnil);
1351  
1352      rb_cSymbol = rb_define_class("Symbol", rb_cObject);
1353      rb_define_singleton_method(rb_cSymbol, "all_symbols", rb_sym_all_symbols, 0);
1354      rb_undef_method(CLASS_OF(rb_cSymbol), "allocate");
1355      rb_undef_method(CLASS_OF(rb_cSymbol), "new");
1356  
1357      rb_define_method(rb_cSymbol, "to_i", sym_to_i, 0);
1358      rb_define_method(rb_cSymbol, "to_int", sym_to_i, 0);
1359      rb_define_method(rb_cSymbol, "inspect", sym_inspect, 0);
1360      rb_define_method(rb_cSymbol, "to_s", sym_to_s, 0);
1361      rb_define_method(rb_cSymbol, "id2name", sym_to_s, 0);
1362      rb_define_method(rb_cSymbol, "intern", sym_intern, 0);
1363  
1364      rb_define_method(rb_cModule, "===", rb_mod_eqq, 1);
1365      rb_define_method(rb_cModule, "==", rb_obj_equal, 1);
1366      rb_define_method(rb_cModule, "<=>",  rb_mod_cmp, 1);
1367      rb_define_method(rb_cModule, "<",  rb_mod_lt, 1);
1368      rb_define_method(rb_cModule, "<=", rb_mod_le, 1);
1369      rb_define_method(rb_cModule, ">",  rb_mod_gt, 1);
1370      rb_define_method(rb_cModule, ">=", rb_mod_ge, 1);
1371      rb_define_method(rb_cModule, "clone", rb_mod_clone, 0);
1372      rb_define_method(rb_cModule, "dup", rb_mod_dup, 0);
1373      rb_define_method(rb_cModule, "to_s", rb_mod_to_s, 0);
1374      rb_define_method(rb_cModule, "included_modules", rb_mod_included_modules, 0);
1375      rb_define_method(rb_cModule, "include?", rb_mod_include_p, 1);
1376      rb_define_method(rb_cModule, "name", rb_mod_name, 0);
1377      rb_define_method(rb_cModule, "ancestors", rb_mod_ancestors, 0);
1378  
1379      rb_define_private_method(rb_cModule, "attr", rb_mod_attr, -1);
1380      rb_define_private_method(rb_cModule, "attr_reader", rb_mod_attr_reader, -1);
1381      rb_define_private_method(rb_cModule, "attr_writer", rb_mod_attr_writer, -1);
1382      rb_define_private_method(rb_cModule, "attr_accessor", rb_mod_attr_accessor, -1);
1383  
1384      rb_define_singleton_method(rb_cModule, "allocate", rb_module_s_alloc, 0);
1385      rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0);
1386      rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1);
1387      rb_define_method(rb_cModule, "public_instance_methods", rb_class_instance_methods, -1);
1388      rb_define_method(rb_cModule, "protected_instance_methods", rb_class_protected_instance_methods, -1);
1389      rb_define_method(rb_cModule, "private_instance_methods", rb_class_private_instance_methods, -1);
1390  
1391      rb_define_method(rb_cModule, "constants", rb_mod_constants, 0);
1392      rb_define_method(rb_cModule, "const_get", rb_mod_const_get, 1);
1393      rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
1394      rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, 1);
1395      rb_define_private_method(rb_cModule, "remove_const", rb_mod_remove_const, 1);
1396      rb_define_method(rb_cModule, "class_variables", rb_mod_class_variables, 0);
1397      rb_define_private_method(rb_cModule, "remove_class_variable", rb_mod_remove_cvar, 1);
1398  
1399      rb_define_method(rb_cClass, "allocate", rb_class_allocate_instance, 0);
1400      rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
1401      rb_define_method(rb_cClass, "initialize", rb_class_initialize, -1);
1402      rb_define_method(rb_cClass, "superclass", rb_class_superclass, 0);
1403      rb_undef_method(CLASS_OF(rb_cClass), "allocate");
1404      rb_define_singleton_method(rb_cClass, "new", rb_class_s_new, -1);
1405      rb_undef_method(rb_cClass, "extend_object");
1406      rb_undef_method(rb_cClass, "append_features");
1407  
1408      rb_cData = rb_define_class("Data", rb_cObject);
1409      rb_undef_method(CLASS_OF(rb_cData), "allocate");
1410  
1411      ruby_top_self = rb_obj_alloc(rb_cObject);
1412      rb_global_variable(&ruby_top_self);
1413      rb_define_singleton_method(ruby_top_self, "to_s", main_to_s, 0);
1414  
1415      rb_cTrueClass = rb_define_class("TrueClass", rb_cObject);
1416      rb_define_method(rb_cTrueClass, "to_s", true_to_s, 0);
1417      rb_define_method(rb_cTrueClass, "&", true_and, 1);
1418      rb_define_method(rb_cTrueClass, "|", true_or, 1);
1419      rb_define_method(rb_cTrueClass, "^", true_xor, 1);
1420      rb_undef_method(CLASS_OF(rb_cTrueClass), "allocate");
1421      rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
1422      rb_define_global_const("TRUE", Qtrue);
1423  
1424      rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
1425      rb_define_method(rb_cFalseClass, "to_s", false_to_s, 0);
1426      rb_define_method(rb_cFalseClass, "&", false_and, 1);
1427      rb_define_method(rb_cFalseClass, "|", false_or, 1);
1428      rb_define_method(rb_cFalseClass, "^", false_xor, 1);
1429      rb_undef_method(CLASS_OF(rb_cFalseClass), "allocate");
1430      rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
1431      rb_define_global_const("FALSE", Qfalse);
1432  
1433      eq = rb_intern("==");
1434      eql = rb_intern("eql?");
1435      inspect = rb_intern("inspect");
1436      become = rb_intern("become");
1437  }