~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to object.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  object.c -
4
4
 
5
 
  $Author: yugui $
 
5
  $Author: drbrain $
6
6
  created at: Thu Jul 15 12:01:24 JST 1993
7
7
 
8
8
  Copyright (C) 1993-2007 Yukihiro Matsumoto
19
19
#include <ctype.h>
20
20
#include <math.h>
21
21
#include <float.h>
 
22
#include "constant.h"
 
23
#include "internal.h"
22
24
 
23
25
VALUE rb_cBasicObject;
24
26
VALUE rb_mKernel;
96
98
    return Qfalse;
97
99
}
98
100
 
 
101
/*
 
102
 * Generates a <code>Fixnum</code> hash value for this object.
 
103
 * This function must have the property that a.eql?(b) implies
 
104
 * a.hash <code>==</code> b.hash.
 
105
 * The hash value is used by class <code>Hash</code>.
 
106
 * Any hash value that exceeds the capacity of a <code>Fixnum</code> will be
 
107
 * truncated before being used.
 
108
 *
 
109
 *      "waffle".hash #=> -910576647
 
110
 */
99
111
VALUE
100
112
rb_obj_hash(VALUE obj)
101
113
{
221
233
            st_free_table(RCLASS_IV_TBL(dest));
222
234
            RCLASS_IV_TBL(dest) = 0;
223
235
        }
 
236
        if (RCLASS_CONST_TBL(dest)) {
 
237
            rb_free_const_table(RCLASS_CONST_TBL(dest));
 
238
            RCLASS_CONST_TBL(dest) = 0;
 
239
        }
224
240
        if (RCLASS_IV_TBL(obj)) {
225
241
            RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj));
226
242
        }
262
278
    }
263
279
    clone = rb_obj_alloc(rb_obj_class(obj));
264
280
    RBASIC(clone)->klass = rb_singleton_class_clone(obj);
265
 
    RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT) | FL_TEST(clone, FL_UNTRUSTED)) & ~(FL_FREEZE|FL_FINALIZE);
 
281
    RBASIC(clone)->flags = (RBASIC(obj)->flags | FL_TEST(clone, FL_TAINT) | FL_TEST(clone, FL_UNTRUSTED)) & ~(FL_FREEZE|FL_FINALIZE|FL_MARK);
266
282
    init_copy(clone, obj);
267
283
    rb_funcall(clone, id_init_clone, 1, obj);
268
284
    RBASIC(clone)->flags |= RBASIC(obj)->flags & FL_FREEZE;
