1/********************************************************************** 2 3 class.c - 4 5 $Author: nagachika $ 6 created at: Tue Aug 10 15:05:44 JST 1993 7 8 Copyright (C) 1993-2007 Yukihiro Matsumoto 9 10**********************************************************************/ 11 12/*! 13 * \defgroup class Classes and their hierarchy. 14 * \par Terminology 15 * - class: same as in Ruby. 16 * - singleton class: class for a particular object 17 * - eigenclass: = singleton class 18 * - metaclass: class of a class. metaclass is a kind of singleton class. 19 * - metametaclass: class of a metaclass. 20 * - meta^(n)-class: class of a meta^(n-1)-class. 21 * - attached object: A singleton class knows its unique instance. 22 * The instance is called the attached object for the singleton class. 23 * \{ 24 */ 25 26#include "ruby/ruby.h" 27#include "ruby/st.h" 28#include "method.h" 29#include "constant.h" 30#include "vm_core.h" 31#include "internal.h" 32#include <ctype.h> 33 34extern st_table *rb_class_tbl; 35static ID id_attached; 36 37/** 38 * Allocates a struct RClass for a new class. 39 * 40 * \param flags initial value for basic.flags of the returned class. 41 * \param klass the class of the returned class. 42 * \return an uninitialized Class object. 43 * \pre \p klass must refer \c Class class or an ancestor of Class. 44 * \pre \code (flags | T_CLASS) != 0 \endcode 45 * \post the returned class can safely be \c #initialize 'd. 46 * 47 * \note this function is not Class#allocate. 48 */ 49static VALUE 50class_alloc(VALUE flags, VALUE klass) 51{ 52 NEWOBJ_OF(obj, struct RClass, klass, flags); 53 obj->ptr = ALLOC(rb_classext_t); 54 RCLASS_IV_TBL(obj) = 0; 55 RCLASS_CONST_TBL(obj) = 0; 56 RCLASS_M_TBL(obj) = 0; 57 RCLASS_SUPER(obj) = 0; 58 RCLASS_ORIGIN(obj) = (VALUE)obj; 59 RCLASS_IV_INDEX_TBL(obj) = 0; 60 RCLASS_REFINED_CLASS(obj) = Qnil; 61 RCLASS_EXT(obj)->allocator = 0; 62 return (VALUE)obj; 63} 64 65 66/*! 67 * A utility function that wraps class_alloc. 68 * 69 * allocates a class and initializes safely. 70 * \param super a class from which the new class derives. 71 * \return a class object. 72 * \pre \a super must be a class. 73 * \post the metaclass of the new class is Class. 74 */ 75VALUE 76rb_class_boot(VALUE super) 77{ 78 VALUE klass = class_alloc(T_CLASS, rb_cClass); 79 80 RCLASS_SUPER(klass) = super; 81 RCLASS_M_TBL(klass) = st_init_numtable(); 82 83 OBJ_INFECT(klass, super); 84 return (VALUE)klass; 85} 86 87 88/*! 89 * Ensures a class can be derived from super. 90 * 91 * \param super a reference to an object. 92 * \exception TypeError if \a super is not a Class or \a super is a singleton class. 93 */ 94void 95rb_check_inheritable(VALUE super) 96{ 97 if (!RB_TYPE_P(super, T_CLASS)) { 98 rb_raise(rb_eTypeError, "superclass must be a Class (%s given)", 99 rb_obj_classname(super)); 100 } 101 if (RBASIC(super)->flags & FL_SINGLETON) { 102 rb_raise(rb_eTypeError, "can't make subclass of singleton class"); 103 } 104 if (super == rb_cClass) { 105 rb_raise(rb_eTypeError, "can't make subclass of Class"); 106 } 107} 108 109 110/*! 111 * Creates a new class. 112 * \param super a class from which the new class derives. 113 * \exception TypeError \a super is not inheritable. 114 * \exception TypeError \a super is the Class class. 115 */ 116VALUE 117rb_class_new(VALUE super) 118{ 119 Check_Type(super, T_CLASS); 120 rb_check_inheritable(super); 121 return rb_class_boot(super); 122} 123 124static NODE* 125rewrite_cref_stack(NODE *node, VALUE old_klass, VALUE new_klass) 126{ 127 NODE *new_node; 128 if (!node) { 129 return NULL; 130 } 131 if (node->nd_clss == old_klass) { 132 new_node = NEW_CREF(new_klass); 133 new_node->nd_next = node->nd_next; 134 } else { 135 new_node = NEW_CREF(node->nd_clss); 136 new_node->nd_next = rewrite_cref_stack(node->nd_next, old_klass, new_klass); 137 } 138 return new_node; 139} 140 141static void 142clone_method(VALUE klass, ID mid, const rb_method_entry_t *me) 143{ 144 VALUE newiseqval; 145 if (me->def && me->def->type == VM_METHOD_TYPE_ISEQ) { 146 rb_iseq_t *iseq; 147 newiseqval = rb_iseq_clone(me->def->body.iseq->self, klass); 148 GetISeqPtr(newiseqval, iseq); 149 iseq->cref_stack = rewrite_cref_stack(me->def->body.iseq->cref_stack, me->klass, klass); 150 rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, iseq, me->flag); 151 RB_GC_GUARD(newiseqval); 152 } 153 else { 154 rb_method_entry_set(klass, mid, me, me->flag); 155 } 156} 157 158static int 159clone_method_i(st_data_t key, st_data_t value, st_data_t data) 160{ 161 clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value); 162 return ST_CONTINUE; 163} 164 165static int 166clone_const(ID key, const rb_const_entry_t *ce, st_table *tbl) 167{ 168 rb_const_entry_t *nce = ALLOC(rb_const_entry_t); 169 *nce = *ce; 170 st_insert(tbl, key, (st_data_t)nce); 171 return ST_CONTINUE; 172} 173 174static int 175clone_const_i(st_data_t key, st_data_t value, st_data_t data) 176{ 177 return clone_const((ID)key, (const rb_const_entry_t *)value, (st_table *)data); 178} 179 180static void 181class_init_copy_check(VALUE clone, VALUE orig) 182{ 183 if (orig == rb_cBasicObject) { 184 rb_raise(rb_eTypeError, "can't copy the root class"); 185 } 186 if (RCLASS_SUPER(clone) != 0 || clone == rb_cBasicObject) { 187 rb_raise(rb_eTypeError, "already initialized class"); 188 } 189 if (FL_TEST(orig, FL_SINGLETON)) { 190 rb_raise(rb_eTypeError, "can't copy singleton class"); 191 } 192} 193 194/* :nodoc: */ 195VALUE 196rb_mod_init_copy(VALUE clone, VALUE orig) 197{ 198 if (RB_TYPE_P(clone, T_CLASS)) { 199 class_init_copy_check(clone, orig); 200 } 201 if (!OBJ_INIT_COPY(clone, orig)) return clone; 202 if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) { 203 RBASIC(clone)->klass = rb_singleton_class_clone(orig); 204 rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone); 205 } 206 RCLASS_SUPER(clone) = RCLASS_SUPER(orig); 207 RCLASS_EXT(clone)->allocator = RCLASS_EXT(orig)->allocator; 208 if (RCLASS_IV_TBL(orig)) { 209 st_data_t id; 210 211 if (RCLASS_IV_TBL(clone)) { 212 st_free_table(RCLASS_IV_TBL(clone)); 213 } 214 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig)); 215 CONST_ID(id, "__tmp_classpath__"); 216 st_delete(RCLASS_IV_TBL(clone), &id, 0); 217 CONST_ID(id, "__classpath__"); 218 st_delete(RCLASS_IV_TBL(clone), &id, 0); 219 CONST_ID(id, "__classid__"); 220 st_delete(RCLASS_IV_TBL(clone), &id, 0); 221 } 222 if (RCLASS_CONST_TBL(orig)) { 223 if (RCLASS_CONST_TBL(clone)) { 224 rb_free_const_table(RCLASS_CONST_TBL(clone)); 225 } 226 RCLASS_CONST_TBL(clone) = st_init_numtable(); 227 st_foreach(RCLASS_CONST_TBL(orig), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone)); 228 } 229 if (RCLASS_M_TBL(orig)) { 230 if (RCLASS_M_TBL(clone)) { 231 rb_free_m_table(RCLASS_M_TBL(clone)); 232 } 233 RCLASS_M_TBL(clone) = st_init_numtable(); 234 st_foreach(RCLASS_M_TBL(orig), clone_method_i, (st_data_t)clone); 235 } 236 237 return clone; 238} 239 240VALUE 241rb_singleton_class_clone(VALUE obj) 242{ 243 return rb_singleton_class_clone_and_attach(obj, Qundef); 244} 245 246VALUE 247rb_singleton_class_clone_and_attach(VALUE obj, VALUE attach) 248{ 249 VALUE klass = RBASIC(obj)->klass; 250 251 if (!FL_TEST(klass, FL_SINGLETON)) 252 return klass; 253 else { 254 /* copy singleton(unnamed) class */ 255 VALUE clone = class_alloc(RBASIC(klass)->flags, 0); 256 257 if (BUILTIN_TYPE(obj) == T_CLASS) { 258 RBASIC(clone)->klass = clone; 259 } 260 else { 261 RBASIC(clone)->klass = rb_singleton_class_clone(klass); 262 } 263 264 RCLASS_SUPER(clone) = RCLASS_SUPER(klass); 265 RCLASS_EXT(clone)->allocator = RCLASS_EXT(klass)->allocator; 266 if (RCLASS_IV_TBL(klass)) { 267 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass)); 268 } 269 if (RCLASS_CONST_TBL(klass)) { 270 RCLASS_CONST_TBL(clone) = st_init_numtable(); 271 st_foreach(RCLASS_CONST_TBL(klass), clone_const_i, (st_data_t)RCLASS_CONST_TBL(clone)); 272 } 273 if (attach != Qundef) { 274 rb_singleton_class_attached(clone, attach); 275 } 276 RCLASS_M_TBL(clone) = st_init_numtable(); 277 st_foreach(RCLASS_M_TBL(klass), clone_method_i, (st_data_t)clone); 278 rb_singleton_class_attached(RBASIC(clone)->klass, clone); 279 FL_SET(clone, FL_SINGLETON); 280 return clone; 281 } 282} 283 284/*! 285 * Attach a object to a singleton class. 286 * @pre \a klass is the singleton class of \a obj. 287 */ 288void 289rb_singleton_class_attached(VALUE klass, VALUE obj) 290{ 291 if (FL_TEST(klass, FL_SINGLETON)) { 292 if (!RCLASS_IV_TBL(klass)) { 293 RCLASS_IV_TBL(klass) = st_init_numtable(); 294 } 295 st_insert(RCLASS_IV_TBL(klass), id_attached, obj); 296 } 297} 298 299 300 301#define METACLASS_OF(k) RBASIC(k)->klass 302 303/*! 304 * whether k is a meta^(n)-class of Class class 305 * @retval 1 if \a k is a meta^(n)-class of Class class (n >= 0) 306 * @retval 0 otherwise 307 */ 308#define META_CLASS_OF_CLASS_CLASS_P(k) (METACLASS_OF(k) == (k)) 309 310/*! 311 * whether k has a metaclass 312 * @retval 1 if \a k has a metaclass 313 * @retval 0 otherwise 314 */ 315#define HAVE_METACLASS_P(k) \ 316 (FL_TEST(METACLASS_OF(k), FL_SINGLETON) && \ 317 rb_ivar_get(METACLASS_OF(k), id_attached) == (k)) 318 319/*! 320 * ensures \a klass belongs to its own eigenclass. 321 * @return the eigenclass of \a klass 322 * @post \a klass belongs to the returned eigenclass. 323 * i.e. the attached object of the eigenclass is \a klass. 324 * @note this macro creates a new eigenclass if necessary. 325 */ 326#define ENSURE_EIGENCLASS(klass) \ 327 (HAVE_METACLASS_P(klass) ? METACLASS_OF(klass) : make_metaclass(klass)) 328 329 330/*! 331 * Creates a metaclass of \a klass 332 * \param klass a class 333 * \return created metaclass for the class 334 * \pre \a klass is a Class object 335 * \pre \a klass has no singleton class. 336 * \post the class of \a klass is the returned class. 337 * \post the returned class is meta^(n+1)-class when \a klass is a meta^(n)-klass for n >= 0 338 */ 339static inline VALUE 340make_metaclass(VALUE klass) 341{ 342 VALUE super; 343 VALUE metaclass = rb_class_boot(Qundef); 344 345 FL_SET(metaclass, FL_SINGLETON); 346 rb_singleton_class_attached(metaclass, klass); 347 348 if (META_CLASS_OF_CLASS_CLASS_P(klass)) { 349 METACLASS_OF(klass) = METACLASS_OF(metaclass) = metaclass; 350 } 351 else { 352 VALUE tmp = METACLASS_OF(klass); /* for a meta^(n)-class klass, tmp is meta^(n)-class of Class class */ 353 METACLASS_OF(klass) = metaclass; 354 METACLASS_OF(metaclass) = ENSURE_EIGENCLASS(tmp); 355 } 356 357 super = RCLASS_SUPER(klass); 358 while (RB_TYPE_P(super, T_ICLASS)) super = RCLASS_SUPER(super); 359 RCLASS_SUPER(metaclass) = super ? ENSURE_EIGENCLASS(super) : rb_cClass; 360 361 OBJ_INFECT(metaclass, RCLASS_SUPER(metaclass)); 362 363 return metaclass; 364} 365 366/*! 367 * Creates a singleton class for \a obj. 368 * \pre \a obj must not a immediate nor a special const. 369 * \pre \a obj must not a Class object. 370 * \pre \a obj has no singleton class. 371 */ 372static inline VALUE 373make_singleton_class(VALUE obj) 374{ 375 VALUE orig_class = RBASIC(obj)->klass; 376 VALUE klass = rb_class_boot(orig_class); 377 378 FL_SET(klass, FL_SINGLETON); 379 RBASIC(obj)->klass = klass; 380 rb_singleton_class_attached(klass, obj); 381 382 METACLASS_OF(klass) = METACLASS_OF(rb_class_real(orig_class)); 383 return klass; 384} 385 386 387static VALUE 388boot_defclass(const char *name, VALUE super) 389{ 390 extern st_table *rb_class_tbl; 391 VALUE obj = rb_class_boot(super); 392 ID id = rb_intern(name); 393 394 rb_name_class(obj, id); 395 st_add_direct(rb_class_tbl, id, obj); 396 rb_const_set((rb_cObject ? rb_cObject : obj), id, obj); 397 return obj; 398} 399 400void 401Init_class_hierarchy(void) 402{ 403 id_attached = rb_intern("__attached__"); 404 405 rb_cBasicObject = boot_defclass("BasicObject", 0); 406 rb_cObject = boot_defclass("Object", rb_cBasicObject); 407 rb_cModule = boot_defclass("Module", rb_cObject); 408 rb_cClass = boot_defclass("Class", rb_cModule); 409 410 rb_const_set(rb_cObject, rb_intern("BasicObject"), rb_cBasicObject); 411 RBASIC(rb_cClass)->klass 412 = RBASIC(rb_cModule)->klass 413 = RBASIC(rb_cObject)->klass 414 = RBASIC(rb_cBasicObject)->klass 415 = rb_cClass; 416} 417 418 419/*! 420 * \internal 421 * Creates a new *singleton class* for an object. 422 * 423 * \pre \a obj has no singleton class. 424 * \note DO NOT USE the function in an extension libraries. Use \ref rb_singleton_class. 425 * \param obj An object. 426 * \param unused ignored. 427 * \return The singleton class of the object. 428 */ 429VALUE 430rb_make_metaclass(VALUE obj, VALUE unused) 431{ 432 if (BUILTIN_TYPE(obj) == T_CLASS) { 433 return make_metaclass(obj); 434 } 435 else { 436 return make_singleton_class(obj); 437 } 438} 439 440 441/*! 442 * Defines a new class. 443 * \param id ignored 444 * \param super A class from which the new class will derive. NULL means \c Object class. 445 * \return the created class 446 * \throw TypeError if super is not a \c Class object. 447 * 448 * \note the returned class will not be associated with \a id. 449 * You must explicitly set a class name if necessary. 450 */ 451VALUE 452rb_define_class_id(ID id, VALUE super) 453{ 454 VALUE klass; 455 456 if (!super) super = rb_cObject; 457 klass = rb_class_new(super); 458 rb_make_metaclass(klass, RBASIC(super)->klass); 459 460 return klass; 461} 462 463 464/*! 465 * Calls Class#inherited. 466 * \param super A class which will be called #inherited. 467 * NULL means Object class. 468 * \param klass A Class object which derived from \a super 469 * \return the value \c Class#inherited's returns 470 * \pre Each of \a super and \a klass must be a \c Class object. 471 */ 472VALUE 473rb_class_inherited(VALUE super, VALUE klass) 474{ 475 ID inherited; 476 if (!super) super = rb_cObject; 477 CONST_ID(inherited, "inherited"); 478 return rb_funcall(super, inherited, 1, klass); 479} 480 481 482 483/*! 484 * Defines a top-level class. 485 * \param name name of the class 486 * \param super a class from which the new class will derive. 487 * NULL means \c Object class. 488 * \return the created class 489 * \throw TypeError if the constant name \a name is already taken but 490 * the constant is not a \c Class. 491 * \throw NameError if the class is already defined but the class can not 492 * be reopened because its superclass is not \a super. 493 * \post top-level constant named \a name refers the returned class. 494 * 495 * \note if a class named \a name is already defined and its superclass is 496 * \a super, the function just returns the defined class. 497 */ 498VALUE 499rb_define_class(const char *name, VALUE super) 500{ 501 VALUE klass; 502 ID id; 503 504 id = rb_intern(name); 505 if (rb_const_defined(rb_cObject, id)) { 506 klass = rb_const_get(rb_cObject, id); 507 if (!RB_TYPE_P(klass, T_CLASS)) { 508 rb_raise(rb_eTypeError, "%s is not a class", name); 509 } 510 if (rb_class_real(RCLASS_SUPER(klass)) != super) { 511 rb_raise(rb_eTypeError, "superclass mismatch for class %s", name); 512 } 513 return klass; 514 } 515 if (!super) { 516 rb_warn("no super class for `%s', Object assumed", name); 517 } 518 klass = rb_define_class_id(id, super); 519 st_add_direct(rb_class_tbl, id, klass); 520 rb_name_class(klass, id); 521 rb_const_set(rb_cObject, id, klass); 522 rb_class_inherited(super, klass); 523 524 return klass; 525} 526 527 528/*! 529 * Defines a class under the namespace of \a outer. 530 * \param outer a class which contains the new class. 531 * \param name name of the new class 532 * \param super a class from which the new class will derive. 533 * NULL means \c Object class. 534 * \return the created class 535 * \throw TypeError if the constant name \a name is already taken but 536 * the constant is not a \c Class. 537 * \throw NameError if the class is already defined but the class can not 538 * be reopened because its superclass is not \a super. 539 * \post top-level constant named \a name refers the returned class. 540 * 541 * \note if a class named \a name is already defined and its superclass is 542 * \a super, the function just returns the defined class. 543 */ 544VALUE 545rb_define_class_under(VALUE outer, const char *name, VALUE super) 546{ 547 return rb_define_class_id_under(outer, rb_intern(name), super); 548} 549 550 551/*! 552 * Defines a class under the namespace of \a outer. 553 * \param outer a class which contains the new class. 554 * \param id name of the new class 555 * \param super a class from which the new class will derive. 556 * NULL means \c Object class. 557 * \return the created class 558 * \throw TypeError if the constant name \a name is already taken but 559 * the constant is not a \c Class. 560 * \throw NameError if the class is already defined but the class can not 561 * be reopened because its superclass is not \a super. 562 * \post top-level constant named \a name refers the returned class. 563 * 564 * \note if a class named \a name is already defined and its superclass is 565 * \a super, the function just returns the defined class. 566 */ 567VALUE 568rb_define_class_id_under(VALUE outer, ID id, VALUE super) 569{ 570 VALUE klass; 571 572 if (rb_const_defined_at(outer, id)) { 573 klass = rb_const_get_at(outer, id); 574 if (!RB_TYPE_P(klass, T_CLASS)) { 575 rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id)); 576 } 577 if (rb_class_real(RCLASS_SUPER(klass)) != super) { 578 rb_name_error(id, "%s is already defined", rb_id2name(id)); 579 } 580 return klass; 581 } 582 if (!super) { 583 rb_warn("no super class for `%s::%s', Object assumed", 584 rb_class2name(outer), rb_id2name(id)); 585 } 586 klass = rb_define_class_id(id, super); 587 rb_set_class_path_string(klass, outer, rb_id2str(id)); 588 rb_const_set(outer, id, klass); 589 rb_class_inherited(super, klass); 590 rb_gc_register_mark_object(klass); 591 592 return klass; 593} 594 595VALUE 596rb_module_new(void) 597{ 598 VALUE mdl = class_alloc(T_MODULE, rb_cModule); 599 600 RCLASS_M_TBL(mdl) = st_init_numtable(); 601 602 return (VALUE)mdl; 603} 604 605VALUE 606rb_define_module_id(ID id) 607{ 608 VALUE mdl; 609 610 mdl = rb_module_new(); 611 rb_name_class(mdl, id); 612 613 return mdl; 614} 615 616VALUE 617rb_define_module(const char *name) 618{ 619 VALUE module; 620 ID id; 621 622 id = rb_intern(name); 623 if (rb_const_defined(rb_cObject, id)) { 624 module = rb_const_get(rb_cObject, id); 625 if (RB_TYPE_P(module, T_MODULE)) 626 return module; 627 rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module)); 628 } 629 module = rb_define_module_id(id); 630 st_add_direct(rb_class_tbl, id, module); 631 rb_const_set(rb_cObject, id, module); 632 633 return module; 634} 635 636VALUE 637rb_define_module_under(VALUE outer, const char *name) 638{ 639 return rb_define_module_id_under(outer, rb_intern(name)); 640} 641 642VALUE 643rb_define_module_id_under(VALUE outer, ID id) 644{ 645 VALUE module; 646 647 if (rb_const_defined_at(outer, id)) { 648 module = rb_const_get_at(outer, id); 649 if (RB_TYPE_P(module, T_MODULE)) 650 return module; 651 rb_raise(rb_eTypeError, "%s::%s is not a module", 652 rb_class2name(outer), rb_obj_classname(module)); 653 } 654 module = rb_define_module_id(id); 655 rb_const_set(outer, id, module); 656 rb_set_class_path_string(module, outer, rb_id2str(id)); 657 rb_gc_register_mark_object(module); 658 659 return module; 660} 661 662VALUE 663rb_include_class_new(VALUE module, VALUE super) 664{ 665 VALUE klass = class_alloc(T_ICLASS, rb_cClass); 666 667 if (BUILTIN_TYPE(module) == T_ICLASS) { 668 module = RBASIC(module)->klass; 669 } 670 if (!RCLASS_IV_TBL(module)) { 671 RCLASS_IV_TBL(module) = st_init_numtable(); 672 } 673 if (!RCLASS_CONST_TBL(module)) { 674 RCLASS_CONST_TBL(module) = st_init_numtable(); 675 } 676 RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module); 677 RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module); 678 RCLASS_M_TBL(klass) = RCLASS_M_TBL(RCLASS_ORIGIN(module)); 679 RCLASS_SUPER(klass) = super; 680 if (RB_TYPE_P(module, T_ICLASS)) { 681 RBASIC(klass)->klass = RBASIC(module)->klass; 682 } 683 else { 684 RBASIC(klass)->klass = module; 685 } 686 OBJ_INFECT(klass, module); 687 OBJ_INFECT(klass, super); 688 689 return (VALUE)klass; 690} 691 692static int include_modules_at(const VALUE klass, VALUE c, VALUE module); 693 694void 695rb_include_module(VALUE klass, VALUE module) 696{ 697 int changed = 0; 698 699 rb_frozen_class_p(klass); 700 if (!OBJ_UNTRUSTED(klass)) { 701 rb_secure(4); 702 } 703 704 if (!RB_TYPE_P(module, T_MODULE)) { 705 Check_Type(module, T_MODULE); 706 } 707 708 OBJ_INFECT(klass, module); 709 710 changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module); 711 if (changed < 0) 712 rb_raise(rb_eArgError, "cyclic include detected"); 713 if (changed) rb_clear_cache(); 714} 715 716static int 717add_refined_method_entry_i(st_data_t key, st_data_t value, st_data_t data) 718{ 719 rb_add_refined_method_entry((VALUE) data, (ID) key); 720 return ST_CONTINUE; 721} 722 723static int 724include_modules_at(const VALUE klass, VALUE c, VALUE module) 725{ 726 VALUE p; 727 int changed = 0; 728 const st_table *const klass_m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(klass)); 729 730 while (module) { 731 int superclass_seen = FALSE; 732 733 if (RCLASS_ORIGIN(module) != module) 734 goto skip; 735 if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module)) 736 return -1; 737 /* ignore if the module included already in superclasses */ 738 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) { 739 switch (BUILTIN_TYPE(p)) { 740 case T_ICLASS: 741 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) { 742 if (!superclass_seen) { 743 c = p; /* move insertion point */ 744 } 745 goto skip; 746 } 747 break; 748 case T_CLASS: 749 superclass_seen = TRUE; 750 break; 751 } 752 } 753 c = RCLASS_SUPER(c) = rb_include_class_new(module, RCLASS_SUPER(c)); 754 if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) { 755 VALUE refined_class = 756 rb_refinement_module_get_refined_class(klass); 757 758 st_foreach(RMODULE_M_TBL(module), add_refined_method_entry_i, 759 (st_data_t) refined_class); 760 FL_SET(c, RMODULE_INCLUDED_INTO_REFINEMENT); 761 } 762 if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries) 763 changed = 1; 764 if (RMODULE_CONST_TBL(module) && RMODULE_CONST_TBL(module)->num_entries) 765 changed = 1; 766 skip: 767 module = RCLASS_SUPER(module); 768 } 769 770 return changed; 771} 772 773static int 774move_refined_method(st_data_t key, st_data_t value, st_data_t data) 775{ 776 rb_method_entry_t *me = (rb_method_entry_t *) value; 777 st_table *tbl = (st_table *) data; 778 779 if (me->def->type == VM_METHOD_TYPE_REFINED) { 780 if (me->def->body.orig_me) { 781 rb_method_entry_t *orig_me = me->def->body.orig_me, *new_me; 782 me->def->body.orig_me = NULL; 783 new_me = ALLOC(rb_method_entry_t); 784 *new_me = *me; 785 st_add_direct(tbl, key, (st_data_t) new_me); 786 *me = *orig_me; 787 xfree(orig_me); 788 return ST_CONTINUE; 789 } 790 else { 791 st_add_direct(tbl, key, (st_data_t) me); 792 return ST_DELETE; 793 } 794 } 795 else { 796 return ST_CONTINUE; 797 } 798} 799 800void 801rb_prepend_module(VALUE klass, VALUE module) 802{ 803 void rb_vm_check_redefinition_by_prepend(VALUE klass); 804 VALUE origin; 805 int changed = 0; 806 807 rb_frozen_class_p(klass); 808 if (!OBJ_UNTRUSTED(klass)) { 809 rb_secure(4); 810 } 811 812 Check_Type(module, T_MODULE); 813 814 OBJ_INFECT(klass, module); 815 816 origin = RCLASS_ORIGIN(klass); 817 if (origin == klass) { 818 origin = class_alloc(T_ICLASS, klass); 819 RCLASS_SUPER(origin) = RCLASS_SUPER(klass); 820 RCLASS_SUPER(klass) = origin; 821 RCLASS_ORIGIN(klass) = origin; 822 RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass); 823 RCLASS_M_TBL(klass) = st_init_numtable(); 824 st_foreach(RCLASS_M_TBL(origin), move_refined_method, 825 (st_data_t) RCLASS_M_TBL(klass)); 826 } 827 changed = include_modules_at(klass, klass, module); 828 if (changed < 0) 829 rb_raise(rb_eArgError, "cyclic prepend detected"); 830 if (changed) { 831 rb_clear_cache(); 832 rb_vm_check_redefinition_by_prepend(klass); 833 } 834} 835 836/* 837 * call-seq: 838 * mod.included_modules -> array 839 * 840 * Returns the list of modules included in <i>mod</i>. 841 * 842 * module Mixin 843 * end 844 * 845 * module Outer 846 * include Mixin 847 * end 848 * 849 * Mixin.included_modules #=> [] 850 * Outer.included_modules #=> [Mixin] 851 */ 852 853VALUE 854rb_mod_included_modules(VALUE mod) 855{ 856 VALUE ary = rb_ary_new(); 857 VALUE p; 858 VALUE origin = RCLASS_ORIGIN(mod); 859 860 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) { 861 if (p != origin && BUILTIN_TYPE(p) == T_ICLASS) { 862 VALUE m = RBASIC(p)->klass; 863 if (RB_TYPE_P(m, T_MODULE)) 864 rb_ary_push(ary, m); 865 } 866 } 867 return ary; 868} 869 870/* 871 * call-seq: 872 * mod.include?(module) -> true or false 873 * 874 * Returns <code>true</code> if <i>module</i> is included in 875 * <i>mod</i> or one of <i>mod</i>'s ancestors. 876 * 877 * module A 878 * end 879 * class B 880 * include A 881 * end 882 * class C < B 883 * end 884 * B.include?(A) #=> true 885 * C.include?(A) #=> true 886 * A.include?(A) #=> false 887 */ 888 889VALUE 890rb_mod_include_p(VALUE mod, VALUE mod2) 891{ 892 VALUE p; 893 894 Check_Type(mod2, T_MODULE); 895 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) { 896 if (BUILTIN_TYPE(p) == T_ICLASS) { 897 if (RBASIC(p)->klass == mod2) return Qtrue; 898 } 899 } 900 return Qfalse; 901} 902 903/* 904 * call-seq: 905 * mod.ancestors -> array 906 * 907 * Returns a list of modules included in <i>mod</i> (including 908 * <i>mod</i> itself). 909 * 910 * module Mod 911 * include Math 912 * include Comparable 913 * end 914 * 915 * Mod.ancestors #=> [Mod, Comparable, Math] 916 * Math.ancestors #=> [Math] 917 */ 918 919VALUE 920rb_mod_ancestors(VALUE mod) 921{ 922 VALUE p, ary = rb_ary_new(); 923 924 for (p = mod; p; p = RCLASS_SUPER(p)) { 925 if (FL_TEST(p, FL_SINGLETON)) 926 continue; 927 if (BUILTIN_TYPE(p) == T_ICLASS) { 928 rb_ary_push(ary, RBASIC(p)->klass); 929 } 930 else if (p == RCLASS_ORIGIN(p)) { 931 rb_ary_push(ary, p); 932 } 933 } 934 return ary; 935} 936 937#define VISI(x) ((x)&NOEX_MASK) 938#define VISI_CHECK(x,f) (VISI(x) == (f)) 939 940static int 941ins_methods_push(ID name, long type, VALUE ary, long visi) 942{ 943 if (type == -1) return ST_CONTINUE; 944 945 switch (visi) { 946 case NOEX_PRIVATE: 947 case NOEX_PROTECTED: 948 case NOEX_PUBLIC: 949 visi = (type == visi); 950 break; 951 default: 952 visi = (type != NOEX_PRIVATE); 953 break; 954 } 955 if (visi) { 956 rb_ary_push(ary, ID2SYM(name)); 957 } 958 return ST_CONTINUE; 959} 960 961static int 962ins_methods_i(st_data_t name, st_data_t type, st_data_t ary) 963{ 964 return ins_methods_push((ID)name, (long)type, (VALUE)ary, -1); /* everything but private */ 965} 966 967static int 968ins_methods_prot_i(st_data_t name, st_data_t type, st_data_t ary) 969{ 970 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PROTECTED); 971} 972 973static int 974ins_methods_priv_i(st_data_t name, st_data_t type, st_data_t ary) 975{ 976 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PRIVATE); 977} 978 979static int 980ins_methods_pub_i(st_data_t name, st_data_t type, st_data_t ary) 981{ 982 return ins_methods_push((ID)name, (long)type, (VALUE)ary, NOEX_PUBLIC); 983} 984 985static int 986method_entry_i(st_data_t key, st_data_t value, st_data_t data) 987{ 988 const rb_method_entry_t *me = (const rb_method_entry_t *)value; 989 st_table *list = (st_table *)data; 990 long type; 991 992 if (me && me->def->type == VM_METHOD_TYPE_REFINED) { 993 me = rb_resolve_refined_method(Qnil, me, NULL); 994 if (!me) return ST_CONTINUE; 995 } 996 if (!st_lookup(list, key, 0)) { 997 if (UNDEFINED_METHOD_ENTRY_P(me)) { 998 type = -1; /* none */ 999 } 1000 else { 1001 type = VISI(me->flag); 1002 } 1003 st_add_direct(list, key, type); 1004 } 1005 return ST_CONTINUE; 1006} 1007 1008static VALUE 1009class_instance_method_list(int argc, VALUE *argv, VALUE mod, int obj, int (*func) (st_data_t, st_data_t, st_data_t)) 1010{ 1011 VALUE ary; 1012 int recur, prepended = 0; 1013 st_table *list; 1014 1015 if (argc == 0) { 1016 recur = TRUE; 1017 } 1018 else { 1019 VALUE r; 1020 rb_scan_args(argc, argv, "01", &r); 1021 recur = RTEST(r); 1022 } 1023 1024 if (!recur && RCLASS_ORIGIN(mod) != mod) { 1025 mod = RCLASS_ORIGIN(mod); 1026 prepended = 1; 1027 } 1028 1029 list = st_init_numtable(); 1030 for (; mod; mod = RCLASS_SUPER(mod)) { 1031 if (RCLASS_M_TBL(mod)) st_foreach(RCLASS_M_TBL(mod), method_entry_i, (st_data_t)list); 1032 if (BUILTIN_TYPE(mod) == T_ICLASS && !prepended) continue; 1033 if (obj && FL_TEST(mod, FL_SINGLETON)) continue; 1034 if (!recur) break; 1035 } 1036 ary = rb_ary_new(); 1037 st_foreach(list, func, ary); 1038 st_free_table(list); 1039 1040 return ary; 1041} 1042 1043/* 1044 * call-seq: 1045 * mod.instance_methods(include_super=true) -> array 1046 * 1047 * Returns an array containing the names of the public and protected instance 1048 * methods in the receiver. For a module, these are the public and protected methods; 1049 * for a class, they are the instance (not singleton) methods. With no 1050 * argument, or with an argument that is <code>false</code>, the 1051 * instance methods in <i>mod</i> are returned, otherwise the methods 1052 * in <i>mod</i> and <i>mod</i>'s superclasses are returned. 1053 * 1054 * module A 1055 * def method1() end 1056 * end 1057 * class B 1058 * def method2() end 1059 * end 1060 * class C < B 1061 * def method3() end 1062 * end 1063 * 1064 * A.instance_methods #=> [:method1] 1065 * B.instance_methods(false) #=> [:method2] 1066 * C.instance_methods(false) #=> [:method3] 1067 * C.instance_methods(true).length #=> 43 1068 */ 1069 1070VALUE 1071rb_class_instance_methods(int argc, VALUE *argv, VALUE mod) 1072{ 1073 return class_instance_method_list(argc, argv, mod, 0, ins_methods_i); 1074} 1075 1076/* 1077 * call-seq: 1078 * mod.protected_instance_methods(include_super=true) -> array 1079 * 1080 * Returns a list of the protected instance methods defined in 1081 * <i>mod</i>. If the optional parameter is not <code>false</code>, the 1082 * methods of any ancestors are included. 1083 */ 1084 1085VALUE 1086rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod) 1087{ 1088 return class_instance_method_list(argc, argv, mod, 0, ins_methods_prot_i); 1089} 1090 1091/* 1092 * call-seq: 1093 * mod.private_instance_methods(include_super=true) -> array 1094 * 1095 * Returns a list of the private instance methods defined in 1096 * <i>mod</i>. If the optional parameter is not <code>false</code>, the 1097 * methods of any ancestors are included. 1098 * 1099 * module Mod 1100 * def method1() end 1101 * private :method1 1102 * def method2() end 1103 * end 1104 * Mod.instance_methods #=> [:method2] 1105 * Mod.private_instance_methods #=> [:method1] 1106 */ 1107 1108VALUE 1109rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod) 1110{ 1111 return class_instance_method_list(argc, argv, mod, 0, ins_methods_priv_i); 1112} 1113 1114/* 1115 * call-seq: 1116 * mod.public_instance_methods(include_super=true) -> array 1117 * 1118 * Returns a list of the public instance methods defined in <i>mod</i>. 1119 * If the optional parameter is not <code>false</code>, the methods of 1120 * any ancestors are included. 1121 */ 1122 1123VALUE 1124rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod) 1125{ 1126 return class_instance_method_list(argc, argv, mod, 0, ins_methods_pub_i); 1127} 1128 1129/* 1130 * call-seq: 1131 * obj.methods(all=true) -> array 1132 * 1133 * Returns a list of the names of public and protected methods of 1134 * <i>obj</i>. This will include all the methods accessible in 1135 * <i>obj</i>'s ancestors. 1136 * If the <i>all</i> parameter is set to <code>false</code>, only those methods 1137 * in the receiver will be listed. 1138 * 1139 * class Klass 1140 * def klass_method() 1141 * end 1142 * end 1143 * k = Klass.new 1144 * k.methods[0..9] #=> [:klass_method, :nil?, :===, 1145 * # :==~, :!, :eql? 1146 * # :hash, :<=>, :class, :singleton_class] 1147 * k.methods.length #=> 57 1148 */ 1149 1150VALUE 1151rb_obj_methods(int argc, VALUE *argv, VALUE obj) 1152{ 1153 retry: 1154 if (argc == 0) { 1155 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i); 1156 } 1157 else { 1158 VALUE recur; 1159 1160 rb_scan_args(argc, argv, "1", &recur); 1161 if (RTEST(recur)) { 1162 argc = 0; 1163 goto retry; 1164 } 1165 return rb_obj_singleton_methods(argc, argv, obj); 1166 } 1167} 1168 1169/* 1170 * call-seq: 1171 * obj.protected_methods(all=true) -> array 1172 * 1173 * Returns the list of protected methods accessible to <i>obj</i>. If 1174 * the <i>all</i> parameter is set to <code>false</code>, only those methods 1175 * in the receiver will be listed. 1176 */ 1177 1178VALUE 1179rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj) 1180{ 1181 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i); 1182} 1183 1184/* 1185 * call-seq: 1186 * obj.private_methods(all=true) -> array 1187 * 1188 * Returns the list of private methods accessible to <i>obj</i>. If 1189 * the <i>all</i> parameter is set to <code>false</code>, only those methods 1190 * in the receiver will be listed. 1191 */ 1192 1193VALUE 1194rb_obj_private_methods(int argc, VALUE *argv, VALUE obj) 1195{ 1196 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i); 1197} 1198 1199/* 1200 * call-seq: 1201 * obj.public_methods(all=true) -> array 1202 * 1203 * Returns the list of public methods accessible to <i>obj</i>. If 1204 * the <i>all</i> parameter is set to <code>false</code>, only those methods 1205 * in the receiver will be listed. 1206 */ 1207 1208VALUE 1209rb_obj_public_methods(int argc, VALUE *argv, VALUE obj) 1210{ 1211 return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i); 1212} 1213 1214/* 1215 * call-seq: 1216 * obj.singleton_methods(all=true) -> array 1217 * 1218 * Returns an array of the names of singleton methods for <i>obj</i>. 1219 * If the optional <i>all</i> parameter is true, the list will include 1220 * methods in modules included in <i>obj</i>. 1221 * Only public and protected singleton methods are returned. 1222 * 1223 * module Other 1224 * def three() end 1225 * end 1226 * 1227 * class Single 1228 * def Single.four() end 1229 * end 1230 * 1231 * a = Single.new 1232 * 1233 * def a.one() 1234 * end 1235 * 1236 * class << a 1237 * include Other 1238 * def two() 1239 * end 1240 * end 1241 * 1242 * Single.singleton_methods #=> [:four] 1243 * a.singleton_methods(false) #=> [:two, :one] 1244 * a.singleton_methods #=> [:two, :one, :three] 1245 */ 1246 1247VALUE 1248rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj) 1249{ 1250 VALUE recur, ary, klass; 1251 st_table *list; 1252 1253 if (argc == 0) { 1254 recur = Qtrue; 1255 } 1256 else { 1257 rb_scan_args(argc, argv, "01", &recur); 1258 } 1259 klass = CLASS_OF(obj); 1260 list = st_init_numtable(); 1261 if (klass && FL_TEST(klass, FL_SINGLETON)) { 1262 if (RCLASS_M_TBL(klass)) 1263 st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list); 1264 klass = RCLASS_SUPER(klass); 1265 } 1266 if (RTEST(recur)) { 1267 while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) { 1268 if (RCLASS_M_TBL(klass)) 1269 st_foreach(RCLASS_M_TBL(klass), method_entry_i, (st_data_t)list); 1270 klass = RCLASS_SUPER(klass); 1271 } 1272 } 1273 ary = rb_ary_new(); 1274 st_foreach(list, ins_methods_i, ary); 1275 st_free_table(list); 1276 1277 return ary; 1278} 1279 1280/*! 1281 * \} 1282 */ 1283/*! 1284 * \defgroup defmethod Defining methods 1285 * There are some APIs to define a method from C. 1286 * These API takes a C function as a method body. 1287 * 1288 * \par Method body functions 1289 * Method body functions must return a VALUE and 1290 * can be one of the following form: 1291 * <dl> 1292 * <dt>Fixed number of parameters</dt> 1293 * <dd> 1294 * This form is a normal C function, excepting it takes 1295 * a receiver object as the first argument. 1296 * 1297 * \code 1298 * static VALUE my_method(VALUE self, VALUE x, VALUE y); 1299 * \endcode 1300 * </dd> 1301 * <dt>argc and argv style</dt> 1302 * <dd> 1303 * This form takes three parameters: \a argc, \a argv and \a self. 1304 * \a self is the receiver. \a argc is the number of arguments. 1305 * \a argv is a pointer to an array of the arguments. 1306 * 1307 * \code 1308 * static VALUE my_method(int argc, VALUE *argv, VALUE self); 1309 * \endcode 1310 * </dd> 1311 * <dt>Ruby array style</dt> 1312 * <dd> 1313 * This form takes two parameters: self and args. 1314 * \a self is the receiver. \a args is an Array object which 1315 * contains the arguments. 1316 * 1317 * \code 1318 * static VALUE my_method(VALUE self, VALUE args); 1319 * \endcode 1320 * </dd> 1321 * 1322 * \par Number of parameters 1323 * Method defining APIs takes the number of parameters which the 1324 * method will takes. This number is called \a argc. 1325 * \a argc can be: 1326 * <dl> 1327 * <dt>zero or positive number</dt> 1328 * <dd>This means the method body function takes a fixed number of parameters</dd> 1329 * <dt>-1</dt> 1330 * <dd>This means the method body function is "argc and argv" style.</dd> 1331 * <dt>-2</dt> 1332 * <dd>This means the method body function is "self and args" style.</dd> 1333 * </dl> 1334 * \{ 1335 */ 1336 1337void 1338rb_define_method_id(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc) 1339{ 1340 rb_add_method_cfunc(klass, mid, func, argc, NOEX_PUBLIC); 1341} 1342 1343void 1344rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc) 1345{ 1346 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PUBLIC); 1347} 1348 1349void 1350rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc) 1351{ 1352 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PROTECTED); 1353} 1354 1355void 1356rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc) 1357{ 1358 rb_add_method_cfunc(klass, rb_intern(name), func, argc, NOEX_PRIVATE); 1359} 1360 1361void 1362rb_undef_method(VALUE klass, const char *name) 1363{ 1364 rb_add_method(klass, rb_intern(name), VM_METHOD_TYPE_UNDEF, 0, NOEX_UNDEF); 1365} 1366 1367/*! 1368 * \} 1369 */ 1370/*! 1371 * \addtogroup class 1372 * \{ 1373 */ 1374 1375#define SPECIAL_SINGLETON(x,c) do {\ 1376 if (obj == (x)) {\ 1377 return (c);\ 1378 }\ 1379} while (0) 1380 1381static inline VALUE 1382special_singleton_class_of(VALUE obj) 1383{ 1384 SPECIAL_SINGLETON(Qnil, rb_cNilClass); 1385 SPECIAL_SINGLETON(Qfalse, rb_cFalseClass); 1386 SPECIAL_SINGLETON(Qtrue, rb_cTrueClass); 1387 return Qnil; 1388} 1389 1390VALUE 1391rb_special_singleton_class(VALUE obj) 1392{ 1393 return special_singleton_class_of(obj); 1394} 1395 1396/*! 1397 * \internal 1398 * Returns the singleton class of \a obj. Creates it if necessary. 1399 * 1400 * \note DO NOT expose the returned singleton class to 1401 * outside of class.c. 1402 * Use \ref rb_singleton_class instead for 1403 * consistency of the metaclass hierarchy. 1404 */ 1405static VALUE 1406singleton_class_of(VALUE obj) 1407{ 1408 VALUE klass; 1409 1410 if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) { 1411 rb_raise(rb_eTypeError, "can't define singleton"); 1412 } 1413 if (SPECIAL_CONST_P(obj)) { 1414 klass = special_singleton_class_of(obj); 1415 if (NIL_P(klass)) 1416 rb_bug("unknown immediate %p", (void *)obj); 1417 return klass; 1418 } 1419 else { 1420 enum ruby_value_type type = BUILTIN_TYPE(obj); 1421 if (type == T_FLOAT || type == T_BIGNUM) { 1422 rb_raise(rb_eTypeError, "can't define singleton"); 1423 } 1424 } 1425 1426 if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) && 1427 rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) { 1428 klass = RBASIC(obj)->klass; 1429 } 1430 else { 1431 klass = rb_make_metaclass(obj, RBASIC(obj)->klass); 1432 } 1433 1434 if (OBJ_TAINTED(obj)) { 1435 OBJ_TAINT(klass); 1436 } 1437 else { 1438 FL_UNSET(klass, FL_TAINT); 1439 } 1440 if (OBJ_UNTRUSTED(obj)) { 1441 OBJ_UNTRUST(klass); 1442 } 1443 else { 1444 FL_UNSET(klass, FL_UNTRUSTED); 1445 } 1446 if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass); 1447 1448 return klass; 1449} 1450 1451 1452/*! 1453 * Returns the singleton class of \a obj. Creates it if necessary. 1454 * 1455 * \param obj an arbitrary object. 1456 * \throw TypeError if \a obj is a Fixnum or a Symbol. 1457 * \return the singleton class. 1458 * 1459 * \post \a obj has its own singleton class. 1460 * \post if \a obj is a class, 1461 * the returned singleton class also has its own 1462 * singleton class in order to keep consistency of the 1463 * inheritance structure of metaclasses. 1464 * \note a new singleton class will be created 1465 * if \a obj does not have it. 1466 * \note the singleton classes for nil, true and false are: 1467 * NilClass, TrueClass and FalseClass. 1468 */ 1469VALUE 1470rb_singleton_class(VALUE obj) 1471{ 1472 VALUE klass = singleton_class_of(obj); 1473 1474 /* ensures an exposed class belongs to its own eigenclass */ 1475 if (RB_TYPE_P(obj, T_CLASS)) (void)ENSURE_EIGENCLASS(klass); 1476 1477 return klass; 1478} 1479 1480/*! 1481 * \} 1482 */ 1483 1484/*! 1485 * \addtogroup defmethod 1486 * \{ 1487 */ 1488 1489/*! 1490 * Defines a singleton method for \a obj. 1491 * \param obj an arbitrary object 1492 * \param name name of the singleton method 1493 * \param func the method body 1494 * \param argc the number of parameters, or -1 or -2. see \ref defmethod. 1495 */ 1496void 1497rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc) 1498{ 1499 rb_define_method(singleton_class_of(obj), name, func, argc); 1500} 1501 1502 1503 1504/*! 1505 * Defines a module function for \a module. 1506 * \param module an module or a class. 1507 * \param name name of the function 1508 * \param func the method body 1509 * \param argc the number of parameters, or -1 or -2. see \ref defmethod. 1510 */ 1511void 1512rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc) 1513{ 1514 rb_define_private_method(module, name, func, argc); 1515 rb_define_singleton_method(module, name, func, argc); 1516} 1517 1518 1519/*! 1520 * Defines a global function 1521 * \param name name of the function 1522 * \param func the method body 1523 * \param argc the number of parameters, or -1 or -2. see \ref defmethod. 1524 */ 1525void 1526rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc) 1527{ 1528 rb_define_module_function(rb_mKernel, name, func, argc); 1529} 1530 1531 1532/*! 1533 * Defines an alias of a method. 1534 * \param klass the class which the original method belongs to 1535 * \param name1 a new name for the method 1536 * \param name2 the original name of the method 1537 */ 1538void 1539rb_define_alias(VALUE klass, const char *name1, const char *name2) 1540{ 1541 rb_alias(klass, rb_intern(name1), rb_intern(name2)); 1542} 1543 1544/*! 1545 * Defines (a) public accessor method(s) for an attribute. 1546 * \param klass the class which the attribute will belongs to 1547 * \param name name of the attribute 1548 * \param read a getter method for the attribute will be defined if \a read is non-zero. 1549 * \param write a setter method for the attribute will be defined if \a write is non-zero. 1550 */ 1551void 1552rb_define_attr(VALUE klass, const char *name, int read, int write) 1553{ 1554 rb_attr(klass, rb_intern(name), read, write, FALSE); 1555} 1556 1557int 1558rb_obj_basic_to_s_p(VALUE obj) 1559{ 1560 const rb_method_entry_t *me = rb_method_entry(CLASS_OF(obj), rb_intern("to_s"), 0); 1561 if (me && me->def && me->def->type == VM_METHOD_TYPE_CFUNC && 1562 me->def->body.cfunc.func == rb_any_to_s) 1563 return 1; 1564 return 0; 1565} 1566 1567#include <stdarg.h> 1568 1569int 1570rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...) 1571{ 1572 int i; 1573 const char *p = fmt; 1574 VALUE *var; 1575 va_list vargs; 1576 int f_var = 0, f_hash = 0, f_block = 0; 1577 int n_lead = 0, n_opt = 0, n_trail = 0, n_mand; 1578 int argi = 0; 1579 VALUE hash = Qnil; 1580 1581 if (ISDIGIT(*p)) { 1582 n_lead = *p - '0'; 1583 p++; 1584 if (ISDIGIT(*p)) { 1585 n_opt = *p - '0'; 1586 p++; 1587 if (ISDIGIT(*p)) { 1588 n_trail = *p - '0'; 1589 p++; 1590 goto block_arg; 1591 } 1592 } 1593 } 1594 if (*p == '*') { 1595 f_var = 1; 1596 p++; 1597 if (ISDIGIT(*p)) { 1598 n_trail = *p - '0'; 1599 p++; 1600 } 1601 } 1602 block_arg: 1603 if (*p == ':') { 1604 f_hash = 1; 1605 p++; 1606 } 1607 if (*p == '&') { 1608 f_block = 1; 1609 p++; 1610 } 1611 if (*p != '\0') { 1612 rb_fatal("bad scan arg format: %s", fmt); 1613 } 1614 n_mand = n_lead + n_trail; 1615 1616 if (argc < n_mand) 1617 goto argc_error; 1618 1619 va_start(vargs, fmt); 1620 1621 /* capture an option hash - phase 1: pop */ 1622 if (f_hash && n_mand < argc) { 1623 VALUE last = argv[argc - 1]; 1624 1625 if (NIL_P(last)) { 1626 /* nil is taken as an empty option hash only if it is not 1627 ambiguous; i.e. '*' is not specified and arguments are 1628 given more than sufficient */ 1629 if (!f_var && n_mand + n_opt < argc) 1630 argc--; 1631 } 1632 else { 1633 hash = rb_check_hash_type(last); 1634 if (!NIL_P(hash)) 1635 argc--; 1636 } 1637 } 1638 /* capture leading mandatory arguments */ 1639 for (i = n_lead; i-- > 0; ) { 1640 var = va_arg(vargs, VALUE *); 1641 if (var) *var = argv[argi]; 1642 argi++; 1643 } 1644 /* capture optional arguments */ 1645 for (i = n_opt; i-- > 0; ) { 1646 var = va_arg(vargs, VALUE *); 1647 if (argi < argc - n_trail) { 1648 if (var) *var = argv[argi]; 1649 argi++; 1650 } 1651 else { 1652 if (var) *var = Qnil; 1653 } 1654 } 1655 /* capture variable length arguments */ 1656 if (f_var) { 1657 int n_var = argc - argi - n_trail; 1658 1659 var = va_arg(vargs, VALUE *); 1660 if (0 < n_var) { 1661 if (var) *var = rb_ary_new4(n_var, &argv[argi]); 1662 argi += n_var; 1663 } 1664 else { 1665 if (var) *var = rb_ary_new(); 1666 } 1667 } 1668 /* capture trailing mandatory arguments */ 1669 for (i = n_trail; i-- > 0; ) { 1670 var = va_arg(vargs, VALUE *); 1671 if (var) *var = argv[argi]; 1672 argi++; 1673 } 1674 /* capture an option hash - phase 2: assignment */ 1675 if (f_hash) { 1676 var = va_arg(vargs, VALUE *); 1677 if (var) *var = hash; 1678 } 1679 /* capture iterator block */ 1680 if (f_block) { 1681 var = va_arg(vargs, VALUE *); 1682 if (rb_block_given_p()) { 1683 *var = rb_block_proc(); 1684 } 1685 else { 1686 *var = Qnil; 1687 } 1688 } 1689 va_end(vargs); 1690 1691 if (argi < argc) { 1692 argc_error: 1693 rb_error_arity(argc, n_mand, f_var ? UNLIMITED_ARGUMENTS : n_mand + n_opt); 1694 } 1695 1696 return argc; 1697} 1698 1699/*! 1700 * \} 1701 */ 1702