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