410
426
static VALUE
411
427
rb_obj_inspect(VALUE obj)
412
428
{
413
 
    extern int rb_obj_basic_to_s_p(VALUE);
414
 
 
415
429
    if (TYPE(obj) == T_OBJECT && rb_obj_basic_to_s_p(obj)) {
416
430
        int has_ivar = 0;
417
431
        VALUE *ptr = ROBJECT_IVPTR(obj);
564
578
 *    New subclass: Baz
565
579
 */
566
580
 
 
581
/* Document-method: method_added
 
582
 *
 
583
 * call-seq:
 
584
 *   method_added(method_name)
 
585
 *
 
586
 * Invoked as a callback whenever an instance method is added to the
 
587
 * receiver.
 
588
 *
 
589
 *   module Chatty
 
590
 *     def self.method_added(method_name)
 
591
 *       puts "Adding #{method_name.inspect}"
 
592
 *     end
 
593
 *     def self.some_class_method() end
 
594
 *     def some_instance_method() end
 
595
 *   end
 
596
 *
 
597
 * produces:
 
598
 *
 
599
 *   Adding :some_instance_method
 
600
 *
 
601
 */
 
602
 
 
603
/* Document-method: method_removed
 
604
 *
 
605
 * call-seq:
 
606
 *   method_removed(method_name)
 
607
 *
 
608
 * Invoked as a callback whenever an instance method is removed from the
 
609
 * receiver.
 
610
 *
 
611
 *   module Chatty
 
612
 *     def self.method_removed(method_name)
 
613
 *       puts "Removing #{method_name.inspect}"
 
614
 *     end
 
615
 *     def self.some_class_method() end
 
616
 *     def some_instance_method() end
 
617
 *     class << self
 
618
 *       remove_method :some_class_method
 
619
 *     end
 
620
 *     remove_method :some_instance_method
 
621
 *   end
 
622
 *
 
623
 * produces:
 
624
 *
 
625
 *   Removing :some_instance_method
 
626
 *
 
627
 */
 
628
 
567
629
/*
568
630
 * Document-method: singleton_method_added
569
631
 *
668
730
 * Document-method: initialize
669
731
 *
670
732
 * call-seq:
671
 
 *    BasicObject.new( *args )
 
733
 *    BasicObject.new
672
734
 *
673
 
 * Returns a new BasicObject. Arguments are ignored.
 
735
 * Returns a new BasicObject.
674
736
 */
675
737
 
676
738
/*
712
774
{
713
775
    rb_secure(4);
714
776
    if (!OBJ_TAINTED(obj)) {
715
 
        if (OBJ_FROZEN(obj)) {
716
 
            rb_error_frozen("object");
717
 
        }
 
777
        rb_check_frozen(obj);
718
778
        OBJ_TAINT(obj);
719
779
    }
720
780
    return obj;
733
793
{
734
794
    rb_secure(3);
735
795
    if (OBJ_TAINTED(obj)) {
736
 
        if (OBJ_FROZEN(obj)) {
737
 
            rb_error_frozen("object");
738
 
        }
 
796
        rb_check_frozen(obj);
739
797
        FL_UNSET(obj, FL_TAINT);
740
798
    }
741
799
    return obj;
768
826
{
769
827
    rb_secure(4);
770
828
    if (!OBJ_UNTRUSTED(obj)) {
771
 
        if (OBJ_FROZEN(obj)) {
772
 
            rb_error_frozen("object");
773
 
        }
 
829
        rb_check_frozen(obj);
774
830
        OBJ_UNTRUST(obj);
775
831
    }
776
832
    return obj;
789
845
{
790
846
    rb_secure(3);
791
847
    if (OBJ_UNTRUSTED(obj)) {
792
 
        if (OBJ_FROZEN(obj)) {
793
 
            rb_error_frozen("object");
794
 
        }
 
848
        rb_check_frozen(obj);
795
849
        FL_UNSET(obj, FL_UNTRUSTED);
796
850
    }
797
851
    return obj;
1158
1212
}
1159
1213
 
1160
1214
 
1161
 
/* :nodoc: */
 
1215
/*
 
1216
 *  call-seq:
 
1217
 *     obj <=> other -> 0 or nil
 
1218
 *
 
1219
 *  Returns 0 if obj === other, otherwise nil.
 
1220
 */
1162
1221
static VALUE
1163
1222
rb_obj_cmp(VALUE obj1, VALUE obj2)
1164
1223
{
1418
1477
 *  the module object, and the block is evaluated in the context of this
1419
1478
 *  module using <code>module_eval</code>.
1420
1479
 *
1421
 
 *     Fred = Module.new do
 
1480
 *     fred = Module.new do
1422
1481
 *       def meth1
1423
1482
 *         "hello"
1424
1483
 *       end
1427
1486
 *       end
1428
1487
 *     end
1429
1488
 *     a = "my string"
1430
 
 *     a.extend(Fred)   #=> "my string"
 
1489
 *     a.extend(fred)   #=> "my string"
1431
1490
 *     a.meth1          #=> "hello"
1432
1491
 *     a.meth2          #=> "bye"
 
1492
 *
 
1493
 *  Assign the module to a constant (name starting uppercase) if you
 
1494
 *  want to treat it like a regular module.
1433
1495
 */
1434
1496
 
1435
1497
static VALUE
1436
1498
rb_mod_initialize(VALUE module)
1437
1499
{
1438
 
    extern VALUE rb_mod_module_exec(int argc, VALUE *argv, VALUE mod);
1439
 
 
1440
1500
    if (rb_block_given_p()) {
1441
1501
        rb_mod_module_exec(1, &module, module);
1442
1502
    }
1445
1505
 
1446
1506
/*
1447
1507
 *  call-seq:
1448
 
 *     Class.new(super_class=Object)   ->    a_class
 
1508
 *     Class.new(super_class=Object)               -> a_class
 
1509
 *     Class.new(super_class=Object) { |mod| ... } -> a_class
1449
1510
 *
1450
1511
 *  Creates a new anonymous (unnamed) class with the given superclass
1451
1512
 *  (or <code>Object</code> if no parameter is given). You can give a
1452
1513
 *  class a name by assigning the class object to a constant.
1453
1514
 *
 
1515
 *  If a block is given, it is passed the class object, and the block
 
1516
 *  is evaluated in the context of this class using
 
1517
 *  <code>class_eval</code>.
 
1518
 *
 
1519
 *     fred = Class.new do
 
1520
 *       def meth1
 
1521
 *         "hello"
 
1522
 *       end
 
1523
 *       def meth2
 
1524
 *         "bye"
 
1525
 *       end
 
1526
 *     end
 
1527
 *
 
1528
 *     a = fred.new     #=> #<#<Class:0x100381890>:0x100376b98>
 
1529
 *     a.meth1          #=> "hello"
 
1530
 *     a.meth2          #=> "bye"
 
1531
 *
 
1532
 *  Assign the class to a constant (name starting uppercase) if you
 
1533
 *  want to treat it like a regular class.
1454
1534
 */
1455
1535
 
1456
1536
static VALUE
1566
1646
 *
1567
1647
 */
1568
1648
 
1569
 
static VALUE
 
1649
VALUE
1570
1650
rb_class_superclass(VALUE klass)
1571
1651
{
1572
1652
    VALUE super = RCLASS_SUPER(klass);
1584
1664
    return super;
1585
1665
}
1586
1666
 
 
1667
VALUE
 
1668
rb_class_get_superclass(VALUE klass)
 
1669
{
 
1670
    return RCLASS_SUPER(klass);
 
1671
}
 
1672
 
1587
1673
/*
1588
1674
 *  call-seq:
1589
1675
 *     attr_reader(symbol, ...)    -> nil
1665
1751
 *  call-seq:
1666
1752
 *     mod.const_get(sym, inherit=true)    -> obj
1667
1753
 *
1668
 
 *  Returns the value of the named constant in <i>mod</i>.
 
1754
 *  Checks for a constant with the given name in <i>mod</i>
 
1755
 *  If +inherit+ is set, the lookup will also search
 
1756
 *  the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
 
1757
 *
 
1758
 *  The value of the constant is returned if a definition is found,
 
1759
 *  otherwise a +NameError+ is raised.
1669
1760
 *
1670
1761
 *     Math.const_get(:PI)   #=> 3.14159265358979
1671
 
 *
1672
 
 *  If the constant is not defined or is defined by the ancestors and
1673
 
 *  +inherit+ is false, +NameError+ will be raised.
1674
1762
 */
1675
1763
 
1676
1764
static VALUE
1721
1809
 *  call-seq:
1722
1810
 *     mod.const_defined?(sym, inherit=true)   -> true or false
1723
1811
 *
1724
 
 *  Returns <code>true</code> if a constant with the given name is
1725
 
 *  defined by <i>mod</i>, or its ancestors if +inherit+ is not false.
 
1812
 *  Checks for a constant with the given name in <i>mod</i>
 
1813
 *  If +inherit+ is set, the lookup will also search
 
1814
 *  the ancestors (and +Object+ if <i>mod</i> is a +Module+.)
 
1815
 *
 
1816
 *  Returns whether or not a definition is found:
1726
1817
 *
1727
1818
 *     Math.const_defined? "PI"   #=> true
1728
 
 *     IO.const_defined? "SYNC"   #=> true
1729
 
 *     IO.const_defined? "SYNC", false   #=> false
 
1819
 *     IO.const_defined? :SYNC   #=> true
 
1820
 *     IO.const_defined? :SYNC, false   #=> false
1730
1821
 */
1731
1822
 
1732
1823
static VALUE
1749
1840
    return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id);
1750
1841
}
1751
1842
 
1752
 
VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj);
1753
 
VALUE rb_obj_protected_methods(int argc, VALUE *argv, VALUE obj);
1754
 
VALUE rb_obj_private_methods(int argc, VALUE *argv, VALUE obj);
1755
 
VALUE rb_obj_public_methods(int argc, VALUE *argv, VALUE obj);
1756
 
 
1757
1843
/*
1758
1844
 *  call-seq:
1759
1845
 *     obj.instance_variable_get(symbol)    -> obj
2108
2194
 *     Integer(123.999)    #=> 123
2109
2195
 *     Integer("0x1a")     #=> 26
2110
2196
 *     Integer(Time.new)   #=> 1204973019
 
2197
 *     Integer("0930", 10) #=> 930
 
2198
 *     Integer("111", 2)   #=> 7
2111
2199
 */
2112
2200
 
2113
2201
static VALUE
2218
2306
{
2219
2307
    char *s;
2220
2308
    long len;
 
2309
    double ret;
 
2310
    VALUE v = 0;
2221
2311
 
2222
2312
    StringValue(str);
2223
2313
    s = RSTRING_PTR(str);
2227
2317
            rb_raise(rb_eArgError, "string for Float contains null byte");
2228
2318
        }
2229
2319
        if (s[len]) {           /* no sentinel somehow */
2230
 
            char *p = ALLOCA_N(char, len+1);
2231
 
 
 
2320
            char *p =  ALLOCV(v, len);
2232
2321
            MEMCPY(p, s, char, len);
2233
2322
            p[len] = '\0';
2234
2323
            s = p;
2235
2324
        }
2236
2325
    }
