* test/ruby/envutil.rb (assert_normal_exit): show pid when fail.
[ruby-svn.git] / class.c
blob60bd2e30be63418cf5556c4225974b6cb75412ca
1 /**********************************************************************
3 class.c -
5 $Author$
6 created at: Tue Aug 10 15:05:44 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
13 #include "ruby/signal.h"
14 #include "ruby/node.h"
15 #include "ruby/st.h"
16 #include <ctype.h>
18 extern st_table *rb_class_tbl;
20 static VALUE
21 class_alloc(VALUE flags, VALUE klass)
23 rb_classext_t *ext = ALLOC(rb_classext_t);
24 NEWOBJ(obj, struct RClass);
25 OBJSETUP(obj, klass, flags);
26 obj->ptr = ext;
27 RCLASS_IV_TBL(obj) = 0;
28 RCLASS_M_TBL(obj) = 0;
29 RCLASS_SUPER(obj) = 0;
30 RCLASS_IV_INDEX_TBL(obj) = 0;
31 return (VALUE)obj;
34 VALUE
35 rb_class_boot(VALUE super)
37 VALUE klass = class_alloc(T_CLASS, rb_cClass);
39 RCLASS_SUPER(klass) = super;
40 RCLASS_M_TBL(klass) = st_init_numtable();
42 OBJ_INFECT(klass, super);
43 return (VALUE)klass;
46 void
47 rb_check_inheritable(VALUE super)
49 if (TYPE(super) != T_CLASS) {
50 rb_raise(rb_eTypeError, "superclass must be a Class (%s given)",
51 rb_obj_classname(super));
53 if (RBASIC(super)->flags & FL_SINGLETON) {
54 rb_raise(rb_eTypeError, "can't make subclass of singleton class");
58 VALUE
59 rb_class_new(VALUE super)
61 Check_Type(super, T_CLASS);
62 rb_check_inheritable(super);
63 if (super == rb_cClass) {
64 rb_raise(rb_eTypeError, "can't make subclass of Class");
66 return rb_class_boot(super);
69 struct clone_method_data {
70 st_table *tbl;
71 VALUE klass;
74 static int
75 clone_method(ID mid, NODE *body, struct clone_method_data *data)
77 if (body == 0) {
78 st_insert(data->tbl, mid, 0);
80 else {
81 st_insert(data->tbl, mid,
82 (st_data_t)
83 NEW_FBODY(
84 NEW_METHOD(body->nd_body->nd_body,
85 data->klass, /* TODO */
86 body->nd_body->nd_noex),
87 0));
89 return ST_CONTINUE;
92 /* :nodoc: */
93 VALUE
94 rb_mod_init_copy(VALUE clone, VALUE orig)
96 rb_obj_init_copy(clone, orig);
97 if (!FL_TEST(CLASS_OF(clone), FL_SINGLETON)) {
98 RBASIC(clone)->klass = rb_singleton_class_clone(orig);
100 RCLASS_SUPER(clone) = RCLASS_SUPER(orig);
101 if (RCLASS_IV_TBL(orig)) {
102 ID id;
104 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig));
105 id = rb_intern("__classpath__");
106 st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0);
107 id = rb_intern("__classid__");
108 st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0);
110 if (RCLASS_M_TBL(orig)) {
111 struct clone_method_data data;
112 data.tbl = RCLASS_M_TBL(clone) = st_init_numtable();
113 data.klass = clone;
114 st_foreach(RCLASS_M_TBL(orig), clone_method,
115 (st_data_t)&data);
118 return clone;
121 /* :nodoc: */
122 VALUE
123 rb_class_init_copy(VALUE clone, VALUE orig)
125 if (RCLASS_SUPER(clone) != 0) {
126 rb_raise(rb_eTypeError, "already initialized class");
128 if (FL_TEST(orig, FL_SINGLETON)) {
129 rb_raise(rb_eTypeError, "can't copy singleton class");
131 return rb_mod_init_copy(clone, orig);
134 VALUE
135 rb_singleton_class_clone(VALUE obj)
137 VALUE klass = RBASIC(obj)->klass;
139 if (!FL_TEST(klass, FL_SINGLETON))
140 return klass;
141 else {
142 struct clone_method_data data;
143 /* copy singleton(unnamed) class */
144 VALUE clone = class_alloc(RBASIC(klass)->flags, 0);
146 if (BUILTIN_TYPE(obj) == T_CLASS) {
147 RBASIC(clone)->klass = (VALUE)clone;
149 else {
150 RBASIC(clone)->klass = rb_singleton_class_clone(klass);
153 RCLASS_SUPER(clone) = RCLASS_SUPER(klass);
154 if (RCLASS_IV_TBL(klass)) {
155 RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass));
157 RCLASS_M_TBL(clone) = st_init_numtable();
158 data.tbl = RCLASS_M_TBL(clone);
159 data.klass = (VALUE)clone;
160 st_foreach(RCLASS_M_TBL(klass), clone_method,
161 (st_data_t)&data);
162 rb_singleton_class_attached(RBASIC(clone)->klass, (VALUE)clone);
163 FL_SET(clone, FL_SINGLETON);
164 return (VALUE)clone;
168 void
169 rb_singleton_class_attached(VALUE klass, VALUE obj)
171 if (FL_TEST(klass, FL_SINGLETON)) {
172 if (!RCLASS_IV_TBL(klass)) {
173 RCLASS_IV_TBL(klass) = st_init_numtable();
175 st_insert(RCLASS_IV_TBL(klass), rb_intern("__attached__"), obj);
179 VALUE
180 rb_make_metaclass(VALUE obj, VALUE super)
182 if (BUILTIN_TYPE(obj) == T_CLASS && FL_TEST(obj, FL_SINGLETON)) {
183 return RBASIC(obj)->klass = rb_cClass;
185 else {
186 VALUE metasuper;
187 VALUE klass = rb_class_boot(super);
189 FL_SET(klass, FL_SINGLETON);
190 RBASIC(obj)->klass = klass;
191 rb_singleton_class_attached(klass, obj);
193 metasuper = RBASIC(rb_class_real(super))->klass;
194 /* metaclass of a superclass may be NULL at boot time */
195 if (metasuper) {
196 RBASIC(klass)->klass = metasuper;
198 return klass;
202 VALUE
203 rb_define_class_id(ID id, VALUE super)
205 VALUE klass;
207 if (!super) super = rb_cObject;
208 klass = rb_class_new(super);
209 rb_make_metaclass(klass, RBASIC(super)->klass);
211 return klass;
214 VALUE
215 rb_class_inherited(VALUE super, VALUE klass)
217 if (!super) super = rb_cObject;
218 return rb_funcall(super, rb_intern("inherited"), 1, klass);
221 VALUE
222 rb_define_class(const char *name, VALUE super)
224 VALUE klass;
225 ID id;
227 id = rb_intern(name);
228 if (rb_const_defined(rb_cObject, id)) {
229 klass = rb_const_get(rb_cObject, id);
230 if (TYPE(klass) != T_CLASS) {
231 rb_raise(rb_eTypeError, "%s is not a class", name);
233 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
234 rb_name_error(id, "%s is already defined", name);
236 return klass;
238 if (!super) {
239 rb_warn("no super class for `%s', Object assumed", name);
241 klass = rb_define_class_id(id, super);
242 st_add_direct(rb_class_tbl, id, klass);
243 rb_name_class(klass, id);
244 rb_const_set(rb_cObject, id, klass);
245 rb_class_inherited(super, klass);
247 return klass;
250 VALUE
251 rb_define_class_under(VALUE outer, const char *name, VALUE super)
253 VALUE klass;
254 ID id;
256 id = rb_intern(name);
257 if (rb_const_defined_at(outer, id)) {
258 klass = rb_const_get_at(outer, id);
259 if (TYPE(klass) != T_CLASS) {
260 rb_raise(rb_eTypeError, "%s is not a class", name);
262 if (rb_class_real(RCLASS_SUPER(klass)) != super) {
263 rb_name_error(id, "%s is already defined", name);
265 return klass;
267 if (!super) {
268 rb_warn("no super class for `%s::%s', Object assumed",
269 rb_class2name(outer), name);
271 klass = rb_define_class_id(id, super);
272 rb_set_class_path(klass, outer, name);
273 rb_const_set(outer, id, klass);
274 rb_class_inherited(super, klass);
276 return klass;
279 VALUE
280 rb_module_new(void)
282 VALUE mdl = class_alloc(T_MODULE, rb_cModule);
284 RCLASS_M_TBL(mdl) = st_init_numtable();
286 return (VALUE)mdl;
289 VALUE
290 rb_define_module_id(ID id)
292 VALUE mdl;
294 mdl = rb_module_new();
295 rb_name_class(mdl, id);
297 return mdl;
300 VALUE
301 rb_define_module(const char *name)
303 VALUE module;
304 ID id;
306 id = rb_intern(name);
307 if (rb_const_defined(rb_cObject, id)) {
308 module = rb_const_get(rb_cObject, id);
309 if (TYPE(module) == T_MODULE)
310 return module;
311 rb_raise(rb_eTypeError, "%s is not a module", rb_obj_classname(module));
313 module = rb_define_module_id(id);
314 st_add_direct(rb_class_tbl, id, module);
315 rb_const_set(rb_cObject, id, module);
317 return module;
320 VALUE
321 rb_define_module_under(VALUE outer, const char *name)
323 VALUE module;
324 ID id;
326 id = rb_intern(name);
327 if (rb_const_defined_at(outer, id)) {
328 module = rb_const_get_at(outer, id);
329 if (TYPE(module) == T_MODULE)
330 return module;
331 rb_raise(rb_eTypeError, "%s::%s is not a module",
332 rb_class2name(outer), rb_obj_classname(module));
334 module = rb_define_module_id(id);
335 rb_const_set(outer, id, module);
336 rb_set_class_path(module, outer, name);
338 return module;
341 static VALUE
342 include_class_new(VALUE module, VALUE super)
344 VALUE klass = class_alloc(T_ICLASS, rb_cClass);
346 if (BUILTIN_TYPE(module) == T_ICLASS) {
347 module = RBASIC(module)->klass;
349 if (!RCLASS_IV_TBL(module)) {
350 RCLASS_IV_TBL(module) = st_init_numtable();
352 RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module);
353 RCLASS_M_TBL(klass) = RCLASS_M_TBL(module);
354 RCLASS_SUPER(klass) = super;
355 if (TYPE(module) == T_ICLASS) {
356 RBASIC(klass)->klass = RBASIC(module)->klass;
358 else {
359 RBASIC(klass)->klass = module;
361 OBJ_INFECT(klass, module);
362 OBJ_INFECT(klass, super);
364 return (VALUE)klass;
367 void
368 rb_include_module(VALUE klass, VALUE module)
370 VALUE p, c;
371 int changed = 0;
373 rb_frozen_class_p(klass);
374 if (!OBJ_TAINTED(klass)) {
375 rb_secure(4);
378 if (TYPE(module) != T_MODULE) {
379 Check_Type(module, T_MODULE);
382 OBJ_INFECT(klass, module);
383 c = klass;
384 while (module) {
385 int superclass_seen = Qfalse;
387 if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
388 rb_raise(rb_eArgError, "cyclic include detected");
389 /* ignore if the module included already in superclasses */
390 for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
391 switch (BUILTIN_TYPE(p)) {
392 case T_ICLASS:
393 if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
394 if (!superclass_seen) {
395 c = p; /* move insertion point */
397 goto skip;
399 break;
400 case T_CLASS:
401 superclass_seen = Qtrue;
402 break;
405 c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
406 changed = 1;
407 skip:
408 module = RCLASS_SUPER(module);
410 if (changed) rb_clear_cache();
414 * call-seq:
415 * mod.included_modules -> array
417 * Returns the list of modules included in <i>mod</i>.
419 * module Mixin
420 * end
422 * module Outer
423 * include Mixin
424 * end
426 * Mixin.included_modules #=> []
427 * Outer.included_modules #=> [Mixin]
430 VALUE
431 rb_mod_included_modules(VALUE mod)
433 VALUE ary = rb_ary_new();
434 VALUE p;
436 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
437 if (BUILTIN_TYPE(p) == T_ICLASS) {
438 rb_ary_push(ary, RBASIC(p)->klass);
441 return ary;
445 * call-seq:
446 * mod.include?(module) => true or false
448 * Returns <code>true</code> if <i>module</i> is included in
449 * <i>mod</i> or one of <i>mod</i>'s ancestors.
451 * module A
452 * end
453 * class B
454 * include A
455 * end
456 * class C < B
457 * end
458 * B.include?(A) #=> true
459 * C.include?(A) #=> true
460 * A.include?(A) #=> false
463 VALUE
464 rb_mod_include_p(VALUE mod, VALUE mod2)
466 VALUE p;
468 Check_Type(mod2, T_MODULE);
469 for (p = RCLASS_SUPER(mod); p; p = RCLASS_SUPER(p)) {
470 if (BUILTIN_TYPE(p) == T_ICLASS) {
471 if (RBASIC(p)->klass == mod2) return Qtrue;
474 return Qfalse;
478 * call-seq:
479 * mod.ancestors -> array
481 * Returns a list of modules included in <i>mod</i> (including
482 * <i>mod</i> itself).
484 * module Mod
485 * include Math
486 * include Comparable
487 * end
489 * Mod.ancestors #=> [Mod, Comparable, Math]
490 * Math.ancestors #=> [Math]
493 VALUE
494 rb_mod_ancestors(VALUE mod)
496 VALUE p, ary = rb_ary_new();
498 for (p = mod; p; p = RCLASS_SUPER(p)) {
499 if (FL_TEST(p, FL_SINGLETON))
500 continue;
501 if (BUILTIN_TYPE(p) == T_ICLASS) {
502 rb_ary_push(ary, RBASIC(p)->klass);
504 else {
505 rb_ary_push(ary, p);
508 return ary;
511 #define VISI(x) ((x)&NOEX_MASK)
512 #define VISI_CHECK(x,f) (VISI(x) == (f))
514 static int
515 ins_methods_push(ID name, long type, VALUE ary, long visi)
517 if (type == -1) return ST_CONTINUE;
519 switch (visi) {
520 case NOEX_PRIVATE:
521 case NOEX_PROTECTED:
522 case NOEX_PUBLIC:
523 visi = (type == visi);
524 break;
525 default:
526 visi = (type != NOEX_PRIVATE);
527 break;
529 if (visi) {
530 rb_ary_push(ary, ID2SYM(name));
532 return ST_CONTINUE;
535 static int
536 ins_methods_i(ID name, long type, VALUE ary)
538 return ins_methods_push(name, type, ary, -1); /* everything but private */
541 static int
542 ins_methods_prot_i(ID name, long type, VALUE ary)
544 return ins_methods_push(name, type, ary, NOEX_PROTECTED);
547 static int
548 ins_methods_priv_i(ID name, long type, VALUE ary)
550 return ins_methods_push(name, type, ary, NOEX_PRIVATE);
553 static int
554 ins_methods_pub_i(ID name, long type, VALUE ary)
556 return ins_methods_push(name, type, ary, NOEX_PUBLIC);
559 static int
560 method_entry(ID key, NODE *body, st_table *list)
562 long type;
564 if (key == ID_ALLOCATOR) {
565 return ST_CONTINUE;
568 if (!st_lookup(list, key, 0)) {
569 if (body ==0 || !body->nd_body->nd_body) {
570 type = -1; /* none */
572 else {
573 type = VISI(body->nd_body->nd_noex);
575 st_add_direct(list, key, type);
577 return ST_CONTINUE;
580 static VALUE
581 class_instance_method_list(int argc, VALUE *argv, VALUE mod, int (*func) (ID, long, VALUE))
583 VALUE ary;
584 int recur;
585 st_table *list;
587 if (argc == 0) {
588 recur = Qtrue;
590 else {
591 VALUE r;
592 rb_scan_args(argc, argv, "01", &r);
593 recur = RTEST(r);
596 list = st_init_numtable();
597 for (; mod; mod = RCLASS_SUPER(mod)) {
598 st_foreach(RCLASS_M_TBL(mod), method_entry, (st_data_t)list);
599 if (BUILTIN_TYPE(mod) == T_ICLASS) continue;
600 if (FL_TEST(mod, FL_SINGLETON)) continue;
601 if (!recur) break;
603 ary = rb_ary_new();
604 st_foreach(list, func, ary);
605 st_free_table(list);
607 return ary;
611 * call-seq:
612 * mod.instance_methods(include_super=true) => array
614 * Returns an array containing the names of public instance methods in
615 * the receiver. For a module, these are the public methods; for a
616 * class, they are the instance (not singleton) methods. With no
617 * argument, or with an argument that is <code>false</code>, the
618 * instance methods in <i>mod</i> are returned, otherwise the methods
619 * in <i>mod</i> and <i>mod</i>'s superclasses are returned.
621 * module A
622 * def method1() end
623 * end
624 * class B
625 * def method2() end
626 * end
627 * class C < B
628 * def method3() end
629 * end
631 * A.instance_methods #=> [:method1]
632 * B.instance_methods(false) #=> [:method2]
633 * C.instance_methods(false) #=> [:method3]
634 * C.instance_methods(true).length #=> 43
637 VALUE
638 rb_class_instance_methods(int argc, VALUE *argv, VALUE mod)
640 return class_instance_method_list(argc, argv, mod, ins_methods_i);
644 * call-seq:
645 * mod.protected_instance_methods(include_super=true) => array
647 * Returns a list of the protected instance methods defined in
648 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
649 * methods of any ancestors are included.
652 VALUE
653 rb_class_protected_instance_methods(int argc, VALUE *argv, VALUE mod)
655 return class_instance_method_list(argc, argv, mod, ins_methods_prot_i);
659 * call-seq:
660 * mod.private_instance_methods(include_super=true) => array
662 * Returns a list of the private instance methods defined in
663 * <i>mod</i>. If the optional parameter is not <code>false</code>, the
664 * methods of any ancestors are included.
666 * module Mod
667 * def method1() end
668 * private :method1
669 * def method2() end
670 * end
671 * Mod.instance_methods #=> [:method2]
672 * Mod.private_instance_methods #=> [:method1]
675 VALUE
676 rb_class_private_instance_methods(int argc, VALUE *argv, VALUE mod)
678 return class_instance_method_list(argc, argv, mod, ins_methods_priv_i);
682 * call-seq:
683 * mod.public_instance_methods(include_super=true) => array
685 * Returns a list of the public instance methods defined in <i>mod</i>.
686 * If the optional parameter is not <code>false</code>, the methods of
687 * any ancestors are included.
690 VALUE
691 rb_class_public_instance_methods(int argc, VALUE *argv, VALUE mod)
693 return class_instance_method_list(argc, argv, mod, ins_methods_pub_i);
697 * call-seq:
698 * obj.singleton_methods(all=true) => array
700 * Returns an array of the names of singleton methods for <i>obj</i>.
701 * If the optional <i>all</i> parameter is true, the list will include
702 * methods in modules included in <i>obj</i>.
704 * module Other
705 * def three() end
706 * end
708 * class Single
709 * def Single.four() end
710 * end
712 * a = Single.new
714 * def a.one()
715 * end
717 * class << a
718 * include Other
719 * def two()
720 * end
721 * end
723 * Single.singleton_methods #=> [:four]
724 * a.singleton_methods(false) #=> [:two, :one]
725 * a.singleton_methods #=> [:two, :one, :three]
728 VALUE
729 rb_obj_singleton_methods(int argc, VALUE *argv, VALUE obj)
731 VALUE recur, ary, klass;
732 st_table *list;
734 if (argc == 0) {
735 recur = Qtrue;
737 else {
738 rb_scan_args(argc, argv, "01", &recur);
740 klass = CLASS_OF(obj);
741 list = st_init_numtable();
742 if (klass && FL_TEST(klass, FL_SINGLETON)) {
743 st_foreach(RCLASS_M_TBL(klass), method_entry, (st_data_t)list);
744 klass = RCLASS_SUPER(klass);
746 if (RTEST(recur)) {
747 while (klass && (FL_TEST(klass, FL_SINGLETON) || TYPE(klass) == T_ICLASS)) {
748 st_foreach(RCLASS_M_TBL(klass), method_entry, (st_data_t)list);
749 klass = RCLASS_SUPER(klass);
752 ary = rb_ary_new();
753 st_foreach(list, ins_methods_i, ary);
754 st_free_table(list);
756 return ary;
759 void
760 rb_define_method_id(VALUE klass, ID name, VALUE (*func)(ANYARGS), int argc)
762 rb_add_method(klass, name, NEW_CFUNC(func,argc), NOEX_PUBLIC);
765 void
766 rb_define_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
768 rb_add_method(klass, rb_intern(name), NEW_CFUNC(func, argc), NOEX_PUBLIC);
771 void
772 rb_define_protected_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
774 rb_add_method(klass, rb_intern(name), NEW_CFUNC(func, argc), NOEX_PROTECTED);
777 void
778 rb_define_private_method(VALUE klass, const char *name, VALUE (*func)(ANYARGS), int argc)
780 rb_add_method(klass, rb_intern(name), NEW_CFUNC(func, argc), NOEX_PRIVATE);
783 void
784 rb_undef_method(VALUE klass, const char *name)
786 rb_add_method(klass, rb_intern(name), 0, NOEX_UNDEF);
789 #define SPECIAL_SINGLETON(x,c) do {\
790 if (obj == (x)) {\
791 return c;\
793 } while (0)
795 VALUE
796 rb_singleton_class(VALUE obj)
798 VALUE klass;
800 if (FIXNUM_P(obj) || SYMBOL_P(obj)) {
801 rb_raise(rb_eTypeError, "can't define singleton");
803 if (rb_special_const_p(obj)) {
804 SPECIAL_SINGLETON(Qnil, rb_cNilClass);
805 SPECIAL_SINGLETON(Qfalse, rb_cFalseClass);
806 SPECIAL_SINGLETON(Qtrue, rb_cTrueClass);
807 rb_bug("unknown immediate %ld", obj);
810 DEFER_INTS;
811 if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
812 rb_iv_get(RBASIC(obj)->klass, "__attached__") == obj) {
813 klass = RBASIC(obj)->klass;
815 else {
816 klass = rb_make_metaclass(obj, RBASIC(obj)->klass);
818 if (OBJ_TAINTED(obj)) {
819 OBJ_TAINT(klass);
821 else {
822 FL_UNSET(klass, FL_TAINT);
824 if (OBJ_FROZEN(obj)) OBJ_FREEZE(klass);
825 ALLOW_INTS;
827 return klass;
830 void
831 rb_define_singleton_method(VALUE obj, const char *name, VALUE (*func)(ANYARGS), int argc)
833 rb_define_method(rb_singleton_class(obj), name, func, argc);
836 void
837 rb_define_module_function(VALUE module, const char *name, VALUE (*func)(ANYARGS), int argc)
839 rb_define_private_method(module, name, func, argc);
840 rb_define_singleton_method(module, name, func, argc);
843 void
844 rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)
846 rb_define_module_function(rb_mKernel, name, func, argc);
849 void
850 rb_define_alias(VALUE klass, const char *name1, const char *name2)
852 rb_alias(klass, rb_intern(name1), rb_intern(name2));
855 void
856 rb_define_attr(VALUE klass, const char *name, int read, int write)
858 rb_attr(klass, rb_intern(name), read, write, Qfalse);
861 #include <stdarg.h>
864 rb_scan_args(int argc, const VALUE *argv, const char *fmt, ...)
866 int n, i = 0;
867 const char *p = fmt;
868 VALUE *var;
869 va_list vargs;
871 va_start(vargs, fmt);
873 if (*p == '*') goto rest_arg;
875 if (ISDIGIT(*p)) {
876 n = *p - '0';
877 if (n > argc)
878 rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, n);
879 for (i=0; i<n; i++) {
880 var = va_arg(vargs, VALUE*);
881 if (var) *var = argv[i];
883 p++;
885 else {
886 goto error;
889 if (ISDIGIT(*p)) {
890 n = i + *p - '0';
891 for (; i<n; i++) {
892 var = va_arg(vargs, VALUE*);
893 if (argc > i) {
894 if (var) *var = argv[i];
896 else {
897 if (var) *var = Qnil;
900 p++;
903 if(*p == '*') {
904 rest_arg:
905 var = va_arg(vargs, VALUE*);
906 if (argc > i) {
907 if (var) *var = rb_ary_new4(argc-i, argv+i);
908 i = argc;
910 else {
911 if (var) *var = rb_ary_new();
913 p++;
916 if (*p == '&') {
917 var = va_arg(vargs, VALUE*);
918 if (rb_block_given_p()) {
919 *var = rb_block_proc();
921 else {
922 *var = Qnil;
924 p++;
926 va_end(vargs);
928 if (*p != '\0') {
929 goto error;
932 if (argc > i) {
933 rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", argc, i);
936 return argc;
938 error:
939 rb_fatal("bad scan arg format: %s", fmt);
940 return 0;