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