add rdoc.
[ruby-svn.git] / vm_method.c
blobd3f38d794577f15779628c07f674535329bbf757
1 /* -*-c-*- */
2 /*
3 * This file is included by vm_eval.c
4 */
6 #define CACHE_SIZE 0x800
7 #define CACHE_MASK 0x7ff
8 #define EXPR1(c,m) ((((c)>>3)^(m))&CACHE_MASK)
10 static void rb_vm_check_redefinition_opt_method(const NODE *node);
12 static ID __send__, object_id;
13 static ID removed, singleton_removed, undefined, singleton_undefined;
14 static ID eqq, each, aref, aset, match, missing;
15 static ID added, singleton_added;
17 struct cache_entry { /* method hash table. */
18 ID mid; /* method's id */
19 ID mid0; /* method's original id */
20 VALUE klass; /* receiver's class */
21 VALUE oklass; /* original's class */
22 NODE *method;
25 static struct cache_entry cache[CACHE_SIZE];
26 #define ruby_running (GET_VM()->running)
27 /* int ruby_running = 0; */
29 void
30 rb_clear_cache(void)
32 struct cache_entry *ent, *end;
34 rb_vm_change_state();
36 if (!ruby_running)
37 return;
38 ent = cache;
39 end = ent + CACHE_SIZE;
40 while (ent < end) {
41 ent->mid = 0;
42 ent++;
46 static void
47 rb_clear_cache_for_undef(VALUE klass, ID id)
49 struct cache_entry *ent, *end;
51 rb_vm_change_state();
53 if (!ruby_running)
54 return;
55 ent = cache;
56 end = ent + CACHE_SIZE;
57 while (ent < end) {
58 if (ent->oklass == klass && ent->mid == id) {
59 ent->mid = 0;
61 ent++;
65 static void
66 rb_clear_cache_by_id(ID id)
68 struct cache_entry *ent, *end;
70 rb_vm_change_state();
72 if (!ruby_running)
73 return;
74 ent = cache;
75 end = ent + CACHE_SIZE;
76 while (ent < end) {
77 if (ent->mid == id) {
78 ent->mid = 0;
80 ent++;
84 void
85 rb_clear_cache_by_class(VALUE klass)
87 struct cache_entry *ent, *end;
89 rb_vm_change_state();
91 if (!ruby_running)
92 return;
93 ent = cache;
94 end = ent + CACHE_SIZE;
95 while (ent < end) {
96 if (ent->klass == klass || ent->oklass == klass) {
97 ent->mid = 0;
99 ent++;
103 void
104 rb_add_method(VALUE klass, ID mid, NODE * node, int noex)
106 NODE *body;
108 if (NIL_P(klass)) {
109 klass = rb_cObject;
111 if (rb_safe_level() >= 4 &&
112 (klass == rb_cObject || !OBJ_UNTRUSTED(klass))) {
113 rb_raise(rb_eSecurityError, "Insecure: can't define method");
115 if (!FL_TEST(klass, FL_SINGLETON) &&
116 node && nd_type(node) != NODE_ZSUPER &&
117 (mid == rb_intern("initialize") || mid == rb_intern("initialize_copy"))) {
118 noex = NOEX_PRIVATE | noex;
120 else if (FL_TEST(klass, FL_SINGLETON) && node
121 && nd_type(node) == NODE_CFUNC && mid == rb_intern("allocate")) {
122 rb_warn
123 ("defining %s.allocate is deprecated; use rb_define_alloc_func()",
124 rb_class2name(rb_iv_get(klass, "__attached__")));
125 mid = ID_ALLOCATOR;
127 if (OBJ_FROZEN(klass)) {
128 rb_error_frozen("class/module");
130 rb_clear_cache_by_id(mid);
133 * NODE_METHOD (NEW_METHOD(body, klass, vis)):
134 * nd_body : method body // (2) // mark
135 * nd_clss : klass // (1) // mark
136 * nd_noex : visibility // (3)
138 * NODE_FBODY (NEW_FBODY(method, alias)):
139 * nd_body : method (NODE_METHOD) // (2) // mark
140 * nd_oid : original id // (1)
141 * nd_cnt : alias count // (3)
143 if (node) {
144 body = NEW_FBODY(NEW_METHOD(node, klass, NOEX_WITH_SAFE(noex)), 0);
146 else {
147 body = 0;
151 /* check re-definition */
152 st_data_t data;
153 NODE *old_node;
155 if (st_lookup(RCLASS_M_TBL(klass), mid, &data)) {
156 old_node = (NODE *)data;
157 if (old_node) {
158 if (nd_type(old_node->nd_body->nd_body) == NODE_CFUNC) {
159 rb_vm_check_redefinition_opt_method(old_node);
161 if (RTEST(ruby_verbose) && node && old_node->nd_cnt == 0 && old_node->nd_body) {
162 rb_warning("method redefined; discarding old %s", rb_id2name(mid));
166 if (klass == rb_cObject && node && mid == idInitialize) {
167 rb_warn("redefining Object#initialize may cause infinite loop");
170 if (mid == object_id || mid == __send__) {
171 if (node && nd_type(node) == RUBY_VM_METHOD_NODE) {
172 rb_warn("redefining `%s' may cause serious problem",
173 rb_id2name(mid));
178 st_insert(RCLASS_M_TBL(klass), mid, (st_data_t) body);
180 if (node && mid != ID_ALLOCATOR && ruby_running) {
181 if (FL_TEST(klass, FL_SINGLETON)) {
182 rb_funcall(rb_iv_get(klass, "__attached__"), singleton_added, 1,
183 ID2SYM(mid));
185 else {
186 rb_funcall(klass, added, 1, ID2SYM(mid));
191 void
192 rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE))
194 Check_Type(klass, T_CLASS);
195 rb_add_method(rb_singleton_class(klass), ID_ALLOCATOR, NEW_CFUNC(func, 0),
196 NOEX_PRIVATE);
199 void
200 rb_undef_alloc_func(VALUE klass)
202 Check_Type(klass, T_CLASS);
203 rb_add_method(rb_singleton_class(klass), ID_ALLOCATOR, 0, NOEX_UNDEF);
206 rb_alloc_func_t
207 rb_get_alloc_func(VALUE klass)
209 NODE *n;
210 Check_Type(klass, T_CLASS);
211 n = rb_method_node(CLASS_OF(klass), ID_ALLOCATOR);
212 if (!n) return 0;
213 if (nd_type(n) != NODE_METHOD) return 0;
214 n = n->nd_body;
215 if (nd_type(n) != NODE_CFUNC) return 0;
216 return (rb_alloc_func_t)n->nd_cfnc;
219 static NODE *
220 search_method(VALUE klass, ID id, VALUE *klassp)
222 st_data_t body;
224 if (!klass) {
225 return 0;
228 while (!st_lookup(RCLASS_M_TBL(klass), id, &body)) {
229 klass = RCLASS_SUPER(klass);
230 if (!klass)
231 return 0;
234 if (klassp) {
235 *klassp = klass;
238 return (NODE *)body;
242 * search method body (NODE_METHOD)
243 * with : klass and id
244 * without : method cache
246 * if you need method node with method cache, use
247 * rb_method_node()
249 NODE *
250 rb_get_method_body(VALUE klass, ID id, ID *idp)
252 NODE *volatile fbody, *body;
253 NODE *method;
255 if ((fbody = search_method(klass, id, 0)) == 0 || !fbody->nd_body) {
256 /* store empty info in cache */
257 struct cache_entry *ent;
258 ent = cache + EXPR1(klass, id);
259 ent->klass = klass;
260 ent->mid = ent->mid0 = id;
261 ent->method = 0;
262 ent->oklass = 0;
263 return 0;
266 method = fbody->nd_body;
268 if (ruby_running) {
269 /* store in cache */
270 struct cache_entry *ent;
271 ent = cache + EXPR1(klass, id);
272 ent->klass = klass;
273 ent->mid = id;
274 ent->mid0 = fbody->nd_oid;
275 ent->method = body = method;
276 ent->oklass = method->nd_clss;
278 else {
279 body = method;
282 if (idp) {
283 *idp = fbody->nd_oid;
286 return body;
289 NODE *
290 rb_method_node(VALUE klass, ID id)
292 struct cache_entry *ent;
294 ent = cache + EXPR1(klass, id);
295 if (ent->mid == id && ent->klass == klass && ent->method) {
296 return ent->method;
299 return rb_get_method_body(klass, id, 0);
302 static void
303 remove_method(VALUE klass, ID mid)
305 st_data_t data;
306 NODE *body = 0;
308 if (klass == rb_cObject) {
309 rb_secure(4);
311 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(klass)) {
312 rb_raise(rb_eSecurityError, "Insecure: can't remove method");
314 if (OBJ_FROZEN(klass))
315 rb_error_frozen("class/module");
316 if (mid == object_id || mid == __send__ || mid == idInitialize) {
317 rb_warn("removing `%s' may cause serious problem", rb_id2name(mid));
319 if (st_lookup(RCLASS_M_TBL(klass), mid, &data)) {
320 body = (NODE *)data;
321 if (!body || !body->nd_body) body = 0;
322 else {
323 st_delete(RCLASS_M_TBL(klass), &mid, &data);
326 if (!body) {
327 rb_name_error(mid, "method `%s' not defined in %s",
328 rb_id2name(mid), rb_class2name(klass));
331 if (nd_type(body->nd_body->nd_body) == NODE_CFUNC) {
332 rb_vm_check_redefinition_opt_method(body);
335 rb_clear_cache_for_undef(klass, mid);
336 if (FL_TEST(klass, FL_SINGLETON)) {
337 rb_funcall(rb_iv_get(klass, "__attached__"), singleton_removed, 1,
338 ID2SYM(mid));
340 else {
341 rb_funcall(klass, removed, 1, ID2SYM(mid));
345 void
346 rb_remove_method(VALUE klass, const char *name)
348 remove_method(klass, rb_intern(name));
352 * call-seq:
353 * remove_method(symbol) => self
355 * Removes the method identified by _symbol_ from the current
356 * class. For an example, see <code>Module.undef_method</code>.
359 static VALUE
360 rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
362 int i;
364 for (i = 0; i < argc; i++) {
365 remove_method(mod, rb_to_id(argv[i]));
367 return mod;
370 #undef rb_disable_super
371 #undef rb_enable_super
373 void
374 rb_disable_super(VALUE klass, const char *name)
376 /* obsolete - no use */
379 void
380 rb_enable_super(VALUE klass, const char *name)
382 rb_warning("rb_enable_super() is obsolete");
385 static void
386 rb_export_method(VALUE klass, ID name, ID noex)
388 NODE *fbody;
389 VALUE origin;
391 if (klass == rb_cObject) {
392 rb_secure(4);
394 fbody = search_method(klass, name, &origin);
395 if (!fbody && TYPE(klass) == T_MODULE) {
396 fbody = search_method(rb_cObject, name, &origin);
398 if (!fbody || !fbody->nd_body) {
399 rb_print_undef(klass, name, 0);
401 if (fbody->nd_body->nd_noex != noex) {
402 if (nd_type(fbody->nd_body->nd_body) == NODE_CFUNC) {
403 rb_vm_check_redefinition_opt_method(fbody);
405 if (klass == origin) {
406 fbody->nd_body->nd_noex = noex;
408 else {
409 rb_add_method(klass, name, NEW_ZSUPER(), noex);
415 rb_method_boundp(VALUE klass, ID id, int ex)
417 NODE *method;
419 if ((method = rb_method_node(klass, id)) != 0) {
420 if (ex && (method->nd_noex & NOEX_PRIVATE)) {
421 return Qfalse;
423 return Qtrue;
425 return Qfalse;
428 void
429 rb_attr(VALUE klass, ID id, int read, int write, int ex)
431 const char *name;
432 ID attriv;
433 int noex;
435 if (!ex) {
436 noex = NOEX_PUBLIC;
438 else {
439 if (SCOPE_TEST(NOEX_PRIVATE)) {
440 noex = NOEX_PRIVATE;
441 rb_warning((SCOPE_CHECK(NOEX_MODFUNC)) ?
442 "attribute accessor as module_function" :
443 "private attribute?");
445 else if (SCOPE_TEST(NOEX_PROTECTED)) {
446 noex = NOEX_PROTECTED;
448 else {
449 noex = NOEX_PUBLIC;
453 if (!rb_is_local_id(id) && !rb_is_const_id(id)) {
454 rb_name_error(id, "invalid attribute name `%s'", rb_id2name(id));
456 name = rb_id2name(id);
457 if (!name) {
458 rb_raise(rb_eArgError, "argument needs to be symbol or string");
460 attriv = rb_intern_str(rb_sprintf("@%s", name));
461 if (read) {
462 rb_add_method(klass, id, NEW_IVAR(attriv), noex);
464 if (write) {
465 rb_add_method(klass, rb_id_attrset(id), NEW_ATTRSET(attriv), noex);
469 void
470 rb_undef(VALUE klass, ID id)
472 VALUE origin;
473 NODE *body;
475 if (rb_vm_cbase() == rb_cObject && klass == rb_cObject) {
476 rb_secure(4);
478 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(klass)) {
479 rb_raise(rb_eSecurityError, "Insecure: can't undef `%s'",
480 rb_id2name(id));
482 rb_frozen_class_p(klass);
483 if (id == object_id || id == __send__ || id == idInitialize) {
484 rb_warn("undefining `%s' may cause serious problem", rb_id2name(id));
486 body = search_method(klass, id, &origin);
487 if (!body || !body->nd_body) {
488 const char *s0 = " class";
489 VALUE c = klass;
491 if (FL_TEST(c, FL_SINGLETON)) {
492 VALUE obj = rb_iv_get(klass, "__attached__");
494 switch (TYPE(obj)) {
495 case T_MODULE:
496 case T_CLASS:
497 c = obj;
498 s0 = "";
501 else if (TYPE(c) == T_MODULE) {
502 s0 = " module";
504 rb_name_error(id, "undefined method `%s' for%s `%s'",
505 rb_id2name(id), s0, rb_class2name(c));
508 rb_add_method(klass, id, 0, NOEX_PUBLIC);
510 if (FL_TEST(klass, FL_SINGLETON)) {
511 rb_funcall(rb_iv_get(klass, "__attached__"),
512 singleton_undefined, 1, ID2SYM(id));
514 else {
515 rb_funcall(klass, undefined, 1, ID2SYM(id));
520 * call-seq:
521 * undef_method(symbol) => self
523 * Prevents the current class from responding to calls to the named
524 * method. Contrast this with <code>remove_method</code>, which deletes
525 * the method from the particular class; Ruby will still search
526 * superclasses and mixed-in modules for a possible receiver.
528 * class Parent
529 * def hello
530 * puts "In parent"
531 * end
532 * end
533 * class Child < Parent
534 * def hello
535 * puts "In child"
536 * end
537 * end
540 * c = Child.new
541 * c.hello
544 * class Child
545 * remove_method :hello # remove from child, still in parent
546 * end
547 * c.hello
550 * class Child
551 * undef_method :hello # prevent any calls to 'hello'
552 * end
553 * c.hello
555 * <em>produces:</em>
557 * In child
558 * In parent
559 * prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
562 static VALUE
563 rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
565 int i;
566 for (i = 0; i < argc; i++) {
567 rb_undef(mod, rb_to_id(argv[i]));
569 return mod;
573 * call-seq:
574 * mod.method_defined?(symbol) => true or false
576 * Returns +true+ if the named method is defined by
577 * _mod_ (or its included modules and, if _mod_ is a class,
578 * its ancestors). Public and protected methods are matched.
580 * module A
581 * def method1() end
582 * end
583 * class B
584 * def method2() end
585 * end
586 * class C < B
587 * include A
588 * def method3() end
589 * end
591 * A.method_defined? :method1 #=> true
592 * C.method_defined? "method1" #=> true
593 * C.method_defined? "method2" #=> true
594 * C.method_defined? "method3" #=> true
595 * C.method_defined? "method4" #=> false
598 static VALUE
599 rb_mod_method_defined(VALUE mod, VALUE mid)
601 return rb_method_boundp(mod, rb_to_id(mid), 1);
604 #define VISI_CHECK(x,f) (((x)&NOEX_MASK) == (f))
607 * call-seq:
608 * mod.public_method_defined?(symbol) => true or false
610 * Returns +true+ if the named public method is defined by
611 * _mod_ (or its included modules and, if _mod_ is a class,
612 * its ancestors).
614 * module A
615 * def method1() end
616 * end
617 * class B
618 * protected
619 * def method2() end
620 * end
621 * class C < B
622 * include A
623 * def method3() end
624 * end
626 * A.method_defined? :method1 #=> true
627 * C.public_method_defined? "method1" #=> true
628 * C.public_method_defined? "method2" #=> false
629 * C.method_defined? "method2" #=> true
632 static VALUE
633 rb_mod_public_method_defined(VALUE mod, VALUE mid)
635 ID id = rb_to_id(mid);
636 NODE *method;
638 method = rb_method_node(mod, id);
639 if (method) {
640 if (VISI_CHECK(method->nd_noex, NOEX_PUBLIC))
641 return Qtrue;
643 return Qfalse;
647 * call-seq:
648 * mod.private_method_defined?(symbol) => true or false
650 * Returns +true+ if the named private method is defined by
651 * _ mod_ (or its included modules and, if _mod_ is a class,
652 * its ancestors).
654 * module A
655 * def method1() end
656 * end
657 * class B
658 * private
659 * def method2() end
660 * end
661 * class C < B
662 * include A
663 * def method3() end
664 * end
666 * A.method_defined? :method1 #=> true
667 * C.private_method_defined? "method1" #=> false
668 * C.private_method_defined? "method2" #=> true
669 * C.method_defined? "method2" #=> false
672 static VALUE
673 rb_mod_private_method_defined(VALUE mod, VALUE mid)
675 ID id = rb_to_id(mid);
676 NODE *method;
678 method = rb_method_node(mod, id);
679 if (method) {
680 if (VISI_CHECK(method->nd_noex, NOEX_PRIVATE))
681 return Qtrue;
683 return Qfalse;
687 * call-seq:
688 * mod.protected_method_defined?(symbol) => true or false
690 * Returns +true+ if the named protected method is defined
691 * by _mod_ (or its included modules and, if _mod_ is a
692 * class, its ancestors).
694 * module A
695 * def method1() end
696 * end
697 * class B
698 * protected
699 * def method2() end
700 * end
701 * class C < B
702 * include A
703 * def method3() end
704 * end
706 * A.method_defined? :method1 #=> true
707 * C.protected_method_defined? "method1" #=> false
708 * C.protected_method_defined? "method2" #=> true
709 * C.method_defined? "method2" #=> true
712 static VALUE
713 rb_mod_protected_method_defined(VALUE mod, VALUE mid)
715 ID id = rb_to_id(mid);
716 NODE *method;
718 method = rb_method_node(mod, id);
719 if (method) {
720 if (VISI_CHECK(method->nd_noex, NOEX_PROTECTED))
721 return Qtrue;
723 return Qfalse;
726 void
727 rb_alias(VALUE klass, ID name, ID def)
729 NODE *orig_fbody, *node;
730 VALUE singleton = 0;
731 st_data_t data;
733 rb_frozen_class_p(klass);
734 if (klass == rb_cObject) {
735 rb_secure(4);
737 orig_fbody = search_method(klass, def, 0);
738 if (!orig_fbody || !orig_fbody->nd_body) {
739 if (TYPE(klass) == T_MODULE) {
740 orig_fbody = search_method(rb_cObject, def, 0);
743 if (!orig_fbody || !orig_fbody->nd_body) {
744 rb_print_undef(klass, def, 0);
746 if (FL_TEST(klass, FL_SINGLETON)) {
747 singleton = rb_iv_get(klass, "__attached__");
750 orig_fbody->nd_cnt++;
752 if (st_lookup(RCLASS_M_TBL(klass), name, &data)) {
753 node = (NODE *)data;
754 if (node) {
755 if (RTEST(ruby_verbose) && node->nd_cnt == 0 && node->nd_body) {
756 rb_warning("discarding old %s", rb_id2name(name));
758 if (nd_type(node->nd_body->nd_body) == NODE_CFUNC) {
759 rb_vm_check_redefinition_opt_method(node);
764 st_insert(RCLASS_M_TBL(klass), name,
765 (st_data_t) NEW_FBODY(
766 NEW_METHOD(orig_fbody->nd_body->nd_body,
767 orig_fbody->nd_body->nd_clss,
768 NOEX_WITH_SAFE(orig_fbody->nd_body->nd_noex)), def));
770 rb_clear_cache_by_id(name);
772 if (!ruby_running) return;
774 if (singleton) {
775 rb_funcall(singleton, singleton_added, 1, ID2SYM(name));
777 else {
778 rb_funcall(klass, added, 1, ID2SYM(name));
783 * call-seq:
784 * alias_method(new_name, old_name) => self
786 * Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
787 * be used to retain access to methods that are overridden.
789 * module Mod
790 * alias_method :orig_exit, :exit
791 * def exit(code=0)
792 * puts "Exiting with code #{code}"
793 * orig_exit(code)
794 * end
795 * end
796 * include Mod
797 * exit(99)
799 * <em>produces:</em>
801 * Exiting with code 99
804 static VALUE
805 rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
807 rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));
808 return mod;
811 static void
812 secure_visibility(VALUE self)
814 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(self)) {
815 rb_raise(rb_eSecurityError,
816 "Insecure: can't change method visibility");
820 static void
821 set_method_visibility(VALUE self, int argc, VALUE *argv, ID ex)
823 int i;
824 secure_visibility(self);
825 for (i = 0; i < argc; i++) {
826 rb_export_method(self, rb_to_id(argv[i]), ex);
828 rb_clear_cache_by_class(self);
832 * call-seq:
833 * public => self
834 * public(symbol, ...) => self
836 * With no arguments, sets the default visibility for subsequently
837 * defined methods to public. With arguments, sets the named methods to
838 * have public visibility.
841 static VALUE
842 rb_mod_public(int argc, VALUE *argv, VALUE module)
844 secure_visibility(module);
845 if (argc == 0) {
846 SCOPE_SET(NOEX_PUBLIC);
848 else {
849 set_method_visibility(module, argc, argv, NOEX_PUBLIC);
851 return module;
855 * call-seq:
856 * protected => self
857 * protected(symbol, ...) => self
859 * With no arguments, sets the default visibility for subsequently
860 * defined methods to protected. With arguments, sets the named methods
861 * to have protected visibility.
864 static VALUE
865 rb_mod_protected(int argc, VALUE *argv, VALUE module)
867 secure_visibility(module);
868 if (argc == 0) {
869 SCOPE_SET(NOEX_PROTECTED);
871 else {
872 set_method_visibility(module, argc, argv, NOEX_PROTECTED);
874 return module;
878 * call-seq:
879 * private => self
880 * private(symbol, ...) => self
882 * With no arguments, sets the default visibility for subsequently
883 * defined methods to private. With arguments, sets the named methods
884 * to have private visibility.
886 * module Mod
887 * def a() end
888 * def b() end
889 * private
890 * def c() end
891 * private :a
892 * end
893 * Mod.private_instance_methods #=> [:a, :c]
896 static VALUE
897 rb_mod_private(int argc, VALUE *argv, VALUE module)
899 secure_visibility(module);
900 if (argc == 0) {
901 SCOPE_SET(NOEX_PRIVATE);
903 else {
904 set_method_visibility(module, argc, argv, NOEX_PRIVATE);
906 return module;
910 * call-seq:
911 * mod.public_class_method(symbol, ...) => mod
913 * Makes a list of existing class methods public.
916 static VALUE
917 rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
919 set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PUBLIC);
920 return obj;
924 * call-seq:
925 * mod.private_class_method(symbol, ...) => mod
927 * Makes existing class methods private. Often used to hide the default
928 * constructor <code>new</code>.
930 * class SimpleSingleton # Not thread safe
931 * private_class_method :new
932 * def SimpleSingleton.create(*args, &block)
933 * @me = new(*args, &block) if ! @me
934 * @me
935 * end
936 * end
939 static VALUE
940 rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
942 set_method_visibility(CLASS_OF(obj), argc, argv, NOEX_PRIVATE);
943 return obj;
947 * call-seq:
948 * public
949 * public(symbol, ...)
951 * With no arguments, sets the default visibility for subsequently
952 * defined methods to public. With arguments, sets the named methods to
953 * have public visibility.
956 static VALUE
957 top_public(int argc, VALUE *argv)
959 return rb_mod_public(argc, argv, rb_cObject);
962 static VALUE
963 top_private(int argc, VALUE *argv)
965 return rb_mod_private(argc, argv, rb_cObject);
969 * call-seq:
970 * module_function(symbol, ...) => self
972 * Creates module functions for the named methods. These functions may
973 * be called with the module as a receiver, and also become available
974 * as instance methods to classes that mix in the module. Module
975 * functions are copies of the original, and so may be changed
976 * independently. The instance-method versions are made private. If
977 * used with no arguments, subsequently defined methods become module
978 * functions.
980 * module Mod
981 * def one
982 * "This is one"
983 * end
984 * module_function :one
985 * end
986 * class Cls
987 * include Mod
988 * def callOne
989 * one
990 * end
991 * end
992 * Mod.one #=> "This is one"
993 * c = Cls.new
994 * c.callOne #=> "This is one"
995 * module Mod
996 * def one
997 * "This is the new one"
998 * end
999 * end
1000 * Mod.one #=> "This is one"
1001 * c.callOne #=> "This is the new one"
1004 static VALUE
1005 rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
1007 int i;
1008 ID id;
1009 NODE *fbody;
1011 if (TYPE(module) != T_MODULE) {
1012 rb_raise(rb_eTypeError, "module_function must be called for modules");
1015 secure_visibility(module);
1016 if (argc == 0) {
1017 SCOPE_SET(NOEX_MODFUNC);
1018 return module;
1021 set_method_visibility(module, argc, argv, NOEX_PRIVATE);
1023 for (i = 0; i < argc; i++) {
1024 VALUE m = module;
1026 id = rb_to_id(argv[i]);
1027 for (;;) {
1028 fbody = search_method(m, id, &m);
1029 if (fbody == 0) {
1030 fbody = search_method(rb_cObject, id, &m);
1032 if (fbody == 0 || fbody->nd_body == 0) {
1033 rb_bug("undefined method `%s'; can't happen", rb_id2name(id));
1035 if (nd_type(fbody->nd_body->nd_body) != NODE_ZSUPER) {
1036 break; /* normal case: need not to follow 'super' link */
1038 m = RCLASS_SUPER(m);
1039 if (!m)
1040 break;
1042 rb_add_method(rb_singleton_class(module), id, fbody->nd_body->nd_body,
1043 NOEX_PUBLIC);
1045 return module;
1049 rb_method_basic_definition_p(VALUE klass, ID id)
1051 NODE *node = rb_method_node(klass, id);
1052 if (node && (node->nd_noex & NOEX_BASIC))
1053 return 1;
1054 return 0;
1058 * call-seq:
1059 * obj.respond_to?(symbol, include_private=false) => true or false
1061 * Returns +true+> if _obj_ responds to the given
1062 * method. Private methods are included in the search only if the
1063 * optional second parameter evaluates to +true+.
1067 rb_obj_respond_to(VALUE obj, ID id, int priv)
1069 VALUE klass = CLASS_OF(obj);
1071 if (rb_method_basic_definition_p(klass, idRespond_to)) {
1072 return rb_method_boundp(klass, id, !priv);
1074 else {
1075 VALUE args[2];
1076 int n = 0;
1077 args[n++] = ID2SYM(id);
1078 if (priv)
1079 args[n++] = Qtrue;
1080 return RTEST(rb_funcall2(obj, idRespond_to, n, args));
1085 rb_respond_to(VALUE obj, ID id)
1087 return rb_obj_respond_to(obj, id, Qfalse);
1091 * call-seq:
1092 * obj.respond_to?(symbol, include_private=false) => true or false
1094 * Returns +true+> if _obj_ responds to the given
1095 * method. Private methods are included in the search only if the
1096 * optional second parameter evaluates to +true+.
1099 static VALUE
1100 obj_respond_to(int argc, VALUE *argv, VALUE obj)
1102 VALUE mid, priv;
1103 ID id;
1105 rb_scan_args(argc, argv, "11", &mid, &priv);
1106 id = rb_to_id(mid);
1107 if (rb_method_boundp(CLASS_OF(obj), id, !RTEST(priv))) {
1108 return Qtrue;
1110 return Qfalse;
1113 void
1114 Init_eval_method(void)
1116 #undef rb_intern
1118 rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
1120 rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
1121 rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
1122 rb_define_private_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
1123 rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
1124 rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
1125 rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
1126 rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
1128 rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, 1);
1129 rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, 1);
1130 rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, 1);
1131 rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, 1);
1132 rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
1133 rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);
1135 rb_define_singleton_method(rb_vm_top_self(), "public", top_public, -1);
1136 rb_define_singleton_method(rb_vm_top_self(), "private", top_private, -1);
1138 object_id = rb_intern("object_id");
1139 __send__ = rb_intern("__send__");
1140 eqq = rb_intern("===");
1141 each = rb_intern("each");
1142 aref = rb_intern("[]");
1143 aset = rb_intern("[]=");
1144 match = rb_intern("=~");
1145 missing = rb_intern("method_missing");
1146 added = rb_intern("method_added");
1147 singleton_added = rb_intern("singleton_method_added");
1148 removed = rb_intern("method_removed");
1149 singleton_removed = rb_intern("singleton_method_removed");
1150 undefined = rb_intern("method_undefined");
1151 singleton_undefined = rb_intern("singleton_method_undefined");