vm_eval.c: allow symbols to instance_eval/exec
* vm_eval.c (rb_obj_instance_eval, rb_obj_instance_exec): allow symbols to just instance_eval/exec, execept for definition of singletons. [ruby-core:68961] [Bug #11086] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3badcfc1cb
commit
c2a04d87d0
@ -1,3 +1,9 @@
|
|||||||
|
Thu Apr 23 11:35:55 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* vm_eval.c (rb_obj_instance_eval, rb_obj_instance_exec): allow
|
||||||
|
symbols to just instance_eval/exec, execept for definition of
|
||||||
|
singletons. [ruby-core:68961] [Bug #11086]
|
||||||
|
|
||||||
Thu Apr 23 10:01:36 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
Thu Apr 23 10:01:36 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
||||||
|
|
||||||
* lib/delegate.rb: fix a typo.
|
* lib/delegate.rb: fix a typo.
|
||||||
|
9
class.c
9
class.c
@ -1539,7 +1539,8 @@ singleton_class_of(VALUE obj)
|
|||||||
{
|
{
|
||||||
VALUE klass;
|
VALUE klass;
|
||||||
|
|
||||||
if (FIXNUM_P(obj) || FLONUM_P(obj) || SYMBOL_P(obj)) {
|
if (FIXNUM_P(obj) || FLONUM_P(obj) || STATIC_SYM_P(obj)) {
|
||||||
|
no_singleton:
|
||||||
rb_raise(rb_eTypeError, "can't define singleton");
|
rb_raise(rb_eTypeError, "can't define singleton");
|
||||||
}
|
}
|
||||||
if (SPECIAL_CONST_P(obj)) {
|
if (SPECIAL_CONST_P(obj)) {
|
||||||
@ -1549,9 +1550,9 @@ singleton_class_of(VALUE obj)
|
|||||||
return klass;
|
return klass;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
enum ruby_value_type type = BUILTIN_TYPE(obj);
|
switch (BUILTIN_TYPE(obj)) {
|
||||||
if (type == T_FLOAT || type == T_BIGNUM) {
|
case T_FLOAT: case T_BIGNUM: case T_SYMBOL:
|
||||||
rb_raise(rb_eTypeError, "can't define singleton");
|
goto no_singleton;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,6 +199,35 @@ class TestSymbol < Test::Unit::TestCase
|
|||||||
assert_raise(TypeError) { a = :foo; def a.foo; end }
|
assert_raise(TypeError) { a = :foo; def a.foo; end }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
SymbolsForEval = [
|
||||||
|
:foo,
|
||||||
|
"dynsym_#{Random.rand(10000)}_#{Time.now}".to_sym
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_instance_eval
|
||||||
|
bug11086 = '[ruby-core:68961] [Bug #11086]'
|
||||||
|
SymbolsForEval.each do |sym|
|
||||||
|
assert_nothing_raised(TypeError, sym, bug11086) {
|
||||||
|
sym.instance_eval {}
|
||||||
|
}
|
||||||
|
assert_raise(TypeError, sym, bug11086) {
|
||||||
|
sym.instance_eval {def foo; end}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_instance_exec
|
||||||
|
bug11086 = '[ruby-core:68961] [Bug #11086]'
|
||||||
|
SymbolsForEval.each do |sym|
|
||||||
|
assert_nothing_raised(TypeError, sym, bug11086) {
|
||||||
|
sym.instance_exec {}
|
||||||
|
}
|
||||||
|
assert_raise(TypeError, sym, bug11086) {
|
||||||
|
sym.instance_exec {def foo; end}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_frozen_symbol
|
def test_frozen_symbol
|
||||||
assert_equal(true, :foo.frozen?)
|
assert_equal(true, :foo.frozen?)
|
||||||
assert_equal(true, :each.frozen?)
|
assert_equal(true, :each.frozen?)
|
||||||
|
32
vm_eval.c
32
vm_eval.c
@ -1627,6 +1627,20 @@ specific_eval(int argc, const VALUE *argv, VALUE klass, VALUE self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
singleton_class_for_eval(VALUE self)
|
||||||
|
{
|
||||||
|
if (SPECIAL_CONST_P(self)) {
|
||||||
|
return rb_special_singleton_class(self);
|
||||||
|
}
|
||||||
|
switch (BUILTIN_TYPE(self)) {
|
||||||
|
case T_FLOAT: case T_BIGNUM: case T_SYMBOL:
|
||||||
|
return Qnil;
|
||||||
|
default:
|
||||||
|
return rb_singleton_class(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* obj.instance_eval(string [, filename [, lineno]] ) -> obj
|
* obj.instance_eval(string [, filename [, lineno]] ) -> obj
|
||||||
@ -1663,14 +1677,7 @@ specific_eval(int argc, const VALUE *argv, VALUE klass, VALUE self)
|
|||||||
VALUE
|
VALUE
|
||||||
rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
|
rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
|
||||||
{
|
{
|
||||||
VALUE klass;
|
VALUE klass = singleton_class_for_eval(self);
|
||||||
|
|
||||||
if (SPECIAL_CONST_P(self)) {
|
|
||||||
klass = rb_special_singleton_class(self);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
klass = rb_singleton_class(self);
|
|
||||||
}
|
|
||||||
return specific_eval(argc, argv, klass, self);
|
return specific_eval(argc, argv, klass, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1695,14 +1702,7 @@ rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
|
|||||||
VALUE
|
VALUE
|
||||||
rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
|
rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
|
||||||
{
|
{
|
||||||
VALUE klass;
|
VALUE klass = singleton_class_for_eval(self);
|
||||||
|
|
||||||
if (SPECIAL_CONST_P(self)) {
|
|
||||||
klass = rb_special_singleton_class(self);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
klass = rb_singleton_class(self);
|
|
||||||
}
|
|
||||||
return yield_under(klass, self, rb_ary_new4(argc, argv));
|
return yield_under(klass, self, rb_ary_new4(argc, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user