diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-01-29 07:49:22 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-01-29 07:49:22 +0000 |
commit | b0c4ac77799fdf60a489f9e735158647c630865a (patch) | |
tree | 9ce10d018542a7dc056975f659fa064ce0c1c9e8 | |
parent | 2d55537dce5fde5e3581351bd274ecabf1e284d7 (diff) |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | ext/-test-/method/arity.c | 22 | ||||
-rw-r--r-- | ext/-test-/method/extconf.rb | 6 | ||||
-rw-r--r-- | ext/-test-/method/init.c | 11 | ||||
-rw-r--r-- | proc.c | 18 | ||||
-rw-r--r-- | test/-ext-/method/test_arity.rb | 37 |
6 files changed, 98 insertions, 1 deletions
@@ -1,3 +1,8 @@ +Tue Jan 29 16:49:19 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * 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 <akr@fsij.org> * 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); +} @@ -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<A + private :foo1, :foo2 + end + + METHODS = {foo0: 0, foo1: 1, foo2: 2, foom1: -1, foom2: -2} + + def test_base + METHODS.each do |name, arity| + assert_equal(arity, Bug::Method.mod_method_arity(A, name), "A##{name}") + end + end + + def test_zsuper + METHODS.each do |name, arity| + assert_equal(arity, Bug::Method.mod_method_arity(B, name), "B##{name}") + end + end + end +end |