From ed1881510950af3d8599c1a7dca14d53c35eb348 Mon Sep 17 00:00:00 2001 From: matz Date: Wed, 30 Oct 2002 08:04:32 +0000 Subject: * eval.c (rb_mod_public_method_defined, etc.): new methods: public_method_defined?, private_method_defined?, protected_method_defined? * object.c (rb_obj_public_methods): new method Object#public_methods. * class.c (ins_methods_i): Object#methods should list both public and protected methods. * class.c (rb_class_public_instance_methods): new method Module#public_instance_methods. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3015 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 15 +++++++++++++++ class.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- eval.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- intern.h | 1 + node.h | 1 + object.c | 13 ++++++++++++- 6 files changed, 124 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9d74526030..6bf80f8e0d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +Wed Oct 30 17:00:47 2002 Yukihiro Matsumoto + + * eval.c (rb_mod_public_method_defined, etc.): new methods: + public_method_defined?, private_method_defined?, + protected_method_defined? + + * object.c (rb_obj_public_methods): new method + Object#public_methods. + + * class.c (ins_methods_i): Object#methods should list both public + and protected methods. + + * class.c (rb_class_public_instance_methods): new method + Module#public_instance_methods. + Wed Oct 30 06:29:00 2002 Akinori MUSHA * eval.c, file.c, gc.c, io.c, object.c, ruby.c, ruby.h, struct.c, diff --git a/class.c b/class.c index fb3ea6ac6d..edd17e1250 100644 --- a/class.c +++ b/class.c @@ -456,23 +456,26 @@ rb_mod_ancestors(mod) return ary; } +#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f)) + static int ins_methods_i(key, body, ary) ID key; NODE *body; VALUE ary; { - if ((body->nd_noex&(NOEX_PRIVATE|NOEX_PROTECTED)) == 0) { + if (!body->nd_body) { + rb_ary_push(ary, Qnil); + rb_ary_push(ary, rb_str_new2(rb_id2name(key))); + } + else if (!VISI_CHECK(body->nd_noex, NOEX_PRIVATE)) { VALUE name = rb_str_new2(rb_id2name(key)); if (!rb_ary_includes(ary, name)) { - if (!body->nd_body) { - rb_ary_push(ary, Qnil); - } rb_ary_push(ary, name); } } - else if (body->nd_body && nd_type(body->nd_body) == NODE_ZSUPER) { + else if (nd_type(body->nd_body) == NODE_ZSUPER) { rb_ary_push(ary, Qnil); rb_ary_push(ary, rb_str_new2(rb_id2name(key))); } @@ -489,7 +492,7 @@ ins_methods_prot_i(key, body, ary) rb_ary_push(ary, Qnil); rb_ary_push(ary, rb_str_new2(rb_id2name(key))); } - else if (body->nd_noex & NOEX_PROTECTED) { + else if (VISI_CHECK(body->nd_noex, NOEX_PROTECTED)) { VALUE name = rb_str_new2(rb_id2name(key)); if (!rb_ary_includes(ary, name)) { @@ -513,7 +516,31 @@ ins_methods_priv_i(key, body, ary) rb_ary_push(ary, Qnil); rb_ary_push(ary, rb_str_new2(rb_id2name(key))); } - else if (body->nd_noex & NOEX_PRIVATE) { + else if (VISI_CHECK(body->nd_noex, NOEX_PRIVATE)) { + VALUE name = rb_str_new2(rb_id2name(key)); + + if (!rb_ary_includes(ary, name)) { + rb_ary_push(ary, name); + } + } + else if (nd_type(body->nd_body) == NODE_ZSUPER) { + rb_ary_push(ary, Qnil); + rb_ary_push(ary, rb_str_new2(rb_id2name(key))); + } + return ST_CONTINUE; +} + +static int +ins_methods_pub_i(key, body, ary) + ID key; + NODE *body; + VALUE ary; +{ + if (!body->nd_body) { + rb_ary_push(ary, Qnil); + rb_ary_push(ary, rb_str_new2(rb_id2name(key))); + } + else if (VISI_CHECK(body->nd_noex, NOEX_PUBLIC)) { VALUE name = rb_str_new2(rb_id2name(key)); if (!rb_ary_includes(ary, name)) { @@ -590,6 +617,18 @@ rb_class_private_instance_methods(argc, argv, mod) return method_list(mod, RTEST(option), ins_methods_priv_i); } +VALUE +rb_class_public_instance_methods(argc, argv, mod) + int argc; + VALUE *argv; + VALUE mod; +{ + VALUE option; + + rb_scan_args(argc, argv, "01", &option); + return method_list(mod, RTEST(option), ins_methods_pub_i); +} + VALUE rb_obj_singleton_methods(argc, argv, obj) int argc; diff --git a/eval.c b/eval.c index 8a4646ec40..c39ece1804 100644 --- a/eval.c +++ b/eval.c @@ -3510,8 +3510,52 @@ static VALUE rb_mod_method_defined(mod, mid) VALUE mod, mid; { - if (rb_method_boundp(mod, rb_to_id(mid), 1)) { - return Qtrue; + return rb_method_boundp(mod, rb_to_id(mid), 1); +} + +#define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f)) + +static VALUE +rb_mod_public_method_defined(mod, mid) + VALUE mod, mid; +{ + VALUE klass; + ID id = rb_to_id(mid); + int noex; + + if (rb_get_method_body(&mod, &id, &noex)) { + if (VISI_CHECK(noex, NOEX_PUBLIC)) + return Qtrue; + } + return Qfalse; +} + +static VALUE +rb_mod_private_method_defined(mod, mid) + VALUE mod, mid; +{ + VALUE klass; + ID id = rb_to_id(mid); + int noex; + + if (rb_get_method_body(&mod, &id, &noex)) { + if (VISI_CHECK(noex, NOEX_PRIVATE)) + return Qtrue; + } + return Qfalse; +} + +static VALUE +rb_mod_protected_method_defined(mod, mid) + VALUE mod, mid; +{ + VALUE klass; + ID id = rb_to_id(mid); + int noex; + + if (rb_get_method_body(&mod, &id, &noex)) { + if (VISI_CHECK(noex, NOEX_PROTECTED)) + return Qtrue; } return Qfalse; } @@ -6152,6 +6196,9 @@ Init_eval() 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, "public_class_method", rb_mod_public_method, -1); rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1); rb_define_method(rb_cModule, "module_eval", rb_mod_module_eval, -1); diff --git a/intern.h b/intern.h index 4244a33b71..3fcb60c43b 100644 --- a/intern.h +++ b/intern.h @@ -106,6 +106,7 @@ VALUE rb_mod_included_modules _((VALUE)); VALUE rb_mod_include_p _((VALUE, VALUE)); VALUE rb_mod_ancestors _((VALUE)); VALUE rb_class_instance_methods _((int, VALUE*, VALUE)); +VALUE rb_class_public_instance_methods _((int, VALUE*, VALUE)); VALUE rb_class_protected_instance_methods _((int, VALUE*, VALUE)); VALUE rb_class_private_instance_methods _((int, VALUE*, VALUE)); VALUE rb_obj_singleton_methods _((int, VALUE*, VALUE)); diff --git a/node.h b/node.h index 75c3615e96..ec27c86012 100644 --- a/node.h +++ b/node.h @@ -338,6 +338,7 @@ typedef struct RNode { #define NOEX_CFUNC 1 #define NOEX_PRIVATE 2 #define NOEX_PROTECTED 4 +#define NOEX_MASK 6 NODE *rb_compile_cstr _((const char*, const char*, int, int)); NODE *rb_compile_string _((const char*, VALUE, int)); diff --git a/object.c b/object.c index e826cb9f63..27e6987b83 100644 --- a/object.c +++ b/object.c @@ -910,6 +910,16 @@ rb_obj_private_methods(obj) return rb_class_private_instance_methods(1, argv, CLASS_OF(obj)); } +static VALUE +rb_obj_public_methods(obj) + VALUE obj; +{ + VALUE argv[1]; + + argv[0] = Qtrue; + return rb_class_public_instance_methods(1, argv, CLASS_OF(obj)); +} + static VALUE convert_type(val, tname, method, raise) VALUE val; @@ -1329,6 +1339,7 @@ Init_Object() rb_define_method(rb_mKernel, "singleton_methods", rb_obj_singleton_methods, -1); rb_define_method(rb_mKernel, "protected_methods", rb_obj_protected_methods, 0); rb_define_method(rb_mKernel, "private_methods", rb_obj_private_methods, 0); + rb_define_method(rb_mKernel, "public_methods", rb_obj_public_methods, 0); rb_define_method(rb_mKernel, "instance_variables", rb_obj_instance_variables, 0); rb_define_private_method(rb_mKernel, "remove_instance_variable", rb_obj_remove_instance_variable, 1); @@ -1400,7 +1411,7 @@ Init_Object() rb_define_singleton_method(rb_cModule, "allocate", rb_module_s_alloc, 0); rb_define_method(rb_cModule, "initialize", rb_mod_initialize, 0); rb_define_method(rb_cModule, "instance_methods", rb_class_instance_methods, -1); - rb_define_method(rb_cModule, "public_instance_methods", rb_class_instance_methods, -1); + rb_define_method(rb_cModule, "public_instance_methods", rb_class_public_instance_methods, -1); rb_define_method(rb_cModule, "protected_instance_methods", rb_class_protected_instance_methods, -1); rb_define_method(rb_cModule, "private_instance_methods", rb_class_private_instance_methods, -1); -- cgit v1.2.3