diff --git a/ChangeLog b/ChangeLog index d6deffd149..e73cba4de4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +Sat Oct 27 11:01:10 2012 Koichi Sasada + + * numeric.c (rb_float_new_in_heap), include/ruby/ruby.h: + make all Float objects frozen. + [ruby-dev:46081] [ruby-trunk - Feature #6936] + Most part of patch by NARUSE, Yui . + + * class.c (singleton_class_of): raise TypeError when + trying to define a singleton method on Float objects. + + * vm.c (vm_define_method): ditto. + + * test/ruby/marshaltestlib.rb: catch up above changes. + + * test/ruby/test_class.rb: ditto. + + * test/test_pp.rb: ditto. + Sat Oct 27 10:50:53 2012 Aaron Patterson * object.c (rb_mod_const_get): make sure the constant name is diff --git a/class.c b/class.c index 11c463e1e3..c5f0390853 100644 --- a/class.c +++ b/class.c @@ -1322,6 +1322,10 @@ singleton_class_of(VALUE obj) rb_bug("unknown immediate %p", (void *)obj); return klass; } + else { + if (BUILTIN_TYPE(obj) == T_FLOAT) + rb_raise(rb_eTypeError, "can't define singleton"); + } if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) && rb_ivar_get(RBASIC(obj)->klass, id_attached) == obj) { diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 7fe38a0f1d..34f2c69152 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1133,7 +1133,7 @@ struct RBignum { (FL_TAINT | FL_UNTRUSTED); \ } while (0) -#define OBJ_FROZEN(x) (!!FL_TEST((x), FL_FREEZE)) +#define OBJ_FROZEN(x) (!!(FL_ABLE(x)?(RBASIC(x)->flags&(FL_FREEZE)):FLONUM_P(x))) #define OBJ_FREEZE(x) FL_SET((x), FL_FREEZE) #if SIZEOF_INT < SIZEOF_LONG diff --git a/numeric.c b/numeric.c index 722a6b8640..d1a1932b69 100644 --- a/numeric.c +++ b/numeric.c @@ -620,6 +620,7 @@ rb_float_new_in_heap(double d) NEWOBJ_OF(flt, struct RFloat, rb_cFloat, T_FLOAT); flt->float_value = d; + OBJ_FREEZE(flt); return (VALUE)flt; } diff --git a/test/ruby/marshaltestlib.rb b/test/ruby/marshaltestlib.rb index 39471aac64..d9d06325fe 100644 --- a/test/ruby/marshaltestlib.rb +++ b/test/ruby/marshaltestlib.rb @@ -207,30 +207,6 @@ module MarshalTestLib marshal_equal(NegativeZero) {|o| 1.0/o} end - def test_float_ivar - o1 = 1.23 - o1.instance_eval { @iv = 1 } - marshal_equal(o1) {|o| o.instance_eval { @iv }} - end - - def test_float_ivar_self - o1 = 5.5 - o1.instance_eval { @iv = o1 } - marshal_equal(o1) {|o| o.instance_eval { @iv }} - end - - def test_float_extend - o1 = 0.0/0.0 - o1.extend(Mod1) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } - o1.extend(Mod2) - marshal_equal(o1) { |o| - (class << self; self; end).ancestors - } - end - class MyRange < Range; def initialize(v, *args) super(*args); @v = v; end end def test_range marshal_equal(1..2) diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index 8b2328e289..a3384fa083 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -187,12 +187,8 @@ class TestClass < Test::Unit::TestCase def test_singleton_class assert_raise(TypeError) { 1.extend(Module.new) } - if 1.0.equal? 1.0 # flonum? - assert_raise(TypeError) { 1.0.extend(Module.new) } - else - assert_nothing_raised { 1.0.extend(Module.new) } - end - assert_nothing_raised { (2.0**1000).extend(Module.new) } + assert_raise(TypeError) { 1.0.extend(Module.new) } + assert_raise(TypeError) { (2.0**1000).extend(Module.new) } assert_raise(TypeError) { :foo.extend(Module.new) } assert_in_out_err([], <<-INPUT, %w(:foo :foo true true), []) diff --git a/test/test_pp.rb b/test/test_pp.rb index acd3e835b9..28a33fec5a 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -107,10 +107,6 @@ class PPInspectTest < Test::Unit::TestCase a.instance_eval { @a = nil } result = PP.pp(a, '') assert_equal("#{a.inspect}\n", result) - a = 1.0 - a.instance_eval { @a = nil } - result = PP.pp(a, '') - assert_equal("#{a.inspect}\n", result) end def test_to_s_without_iv diff --git a/vm.c b/vm.c index c47b5924f8..c9a6866d6f 100644 --- a/vm.c +++ b/vm.c @@ -1872,7 +1872,7 @@ vm_define_method(rb_thread_t *th, VALUE obj, ID id, VALUE iseqval, } if (is_singleton) { - if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) { + if (FIXNUM_P(obj) || SYMBOL_P(obj) || CLASS_OF(obj) == rb_cFloat) { rb_raise(rb_eTypeError, "can't define singleton method \"%s\" for %s", rb_id2name(id), rb_obj_classname(obj));