Skip to content

Commit

Permalink
vm_eval.c: allow symbols to instance_eval/exec
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed Apr 23, 2015
1 parent 3badcfc commit c2a04d8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
@@ -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>

* lib/delegate.rb: fix a typo.
Expand Down
9 changes: 5 additions & 4 deletions class.c
Expand Up @@ -1539,7 +1539,8 @@ singleton_class_of(VALUE obj)
{
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");
}
if (SPECIAL_CONST_P(obj)) {
Expand All @@ -1549,9 +1550,9 @@ singleton_class_of(VALUE obj)
return klass;
}
else {
enum ruby_value_type type = BUILTIN_TYPE(obj);
if (type == T_FLOAT || type == T_BIGNUM) {
rb_raise(rb_eTypeError, "can't define singleton");
switch (BUILTIN_TYPE(obj)) {
case T_FLOAT: case T_BIGNUM: case T_SYMBOL:
goto no_singleton;
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/ruby/test_symbol.rb
Expand Up @@ -199,6 +199,35 @@ def test_singleton_method
assert_raise(TypeError) { a = :foo; def a.foo; 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
assert_equal(true, :foo.frozen?)
assert_equal(true, :each.frozen?)
Expand Down
32 changes: 16 additions & 16 deletions vm_eval.c
Expand Up @@ -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:
* obj.instance_eval(string [, filename [, lineno]] ) -> obj
Expand Down Expand Up @@ -1663,14 +1677,7 @@ specific_eval(int argc, const VALUE *argv, VALUE klass, VALUE self)
VALUE
rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
{
VALUE klass;

if (SPECIAL_CONST_P(self)) {
klass = rb_special_singleton_class(self);
}
else {
klass = rb_singleton_class(self);
}
VALUE klass = singleton_class_for_eval(self);
return specific_eval(argc, argv, klass, self);
}

Expand All @@ -1695,14 +1702,7 @@ rb_obj_instance_eval(int argc, const VALUE *argv, VALUE self)
VALUE
rb_obj_instance_exec(int argc, const VALUE *argv, VALUE self)
{
VALUE klass;

if (SPECIAL_CONST_P(self)) {
klass = rb_special_singleton_class(self);
}
else {
klass = rb_singleton_class(self);
}
VALUE klass = singleton_class_for_eval(self);
return yield_under(klass, self, rb_ary_new4(argc, argv));
}

Expand Down

0 comments on commit c2a04d8

Please sign in to comment.