From 97c3bad4782be995863b778afe189aea42b8afc6 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 12 Sep 2009 15:46:42 +0000 Subject: [PATCH] * vm_method.c (CALL_METHOD_HOOK): refined with macro. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24873 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 +++- vm_method.c | 39 ++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3abf75cd43..8df184cd2b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,6 @@ -Sun Sep 13 00:27:57 2009 Nobuyoshi Nakada +Sun Sep 13 00:46:39 2009 Nobuyoshi Nakada + + * vm_method.c (CALL_METHOD_HOOK): refined with macro. * vm_method.c (rb_remove_method_id): no definition body is undefined. diff --git a/vm_method.c b/vm_method.c index 8764a56722..f3a0e92eeb 100644 --- a/vm_method.c +++ b/vm_method.c @@ -10,7 +10,7 @@ static void rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me); static ID object_id; static ID removed, singleton_removed, undefined, singleton_undefined; -static ID added, singleton_added; +static ID added, singleton_added, attached; struct cache_entry { /* method hash table. */ ID mid; /* method's id */ @@ -165,7 +165,7 @@ rb_add_method_def(VALUE klass, ID mid, rb_method_type_t type, rb_method_definiti type == VM_METHOD_TYPE_CFUNC && mid == rb_intern("allocate")) { rb_warn("defining %s.allocate is deprecated; use rb_define_alloc_func()", - rb_class2name(rb_iv_get(klass, "__attached__"))); + rb_class2name(rb_ivar_get(klass, attached))); mid = ID_ALLOCATOR; } if (OBJ_FROZEN(klass)) { @@ -215,16 +215,22 @@ rb_add_method_def(VALUE klass, ID mid, rb_method_type_t type, rb_method_definiti return me; } +#define CALL_METHOD_HOOK(klass, hook, mid) do { \ + const VALUE arg = ID2SYM(mid); \ + VALUE recv_class = klass; \ + ID hook_id = hook; \ + if (FL_TEST(klass, FL_SINGLETON)) { \ + recv_class = rb_ivar_get(klass, attached); \ + hook_id = singleton_##hook; \ + } \ + rb_funcall2(recv_class, hook_id, 1, &arg); \ + } while (0) + static void method_added(VALUE klass, ID mid) { if (mid != ID_ALLOCATOR && ruby_running) { - if (FL_TEST(klass, FL_SINGLETON)) { - rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1, ID2SYM(mid)); - } - else { - rb_funcall(klass, added, 1, ID2SYM(mid)); - } + CALL_METHOD_HOOK(klass, added, mid); } } @@ -398,12 +404,7 @@ remove_method(VALUE klass, ID mid) rb_clear_cache_for_undef(klass, mid); rb_free_method_entry(me); - if (FL_TEST(klass, FL_SINGLETON)) { - rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1, ID2SYM(mid)); - } - else { - rb_funcall(klass, removed, 1, ID2SYM(mid)); - } + CALL_METHOD_HOOK(klass, removed, mid); } void @@ -566,7 +567,7 @@ rb_undef(VALUE klass, ID id) VALUE c = klass; if (FL_TEST(c, FL_SINGLETON)) { - VALUE obj = rb_iv_get(klass, "__attached__"); + VALUE obj = rb_ivar_get(klass, attached); switch (TYPE(obj)) { case T_MODULE: @@ -584,12 +585,7 @@ rb_undef(VALUE klass, ID id) rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, NOEX_PUBLIC); - if (FL_TEST(klass, FL_SINGLETON)) { - rb_funcall(rb_iv_get(klass, "__attached__"), singleton_undefined, 1, ID2SYM(id)); - } - else { - rb_funcall(klass, undefined, 1, ID2SYM(id)); - } + CALL_METHOD_HOOK(klass, undefined, id); } /* @@ -1201,5 +1197,6 @@ Init_eval_method(void) singleton_removed = rb_intern("singleton_method_removed"); undefined = rb_intern("method_undefined"); singleton_undefined = rb_intern("singleton_method_undefined"); + attached = rb_intern("__attached__"); }