Project

General

Profile

Feature #6241 ยป 0001-inherited-flag.patch

nobu (Nobuyoshi Nakada), 04/01/2012 09:01 AM

View differences:

test/ruby/test_module.rb
def assert_method_defined?(klass, mid, message="")
message = build_message(message, "#{klass}\##{mid} expected to be defined.")
_wrap_assertion do
klass.method_defined?(mid) or
klass.method_defined?(*mid) or
raise Test::Unit::AssertionFailedError, message, caller(3)
end
end
......
def assert_method_not_defined?(klass, mid, message="")
message = build_message(message, "#{klass}\##{mid} expected to not be defined.")
_wrap_assertion do
klass.method_defined?(mid) and
klass.method_defined?(*mid) and
raise Test::Unit::AssertionFailedError, message, caller(3)
end
end
......
assert_method_not_defined?(User, :wombat)
assert_method_defined?(User, :user)
assert_method_defined?(User, :mixin)
assert_method_not_defined?(User, :wombat)
assert_method_defined?(User, :user)
assert_method_defined?(User, :mixin)
assert_method_not_defined?(User, [:wombat, false])
assert_method_defined?(User, [:user, false])
assert_method_not_defined?(User, [:mixin, false])
end
def module_exec_aux
......
assert_equal(false, c.private_method_defined?(:foo))
assert_equal(false, c.private_method_defined?(:bar))
assert_equal(true, c.private_method_defined?(:baz))
c = Class.new(c)
assert_equal(true, c.public_method_defined?(:foo))
assert_equal(false, c.public_method_defined?(:bar))
assert_equal(false, c.public_method_defined?(:baz))
assert_equal(false, c.protected_method_defined?(:foo))
assert_equal(true, c.protected_method_defined?(:bar))
assert_equal(false, c.protected_method_defined?(:baz))
assert_equal(false, c.private_method_defined?(:foo))
assert_equal(false, c.private_method_defined?(:bar))
assert_equal(true, c.private_method_defined?(:baz))
assert_equal(true, c.public_method_defined?(:foo, true))
assert_equal(false, c.public_method_defined?(:bar, true))
assert_equal(false, c.public_method_defined?(:baz, true))
assert_equal(false, c.protected_method_defined?(:foo, true))
assert_equal(true, c.protected_method_defined?(:bar, true))
assert_equal(false, c.protected_method_defined?(:baz, true))
assert_equal(false, c.private_method_defined?(:foo, true))
assert_equal(false, c.private_method_defined?(:bar, true))
assert_equal(true, c.private_method_defined?(:baz, true))
assert_equal(false, c.public_method_defined?(:foo, false))
assert_equal(false, c.public_method_defined?(:bar, false))
assert_equal(false, c.public_method_defined?(:baz, false))
assert_equal(false, c.protected_method_defined?(:foo, false))
assert_equal(false, c.protected_method_defined?(:bar, false))
assert_equal(false, c.protected_method_defined?(:baz, false))
assert_equal(false, c.private_method_defined?(:foo, false))
assert_equal(false, c.private_method_defined?(:bar, false))
assert_equal(false, c.private_method_defined?(:baz, false))
end
def test_change_visibility_under_safe4
vm_method.c
return rb_method_entry_get_without_cache(klass, id);
}
rb_method_entry_t *
rb_method_entry_at(VALUE klass, ID id)
{
st_data_t body;
if (!klass || !st_lookup(RCLASS_M_TBL(klass), id, &body)) {
return 0;
}
return (rb_method_entry_t *)body;
}
static void
remove_method(VALUE klass, ID mid)
{
......
}
}
int
rb_method_boundp(VALUE klass, ID id, int ex)
static int
method_accessible_p(rb_method_entry_t *me, int ex)
{
rb_method_entry_t *me = rb_method_entry(klass, id);
if (me != 0) {
if ((ex & ~NOEX_RESPONDS) &&
((me->flag & NOEX_PRIVATE) ||
......
return 0;
}
int
rb_method_bound_at(VALUE klass, ID id, int ex)
{
return method_accessible_p(rb_method_entry_at(klass, id), ex);
}
int
rb_method_boundp(VALUE klass, ID id, int ex)
{
return method_accessible_p(rb_method_entry(klass, id), ex);
}
static int
method_boundp(VALUE klass, ID id, int ex, int inherited)
{
if (inherited)
return rb_method_boundp(klass, id, ex);
else
return rb_method_bound_at(klass, id, ex);
}
void
rb_attr(VALUE klass, ID id, int read, int write, int ex)
{
......
*/
static VALUE
rb_mod_method_defined(VALUE mod, VALUE mid)
rb_mod_method_defined(int argc, VALUE *argv, VALUE mod)
{
ID id = rb_check_id(&mid);
if (!id || !rb_method_boundp(mod, id, 1)) {
VALUE mid;
ID id;
rb_check_arity(argc, 1, 2);
mid = argv[0];
id = rb_check_id(&mid);
if (!id || !method_boundp(mod, id, 1, argc == 1 || RTEST(argv[1]))) {
return Qfalse;
}
return Qtrue;
......
#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))
static VALUE
check_definition(VALUE mod, ID mid, rb_method_flag_t noex)
check_definition(VALUE mod, int argc, VALUE *argv, rb_method_flag_t noex)
{
const rb_method_entry_t *me;
me = rb_method_entry(mod, mid);
VALUE mid;
ID id;
rb_check_arity(argc, 1, 2);
mid = argv[0];
id = rb_check_id(&mid);
if (!id) return Qfalse;
if (argc == 1 || RTEST(argv[1])) {
me = rb_method_entry(mod, id);
}
else {
me = rb_method_entry_at(mod, id);
}
if (me) {
if (VISI_CHECK(me->flag, noex))
return Qtrue;
......
*/
static VALUE
rb_mod_public_method_defined(VALUE mod, VALUE mid)
rb_mod_public_method_defined(int argc, VALUE *argv, VALUE mod)
{
ID id = rb_check_id(&mid);
if (!id) return Qfalse;
return check_definition(mod, id, NOEX_PUBLIC);
return check_definition(mod, argc, argv, NOEX_PUBLIC);
}
/*
......
*/
static VALUE
rb_mod_private_method_defined(VALUE mod, VALUE mid)
rb_mod_private_method_defined(int argc, VALUE *argv, VALUE mod)
{
ID id = rb_check_id(&mid);
if (!id) return Qfalse;
return check_definition(mod, id, NOEX_PRIVATE);
return check_definition(mod, argc, argv, NOEX_PRIVATE);
}
/*
......
*/
static VALUE
rb_mod_protected_method_defined(VALUE mod, VALUE mid)
rb_mod_protected_method_defined(int argc, VALUE *argv, VALUE mod)
{
ID id = rb_check_id(&mid);
if (!id) return Qfalse;
return check_definition(mod, id, NOEX_PROTECTED);
return check_definition(mod, argc, argv, NOEX_PROTECTED);
}
int
......
rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1);
rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, 1);
rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, 1);
rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, 1);
rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, -1);
rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, -1);
rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, -1);
rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, -1);
rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
    (1-1/1)