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