Ruby  2.0.0p648(2015-12-16revision53162)
proc.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  proc.c - Proc, Binding, Env
4 
5  $Author: usa $
6  created at: Wed Jan 17 12:13:14 2007
7 
8  Copyright (C) 2004-2007 Koichi Sasada
9 
10 **********************************************************************/
11 
12 #include "eval_intern.h"
13 #include "internal.h"
14 #include "gc.h"
15 #include "iseq.h"
16 
17 struct METHOD {
21  ID id;
24 };
25 
30 
31 static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE);
32 static int method_arity(VALUE);
33 static int method_min_max_arity(VALUE, int *max);
34 static ID attached;
35 
36 /* Proc */
37 
38 #define IS_METHOD_PROC_NODE(node) (nd_type(node) == NODE_IFUNC && (node)->nd_cfnc == bmcall)
39 
40 static void
41 proc_free(void *ptr)
42 {
43  RUBY_FREE_ENTER("proc");
44  if (ptr) {
45  ruby_xfree(ptr);
46  }
47  RUBY_FREE_LEAVE("proc");
48 }
49 
50 static void
51 proc_mark(void *ptr)
52 {
53  rb_proc_t *proc;
54  RUBY_MARK_ENTER("proc");
55  if (ptr) {
56  proc = ptr;
61  if (proc->block.iseq && RUBY_VM_IFUNC_P(proc->block.iseq)) {
63  }
64  }
65  RUBY_MARK_LEAVE("proc");
66 }
67 
68 static size_t
69 proc_memsize(const void *ptr)
70 {
71  return ptr ? sizeof(rb_proc_t) : 0;
72 }
73 
75  "proc",
76  {
77  proc_mark,
78  proc_free,
80  },
81 };
82 
83 VALUE
85 {
86  rb_proc_t *proc;
87  return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
88 }
89 
90 VALUE
92 {
93  if (rb_typeddata_is_kind_of(proc, &proc_data_type)) {
94  return Qtrue;
95  }
96  else {
97  return Qfalse;
98  }
99 }
100 
101 /* :nodoc: */
102 static VALUE
104 {
105  VALUE procval = rb_proc_alloc(rb_cProc);
106  rb_proc_t *src, *dst;
107  GetProcPtr(self, src);
108  GetProcPtr(procval, dst);
109 
110  dst->block = src->block;
111  dst->block.proc = procval;
112  dst->blockprocval = src->blockprocval;
113  dst->envval = src->envval;
114  dst->safe_level = src->safe_level;
115  dst->is_lambda = src->is_lambda;
116 
117  return procval;
118 }
119 
120 /* :nodoc: */
121 static VALUE
123 {
124  VALUE procval = proc_dup(self);
125  CLONESETUP(procval, self);
126  return procval;
127 }
128 
129 /*
130  * call-seq:
131  * prc.lambda? -> true or false
132  *
133  * Returns +true+ for a Proc object for which argument handling is rigid.
134  * Such procs are typically generated by +lambda+.
135  *
136  * A Proc object generated by +proc+ ignores extra arguments.
137  *
138  * proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
139  *
140  * It provides +nil+ for missing arguments.
141  *
142  * proc {|a,b| [a,b] }.call(1) #=> [1,nil]
143  *
144  * It expands a single array argument.
145  *
146  * proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
147  *
148  * A Proc object generated by +lambda+ doesn't have such tricks.
149  *
150  * lambda {|a,b| [a,b] }.call(1,2,3) #=> ArgumentError
151  * lambda {|a,b| [a,b] }.call(1) #=> ArgumentError
152  * lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
153  *
154  * Proc#lambda? is a predicate for the tricks.
155  * It returns +true+ if no tricks apply.
156  *
157  * lambda {}.lambda? #=> true
158  * proc {}.lambda? #=> false
159  *
160  * Proc.new is the same as +proc+.
161  *
162  * Proc.new {}.lambda? #=> false
163  *
164  * +lambda+, +proc+ and Proc.new preserve the tricks of
165  * a Proc object given by <code>&</code> argument.
166  *
167  * lambda(&lambda {}).lambda? #=> true
168  * proc(&lambda {}).lambda? #=> true
169  * Proc.new(&lambda {}).lambda? #=> true
170  *
171  * lambda(&proc {}).lambda? #=> false
172  * proc(&proc {}).lambda? #=> false
173  * Proc.new(&proc {}).lambda? #=> false
174  *
175  * A Proc object generated by <code>&</code> argument has the tricks
176  *
177  * def n(&b) b.lambda? end
178  * n {} #=> false
179  *
180  * The <code>&</code> argument preserves the tricks if a Proc object
181  * is given by <code>&</code> argument.
182  *
183  * n(&lambda {}) #=> true
184  * n(&proc {}) #=> false
185  * n(&Proc.new {}) #=> false
186  *
187  * A Proc object converted from a method has no tricks.
188  *
189  * def m() end
190  * method(:m).to_proc.lambda? #=> true
191  *
192  * n(&method(:m)) #=> true
193  * n(&method(:m).to_proc) #=> true
194  *
195  * +define_method+ is treated the same as method definition.
196  * The defined method has no tricks.
197  *
198  * class C
199  * define_method(:d) {}
200  * end
201  * C.new.d(1,2) #=> ArgumentError
202  * C.new.method(:d).to_proc.lambda? #=> true
203  *
204  * +define_method+ always defines a method without the tricks,
205  * even if a non-lambda Proc object is given.
206  * This is the only exception for which the tricks are not preserved.
207  *
208  * class C
209  * define_method(:e, &proc {})
210  * end
211  * C.new.e(1,2) #=> ArgumentError
212  * C.new.method(:e).to_proc.lambda? #=> true
213  *
214  * This exception insures that methods never have tricks
215  * and makes it easy to have wrappers to define methods that behave as usual.
216  *
217  * class C
218  * def self.def2(name, &body)
219  * define_method(name, &body)
220  * end
221  *
222  * def2(:f) {}
223  * end
224  * C.new.f(1,2) #=> ArgumentError
225  *
226  * The wrapper <i>def2</i> defines a method which has no tricks.
227  *
228  */
229 
230 VALUE
232 {
233  rb_proc_t *proc;
234  GetProcPtr(procval, proc);
235 
236  return proc->is_lambda ? Qtrue : Qfalse;
237 }
238 
239 /* Binding */
240 
241 static void
242 binding_free(void *ptr)
243 {
244  rb_binding_t *bind;
245  RUBY_FREE_ENTER("binding");
246  if (ptr) {
247  bind = ptr;
248  ruby_xfree(bind);
249  }
250  RUBY_FREE_LEAVE("binding");
251 }
252 
253 static void
254 binding_mark(void *ptr)
255 {
256  rb_binding_t *bind;
257  RUBY_MARK_ENTER("binding");
258  if (ptr) {
259  bind = ptr;
260  RUBY_MARK_UNLESS_NULL(bind->env);
263  }
264  RUBY_MARK_LEAVE("binding");
265 }
266 
267 static size_t
268 binding_memsize(const void *ptr)
269 {
270  return ptr ? sizeof(rb_binding_t) : 0;
271 }
272 
274  "binding",
275  {
276  binding_mark,
277  binding_free,
279  },
280 };
281 
282 VALUE
284 {
285  VALUE obj;
286  rb_binding_t *bind;
287  obj = TypedData_Make_Struct(klass, rb_binding_t, &binding_data_type, bind);
288  return obj;
289 }
290 
291 /* :nodoc: */
292 static VALUE
294 {
296  rb_binding_t *src, *dst;
297  GetBindingPtr(self, src);
298  GetBindingPtr(bindval, dst);
299  dst->env = src->env;
300  dst->path = src->path;
301  dst->blockprocval = src->blockprocval;
302  dst->first_lineno = src->first_lineno;
303  return bindval;
304 }
305 
306 /* :nodoc: */
307 static VALUE
309 {
310  VALUE bindval = binding_dup(self);
311  CLONESETUP(bindval, self);
312  return bindval;
313 }
314 
315 VALUE
317 {
318  return rb_vm_make_binding(th, src_cfp);
319 }
320 
321 VALUE
323 {
324  rb_thread_t *th = GET_THREAD();
325  return rb_binding_new_with_cfp(th, th->cfp);
326 }
327 
328 /*
329  * call-seq:
330  * binding -> a_binding
331  *
332  * Returns a +Binding+ object, describing the variable and
333  * method bindings at the point of call. This object can be used when
334  * calling +eval+ to execute the evaluated command in this
335  * environment. See also the description of class +Binding+.
336  *
337  * def get_binding(param)
338  * return binding
339  * end
340  * b = get_binding("hello")
341  * eval("param", b) #=> "hello"
342  */
343 
344 static VALUE
346 {
347  return rb_binding_new();
348 }
349 
350 /*
351  * call-seq:
352  * binding.eval(string [, filename [,lineno]]) -> obj
353  *
354  * Evaluates the Ruby expression(s) in <em>string</em>, in the
355  * <em>binding</em>'s context. If the optional <em>filename</em> and
356  * <em>lineno</em> parameters are present, they will be used when
357  * reporting syntax errors.
358  *
359  * def get_binding(param)
360  * return binding
361  * end
362  * b = get_binding("hello")
363  * b.eval("param") #=> "hello"
364  */
365 
366 static VALUE
367 bind_eval(int argc, VALUE *argv, VALUE bindval)
368 {
369  VALUE args[4];
370 
371  rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
372  args[1] = bindval;
373  return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */);
374 }
375 
376 static VALUE
377 proc_new(VALUE klass, int is_lambda)
378 {
379  VALUE procval = Qnil;
380  rb_thread_t *th = GET_THREAD();
381  rb_control_frame_t *cfp = th->cfp;
382  rb_block_t *block;
383 
384  if ((block = rb_vm_control_frame_block_ptr(cfp)) != 0) {
385  /* block found */
386  }
387  else {
389 
390  if ((block = rb_vm_control_frame_block_ptr(cfp)) != 0) {
391  if (is_lambda) {
392  rb_warn("tried to create Proc object without a block");
393  }
394  }
395  else {
397  "tried to create Proc object without a block");
398  }
399  }
400 
401  procval = block->proc;
402 
403  if (procval) {
404  if (RBASIC(procval)->klass == klass) {
405  return procval;
406  }
407  else {
408  VALUE newprocval = proc_dup(procval);
409  RBASIC(newprocval)->klass = klass;
410  return newprocval;
411  }
412  }
413 
414  procval = rb_vm_make_proc(th, block, klass);
415 
416  if (is_lambda) {
417  rb_proc_t *proc;
418  GetProcPtr(procval, proc);
419  proc->is_lambda = TRUE;
420  }
421  return procval;
422 }
423 
424 /*
425  * call-seq:
426  * Proc.new {|...| block } -> a_proc
427  * Proc.new -> a_proc
428  *
429  * Creates a new <code>Proc</code> object, bound to the current
430  * context. <code>Proc::new</code> may be called without a block only
431  * within a method with an attached block, in which case that block is
432  * converted to the <code>Proc</code> object.
433  *
434  * def proc_from
435  * Proc.new
436  * end
437  * proc = proc_from { "hello" }
438  * proc.call #=> "hello"
439  */
440 
441 static VALUE
443 {
444  VALUE block = proc_new(klass, FALSE);
445 
446  rb_obj_call_init(block, argc, argv);
447  return block;
448 }
449 
450 /*
451  * call-seq:
452  * proc { |...| block } -> a_proc
453  *
454  * Equivalent to <code>Proc.new</code>.
455  */
456 
457 VALUE
459 {
460  return proc_new(rb_cProc, FALSE);
461 }
462 
463 /*
464  * call-seq:
465  * lambda { |...| block } -> a_proc
466  *
467  * Equivalent to <code>Proc.new</code>, except the resulting Proc objects
468  * check the number of parameters passed when called.
469  */
470 
471 VALUE
473 {
474  return proc_new(rb_cProc, TRUE);
475 }
476 
477 VALUE
479 {
480  rb_warn("rb_f_lambda() is deprecated; use rb_block_proc() instead");
481  return rb_block_lambda();
482 }
483 
484 /* Document-method: ===
485  *
486  * call-seq:
487  * proc === obj -> result_of_proc
488  *
489  * Invokes the block with +obj+ as the proc's parameter like Proc#call. It
490  * is to allow a proc object to be a target of +when+ clause in a case
491  * statement.
492  */
493 
494 /* CHECKME: are the argument checking semantics correct? */
495 
496 /*
497  * call-seq:
498  * prc.call(params,...) -> obj
499  * prc[params,...] -> obj
500  * prc.(params,...) -> obj
501  *
502  * Invokes the block, setting the block's parameters to the values in
503  * <i>params</i> using something close to method calling semantics.
504  * Generates a warning if multiple values are passed to a proc that
505  * expects just one (previously this silently converted the parameters
506  * to an array). Note that prc.() invokes prc.call() with the parameters
507  * given. It's a syntax sugar to hide "call".
508  *
509  * For procs created using <code>lambda</code> or <code>->()</code> an error
510  * is generated if the wrong number of parameters are passed to a Proc with
511  * multiple parameters. For procs created using <code>Proc.new</code> or
512  * <code>Kernel.proc</code>, extra parameters are silently discarded.
513  *
514  * Returns the value of the last expression evaluated in the block. See
515  * also <code>Proc#yield</code>.
516  *
517  * a_proc = Proc.new {|a, *b| b.collect {|i| i*a }}
518  * a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
519  * a_proc[9, 1, 2, 3] #=> [9, 18, 27]
520  * a_proc = lambda {|a,b| a}
521  * a_proc.call(1,2,3)
522  *
523  * <em>produces:</em>
524  *
525  * prog.rb:4:in `block in <main>': wrong number of arguments (3 for 2) (ArgumentError)
526  * from prog.rb:5:in `call'
527  * from prog.rb:5:in `<main>'
528  *
529  */
530 
531 static VALUE
532 proc_call(int argc, VALUE *argv, VALUE procval)
533 {
534  VALUE vret;
535  rb_proc_t *proc;
536  rb_block_t *blockptr = 0;
537  rb_iseq_t *iseq;
538  VALUE passed_procval;
539  GetProcPtr(procval, proc);
540 
541  iseq = proc->block.iseq;
542  if (BUILTIN_TYPE(iseq) == T_NODE || iseq->arg_block != -1) {
543  if (rb_block_given_p()) {
544  rb_proc_t *passed_proc;
545  RB_GC_GUARD(passed_procval) = rb_block_proc();
546  GetProcPtr(passed_procval, passed_proc);
547  blockptr = &passed_proc->block;
548  }
549  }
550 
551  vret = rb_vm_invoke_proc(GET_THREAD(), proc, argc, argv, blockptr);
552  RB_GC_GUARD(procval);
553  return vret;
554 }
555 
556 #if SIZEOF_LONG > SIZEOF_INT
557 static inline int
558 check_argc(long argc)
559 {
560  if (argc > INT_MAX || argc < 0) {
561  rb_raise(rb_eArgError, "too many arguments (%lu)",
562  (unsigned long)argc);
563  }
564  return (int)argc;
565 }
566 #else
567 #define check_argc(argc) (argc)
568 #endif
569 
570 VALUE
572 {
573  VALUE vret;
574  rb_proc_t *proc;
575  GetProcPtr(self, proc);
576  vret = rb_vm_invoke_proc(GET_THREAD(), proc,
577  check_argc(RARRAY_LEN(args)), RARRAY_PTR(args), 0);
578  RB_GC_GUARD(self);
579  RB_GC_GUARD(args);
580  return vret;
581 }
582 
583 VALUE
584 rb_proc_call_with_block(VALUE self, int argc, VALUE *argv, VALUE pass_procval)
585 {
586  VALUE vret;
587  rb_proc_t *proc;
588  rb_block_t *block = 0;
589  GetProcPtr(self, proc);
590 
591  if (!NIL_P(pass_procval)) {
592  rb_proc_t *pass_proc;
593  GetProcPtr(pass_procval, pass_proc);
594  block = &pass_proc->block;
595  }
596 
597  vret = rb_vm_invoke_proc(GET_THREAD(), proc, argc, argv, block);
598  RB_GC_GUARD(self);
599  RB_GC_GUARD(pass_procval);
600  return vret;
601 }
602 
603 /*
604  * call-seq:
605  * prc.arity -> fixnum
606  *
607  * Returns the number of arguments that would not be ignored. If the block
608  * is declared to take no arguments, returns 0. If the block is known
609  * to take exactly n arguments, returns n. If the block has optional
610  * arguments, return -n-1, where n is the number of mandatory
611  * arguments. A <code>proc</code> with no argument declarations
612  * is the same a block declaring <code>||</code> as its arguments.
613  *
614  * proc {}.arity #=> 0
615  * proc {||}.arity #=> 0
616  * proc {|a|}.arity #=> 1
617  * proc {|a,b|}.arity #=> 2
618  * proc {|a,b,c|}.arity #=> 3
619  * proc {|*a|}.arity #=> -1
620  * proc {|a,*b|}.arity #=> -2
621  * proc {|a,*b, c|}.arity #=> -3
622  *
623  * proc { |x = 0| }.arity #=> 0
624  * lambda { |a = 0| }.arity #=> -1
625  * proc { |x=0, y| }.arity #=> 0
626  * lambda { |x=0, y| }.arity #=> -2
627  * proc { |x=0, y=0| }.arity #=> 0
628  * lambda { |x=0, y=0| }.arity #=> -1
629  * proc { |x, y=0| }.arity #=> 1
630  * lambda { |x, y=0| }.arity #=> -2
631  * proc { |(x, y), z=0| }.arity #=> 1
632  * lambda { |(x, y), z=0| }.arity #=> -2
633  */
634 
635 static VALUE
637 {
638  int arity = rb_proc_arity(self);
639  return INT2FIX(arity);
640 }
641 
642 static inline int
644 {
645  *max = iseq->arg_rest == -1 ?
646  iseq->argc + iseq->arg_post_len + iseq->arg_opts - (iseq->arg_opts > 0)
648  return iseq->argc + iseq->arg_post_len;
649 }
650 
651 /*
652  * Returns the number of required parameters and stores the maximum
653  * number of parameters in max, or UNLIMITED_ARGUMENTS if no max.
654  * For non-lambda procs, the maximum is the number of non-ignored
655  * parameters even though there is no actual limit to the number of parameters
656  */
657 static int
659 {
660  rb_proc_t *proc;
661  rb_iseq_t *iseq;
662  GetProcPtr(self, proc);
663  iseq = proc->block.iseq;
664  if (iseq) {
665  if (BUILTIN_TYPE(iseq) != T_NODE) {
666  return rb_iseq_min_max_arity(iseq, max);
667  }
668  else {
669  NODE *node = (NODE *)iseq;
670  if (IS_METHOD_PROC_NODE(node)) {
671  /* e.g. method(:foo).to_proc.arity */
672  return method_min_max_arity(node->nd_tval, max);
673  }
674  }
675  }
676  *max = UNLIMITED_ARGUMENTS;
677  return 0;
678 }
679 
680 int
682 {
683  rb_proc_t *proc;
684  int max, min = rb_proc_min_max_arity(self, &max);
685  GetProcPtr(self, proc);
686  return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1;
687 }
688 
689 #define get_proc_iseq rb_proc_get_iseq
690 
691 rb_iseq_t *
692 rb_proc_get_iseq(VALUE self, int *is_proc)
693 {
694  rb_proc_t *proc;
695  rb_iseq_t *iseq;
696 
697  GetProcPtr(self, proc);
698  iseq = proc->block.iseq;
699  if (is_proc) *is_proc = !proc->is_lambda;
700  if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) {
701  NODE *node = (NODE *)iseq;
702  iseq = 0;
703  if (IS_METHOD_PROC_NODE(node)) {
704  /* method(:foo).to_proc */
705  iseq = rb_method_get_iseq(node->nd_tval);
706  if (is_proc) *is_proc = 0;
707  }
708  }
709  return iseq;
710 }
711 
712 static VALUE
714 {
715  VALUE loc[2];
716 
717  if (!iseq) return Qnil;
718  loc[0] = iseq->location.path;
719  if (iseq->line_info_table) {
720  loc[1] = INT2FIX(rb_iseq_first_lineno(iseq));
721  }
722  else {
723  loc[1] = Qnil;
724  }
725  return rb_ary_new4(2, loc);
726 }
727 
728 /*
729  * call-seq:
730  * prc.source_location -> [String, Fixnum]
731  *
732  * Returns the Ruby source filename and line number containing this proc
733  * or +nil+ if this proc was not defined in Ruby (i.e. native)
734  */
735 
736 VALUE
738 {
739  return iseq_location(get_proc_iseq(self, 0));
740 }
741 
742 static VALUE
744 {
745  VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
746  int n = (arity < 0) ? ~arity : arity;
747  ID req, rest;
748  CONST_ID(req, "req");
749  a = rb_ary_new3(1, ID2SYM(req));
750  OBJ_FREEZE(a);
751  for (; n; --n) {
752  rb_ary_push(param, a);
753  }
754  if (arity < 0) {
755  CONST_ID(rest, "rest");
756  rb_ary_store(param, ~arity, rb_ary_new3(1, ID2SYM(rest)));
757  }
758  return param;
759 }
760 
761 /*
762  * call-seq:
763  * prc.parameters -> array
764  *
765  * Returns the parameter information of this proc.
766  *
767  * prc = lambda{|x, y=42, *other|}
768  * prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
769  */
770 
771 static VALUE
773 {
774  int is_proc;
775  rb_iseq_t *iseq = get_proc_iseq(self, &is_proc);
776  if (!iseq) {
777  return unnamed_parameters(rb_proc_arity(self));
778  }
779  return rb_iseq_parameters(iseq, is_proc);
780 }
781 
784 {
785  rb_proc_t *proc;
786  GetProcPtr(prc, proc);
787  hash = rb_hash_uint(hash, (st_index_t)proc->block.iseq);
788  hash = rb_hash_uint(hash, (st_index_t)proc->envval);
789  return rb_hash_uint(hash, (st_index_t)proc->block.ep >> 16);
790 }
791 
792 /*
793  * call-seq:
794  * prc.hash -> integer
795  *
796  * Returns a hash value corresponding to proc body.
797  */
798 
799 static VALUE
801 {
803  hash = rb_hash_start(0);
804  hash = rb_hash_proc(hash, self);
805  hash = rb_hash_end(hash);
806  return LONG2FIX(hash);
807 }
808 
809 /*
810  * call-seq:
811  * prc.to_s -> string
812  *
813  * Returns the unique identifier for this proc, along with
814  * an indication of where the proc was defined.
815  */
816 
817 static VALUE
819 {
820  VALUE str = 0;
821  rb_proc_t *proc;
822  const char *cname = rb_obj_classname(self);
823  rb_iseq_t *iseq;
824  const char *is_lambda;
825 
826  GetProcPtr(self, proc);
827  iseq = proc->block.iseq;
828  is_lambda = proc->is_lambda ? " (lambda)" : "";
829 
830  if (RUBY_VM_NORMAL_ISEQ_P(iseq)) {
831  int first_lineno = 0;
832 
833  if (iseq->line_info_table) {
834  first_lineno = rb_iseq_first_lineno(iseq);
835  }
836  str = rb_sprintf("#<%s:%p@%s:%d%s>", cname, (void *)self,
837  RSTRING_PTR(iseq->location.path),
838  first_lineno, is_lambda);
839  }
840  else {
841  str = rb_sprintf("#<%s:%p%s>", cname, (void *)proc->block.iseq,
842  is_lambda);
843  }
844 
845  if (OBJ_TAINTED(self)) {
846  OBJ_TAINT(str);
847  }
848  return str;
849 }
850 
851 /*
852  * call-seq:
853  * prc.to_proc -> prc
854  *
855  * Part of the protocol for converting objects to <code>Proc</code>
856  * objects. Instances of class <code>Proc</code> simply return
857  * themselves.
858  */
859 
860 static VALUE
862 {
863  return self;
864 }
865 
866 static void
867 bm_mark(void *ptr)
868 {
869  struct METHOD *data = ptr;
870  rb_gc_mark(data->defined_class);
871  rb_gc_mark(data->rclass);
872  rb_gc_mark(data->recv);
873  if (data->me) rb_mark_method_entry(data->me);
874 }
875 
876 static void
877 bm_free(void *ptr)
878 {
879  struct METHOD *data = ptr;
880  struct unlinked_method_entry_list_entry *ume = data->ume;
881  data->me->mark = 0;
882  ume->me = data->me;
883  ume->next = GET_VM()->unlinked_method_entry_list;
884  GET_VM()->unlinked_method_entry_list = ume;
885  xfree(ptr);
886 }
887 
888 static size_t
889 bm_memsize(const void *ptr)
890 {
891  return ptr ? sizeof(struct METHOD) : 0;
892 }
893 
895  "method",
896  {
897  bm_mark,
898  bm_free,
899  bm_memsize,
900  },
901 };
902 
903 VALUE
905 {
907  return Qtrue;
908  }
909  else {
910  return Qfalse;
911  }
912 }
913 
914 static VALUE
915 mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
916 {
917  VALUE method;
918  VALUE rclass = klass, defined_class;
919  ID rid = id;
920  struct METHOD *data;
921  rb_method_entry_t *me, meb;
922  rb_method_definition_t *def = 0;
924 
925  again:
926  me = rb_method_entry_without_refinements(klass, id, &defined_class);
927  if (UNDEFINED_METHOD_ENTRY_P(me)) {
928  ID rmiss = idRespond_to_missing;
929  VALUE sym = ID2SYM(id);
930 
931  if (obj != Qundef && !rb_method_basic_definition_p(klass, rmiss)) {
932  if (RTEST(rb_funcall(obj, rmiss, 2, sym, scope ? Qfalse : Qtrue))) {
935  def->original_id = id;
936  def->alias_count = 0;
937  defined_class = klass;
938 
939  meb.flag = 0;
940  meb.mark = 0;
941  meb.called_id = id;
942  meb.klass = klass;
943  meb.def = def;
944  me = &meb;
945  def = 0;
946 
947  goto gen_method;
948  }
949  }
950  rb_print_undef(klass, id, 0);
951  }
952  def = me->def;
953  if (flag == NOEX_UNDEF) {
954  flag = me->flag;
955  if (scope && (flag & NOEX_MASK) != NOEX_PUBLIC) {
956  const char *v = "";
957  switch (flag & NOEX_MASK) {
958  case NOEX_PRIVATE: v = "private"; break;
959  case NOEX_PROTECTED: v = "protected"; break;
960  }
961  rb_name_error(id, "method `%s' for %s `%s' is %s",
962  rb_id2name(id),
963  (RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
964  rb_class2name(klass),
965  v);
966  }
967  }
968  if (def && def->type == VM_METHOD_TYPE_ZSUPER) {
969  klass = RCLASS_SUPER(defined_class);
970  id = def->original_id;
971  goto again;
972  }
973 
974  klass = defined_class;
975 
976  while (rclass != klass &&
977  (FL_TEST(rclass, FL_SINGLETON) || RB_TYPE_P(rclass, T_ICLASS))) {
978  rclass = RCLASS_SUPER(rclass);
979  }
980 
981  gen_method:
982  method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
983 
984  data->recv = obj;
985  data->rclass = rclass;
987  data->id = rid;
988  data->me = ALLOC(rb_method_entry_t);
989  *data->me = *me;
990  data->me->def->alias_count++;
992 
993  OBJ_INFECT(method, klass);
994 
995  return method;
996 }
997 
998 
999 /**********************************************************************
1000  *
1001  * Document-class : Method
1002  *
1003  * Method objects are created by <code>Object#method</code>, and are
1004  * associated with a particular object (not just with a class). They
1005  * may be used to invoke the method within the object, and as a block
1006  * associated with an iterator. They may also be unbound from one
1007  * object (creating an <code>UnboundMethod</code>) and bound to
1008  * another.
1009  *
1010  * class Thing
1011  * def square(n)
1012  * n*n
1013  * end
1014  * end
1015  * thing = Thing.new
1016  * meth = thing.method(:square)
1017  *
1018  * meth.call(9) #=> 81
1019  * [ 1, 2, 3 ].collect(&meth) #=> [1, 4, 9]
1020  *
1021  */
1022 
1023 /*
1024  * call-seq:
1025  * meth == other_meth -> true or false
1026  *
1027  * Two method objects are equal if they are bound to the same
1028  * object and refer to the same method definition and their owners are the
1029  * same class or module.
1030  */
1031 
1032 static VALUE
1033 method_eq(VALUE method, VALUE other)
1034 {
1035  struct METHOD *m1, *m2;
1036 
1037  if (!rb_obj_is_method(other))
1038  return Qfalse;
1039  if (CLASS_OF(method) != CLASS_OF(other))
1040  return Qfalse;
1041 
1043  m1 = (struct METHOD *)DATA_PTR(method);
1044  m2 = (struct METHOD *)DATA_PTR(other);
1045 
1046  if (!rb_method_entry_eq(m1->me, m2->me) ||
1047  m1->rclass != m2->rclass ||
1048  m1->recv != m2->recv) {
1049  return Qfalse;
1050  }
1051 
1052  return Qtrue;
1053 }
1054 
1055 /*
1056  * call-seq:
1057  * meth.hash -> integer
1058  *
1059  * Returns a hash value corresponding to the method object.
1060  */
1061 
1062 static VALUE
1064 {
1065  struct METHOD *m;
1066  st_index_t hash;
1067 
1068  TypedData_Get_Struct(method, struct METHOD, &method_data_type, m);
1069  hash = rb_hash_start((st_index_t)m->rclass);
1070  hash = rb_hash_uint(hash, (st_index_t)m->recv);
1071  hash = rb_hash_method_entry(hash, m->me);
1072  hash = rb_hash_end(hash);
1073 
1074  return INT2FIX(hash);
1075 }
1076 
1077 /*
1078  * call-seq:
1079  * meth.unbind -> unbound_method
1080  *
1081  * Dissociates <i>meth</i> from its current receiver. The resulting
1082  * <code>UnboundMethod</code> can subsequently be bound to a new object
1083  * of the same class (see <code>UnboundMethod</code>).
1084  */
1085 
1086 static VALUE
1088 {
1089  VALUE method;
1090  struct METHOD *orig, *data;
1091 
1092  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig);
1094  &method_data_type, data);
1095  data->recv = Qundef;
1096  data->id = orig->id;
1097  data->me = ALLOC(rb_method_entry_t);
1098  *data->me = *orig->me;
1099  if (orig->me->def) orig->me->def->alias_count++;
1100  data->rclass = orig->rclass;
1101  data->defined_class = orig->defined_class;
1102  data->ume = ALLOC(struct unlinked_method_entry_list_entry);
1103  OBJ_INFECT(method, obj);
1104 
1105  return method;
1106 }
1107 
1108 /*
1109  * call-seq:
1110  * meth.receiver -> object
1111  *
1112  * Returns the bound receiver of the method object.
1113  */
1114 
1115 static VALUE
1117 {
1118  struct METHOD *data;
1119 
1120  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
1121  return data->recv;
1122 }
1123 
1124 /*
1125  * call-seq:
1126  * meth.name -> symbol
1127  *
1128  * Returns the name of the method.
1129  */
1130 
1131 static VALUE
1133 {
1134  struct METHOD *data;
1135 
1136  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
1137  return ID2SYM(data->id);
1138 }
1139 
1140 /*
1141  * call-seq:
1142  * meth.owner -> class_or_module
1143  *
1144  * Returns the class or module that defines the method.
1145  */
1146 
1147 static VALUE
1149 {
1150  struct METHOD *data;
1152 
1153  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
1154  defined_class = data->defined_class;
1155 
1156  if (RB_TYPE_P(defined_class, T_ICLASS)) {
1157  defined_class = RBASIC(defined_class)->klass;
1158  }
1159 
1160  return defined_class;
1161 }
1162 
1163 void
1165 {
1166  const char *s0 = " class";
1167  VALUE c = klass;
1168 
1169  if (FL_TEST(c, FL_SINGLETON)) {
1170  VALUE obj = rb_ivar_get(klass, attached);
1171 
1172  switch (TYPE(obj)) {
1173  case T_MODULE:
1174  case T_CLASS:
1175  c = obj;
1176  s0 = "";
1177  }
1178  }
1179  else if (RB_TYPE_P(c, T_MODULE)) {
1180  s0 = " module";
1181  }
1182  rb_name_error_str(str, "undefined method `%"PRIsVALUE"' for%s `%"PRIsVALUE"'",
1183  QUOTE(str), s0, rb_class_name(c));
1184 }
1185 
1186 /*
1187  * call-seq:
1188  * obj.method(sym) -> method
1189  *
1190  * Looks up the named method as a receiver in <i>obj</i>, returning a
1191  * <code>Method</code> object (or raising <code>NameError</code>). The
1192  * <code>Method</code> object acts as a closure in <i>obj</i>'s object
1193  * instance, so instance variables and the value of <code>self</code>
1194  * remain available.
1195  *
1196  * class Demo
1197  * def initialize(n)
1198  * @iv = n
1199  * end
1200  * def hello()
1201  * "Hello, @iv = #{@iv}"
1202  * end
1203  * end
1204  *
1205  * k = Demo.new(99)
1206  * m = k.method(:hello)
1207  * m.call #=> "Hello, @iv = 99"
1208  *
1209  * l = Demo.new('Fred')
1210  * m = l.method("hello")
1211  * m.call #=> "Hello, @iv = Fred"
1212  */
1213 
1214 VALUE
1216 {
1217  ID id = rb_check_id(&vid);
1218  if (!id) {
1219  rb_method_name_error(CLASS_OF(obj), vid);
1220  }
1221  return mnew(CLASS_OF(obj), obj, id, rb_cMethod, FALSE);
1222 }
1223 
1224 /*
1225  * call-seq:
1226  * obj.public_method(sym) -> method
1227  *
1228  * Similar to _method_, searches public method only.
1229  */
1230 
1231 VALUE
1233 {
1234  ID id = rb_check_id(&vid);
1235  if (!id) {
1236  rb_method_name_error(CLASS_OF(obj), vid);
1237  }
1238  return mnew(CLASS_OF(obj), obj, id, rb_cMethod, TRUE);
1239 }
1240 
1241 /*
1242  * call-seq:
1243  * mod.instance_method(symbol) -> unbound_method
1244  *
1245  * Returns an +UnboundMethod+ representing the given
1246  * instance method in _mod_.
1247  *
1248  * class Interpreter
1249  * def do_a() print "there, "; end
1250  * def do_d() print "Hello "; end
1251  * def do_e() print "!\n"; end
1252  * def do_v() print "Dave"; end
1253  * Dispatcher = {
1254  * "a" => instance_method(:do_a),
1255  * "d" => instance_method(:do_d),
1256  * "e" => instance_method(:do_e),
1257  * "v" => instance_method(:do_v)
1258  * }
1259  * def interpret(string)
1260  * string.each_char {|b| Dispatcher[b].bind(self).call }
1261  * end
1262  * end
1263  *
1264  * interpreter = Interpreter.new
1265  * interpreter.interpret('dave')
1266  *
1267  * <em>produces:</em>
1268  *
1269  * Hello there, Dave!
1270  */
1271 
1272 static VALUE
1274 {
1275  ID id = rb_check_id(&vid);
1276  if (!id) {
1277  rb_method_name_error(mod, vid);
1278  }
1279  return mnew(mod, Qundef, id, rb_cUnboundMethod, FALSE);
1280 }
1281 
1282 /*
1283  * call-seq:
1284  * mod.public_instance_method(symbol) -> unbound_method
1285  *
1286  * Similar to _instance_method_, searches public method only.
1287  */
1288 
1289 static VALUE
1291 {
1292  ID id = rb_check_id(&vid);
1293  if (!id) {
1294  rb_method_name_error(mod, vid);
1295  }
1296  return mnew(mod, Qundef, id, rb_cUnboundMethod, TRUE);
1297 }
1298 
1299 /*
1300  * call-seq:
1301  * define_method(symbol, method) -> new_method
1302  * define_method(symbol) { block } -> proc
1303  *
1304  * Defines an instance method in the receiver. The _method_
1305  * parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
1306  * If a block is specified, it is used as the method body. This block
1307  * is evaluated using <code>instance_eval</code>, a point that is
1308  * tricky to demonstrate because <code>define_method</code> is private.
1309  * (This is why we resort to the +send+ hack in this example.)
1310  *
1311  * class A
1312  * def fred
1313  * puts "In Fred"
1314  * end
1315  * def create_method(name, &block)
1316  * self.class.send(:define_method, name, &block)
1317  * end
1318  * define_method(:wilma) { puts "Charge it!" }
1319  * end
1320  * class B < A
1321  * define_method(:barney, instance_method(:fred))
1322  * end
1323  * a = B.new
1324  * a.barney
1325  * a.wilma
1326  * a.create_method(:betty) { p self }
1327  * a.betty
1328  *
1329  * <em>produces:</em>
1330  *
1331  * In Fred
1332  * Charge it!
1333  * #<B:0x401b39e8>
1334  */
1335 
1336 static VALUE
1338 {
1339  ID id;
1340  VALUE body;
1341  int noex = NOEX_PUBLIC;
1342 
1343  if (argc == 1) {
1344  id = rb_to_id(argv[0]);
1345  body = rb_block_lambda();
1346  }
1347  else {
1348  rb_check_arity(argc, 1, 2);
1349  id = rb_to_id(argv[0]);
1350  body = argv[1];
1351  if (!rb_obj_is_method(body) && !rb_obj_is_proc(body)) {
1353  "wrong argument type %s (expected Proc/Method)",
1354  rb_obj_classname(body));
1355  }
1356  }
1357 
1358  if (rb_obj_is_method(body)) {
1359  struct METHOD *method = (struct METHOD *)DATA_PTR(body);
1360  VALUE rclass = method->rclass;
1361  if (rclass != mod && !RB_TYPE_P(rclass, T_MODULE) &&
1362  !RTEST(rb_class_inherited_p(mod, rclass))) {
1363  if (FL_TEST(rclass, FL_SINGLETON)) {
1365  "can't bind singleton method to a different class");
1366  }
1367  else {
1369  "bind argument must be a subclass of %s",
1370  rb_class2name(rclass));
1371  }
1372  }
1373  rb_method_entry_set(mod, id, method->me, noex);
1374  RB_GC_GUARD(body);
1375  }
1376  else if (rb_obj_is_proc(body)) {
1377  rb_proc_t *proc;
1378  body = proc_dup(body);
1379  GetProcPtr(body, proc);
1380  if (BUILTIN_TYPE(proc->block.iseq) != T_NODE) {
1381  proc->block.iseq->defined_method_id = id;
1382  proc->block.iseq->klass = mod;
1383  proc->is_lambda = TRUE;
1384  proc->is_from_method = TRUE;
1385  proc->block.klass = mod;
1386  }
1387  rb_add_method(mod, id, VM_METHOD_TYPE_BMETHOD, (void *)body, noex);
1388  }
1389  else {
1390  /* type error */
1391  rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)");
1392  }
1393 
1394  return body;
1395 }
1396 
1397 /*
1398  * call-seq:
1399  * define_singleton_method(symbol, method) -> new_method
1400  * define_singleton_method(symbol) { block } -> proc
1401  *
1402  * Defines a singleton method in the receiver. The _method_
1403  * parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
1404  * If a block is specified, it is used as the method body.
1405  *
1406  * class A
1407  * class << self
1408  * def class_name
1409  * to_s
1410  * end
1411  * end
1412  * end
1413  * A.define_singleton_method(:who_am_i) do
1414  * "I am: #{class_name}"
1415  * end
1416  * A.who_am_i # ==> "I am: A"
1417  *
1418  * guy = "Bob"
1419  * guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
1420  * guy.hello #=> "Bob: Hello there!"
1421  */
1422 
1423 static VALUE
1425 {
1426  VALUE klass = rb_singleton_class(obj);
1427 
1428  return rb_mod_define_method(argc, argv, klass);
1429 }
1430 
1431 /*
1432  * define_method(symbol, method) -> new_method
1433  * define_method(symbol) { block } -> proc
1434  *
1435  * Defines a global function by _method_ or the block.
1436  */
1437 
1438 static VALUE
1440 {
1441  rb_thread_t *th = GET_THREAD();
1442  VALUE klass;
1443 
1444  rb_secure(4);
1445  klass = th->top_wrapper;
1446  if (klass) {
1447  rb_warning("main.define_method in the wrapped load is effective only in wrapper module");
1448  }
1449  else {
1450  klass = rb_cObject;
1451  }
1452  return rb_mod_define_method(argc, argv, klass);
1453 }
1454 
1455 /*
1456  * call-seq:
1457  * method.clone -> new_method
1458  *
1459  * Returns a clone of this method.
1460  *
1461  * class A
1462  * def foo
1463  * return "bar"
1464  * end
1465  * end
1466  *
1467  * m = A.new.method(:foo)
1468  * m.call # => "bar"
1469  * n = m.clone.call # => "bar"
1470  */
1471 
1472 static VALUE
1474 {
1475  VALUE clone;
1476  struct METHOD *orig, *data;
1477 
1478  TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig);
1479  clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data);
1480  CLONESETUP(clone, self);
1481  *data = *orig;
1482  data->me = ALLOC(rb_method_entry_t);
1483  *data->me = *orig->me;
1484  if (data->me->def) data->me->def->alias_count++;
1485  data->ume = ALLOC(struct unlinked_method_entry_list_entry);
1486 
1487  return clone;
1488 }
1489 
1490 /*
1491  * call-seq:
1492  * meth.call(args, ...) -> obj
1493  * meth[args, ...] -> obj
1494  *
1495  * Invokes the <i>meth</i> with the specified arguments, returning the
1496  * method's return value.
1497  *
1498  * m = 12.method("+")
1499  * m.call(3) #=> 15
1500  * m.call(20) #=> 32
1501  */
1502 
1503 VALUE
1504 rb_method_call(int argc, VALUE *argv, VALUE method)
1505 {
1506  VALUE proc = rb_block_given_p() ? rb_block_proc() : Qnil;
1507  return rb_method_call_with_block(argc, argv, method, proc);
1508 }
1509 
1510 VALUE
1511 rb_method_call_with_block(int argc, VALUE *argv, VALUE method, VALUE pass_procval)
1512 {
1513  VALUE result = Qnil; /* OK */
1514  struct METHOD *data;
1515  int state;
1516  volatile int safe = -1;
1517 
1518  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
1519  if (data->recv == Qundef) {
1520  rb_raise(rb_eTypeError, "can't call unbound method; bind first");
1521  }
1522  PUSH_TAG();
1523  if (OBJ_TAINTED(method)) {
1524  const int safe_level_to_run = 4 /*SAFE_LEVEL_MAX*/;
1525  safe = rb_safe_level();
1526  if (rb_safe_level() < safe_level_to_run) {
1527  rb_set_safe_level_force(safe_level_to_run);
1528  }
1529  }
1530  if ((state = EXEC_TAG()) == 0) {
1531  rb_thread_t *th = GET_THREAD();
1532  rb_block_t *block = 0;
1534 
1535  if (!NIL_P(pass_procval)) {
1536  rb_proc_t *pass_proc;
1537  GetProcPtr(pass_procval, pass_proc);
1538  block = &pass_proc->block;
1539  }
1540 
1541  th->passed_block = block;
1542  defined_class = data->defined_class;
1543  if (BUILTIN_TYPE(defined_class) == T_MODULE) defined_class = data->rclass;
1544  result = rb_vm_call(th, data->recv, data->id, argc, argv, data->me, defined_class);
1545  }
1546  POP_TAG();
1547  if (safe >= 0)
1549  if (state)
1550  JUMP_TAG(state);
1551  return result;
1552 }
1553 
1554 /**********************************************************************
1555  *
1556  * Document-class: UnboundMethod
1557  *
1558  * Ruby supports two forms of objectified methods. Class
1559  * <code>Method</code> is used to represent methods that are associated
1560  * with a particular object: these method objects are bound to that
1561  * object. Bound method objects for an object can be created using
1562  * <code>Object#method</code>.
1563  *
1564  * Ruby also supports unbound methods; methods objects that are not
1565  * associated with a particular object. These can be created either by
1566  * calling <code>Module#instance_method</code> or by calling
1567  * <code>unbind</code> on a bound method object. The result of both of
1568  * these is an <code>UnboundMethod</code> object.
1569  *
1570  * Unbound methods can only be called after they are bound to an
1571  * object. That object must be be a kind_of? the method's original
1572  * class.
1573  *
1574  * class Square
1575  * def area
1576  * @side * @side
1577  * end
1578  * def initialize(side)
1579  * @side = side
1580  * end
1581  * end
1582  *
1583  * area_un = Square.instance_method(:area)
1584  *
1585  * s = Square.new(12)
1586  * area = area_un.bind(s)
1587  * area.call #=> 144
1588  *
1589  * Unbound methods are a reference to the method at the time it was
1590  * objectified: subsequent changes to the underlying class will not
1591  * affect the unbound method.
1592  *
1593  * class Test
1594  * def test
1595  * :original
1596  * end
1597  * end
1598  * um = Test.instance_method(:test)
1599  * class Test
1600  * def test
1601  * :modified
1602  * end
1603  * end
1604  * t = Test.new
1605  * t.test #=> :modified
1606  * um.bind(t).call #=> :original
1607  *
1608  */
1609 
1610 /*
1611  * call-seq:
1612  * umeth.bind(obj) -> method
1613  *
1614  * Bind <i>umeth</i> to <i>obj</i>. If <code>Klass</code> was the class
1615  * from which <i>umeth</i> was obtained,
1616  * <code>obj.kind_of?(Klass)</code> must be true.
1617  *
1618  * class A
1619  * def test
1620  * puts "In test, class = #{self.class}"
1621  * end
1622  * end
1623  * class B < A
1624  * end
1625  * class C < B
1626  * end
1627  *
1628  *
1629  * um = B.instance_method(:test)
1630  * bm = um.bind(C.new)
1631  * bm.call
1632  * bm = um.bind(B.new)
1633  * bm.call
1634  * bm = um.bind(A.new)
1635  * bm.call
1636  *
1637  * <em>produces:</em>
1638  *
1639  * In test, class = C
1640  * In test, class = B
1641  * prog.rb:16:in `bind': bind argument must be an instance of B (TypeError)
1642  * from prog.rb:16
1643  */
1644 
1645 static VALUE
1647 {
1648  struct METHOD *data, *bound;
1649  VALUE methclass;
1650  VALUE rclass;
1651 
1652  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
1653 
1654  methclass = data->rclass;
1655  if (!RB_TYPE_P(methclass, T_MODULE) &&
1656  methclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, methclass)) {
1657  if (FL_TEST(methclass, FL_SINGLETON)) {
1659  "singleton method called for a different object");
1660  }
1661  else {
1662  rb_raise(rb_eTypeError, "bind argument must be an instance of %s",
1663  rb_class2name(methclass));
1664  }
1665  }
1666 
1667  method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
1668  *bound = *data;
1669  bound->me = ALLOC(rb_method_entry_t);
1670  *bound->me = *data->me;
1671  if (bound->me->def) bound->me->def->alias_count++;
1672  rclass = CLASS_OF(recv);
1673  if (BUILTIN_TYPE(bound->defined_class) == T_MODULE) {
1674  VALUE ic = rb_class_search_ancestor(rclass, bound->defined_class);
1675  if (ic) {
1676  rclass = ic;
1677  }
1678  else {
1679  rclass = rb_include_class_new(methclass, rclass);
1680  }
1681  }
1682  bound->recv = recv;
1683  bound->rclass = rclass;
1684  data->ume = ALLOC(struct unlinked_method_entry_list_entry);
1685 
1686  return method;
1687 }
1688 
1689 /*
1690  * Returns the number of required parameters and stores the maximum
1691  * number of parameters in max, or UNLIMITED_ARGUMENTS
1692  * if there is no maximum.
1693  */
1694 static int
1696 {
1697  const rb_method_definition_t *def = me->def;
1698  if (!def) return *max = 0;
1699  switch (def->type) {
1700  case VM_METHOD_TYPE_CFUNC:
1701  if (def->body.cfunc.argc < 0) {
1702  *max = UNLIMITED_ARGUMENTS;
1703  return 0;
1704  }
1705  return *max = check_argc(def->body.cfunc.argc);
1706  case VM_METHOD_TYPE_ZSUPER:
1707  *max = UNLIMITED_ARGUMENTS;
1708  return 0;
1710  return *max = 1;
1711  case VM_METHOD_TYPE_IVAR:
1712  return *max = 0;
1714  return rb_proc_min_max_arity(def->body.proc, max);
1715  case VM_METHOD_TYPE_ISEQ: {
1716  rb_iseq_t *iseq = def->body.iseq;
1717  return rb_iseq_min_max_arity(iseq, max);
1718  }
1719  case VM_METHOD_TYPE_UNDEF:
1721  return *max = 0;
1723  *max = UNLIMITED_ARGUMENTS;
1724  return 0;
1725  case VM_METHOD_TYPE_OPTIMIZED: {
1726  switch (def->body.optimize_type) {
1727  case OPTIMIZED_METHOD_TYPE_SEND:
1728  *max = UNLIMITED_ARGUMENTS;
1729  return 0;
1730  default:
1731  break;
1732  }
1733  }
1735  *max = UNLIMITED_ARGUMENTS;
1736  return 0;
1737  }
1738  rb_bug("rb_method_entry_min_max_arity: invalid method entry type (%d)", def->type);
1739  UNREACHABLE;
1740 }
1741 
1742 int
1744 {
1745  int max, min = rb_method_entry_min_max_arity(me, &max);
1746  return min == max ? min : -min-1;
1747 }
1748 
1749 /*
1750  * call-seq:
1751  * meth.arity -> fixnum
1752  *
1753  * Returns an indication of the number of arguments accepted by a
1754  * method. Returns a nonnegative integer for methods that take a fixed
1755  * number of arguments. For Ruby methods that take a variable number of
1756  * arguments, returns -n-1, where n is the number of required
1757  * arguments. For methods written in C, returns -1 if the call takes a
1758  * variable number of arguments.
1759  *
1760  * class C
1761  * def one; end
1762  * def two(a); end
1763  * def three(*a); end
1764  * def four(a, b); end
1765  * def five(a, b, *c); end
1766  * def six(a, b, *c, &d); end
1767  * end
1768  * c = C.new
1769  * c.method(:one).arity #=> 0
1770  * c.method(:two).arity #=> 1
1771  * c.method(:three).arity #=> -1
1772  * c.method(:four).arity #=> 2
1773  * c.method(:five).arity #=> -3
1774  * c.method(:six).arity #=> -3
1775  *
1776  * "cat".method(:size).arity #=> 0
1777  * "cat".method(:replace).arity #=> 1
1778  * "cat".method(:squeeze).arity #=> -1
1779  * "cat".method(:count).arity #=> -1
1780  */
1781 
1782 static VALUE
1784 {
1785  int n = method_arity(method);
1786  return INT2FIX(n);
1787 }
1788 
1789 static int
1791 {
1792  struct METHOD *data;
1793 
1794  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
1795  return rb_method_entry_arity(data->me);
1796 }
1797 
1798 static rb_method_entry_t *
1800 {
1801  VALUE rclass;
1803  while ((me = rb_method_entry(mod, id, &rclass)) != 0) {
1804  rb_method_definition_t *def = me->def;
1805  if (!def) break;
1806  if (def->type != VM_METHOD_TYPE_ZSUPER) break;
1807  mod = RCLASS_SUPER(rclass);
1808  id = def->original_id;
1809  }
1810  return me;
1811 }
1812 
1813 static int
1815 {
1816  struct METHOD *data;
1817 
1818  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
1819  return rb_method_entry_min_max_arity(data->me, max);
1820 }
1821 
1822 int
1824 {
1826  if (!me) return 0; /* should raise? */
1827  return rb_method_entry_arity(me);
1828 }
1829 
1830 int
1832 {
1833  return rb_mod_method_arity(CLASS_OF(obj), id);
1834 }
1835 
1836 static inline rb_method_definition_t *
1838 {
1839  struct METHOD *data;
1840 
1841  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
1842  return data->me->def;
1843 }
1844 
1845 static rb_iseq_t *
1847 {
1848  switch (def->type) {
1850  return get_proc_iseq(def->body.proc, 0);
1851  case VM_METHOD_TYPE_ISEQ:
1852  return def->body.iseq;
1853  default:
1854  return 0;
1855  }
1856 }
1857 
1858 rb_iseq_t *
1860 {
1861  return method_get_iseq(method_get_def(method));
1862 }
1863 
1864 static VALUE
1866 {
1867  if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
1868  if (!def->body.attr.location)
1869  return Qnil;
1870  return rb_ary_dup(def->body.attr.location);
1871  }
1872  return iseq_location(method_get_iseq(def));
1873 }
1874 
1875 VALUE
1877 {
1878  if (!me || !me->def) return Qnil;
1879  return method_def_location(me->def);
1880 }
1881 
1882 VALUE
1884 {
1886  return rb_method_entry_location(me);
1887 }
1888 
1889 VALUE
1891 {
1892  return rb_mod_method_location(CLASS_OF(obj), id);
1893 }
1894 
1895 /*
1896  * call-seq:
1897  * meth.source_location -> [String, Fixnum]
1898  *
1899  * Returns the Ruby source filename and line number containing this method
1900  * or nil if this method was not defined in Ruby (i.e. native)
1901  */
1902 
1903 VALUE
1905 {
1906  rb_method_definition_t *def = method_get_def(method);
1907  return method_def_location(def);
1908 }
1909 
1910 /*
1911  * call-seq:
1912  * meth.parameters -> array
1913  *
1914  * Returns the parameter information of this method.
1915  */
1916 
1917 static VALUE
1919 {
1920  rb_iseq_t *iseq = rb_method_get_iseq(method);
1921  if (!iseq) {
1922  return unnamed_parameters(method_arity(method));
1923  }
1924  return rb_iseq_parameters(iseq, 0);
1925 }
1926 
1927 /*
1928  * call-seq:
1929  * meth.to_s -> string
1930  * meth.inspect -> string
1931  *
1932  * Returns the name of the underlying method.
1933  *
1934  * "cat".method(:count).inspect #=> "#<Method: String#count>"
1935  */
1936 
1937 static VALUE
1939 {
1940  struct METHOD *data;
1941  VALUE str;
1942  const char *s;
1943  const char *sharp = "#";
1944 
1945  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
1946  str = rb_str_buf_new2("#<");
1947  s = rb_obj_classname(method);
1948  rb_str_buf_cat2(str, s);
1949  rb_str_buf_cat2(str, ": ");
1950 
1951  if (FL_TEST(data->me->klass, FL_SINGLETON)) {
1952  VALUE v = rb_ivar_get(data->me->klass, attached);
1953 
1954  if (data->recv == Qundef) {
1955  rb_str_buf_append(str, rb_inspect(data->me->klass));
1956  }
1957  else if (data->recv == v) {
1958  rb_str_buf_append(str, rb_inspect(v));
1959  sharp = ".";
1960  }
1961  else {
1962  rb_str_buf_append(str, rb_inspect(data->recv));
1963  rb_str_buf_cat2(str, "(");
1964  rb_str_buf_append(str, rb_inspect(v));
1965  rb_str_buf_cat2(str, ")");
1966  sharp = ".";
1967  }
1968  }
1969  else {
1970  rb_str_buf_cat2(str, rb_class2name(data->rclass));
1971  if (data->rclass != data->me->klass) {
1972  rb_str_buf_cat2(str, "(");
1973  rb_str_buf_cat2(str, rb_class2name(data->me->klass));
1974  rb_str_buf_cat2(str, ")");
1975  }
1976  }
1977  rb_str_buf_cat2(str, sharp);
1978  rb_str_append(str, rb_id2str(data->me->def->original_id));
1979  if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
1980  rb_str_buf_cat2(str, " (not-implemented)");
1981  }
1982  rb_str_buf_cat2(str, ">");
1983 
1984  return str;
1985 }
1986 
1987 static VALUE
1988 mproc(VALUE method)
1989 {
1990  return rb_funcall2(rb_mRubyVMFrozenCore, idProc, 0, 0);
1991 }
1992 
1993 static VALUE
1995 {
1996  return rb_funcall(rb_mRubyVMFrozenCore, idLambda, 0, 0);
1997 }
1998 
1999 static VALUE
2000 bmcall(VALUE args, VALUE method, int argc, VALUE *argv, VALUE passed_proc)
2001 {
2002  volatile VALUE a;
2003  VALUE ret;
2004 
2005  if (CLASS_OF(args) != rb_cArray) {
2006  args = rb_ary_new3(1, args);
2007  argc = 1;
2008  }
2009  else {
2010  argc = check_argc(RARRAY_LEN(args));
2011  }
2012  ret = rb_method_call_with_block(argc, RARRAY_PTR(args), method, passed_proc);
2013  RB_GC_GUARD(a) = args;
2014  return ret;
2015 }
2016 
2017 VALUE
2019  VALUE (*func)(ANYARGS), /* VALUE yieldarg[, VALUE procarg] */
2020  VALUE val)
2021 {
2022  VALUE procval = rb_iterate(mproc, 0, func, val);
2023  return procval;
2024 }
2025 
2026 /*
2027  * call-seq:
2028  * meth.to_proc -> prc
2029  *
2030  * Returns a <code>Proc</code> object corresponding to this method.
2031  */
2032 
2033 static VALUE
2035 {
2036  VALUE procval;
2037  struct METHOD *meth;
2038  rb_proc_t *proc;
2039  rb_env_t *env;
2040 
2041  /*
2042  * class Method
2043  * def to_proc
2044  * proc{|*args|
2045  * self.call(*args)
2046  * }
2047  * end
2048  * end
2049  */
2050  TypedData_Get_Struct(method, struct METHOD, &method_data_type, meth);
2051  procval = rb_iterate(mlambda, 0, bmcall, method);
2052  GetProcPtr(procval, proc);
2053  proc->is_from_method = 1;
2054  proc->block.self = meth->recv;
2055  proc->block.klass = meth->defined_class;
2056  GetEnvPtr(proc->envval, env);
2057  env->block.self = meth->recv;
2058  env->block.klass = meth->defined_class;
2059  env->block.iseq = method_get_iseq(meth->me->def);
2060  return procval;
2061 }
2062 
2063 /*
2064  * call_seq:
2065  * local_jump_error.exit_value -> obj
2066  *
2067  * Returns the exit value associated with this +LocalJumpError+.
2068  */
2069 static VALUE
2071 {
2072  return rb_iv_get(exc, "@exit_value");
2073 }
2074 
2075 /*
2076  * call-seq:
2077  * local_jump_error.reason -> symbol
2078  *
2079  * The reason this block was terminated:
2080  * :break, :redo, :retry, :next, :return, or :noreason.
2081  */
2082 
2083 static VALUE
2085 {
2086  return rb_iv_get(exc, "@reason");
2087 }
2088 
2089 /*
2090  * call-seq:
2091  * prc.binding -> binding
2092  *
2093  * Returns the binding associated with <i>prc</i>. Note that
2094  * <code>Kernel#eval</code> accepts either a <code>Proc</code> or a
2095  * <code>Binding</code> object as its second parameter.
2096  *
2097  * def fred(param)
2098  * proc {}
2099  * end
2100  *
2101  * b = fred(99)
2102  * eval("param", b.binding) #=> 99
2103  */
2104 static VALUE
2106 {
2107  rb_proc_t *proc;
2108  VALUE bindval;
2109  rb_binding_t *bind;
2110 
2111  GetProcPtr(self, proc);
2112  if (RB_TYPE_P((VALUE)proc->block.iseq, T_NODE)) {
2113  if (!IS_METHOD_PROC_NODE((NODE *)proc->block.iseq)) {
2114  rb_raise(rb_eArgError, "Can't create Binding from C level Proc");
2115  }
2116  }
2117 
2118  bindval = rb_binding_alloc(rb_cBinding);
2119  GetBindingPtr(bindval, bind);
2120  bind->env = proc->envval;
2121  bind->blockprocval = proc->blockprocval;
2122  if (RUBY_VM_NORMAL_ISEQ_P(proc->block.iseq)) {
2123  bind->path = proc->block.iseq->location.path;
2125  }
2126  else {
2127  bind->path = Qnil;
2128  bind->first_lineno = 0;
2129  }
2130  return bindval;
2131 }
2132 
2133 static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc);
2134 
2135 static VALUE
2136 make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
2137 {
2138  VALUE args = rb_ary_new3(3, proc, passed, arity);
2139  rb_proc_t *procp;
2140  int is_lambda;
2141 
2142  GetProcPtr(proc, procp);
2143  is_lambda = procp->is_lambda;
2144  rb_ary_freeze(passed);
2145  rb_ary_freeze(args);
2146  proc = rb_proc_new(curry, args);
2147  GetProcPtr(proc, procp);
2148  procp->is_lambda = is_lambda;
2149  return proc;
2150 }
2151 
2152 static VALUE
2153 curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
2154 {
2155  VALUE proc, passed, arity;
2156  proc = RARRAY_PTR(args)[0];
2157  passed = RARRAY_PTR(args)[1];
2158  arity = RARRAY_PTR(args)[2];
2159 
2160  passed = rb_ary_plus(passed, rb_ary_new4(argc, argv));
2161  rb_ary_freeze(passed);
2162 
2163  if (RARRAY_LEN(passed) < FIX2INT(arity)) {
2164  if (!NIL_P(passed_proc)) {
2165  rb_warn("given block not used");
2166  }
2167  arity = make_curry_proc(proc, passed, arity);
2168  return arity;
2169  }
2170  else {
2171  return rb_proc_call_with_block(proc, check_argc(RARRAY_LEN(passed)),
2172  RARRAY_PTR(passed), passed_proc);
2173  }
2174 }
2175 
2176  /*
2177  * call-seq:
2178  * prc.curry -> a_proc
2179  * prc.curry(arity) -> a_proc
2180  *
2181  * Returns a curried proc. If the optional <i>arity</i> argument is given,
2182  * it determines the number of arguments.
2183  * A curried proc receives some arguments. If a sufficient number of
2184  * arguments are supplied, it passes the supplied arguments to the original
2185  * proc and returns the result. Otherwise, returns another curried proc that
2186  * takes the rest of arguments.
2187  *
2188  * b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
2189  * p b.curry[1][2][3] #=> 6
2190  * p b.curry[1, 2][3, 4] #=> 6
2191  * p b.curry(5)[1][2][3][4][5] #=> 6
2192  * p b.curry(5)[1, 2][3, 4][5] #=> 6
2193  * p b.curry(1)[1] #=> 1
2194  *
2195  * b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
2196  * p b.curry[1][2][3] #=> 6
2197  * p b.curry[1, 2][3, 4] #=> 10
2198  * p b.curry(5)[1][2][3][4][5] #=> 15
2199  * p b.curry(5)[1, 2][3, 4][5] #=> 15
2200  * p b.curry(1)[1] #=> 1
2201  *
2202  * b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
2203  * p b.curry[1][2][3] #=> 6
2204  * p b.curry[1, 2][3, 4] #=> wrong number of arguments (4 for 3)
2205  * p b.curry(5) #=> wrong number of arguments (5 for 3)
2206  * p b.curry(1) #=> wrong number of arguments (1 for 3)
2207  *
2208  * b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
2209  * p b.curry[1][2][3] #=> 6
2210  * p b.curry[1, 2][3, 4] #=> 10
2211  * p b.curry(5)[1][2][3][4][5] #=> 15
2212  * p b.curry(5)[1, 2][3, 4][5] #=> 15
2213  * p b.curry(1) #=> wrong number of arguments (1 for 3)
2214  *
2215  * b = proc { :foo }
2216  * p b.curry[] #=> :foo
2217  */
2218 static VALUE
2219 proc_curry(int argc, VALUE *argv, VALUE self)
2220 {
2221  int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
2222  VALUE arity;
2223 
2224  rb_scan_args(argc, argv, "01", &arity);
2225  if (NIL_P(arity)) {
2226  arity = INT2FIX(min_arity);
2227  }
2228  else {
2229  sarity = FIX2INT(arity);
2230  if (rb_proc_lambda_p(self)) {
2231  rb_check_arity(sarity, min_arity, max_arity);
2232  }
2233  }
2234 
2235  return make_curry_proc(self, rb_ary_new(), arity);
2236 }
2237 
2238 /*
2239  * Document-class: LocalJumpError
2240  *
2241  * Raised when Ruby can't yield as requested.
2242  *
2243  * A typical scenario is attempting to yield when no block is given:
2244  *
2245  * def call_block
2246  * yield 42
2247  * end
2248  * call_block
2249  *
2250  * <em>raises the exception:</em>
2251  *
2252  * LocalJumpError: no block given (yield)
2253  *
2254  * A more subtle example:
2255  *
2256  * def get_me_a_return
2257  * Proc.new { return 42 }
2258  * end
2259  * get_me_a_return.call
2260  *
2261  * <em>raises the exception:</em>
2262  *
2263  * LocalJumpError: unexpected return
2264  */
2265 
2266 /*
2267  * Document-class: SystemStackError
2268  *
2269  * Raised in case of a stack overflow.
2270  *
2271  * def me_myself_and_i
2272  * me_myself_and_i
2273  * end
2274  * me_myself_and_i
2275  *
2276  * <em>raises the exception:</em>
2277  *
2278  * SystemStackError: stack level too deep
2279  */
2280 
2281 /*
2282  * <code>Proc</code> objects are blocks of code that have been bound to
2283  * a set of local variables. Once bound, the code may be called in
2284  * different contexts and still access those variables.
2285  *
2286  * def gen_times(factor)
2287  * return Proc.new {|n| n*factor }
2288  * end
2289  *
2290  * times3 = gen_times(3)
2291  * times5 = gen_times(5)
2292  *
2293  * times3.call(12) #=> 36
2294  * times5.call(5) #=> 25
2295  * times3.call(times5.call(4)) #=> 60
2296  *
2297  */
2298 
2299 void
2301 {
2302  /* Proc */
2303  rb_cProc = rb_define_class("Proc", rb_cObject);
2306 
2307 #if 0 /* incomplete. */
2309  (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
2311  (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
2313  (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
2315  (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
2316 #else
2317  rb_define_method(rb_cProc, "call", proc_call, -1);
2318  rb_define_method(rb_cProc, "[]", proc_call, -1);
2319  rb_define_method(rb_cProc, "===", proc_call, -1);
2320  rb_define_method(rb_cProc, "yield", proc_call, -1);
2321 #endif
2322  rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0);
2323  rb_define_method(rb_cProc, "arity", proc_arity, 0);
2324  rb_define_method(rb_cProc, "clone", proc_clone, 0);
2325  rb_define_method(rb_cProc, "dup", proc_dup, 0);
2326  rb_define_method(rb_cProc, "hash", proc_hash, 0);
2327  rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
2328  rb_define_alias(rb_cProc, "inspect", "to_s");
2329  rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0);
2330  rb_define_method(rb_cProc, "binding", proc_binding, 0);
2331  rb_define_method(rb_cProc, "curry", proc_curry, -1);
2332  rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);
2333  rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0);
2334 
2335  /* Exceptions */
2339 
2340  rb_eSysStackError = rb_define_class("SystemStackError", rb_eException);
2342  rb_obj_freeze(rb_str_new2("stack level too deep")));
2344 
2345  /* utility functions */
2348 
2349  /* Method */
2350  rb_cMethod = rb_define_class("Method", rb_cObject);
2354  rb_define_method(rb_cMethod, "eql?", method_eq, 1);
2360  rb_define_method(rb_cMethod, "inspect", method_inspect, 0);
2362  rb_define_method(rb_cMethod, "to_proc", method_proc, 0);
2363  rb_define_method(rb_cMethod, "receiver", method_receiver, 0);
2366  rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
2367  rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
2369  rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
2370  rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
2371 
2372  /* UnboundMethod */
2373  rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject);
2386  rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);
2388 
2389  /* Module#*_method */
2390  rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);
2391  rb_define_method(rb_cModule, "public_instance_method", rb_mod_public_instance_method, 1);
2393 
2394  /* Kernel */
2395  rb_define_method(rb_mKernel, "define_singleton_method", rb_obj_define_method, -1);
2396 
2398  "define_method", top_define_method, -1);
2399 }
2400 
2401 /*
2402  * Objects of class <code>Binding</code> encapsulate the execution
2403  * context at some particular place in the code and retain this context
2404  * for future use. The variables, methods, value of <code>self</code>,
2405  * and possibly an iterator block that can be accessed in this context
2406  * are all retained. Binding objects can be created using
2407  * <code>Kernel#binding</code>, and are made available to the callback
2408  * of <code>Kernel#set_trace_func</code>.
2409  *
2410  * These binding objects can be passed as the second argument of the
2411  * <code>Kernel#eval</code> method, establishing an environment for the
2412  * evaluation.
2413  *
2414  * class Demo
2415  * def initialize(n)
2416  * @secret = n
2417  * end
2418  * def get_binding
2419  * return binding()
2420  * end
2421  * end
2422  *
2423  * k1 = Demo.new(99)
2424  * b1 = k1.get_binding
2425  * k2 = Demo.new(-3)
2426  * b2 = k2.get_binding
2427  *
2428  * eval("@secret", b1) #=> 99
2429  * eval("@secret", b2) #=> -3
2430  * eval("@secret") #=> nil
2431  *
2432  * Binding objects have no class-specific methods.
2433  *
2434  */
2435 
2436 void
2438 {
2439  rb_cBinding = rb_define_class("Binding", rb_cObject);
2444  rb_define_method(rb_cBinding, "eval", bind_eval, -1);
2445  rb_define_global_function("binding", rb_f_binding, 0);
2446  attached = rb_intern("__attached__");
2447 }
2448 
const rb_block_t * passed_block
Definition: vm_core.h:511
int is_from_method
Definition: vm_core.h:674
struct unlinked_method_entry_list_entry * next
Definition: method.h:104
static void bm_free(void *ptr)
Definition: proc.c:877
static VALUE method_name(VALUE obj)
Definition: proc.c:1132
rb_control_frame_t * cfp
Definition: vm_core.h:500
char mark
Definition: method.h:97
VALUE rb_eStandardError
Definition: error.c:514
VALUE rb_eLocalJumpError
Definition: eval.c:29
static int method_min_max_arity(VALUE, int *max)
Definition: proc.c:1814
static VALUE rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
Definition: proc.c:1424
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:108
static size_t binding_memsize(const void *ptr)
Definition: proc.c:268
VALUE rb_proc_alloc(VALUE klass)
Definition: proc.c:84
static VALUE method_arity_m(VALUE method)
Definition: proc.c:1783
VALUE rb_ary_new4(long n, const VALUE *elts)
Definition: array.c:451
VALUE rb_obj_public_method(VALUE obj, VALUE vid)
Definition: proc.c:1232
#define RARRAY_LEN(a)
Definition: ruby.h:899
void rb_bug(const char *fmt,...)
Definition: error.c:295
rb_method_type_t type
Definition: method.h:77
static VALUE proc_to_proc(VALUE self)
Definition: proc.c:861
#define FALSE
Definition: nkf.h:174
static rb_method_entry_t * original_method_entry(VALUE mod, ID id)
Definition: proc.c:1799
rb_method_attr_t attr
Definition: method.h:82
static VALUE proc_hash(VALUE self)
Definition: proc.c:800
#define RUBY_VM_IFUNC_P(ptr)
Definition: vm_core.h:798
static const rb_data_type_t proc_data_type
Definition: proc.c:74
VALUE rb_ary_freeze(VALUE ary)
Definition: array.c:330
static VALUE bind_eval(int argc, VALUE *argv, VALUE bindval)
Definition: proc.c:367
static VALUE umethod_bind(VALUE method, VALUE recv)
Definition: proc.c:1646
static VALUE method_proc(VALUE method)
Definition: proc.c:2034
VALUE rb_id2str(ID id)
Definition: ripper.c:16946
VALUE rb_method_entry_location(rb_method_entry_t *me)
Definition: proc.c:1876
static int max(int a, int b)
Definition: strftime.c:141
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:493
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1497
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:789
static VALUE method_def_location(rb_method_definition_t *def)
Definition: proc.c:1865
#define GetProcPtr(obj, ptr)
Definition: vm_core.h:665
VALUE rb_method_call_with_block(int argc, VALUE *argv, VALUE method, VALUE pass_procval)
Definition: proc.c:1511
rb_method_flag_t flag
Definition: method.h:96
#define RUBY_VM_NORMAL_ISEQ_P(ptr)
Definition: vm_core.h:799
#define CLASS_OF(v)
Definition: ruby.h:448
rb_block_t block
Definition: vm_core.h:686
#define T_MODULE
Definition: ruby.h:488
int rb_mod_method_arity(VALUE mod, ID id)
Definition: proc.c:1823
#define Qtrue
Definition: ruby.h:434
rb_iseq_t * iseq
Definition: vm_core.h:446
static int rb_method_entry_min_max_arity(const rb_method_entry_t *me, int *max)
Definition: proc.c:1695
st_index_t rb_hash_end(st_index_t)
static void proc_mark(void *ptr)
Definition: proc.c:51
rb_method_entry_t * rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
Definition: vm_method.c:653
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1030
const int id
Definition: nkf.c:209
static rb_iseq_t * method_get_iseq(rb_method_definition_t *def)
Definition: proc.c:1846
rb_method_entry_t * me
Definition: proc.c:22
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1356
VALUE rb_iterate(VALUE(*)(VALUE), VALUE, VALUE(*)(ANYARGS), VALUE)
Definition: vm_eval.c:1032
void Init_Binding(void)
Definition: proc.c:2437
#define sysstack_error
Definition: vm_core.h:868
static VALUE localjump_reason(VALUE exc)
Definition: proc.c:2084
VALUE rb_eTypeError
Definition: error.c:516
struct unlinked_method_entry_list_entry * ume
Definition: proc.c:23
rb_method_flag_t
Definition: method.h:22
#define UNREACHABLE
Definition: ruby.h:40
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
VALUE rb_str_buf_new2(const char *)
static int rb_proc_min_max_arity(VALUE self, int *max)
Definition: proc.c:658
VALUE rb_f_lambda(void)
Definition: proc.c:478
VALUE rb_cBinding
Definition: proc.c:28
static VALUE method_clone(VALUE self)
Definition: proc.c:1473
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:773
VALUE rb_vm_make_proc(rb_thread_t *th, const rb_block_t *block, VALUE klass)
Definition: vm.c:609
VALUE rb_proc_lambda_p(VALUE procval)
Definition: proc.c:231
union rb_method_definition_struct::@90 body
#define IS_METHOD_PROC_NODE(node)
Definition: proc.c:38
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1788
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1116
#define RUBY_MARK_LEAVE(msg)
Definition: gc.h:54
ID called_id
Definition: method.h:99
#define RB_GC_GUARD(v)
Definition: ruby.h:530
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:593
VALUE rb_obj_method_location(VALUE obj, ID id)
Definition: proc.c:1890
static VALUE iseq_location(rb_iseq_t *iseq)
Definition: proc.c:713
static VALUE unnamed_parameters(int arity)
Definition: proc.c:743
VALUE rb_ary_new3(long n,...)
Definition: array.c:432
VALUE rb_cUnboundMethod
Definition: proc.c:26
#define DATA_PTR(dta)
Definition: ruby.h:985
void rb_gc_mark(VALUE ptr)
Definition: gc.c:2600
void rb_method_name_error(VALUE klass, VALUE str)
Definition: proc.c:1164
ID defined_method_id
Definition: vm_core.h:308
void Init_Proc(void)
Definition: proc.c:2300
static VALUE proc_clone(VALUE self)
Definition: proc.c:122
st_data_t st_index_t
Definition: st.h:63
#define GetEnvPtr(obj, ptr)
Definition: vm_core.h:678
#define PUSH_TAG()
Definition: eval_intern.h:136
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1526
VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, int argc, const VALUE *argv, const rb_block_t *blockptr)
Definition: vm.c:780
VALUE env
Definition: vm_core.h:693
static VALUE method_inspect(VALUE method)
Definition: proc.c:1938
ID rb_check_id(volatile VALUE *namep)
Returns ID for the given name if it is interned already, or 0.
Definition: ripper.c:17106
static VALUE rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
Definition: proc.c:442
int arg_post_len
Definition: vm_core.h:269
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1362
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2109
static VALUE proc_dup(VALUE self)
Definition: proc.c:103
static size_t bm_memsize(const void *ptr)
Definition: proc.c:889
int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
Definition: vm_method.c:1121
void rb_mark_method_entry(const rb_method_entry_t *me)
Definition: gc.c:2449
#define OBJ_TAINTED(x)
Definition: ruby.h:1153
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
void rb_name_error_str(VALUE str, const char *fmt,...)
Definition: error.c:919
VALUE rb_vm_call(rb_thread_t *th, VALUE recv, VALUE id, int argc, const VALUE *argv, const rb_method_entry_t *me, VALUE defined_class)
Definition: vm_eval.c:243
VALUE envval
Definition: vm_core.h:671
#define sym(x)
Definition: date_core.c:3715
VALUE rb_proc_new(VALUE(*func)(ANYARGS), VALUE val)
Definition: proc.c:2018
static VALUE method_hash(VALUE method)
Definition: proc.c:1063
void rb_name_error(ID id, const char *fmt,...)
Definition: error.c:904
Definition: node.h:239
static VALUE method_eq(VALUE method, VALUE other)
Definition: proc.c:1033
static int method_arity(VALUE)
Definition: proc.c:1790
#define FL_SINGLETON
Definition: ruby.h:1111
int args
Definition: win32ole.c:785
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1470
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
rb_method_cfunc_t cfunc
Definition: method.h:81
#define FL_TEST(x, f)
Definition: ruby.h:1146
unsigned short first_lineno
Definition: vm_core.h:696
VALUE rb_obj_is_method(VALUE m)
Definition: proc.c:904
VALUE rb_class_name(VALUE)
Definition: variable.c:383
static VALUE rb_mod_instance_method(VALUE mod, VALUE vid)
Definition: proc.c:1273
int rb_block_given_p(void)
Definition: eval.c:672
#define EXEC_TAG()
Definition: eval_intern.h:141
int rb_iseq_first_lineno(const rb_iseq_t *iseq)
Definition: iseq.c:1058
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1426
VALUE rb_eSysStackError
Definition: eval.c:30
Definition: proc.c:17
static VALUE proc_curry(int argc, VALUE *argv, VALUE self)
Definition: proc.c:2219
VALUE defined_class
Definition: proc.c:20
#define GetBindingPtr(obj, ptr)
Definition: vm_core.h:689
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:478
VALUE rb_ary_new(void)
Definition: array.c:424
VALUE rb_str_buf_cat2(VALUE, const char *)
Definition: string.c:1961
VALUE rb_binding_new_with_cfp(rb_thread_t *th, const rb_control_frame_t *src_cfp)
Definition: proc.c:316
int argc
argument information
Definition: vm_core.h:264
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1414
VALUE rb_iv_get(VALUE, const char *)
Definition: variable.c:2586
#define JUMP_TAG(st)
Definition: eval_intern.h:148
#define NIL_P(v)
Definition: ruby.h:446
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:488
static VALUE proc_arity(VALUE self)
Definition: proc.c:636
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:719
static VALUE rb_proc_parameters(VALUE self)
Definition: proc.c:772
#define RUBY_MARK_ENTER(msg)
Definition: gc.h:53
VALUE rb_class_inherited_p(VALUE, VALUE)
Definition: object.c:1503
#define TYPE(x)
Definition: ruby.h:513
int argc
Definition: ruby.c:130
#define Qfalse
Definition: ruby.h:433
#define CLONESETUP(clone, obj)
Definition: ruby.h:689
VALUE rb_binding_new(void)
Definition: proc.c:322
Definition: method.h:95
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1445
VALUE rb_method_call(int argc, VALUE *argv, VALUE method)
Definition: proc.c:1504
#define T_NODE
Definition: ruby.h:506
#define get_proc_iseq
Definition: proc.c:689
#define OBJ_FREEZE(x)
Definition: ruby.h:1164
#define POP_TAG()
Definition: eval_intern.h:137
static VALUE rb_method_parameters(VALUE method)
Definition: proc.c:1918
VALUE rb_block_proc(void)
Definition: proc.c:458
static VALUE rb_f_binding(VALUE self)
Definition: proc.c:345
rb_method_entry_t * rb_method_entry(VALUE klass, ID id, VALUE *define_class_ptr)
Definition: vm_method.c:572
VALUE rb_vm_top_self()
Definition: vm.c:2494
VALUE klass
Definition: method.h:100
#define ALLOC(type)
Definition: ruby.h:1224
static VALUE rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
Definition: proc.c:1337
VALUE rb_block_lambda(void)
Definition: proc.c:472
VALUE rb_mod_method_location(VALUE mod, ID id)
Definition: proc.c:1883
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE)
Definition: proc.c:2000
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
#define TRUE
Definition: nkf.h:175
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:804
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1272
ID id
Definition: proc.c:21
static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
Definition: proc.c:2153
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:652
#define const
Definition: strftime.c:102
VALUE blockprocval
Definition: vm_core.h:695
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_flag_t noex)
Definition: vm_method.c:405
static void binding_free(void *ptr)
Definition: proc.c:242
void ruby_xfree(void *x)
Definition: gc.c:3653
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
static VALUE rb_mod_public_instance_method(VALUE mod, VALUE vid)
Definition: proc.c:1290
#define PRIsVALUE
Definition: ruby.h:147
unsigned long ID
Definition: ruby.h:105
VALUE rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp)
Definition: vm.c:647
#define Qnil
Definition: ruby.h:435
st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
Definition: vm_method.c:1203
VALUE rb_cMethod
Definition: proc.c:27
#define BUILTIN_TYPE(x)
Definition: ruby.h:510
int rb_obj_method_arity(VALUE obj, ID id)
Definition: proc.c:1831
#define OBJ_TAINT(x)
Definition: ruby.h:1154
unsigned long VALUE
Definition: ruby.h:104
static VALUE proc_binding(VALUE self)
Definition: proc.c:2105
static VALUE binding_dup(VALUE self)
Definition: proc.c:293
static VALUE result
Definition: nkf.c:40
#define RBASIC(obj)
Definition: ruby.h:1094
const char * rb_class2name(VALUE)
Definition: variable.c:389
#define FIX2INT(x)
Definition: ruby.h:624
rb_iseq_location_t location
Definition: vm_core.h:213
static VALUE method_owner(VALUE obj)
Definition: proc.c:1148
static VALUE method_receiver(VALUE obj)
Definition: proc.c:1116
static VALUE proc_new(VALUE klass, int is_lambda)
Definition: proc.c:377
static VALUE make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
Definition: proc.c:2136
void xfree(void *)
static VALUE mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
Definition: proc.c:915
static ID attached
Definition: proc.c:34
VALUE blockprocval
Definition: vm_core.h:672
void rb_set_safe_level_force(int)
Definition: safe.c:34
#define RSTRING_PTR(str)
Definition: ruby.h:866
static VALUE mlambda(VALUE method)
Definition: proc.c:1994
VALUE rb_mRubyVMFrozenCore
Definition: vm.c:92
int is_lambda
Definition: vm_core.h:675
VALUE rb_proc_location(VALUE self)
Definition: proc.c:737
#define rb_check_arity(argc, min, max)
Definition: intern.h:277
#define INT2FIX(i)
Definition: ruby.h:241
VALUE top_wrapper
Definition: vm_core.h:521
#define UNLIMITED_ARGUMENTS
Definition: intern.h:54
int safe_level
Definition: vm_core.h:673
#define RCLASS_SUPER(c)
Definition: classext.h:16
st_index_t rb_hash_proc(st_index_t hash, VALUE prc)
Definition: proc.c:783
rb_block_t block
Definition: vm_core.h:669
static void bm_mark(void *ptr)
Definition: proc.c:867
VALUE klass
Definition: vm_core.h:305
VALUE rb_method_location(VALUE method)
Definition: proc.c:1904
VALUE rb_exc_new3(VALUE etype, VALUE str)
Definition: error.c:553
VALUE rb_ary_plus(VALUE x, VALUE y)
Definition: array.c:3341
rb_method_definition_t * def
Definition: method.h:98
#define ANYARGS
Definition: defines.h:57
#define RUBY_FREE_LEAVE(msg)
Definition: gc.h:56
#define RARRAY_PTR(a)
Definition: ruby.h:904
#define RUBY_FREE_ENTER(msg)
Definition: gc.h:55
VALUE rb_proc_call(VALUE self, VALUE args)
Definition: proc.c:571
#define LONG2FIX(i)
Definition: ruby.h:242
#define RTEST(v)
Definition: ruby.h:445
#define OBJ_INFECT(x, s)
Definition: ruby.h:1157
st_index_t rb_hash_uint(st_index_t, st_index_t)
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1532
static VALUE proc_call(int argc, VALUE *argv, VALUE procval)
Definition: proc.c:532
v
Definition: win32ole.c:798
#define RUBY_MARK_UNLESS_NULL(ptr)
Definition: gc.h:60
rb_block_t * rb_vm_control_frame_block_ptr(rb_control_frame_t *cfp)
Definition: vm.c:61
struct iseq_line_info_entry * line_info_table
Definition: vm_core.h:222
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1019
VALUE rb_ary_dup(VALUE ary)
Definition: array.c:1766
VALUE rb_cArray
Definition: array.c:29
VALUE rb_obj_method(VALUE obj, VALUE vid)
Definition: proc.c:1215
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
static const rb_data_type_t binding_data_type
Definition: proc.c:273
VALUE rb_ary_new2(long capa)
Definition: array.c:417
#define T_CLASS
Definition: ruby.h:486
static VALUE binding_clone(VALUE self)
Definition: proc.c:308
#define rb_safe_level()
Definition: tcltklib.c:94
rb_iseq_t * rb_method_get_iseq(VALUE method)
Definition: proc.c:1859
VALUE rb_f_eval(int argc, VALUE *argv, VALUE self)
Definition: vm_eval.c:1313
#define ID2SYM(x)
Definition: ruby.h:363
static VALUE proc_to_s(VALUE self)
Definition: proc.c:818
const char * rb_id2name(ID id)
Definition: ripper.c:17012
static const rb_data_type_t method_data_type
Definition: proc.c:894
int rb_proc_arity(VALUE self)
Definition: proc.c:681
rb_method_entry_t * me
Definition: method.h:105
VALUE rb_inspect(VALUE)
Definition: object.c:411
#define check_argc(argc)
Definition: proc.c:567
void rb_warning(const char *fmt,...)
Definition: error.c:234
static VALUE localjump_xvalue(VALUE exc)
Definition: proc.c:2070
void rb_secure(int)
Definition: safe.c:79
#define QUOTE(str)
Definition: internal.h:287
rb_method_entry_t * rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_flag_t noex)
Definition: vm_method.c:478
#define CONST_ID(var, str)
Definition: ruby.h:1318
void rb_print_undef(VALUE klass, ID id, int scope)
Definition: eval_error.c:207
VALUE rclass
Definition: proc.c:19
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1012
static rb_method_definition_t * method_get_def(VALUE method)
Definition: proc.c:1837
rb_iseq_t * rb_proc_get_iseq(VALUE self, int *is_proc)
Definition: proc.c:692
#define rb_intern(str)
static void proc_free(void *ptr)
Definition: proc.c:41
static VALUE top_define_method(int argc, VALUE *argv, VALUE obj)
Definition: proc.c:1439
#define mod(x, y)
Definition: date_strftime.c:28
static void binding_mark(void *ptr)
Definition: proc.c:254
VALUE path
Definition: vm_core.h:694
#define env
#define Qundef
Definition: ruby.h:436
#define T_ICLASS
Definition: ruby.h:487
VALUE rb_class_search_ancestor(VALUE klass, VALUE super)
Definition: object.c:613
enum rb_method_definition_struct::@90::method_optimized_type optimize_type
void rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
Definition: eval.c:1233
static rb_thread_t * GET_THREAD(void)
Definition: vm_core.h:890
VALUE recv
Definition: proc.c:18
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2125
static size_t proc_memsize(const void *ptr)
Definition: proc.c:69
static int rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max)
Definition: proc.c:643
VALUE rb_str_new2(const char *)
VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
Definition: iseq.c:1930
void rb_warn(const char *fmt,...)
Definition: error.c:221
#define Check_TypedStruct(v, t)
Definition: ruby.h:1001
ID rb_to_id(VALUE)
Definition: string.c:8172
VALUE rb_eArgError
Definition: error.c:517
VALUE rb_binding_alloc(VALUE klass)
Definition: proc.c:283
static VALUE mproc(VALUE method)
Definition: proc.c:1988
VALUE rb_proc_call_with_block(VALUE self, int argc, VALUE *argv, VALUE pass_procval)
Definition: proc.c:584
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1416
Definition: method.h:103
VALUE rb_obj_is_proc(VALUE proc)
Definition: proc.c:91
int rb_method_entry_arity(const rb_method_entry_t *me)
Definition: proc.c:1743
char ** argv
Definition: ruby.c:131
VALUE rb_cProc
Definition: proc.c:29
VALUE * ep
Definition: vm_core.h:445
static VALUE method_unbind(VALUE obj)
Definition: proc.c:1087
VALUE rb_eException
Definition: error.c:509
#define GET_VM()
Definition: vm_core.h:883