2237
 
    return rb_cstr_to_dbl(s, badcheck);
 
2326
    ret = rb_cstr_to_dbl(s, badcheck);
 
2327
    if (v)
 
2328
        ALLOCV_END(v);
 
2329
    return ret;
2238
2330
}
2239
2331
 
2240
2332
VALUE
2329
2421
VALUE
2330
2422
rb_String(VALUE val)
2331
2423
{
2332
 
    return rb_convert_type(val, T_STRING, "String", "to_s");
 
2424
    VALUE tmp = rb_check_string_type(val);
 
2425
    if (NIL_P(tmp))
 
2426
        tmp = rb_convert_type(val, T_STRING, "String", "to_s");
 
2427
    return tmp;
2333
2428
}
2334
2429
 
2335
2430
 
2458
2553
 * \ingroup class
2459
2554
 */
2460
2555
 
2461
 
/*
2462
 
 *
2463
 
 *  <code>BasicObject</code> is the parent class of all classes in Ruby.
2464
 
 *  It's an explicit blank class.  <code>Object</code>, the root of Ruby's
2465
 
 *  class hierarchy is a direct subclass of <code>BasicObject</code>.  Its
2466
 
 *  methods are therefore available to all objects unless explicitly
2467
 
 *  overridden.
2468
 
 *
2469
 
 *  <code>Object</code> mixes in the <code>Kernel</code> module, making
2470
 
 *  the built-in kernel functions globally accessible. Although the
2471
 
 *  instance methods of <code>Object</code> are defined by the
2472
 
 *  <code>Kernel</code> module, we have chosen to document them here for
2473
 
 *  clarity.
 
2556
/*  Document-class: BasicObject
 
2557
 *
 
2558
 *  BasicObject is the parent class of all classes in Ruby.  It's an explicit
 
2559
 *  blank class.
 
2560
 *
 
2561
 *  BasicObject can be used for creating object hierarchies independent of
 
2562
 *  Ruby's object hierarchy, proxy objects like the Delegator class, or other
 
2563
 *  uses where namespace pollution from Ruby's methods and classes must be
 
2564
 *  avoided.
 
2565
 *
 
2566
 *  To avoid polluting BasicObject for other users an appropriately named
 
2567
 *  subclass of BasicObject should be created instead of directly modifying
 
2568
 *  BasicObject:
 
2569
 *
 
2570
 *    class MyObjectSystem < BasicObject
 
2571
 *    end
 
2572
 *
 
2573
 *  BasicObject does not include Kernel (for methods like +puts+) and
 
2574
 *  BasicObject is outside of the namespace of the standard library so common
 
2575
 *  classes will not be found without a using a full class path.
 
2576
 *
 
2577
 *  A variety of strategies can be used to provide useful portions of the
 
2578
 *  standard library to subclasses of BasicObject.  A subclass could
 
2579
 *  <code>include Kernel</code> to obtain +puts+, +exit+, etc.  A custom
 
2580
 *  Kernel-like module could be created and included or delegation can be used
 
2581
 *  via #method_missing:
 
2582
 *
 
2583
 *    class MyObjectSystem < BasicObject
 
2584
 *      DELEGATE = [:puts, :p]
 
2585
 *
 
2586
 *      def method_missing(name, *args, &block)
 
2587
 *        super unless DELEGATE.include? name
 
2588
 *        ::Kernel.send(name, *args, &block)
 
2589
 *      end
 
2590
 *
 
2591
 *      def respond_to_missing?(name, include_private = false)
 
2592
 *        DELGATE.include?(name) or super
 
2593
 *      end
 
2594
 *    end
 
2595
 *
 
2596
 *  Access to classes and modules from the Ruby standard library can be
 
2597
 *  obtained in a BasicObject subclass by referencing the desired constant
 
2598
 *  from the root like <code>::File</code> or <code>::Enumerator</code>.
 
2599
 *  Like #method_missing, #const_missing can be used to delegate constant
 
2600
 *  lookup to +Object+:
 
2601
 *
 
2602
 *    class MyObjectSystem < BasicObject
 
2603
 *      def self.const_missing(name)
 
2604
 *        ::Object.const_get(name)
 
2605
 *      end
 
2606
 *    end
 
2607
 */
 
