From b0c4ac77799fdf60a489f9e735158647c630865a Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 29 Jan 2013 07:49:22 +0000 Subject: proc.c: original arity * proc.c (rb_mod_method_arity): return original arity of the method if aliased because of visibility change, like as Method#arity. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38967 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ ext/-test-/method/arity.c | 22 ++++++++++++++++++++++ ext/-test-/method/extconf.rb | 6 ++++++ ext/-test-/method/init.c | 11 +++++++++++ proc.c | 18 +++++++++++++++++- test/-ext-/method/test_arity.rb | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 ext/-test-/method/arity.c create mode 100644 ext/-test-/method/extconf.rb create mode 100644 ext/-test-/method/init.c create mode 100644 test/-ext-/method/test_arity.rb diff --git a/ChangeLog b/ChangeLog index c57def42bb..ef78cf14b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Jan 29 16:49:19 2013 Nobuyoshi Nakada + + * proc.c (rb_mod_method_arity): return original arity of the method if + aliased because of visibility change, like as Method#arity. + Tue Jan 29 12:05:18 2013 Tanaka Akira * test/ruby/test_marshal.rb: remove temporally files early. diff --git a/ext/-test-/method/arity.c b/ext/-test-/method/arity.c new file mode 100644 index 0000000000..239b9f7f10 --- /dev/null +++ b/ext/-test-/method/arity.c @@ -0,0 +1,22 @@ +#include "ruby.h" + +static VALUE +obj_method_arity(VALUE self, VALUE obj, VALUE mid) +{ + int arity = rb_obj_method_arity(obj, rb_check_id(&mid)); + return INT2FIX(arity); +} + +static VALUE +mod_method_arity(VALUE self, VALUE mod, VALUE mid) +{ + int arity = rb_mod_method_arity(mod, rb_check_id(&mid)); + return INT2FIX(arity); +} + +void +Init_arity(VALUE mod) +{ + rb_define_module_function(mod, "obj_method_arity", obj_method_arity, 2); + rb_define_module_function(mod, "mod_method_arity", mod_method_arity, 2); +} diff --git a/ext/-test-/method/extconf.rb b/ext/-test-/method/extconf.rb new file mode 100644 index 0000000000..658b7af1f1 --- /dev/null +++ b/ext/-test-/method/extconf.rb @@ -0,0 +1,6 @@ +$srcs = Dir[File.join($srcdir, "*.{#{SRC_EXT.join(%q{,})}}")] +inits = $srcs.map {|s| File.basename(s, ".*")} +inits.delete("init") +inits.map! {|s|"X(#{s})"} +$defs << "-DTEST_INIT_FUNCS(X)=\"#{inits.join(' ')}\"" +create_makefile("-test-/method") diff --git a/ext/-test-/method/init.c b/ext/-test-/method/init.c new file mode 100644 index 0000000000..cea2346240 --- /dev/null +++ b/ext/-test-/method/init.c @@ -0,0 +1,11 @@ +#include "ruby.h" + +#define init(n) {void Init_##n(VALUE klass); Init_##n(klass);} + +void +Init_method(void) +{ + VALUE mBug = rb_define_module("Bug"); + VALUE klass = rb_define_module_under(mBug, "Method"); + TEST_INIT_FUNCS(init); +} diff --git a/proc.c b/proc.c index 07592c12dd..cbb676e8ca 100644 --- a/proc.c +++ b/proc.c @@ -1743,10 +1743,26 @@ method_arity(VALUE method) return rb_method_entry_arity(data->me); } +static rb_method_entry_t * +original_method_entry(VALUE mod, ID id) +{ + VALUE rclass; + rb_method_entry_t *me; + while ((me = rb_method_entry(mod, id, &rclass)) != 0) { + rb_method_definition_t *def = me->def; + if (!def) break; + if (def->type != VM_METHOD_TYPE_ZSUPER) break; + mod = RCLASS_SUPER(rclass); + id = def->original_id; + } + return me; +} + int rb_mod_method_arity(VALUE mod, ID id) { - rb_method_entry_t *me = rb_method_entry(mod, id, 0); + rb_method_entry_t *me = original_method_entry(mod, id); + if (!me) return 0; /* should raise? */ return rb_method_entry_arity(me); } diff --git a/test/-ext-/method/test_arity.rb b/test/-ext-/method/test_arity.rb new file mode 100644 index 0000000000..79ef23b34f --- /dev/null +++ b/test/-ext-/method/test_arity.rb @@ -0,0 +1,37 @@ +require '-test-/method' +require 'test/unit' + +class TestMethod < Test::Unit::TestCase + class TestArity < Test::Unit::TestCase + class A + def foo0() + end + def foom1(*a) + end + def foom2(a,*b) + end + def foo1(a) + end + def foo2(a,b) + end + end + + class B