2608
 
 
2609
/*  Document-class: Object
 
2610
 *
 
2611
 *  Object is the root of Ruby's class hierarchy.  Its methods are available
 
2612
 *  to all classes unless explicitly overridden.
 
2613
 *
 
2614
 *  Object mixes in the Kernel module, making the built-in kernel functions
 
2615
 *  globally accessible. Although the instance methods of Object are defined
 
2616
 *  by the Kernel module, we have chosen to document them here for clarity.
2474
2617
 *
2475
2618
 *  In the descriptions of Object's methods, the parameter <i>symbol</i> refers
2476
 
 *  to a symbol, which is either a quoted string or a
2477
 
 *  <code>Symbol</code> (such as <code>:name</code>).
 
2619
 *  to a symbol, which is either a quoted string or a Symbol (such as
 
2620
 *  <code>:name</code>).
2478
2621
 */
2479
2622
 
2480
2623
void
2481
2624
Init_Object(void)
2482
2625
{
2483
 
    extern void Init_class_hierarchy(void);
2484
2626
    int i;
2485
2627
 
2486
2628
    Init_class_hierarchy();
2487
2629
 
 
2630
#if 0
 
2631
    // teach RDoc about these classes
 
2632
    rb_cBasicObject = rb_define_class("BasicObject", Qnil);
 
2633
    rb_cObject = rb_define_class("Object", rb_cBasicObject);
 
2634
    rb_cModule = rb_define_class("Module", rb_cObject);
 
2635
    rb_cClass =  rb_define_class("Class",  rb_cModule);
 
2636
#endif
 
2637
 
2488
2638
#undef rb_intern
2489
2639
#define rb_intern(str) rb_intern_const(str)
2490
2640
 
2491
 
    rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, -1);
 
2641
    rb_define_private_method(rb_cBasicObject, "initialize", rb_obj_dummy, 0);
2492
2642
    rb_define_alloc_func(rb_cBasicObject, rb_class_allocate_instance);
2493
2643
    rb_define_method(rb_cBasicObject, "==", rb_obj_equal, 1);
2494
2644
    rb_define_method(rb_cBasicObject, "equal?", rb_obj_equal, 1);
2574
2724
    rb_define_method(rb_cNilClass, "nil?", rb_true, 0);
2575
2725
    rb_undef_alloc_func(rb_cNilClass);
2576
2726
    rb_undef_method(CLASS_OF(rb_cNilClass), "new");
 
2727
    /*
 
2728
     * An alias of +nil+
 
2729
     */
2577
2730
    rb_define_global_const("NIL", Qnil);
2578
2731
 
2579
2732
    rb_define_method(rb_cModule, "freeze", rb_mod_freeze, 0);
2621
2774
    rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1);
2622
2775
    rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2);
2623
2776
    rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1);
 
2777
    rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1);
 
2778
    rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1);
2624
2779
 
2625
2780
    rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0);
2626
2781
    rb_define_method(rb_cClass, "new", rb_class_new_instance, -1);
2641
2796
    rb_define_method(rb_cTrueClass, "^", true_xor, 1);
2642
2797
    rb_undef_alloc_func(rb_cTrueClass);
2643
2798
    rb_undef_method(CLASS_OF(rb_cTrueClass), "new");
 
2799
    /*
 
2800
     * An alias of +true+
 
2801
     */
2644
2802
    rb_define_global_const("TRUE", Qtrue);
2645
2803
 
2646
2804
    rb_cFalseClass = rb_define_class("FalseClass", rb_cObject);
2650
2808
    rb_define_method(rb_cFalseClass, "^", false_xor, 1);
2651
2809
    rb_undef_alloc_func(rb_cFalseClass);
2652
2810
    rb_undef_method(CLASS_OF(rb_cFalseClass), "new");
 
2811
    /*
 
2812
     * An alias of +false+
 
2813
     */
2653
2814
    rb_define_global_const("FALSE", Qfalse);
2654
2815
 
2655
2816
    id_eq = rb_intern("==");