Ruby  2.4.2p198(2017-09-14revision59899)
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 
17 /* Proc.new with no block will raise an exception in the future
18  * versions */
19 #define PROC_NEW_REQUIRES_BLOCK 0
20 
21 #if !defined(__GNUC__) || __GNUC__ < 5 || defined(__MINGW32__)
22 # define NO_CLOBBERED(v) (*(volatile VALUE *)&(v))
23 #else
24 # define NO_CLOBBERED(v) (v)
25 #endif
26 
27 const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
28 
29 struct METHOD {
30  const VALUE recv;
31  const VALUE klass;
32  const rb_method_entry_t * const me;
33  /* for bound methods, `me' should be rb_callable_method_entry_t * */
34 };
35 
40 
41 static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE);
42 static int method_arity(VALUE);
43 static int method_min_max_arity(VALUE, int *max);
44 
45 #define attached id__attached__
46 
47 /* Proc */
48 
49 #define IS_METHOD_PROC_IFUNC(ifunc) ((ifunc)->func == bmcall)
50 
51 static VALUE proc_to_s_(VALUE self, const rb_proc_t *proc);
52 
53 static void
54 block_mark(const struct rb_block *block)
55 {
56  switch (vm_block_type(block)) {
57  case block_type_iseq:
58  case block_type_ifunc:
59  {
60  const struct rb_captured_block *captured = &block->as.captured;
61  RUBY_MARK_UNLESS_NULL(captured->self);
62  RUBY_MARK_UNLESS_NULL((VALUE)captured->code.val);
63  if (captured->ep && captured->ep[VM_ENV_DATA_INDEX_ENV] != Qundef /* cfunc_proc_t */) {
65  }
66  }
67  break;
68  case block_type_symbol:
70  break;
71  case block_type_proc:
73  break;
74  }
75 }
76 
77 static void
78 proc_mark(void *ptr)
79 {
80  rb_proc_t *proc = ptr;
81  block_mark(&proc->block);
82  RUBY_MARK_LEAVE("proc");
83 }
84 
85 typedef struct {
87  VALUE env[VM_ENV_DATA_SIZE + 1]; /* ..., envval */
88 } cfunc_proc_t;
89 
90 static size_t
91 proc_memsize(const void *ptr)
92 {
93  const rb_proc_t *proc = ptr;
94  if (proc->block.as.captured.ep == ((const cfunc_proc_t *)ptr)->env+1)
95  return sizeof(cfunc_proc_t);
96  return sizeof(rb_proc_t);
97 }
98 
100  "proc",
101  {
102  proc_mark,
104  proc_memsize,
105  },
107 };
108 
109 VALUE
111 {
112  rb_proc_t *proc;
113  return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
114 }
115 
116 VALUE
118 {
119  if (rb_typeddata_is_kind_of(proc, &proc_data_type)) {
120  return Qtrue;
121  }
122  else {
123  return Qfalse;
124  }
125 }
126 
127 VALUE rb_proc_create(VALUE klass, const struct rb_block *block,
128  int8_t safe_level, int8_t is_from_method, int8_t is_lambda);
129 
130 /* :nodoc: */
131 static VALUE
133 {
134  VALUE procval;
135  rb_proc_t *src;
136 
137  GetProcPtr(self, src);
138  procval = rb_proc_create(rb_cProc, &src->block,
139  src->safe_level, src->is_from_method, src->is_lambda);
140  RB_GC_GUARD(self); /* for: body = proc_dup(body) */
141  return procval;
142 }
143 
144 /* :nodoc: */
145 static VALUE
147 {
148  VALUE procval = proc_dup(self);
149  CLONESETUP(procval, self);
150  return procval;
151 }
152 
153 /*
154  * call-seq:
155  * prc.lambda? -> true or false
156  *
157  * Returns +true+ for a Proc object for which argument handling is rigid.
158  * Such procs are typically generated by +lambda+.
159  *
160  * A Proc object generated by +proc+ ignores extra arguments.
161  *
162  * proc {|a,b| [a,b] }.call(1,2,3) #=> [1,2]
163  *
164  * It provides +nil+ for missing arguments.
165  *
166  * proc {|a,b| [a,b] }.call(1) #=> [1,nil]
167  *
168  * It expands a single array argument.
169  *
170  * proc {|a,b| [a,b] }.call([1,2]) #=> [1,2]
171  *
172  * A Proc object generated by +lambda+ doesn't have such tricks.
173  *
174  * lambda {|a,b| [a,b] }.call(1,2,3) #=> ArgumentError
175  * lambda {|a,b| [a,b] }.call(1) #=> ArgumentError
176  * lambda {|a,b| [a,b] }.call([1,2]) #=> ArgumentError
177  *
178  * Proc#lambda? is a predicate for the tricks.
179  * It returns +true+ if no tricks apply.
180  *
181  * lambda {}.lambda? #=> true
182  * proc {}.lambda? #=> false
183  *
184  * Proc.new is the same as +proc+.
185  *
186  * Proc.new {}.lambda? #=> false
187  *
188  * +lambda+, +proc+ and Proc.new preserve the tricks of
189  * a Proc object given by <code>&</code> argument.
190  *
191  * lambda(&lambda {}).lambda? #=> true
192  * proc(&lambda {}).lambda? #=> true
193  * Proc.new(&lambda {}).lambda? #=> true
194  *
195  * lambda(&proc {}).lambda? #=> false
196  * proc(&proc {}).lambda? #=> false
197  * Proc.new(&proc {}).lambda? #=> false
198  *
199  * A Proc object generated by <code>&</code> argument has the tricks
200  *
201  * def n(&b) b.lambda? end
202  * n {} #=> false
203  *
204  * The <code>&</code> argument preserves the tricks if a Proc object
205  * is given by <code>&</code> argument.
206  *
207  * n(&lambda {}) #=> true
208  * n(&proc {}) #=> false
209  * n(&Proc.new {}) #=> false
210  *
211  * A Proc object converted from a method has no tricks.
212  *
213  * def m() end
214  * method(:m).to_proc.lambda? #=> true
215  *
216  * n(&method(:m)) #=> true
217  * n(&method(:m).to_proc) #=> true
218  *
219  * +define_method+ is treated the same as method definition.
220  * The defined method has no tricks.
221  *
222  * class C
223  * define_method(:d) {}
224  * end
225  * C.new.d(1,2) #=> ArgumentError
226  * C.new.method(:d).to_proc.lambda? #=> true
227  *
228  * +define_method+ always defines a method without the tricks,
229  * even if a non-lambda Proc object is given.
230  * This is the only exception for which the tricks are not preserved.
231  *
232  * class C
233  * define_method(:e, &proc {})
234  * end
235  * C.new.e(1,2) #=> ArgumentError
236  * C.new.method(:e).to_proc.lambda? #=> true
237  *
238  * This exception ensures that methods never have tricks
239  * and makes it easy to have wrappers to define methods that behave as usual.
240  *
241  * class C
242  * def self.def2(name, &body)
243  * define_method(name, &body)
244  * end
245  *
246  * def2(:f) {}
247  * end
248  * C.new.f(1,2) #=> ArgumentError
249  *
250  * The wrapper <i>def2</i> defines a method which has no tricks.
251  *
252  */
253 
254 VALUE
256 {
257  rb_proc_t *proc;
258  GetProcPtr(procval, proc);
259 
260  return proc->is_lambda ? Qtrue : Qfalse;
261 }
262 
263 /* Binding */
264 
265 static void
266 binding_free(void *ptr)
267 {
268  rb_binding_t *bind;
269  RUBY_FREE_ENTER("binding");
270  if (ptr) {
271  bind = ptr;
272  ruby_xfree(bind);
273  }
274  RUBY_FREE_LEAVE("binding");
275 }
276 
277 static void
278 binding_mark(void *ptr)
279 {
280  rb_binding_t *bind = ptr;
281 
282  RUBY_MARK_ENTER("binding");
283 
284  block_mark(&bind->block);
285 
287 
288  RUBY_MARK_LEAVE("binding");
289 }
290 
291 static size_t
292 binding_memsize(const void *ptr)
293 {
294  return sizeof(rb_binding_t);
295 }
296 
298  "binding",
299  {
300  binding_mark,
301  binding_free,
303  },
305 };
306 
307 VALUE
309 {
310  VALUE obj;
311  rb_binding_t *bind;
312  obj = TypedData_Make_Struct(klass, rb_binding_t, &ruby_binding_data_type, bind);
313  return obj;
314 }
315 
316 /* :nodoc: */
317 static VALUE
319 {
321  rb_binding_t *src, *dst;
322  GetBindingPtr(self, src);
323  GetBindingPtr(bindval, dst);
324  dst->block = src->block;
325  dst->path = src->path;
326  dst->first_lineno = src->first_lineno;
327  return bindval;
328 }
329 
330 /* :nodoc: */
331 static VALUE
333 {
334  VALUE bindval = binding_dup(self);
335  CLONESETUP(bindval, self);
336  return bindval;
337 }
338 
339 VALUE
341 {
342  rb_thread_t *th = GET_THREAD();
343  return rb_vm_make_binding(th, th->cfp);
344 }
345 
346 /*
347  * call-seq:
348  * binding -> a_binding
349  *
350  * Returns a +Binding+ object, describing the variable and
351  * method bindings at the point of call. This object can be used when
352  * calling +eval+ to execute the evaluated command in this
353  * environment. See also the description of class +Binding+.
354  *
355  * def get_binding(param)
356  * binding
357  * end
358  * b = get_binding("hello")
359  * eval("param", b) #=> "hello"
360  */
361 
362 static VALUE
364 {
365  return rb_binding_new();
366 }
367 
368 /*
369  * call-seq:
370  * binding.eval(string [, filename [,lineno]]) -> obj
371  *
372  * Evaluates the Ruby expression(s) in <em>string</em>, in the
373  * <em>binding</em>'s context. If the optional <em>filename</em> and
374  * <em>lineno</em> parameters are present, they will be used when
375  * reporting syntax errors.
376  *
377  * def get_binding(param)
378  * binding
379  * end
380  * b = get_binding("hello")
381  * b.eval("param") #=> "hello"
382  */
383 
384 static VALUE
385 bind_eval(int argc, VALUE *argv, VALUE bindval)
386 {
387  VALUE args[4];
388 
389  rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
390  args[1] = bindval;
391  return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */);
392 }
393 
394 static const VALUE *
396 {
397  const rb_env_t *env = *envp;
398  do {
399  if (!VM_ENV_FLAGS(env->ep, VM_FRAME_FLAG_CFRAME)) {
400  const rb_iseq_t *iseq = env->iseq;
401  unsigned int i;
402 
404 
405  for (i=0; i<iseq->body->local_table_size; i++) {
406  if (iseq->body->local_table[i] == lid) {
407  *envp = env;
408  return &env->env[i];
409  }
410  }
411  }
412  else {
413  *envp = NULL;
414  return NULL;
415  }
416  } while ((env = rb_vm_env_prev_env(env)) != NULL);
417 
418  *envp = NULL;
419  return NULL;
420 }
421 
422 /*
423  * check local variable name.
424  * returns ID if it's an already interned symbol, or 0 with setting
425  * local name in String to *namep.
426  */
427 static ID
428 check_local_id(VALUE bindval, volatile VALUE *pname)
429 {
430  ID lid = rb_check_id(pname);
431  VALUE name = *pname;
432 
433  if (lid) {
434  if (!rb_is_local_id(lid)) {
435  rb_name_err_raise("wrong local variable name `%1$s' for %2$s",
436  bindval, ID2SYM(lid));
437  }
438  }
439  else {
440  if (!rb_is_local_name(name)) {
441  rb_name_err_raise("wrong local variable name `%1$s' for %2$s",
442  bindval, name);
443  }
444  return 0;
445  }
446  return lid;
447 }
448 
449 /*
450  * call-seq:
451  * binding.local_variables -> Array
452  *
453  * Returns the names of the binding's local variables as symbols.
454  *
455  * def foo
456  * a = 1
457  * 2.times do |n|
458  * binding.local_variables #=> [:a, :n]
459  * end
460  * end
461  *
462  * This method is the short version of the following code:
463  *
464  * binding.eval("local_variables")
465  *
466  */
467 static VALUE
469 {
470  const rb_binding_t *bind;
471  const rb_env_t *env;
472 
473  GetBindingPtr(bindval, bind);
474  env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
475  return rb_vm_env_local_variables(env);
476 }
477 
478 /*
479  * call-seq:
480  * binding.local_variable_get(symbol) -> obj
481  *
482  * Returns the value of the local variable +symbol+.
483  *
484  * def foo
485  * a = 1
486  * binding.local_variable_get(:a) #=> 1
487  * binding.local_variable_get(:b) #=> NameError
488  * end
489  *
490  * This method is the short version of the following code:
491  *
492  * binding.eval("#{symbol}")
493  *
494  */
495 static VALUE
497 {
498  ID lid = check_local_id(bindval, &sym);
499  const rb_binding_t *bind;
500  const VALUE *ptr;
501  const rb_env_t *env;
502 
503  if (!lid) goto undefined;
504 
505  GetBindingPtr(bindval, bind);
506 
507  env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
508  if ((ptr = get_local_variable_ptr(&env, lid)) == NULL) {
509  sym = ID2SYM(lid);
510  undefined:
511  rb_name_err_raise("local variable `%1$s' not defined for %2$s",
512  bindval, sym);
513  }
514 
515  return *ptr;
516 }
517 
518 /*
519  * call-seq:
520  * binding.local_variable_set(symbol, obj) -> obj
521  *
522  * Set local variable named +symbol+ as +obj+.
523  *
524  * def foo
525  * a = 1
526  * bind = binding
527  * bind.local_variable_set(:a, 2) # set existing local variable `a'
528  * bind.local_variable_set(:b, 3) # create new local variable `b'
529  * # `b' exists only in binding
530  *
531  * p bind.local_variable_get(:a) #=> 2
532  * p bind.local_variable_get(:b) #=> 3
533  * p a #=> 2
534  * p b #=> NameError
535  * end
536  *
537  * This method behaves similarly to the following code:
538  *
539  * binding.eval("#{symbol} = #{obj}")
540  *
541  * if +obj+ can be dumped in Ruby code.
542  */
543 static VALUE
545 {
546  ID lid = check_local_id(bindval, &sym);
547  rb_binding_t *bind;
548  const VALUE *ptr;
549  const rb_env_t *env;
550 
551  if (!lid) lid = rb_intern_str(sym);
552 
553  GetBindingPtr(bindval, bind);
554  env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
555  if ((ptr = get_local_variable_ptr(&env, lid)) == NULL) {
556  /* not found. create new env */
557  ptr = rb_binding_add_dynavars(bind, 1, &lid);
558  env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
559  }
560 
561  RB_OBJ_WRITE(env, ptr, val);
562 
563  return val;
564 }
565 
566 /*
567  * call-seq:
568  * binding.local_variable_defined?(symbol) -> obj
569  *
570  * Returns +true+ if a local variable +symbol+ exists.
571  *
572  * def foo
573  * a = 1
574  * binding.local_variable_defined?(:a) #=> true
575  * binding.local_variable_defined?(:b) #=> false
576  * end
577  *
578  * This method is the short version of the following code:
579  *
580  * binding.eval("defined?(#{symbol}) == 'local-variable'")
581  *
582  */
583 static VALUE
585 {
586  ID lid = check_local_id(bindval, &sym);
587  const rb_binding_t *bind;
588  const rb_env_t *env;
589 
590  if (!lid) return Qfalse;
591 
592  GetBindingPtr(bindval, bind);
593  env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
594  return get_local_variable_ptr(&env, lid) ? Qtrue : Qfalse;
595 }
596 
597 /*
598  * call-seq:
599  * binding.receiver -> object
600  *
601  * Returns the bound receiver of the binding object.
602  */
603 static VALUE
605 {
606  const rb_binding_t *bind;
607  GetBindingPtr(bindval, bind);
608  return vm_block_self(&bind->block);
609 }
610 
611 static VALUE
612 cfunc_proc_new(VALUE klass, VALUE ifunc, int8_t is_lambda)
613 {
614  rb_proc_t *proc;
615  cfunc_proc_t *sproc;
616  VALUE procval = TypedData_Make_Struct(klass, cfunc_proc_t, &proc_data_type, sproc);
617  VALUE *ep;
618 
619  proc = &sproc->basic;
621 
622  *(VALUE **)&proc->block.as.captured.ep = ep = sproc->env + VM_ENV_DATA_SIZE-1;
626  ep[VM_ENV_DATA_INDEX_ENV] = Qundef; /* envval */
627 
628  /* self? */
629  RB_OBJ_WRITE(procval, &proc->block.as.captured.code.ifunc, ifunc);
630  proc->is_lambda = is_lambda;
631  return procval;
632 }
633 
634 static VALUE
636 {
637  VALUE procval = rb_proc_alloc(klass);
638  rb_proc_t *proc;
639  GetProcPtr(procval, proc);
640 
642  RB_OBJ_WRITE(procval, &proc->block.as.symbol, sym);
643  return procval;
644 }
645 
646 struct vm_ifunc *
647 rb_vm_ifunc_new(VALUE (*func)(ANYARGS), const void *data, int min_argc, int max_argc)
648 {
649  union {
650  struct vm_ifunc_argc argc;
651  VALUE packed;
652  } arity;
653 
654  if (min_argc < UNLIMITED_ARGUMENTS ||
655 #if SIZEOF_INT * 2 > SIZEOF_VALUE
656  min_argc >= (int)(1U << (SIZEOF_VALUE * CHAR_BIT) / 2) ||
657 #endif
658  0) {
659  rb_raise(rb_eRangeError, "minimum argument number out of range: %d",
660  min_argc);
661  }
662  if (max_argc < UNLIMITED_ARGUMENTS ||
663 #if SIZEOF_INT * 2 > SIZEOF_VALUE
664  max_argc >= (int)(1U << (SIZEOF_VALUE * CHAR_BIT) / 2) ||
665 #endif
666  0) {
667  rb_raise(rb_eRangeError, "maximum argument number out of range: %d",
668  max_argc);
669  }
670  arity.argc.min = min_argc;
671  arity.argc.max = max_argc;
672  return IFUNC_NEW(func, data, arity.packed);
673 }
674 
675 VALUE
677 {
678  struct vm_ifunc *ifunc = rb_vm_ifunc_proc_new(func, (void *)val);
679  return cfunc_proc_new(rb_cProc, (VALUE)ifunc, 0);
680 }
681 
682 VALUE
683 rb_func_lambda_new(rb_block_call_func_t func, VALUE val, int min_argc, int max_argc)
684 {
685  struct vm_ifunc *ifunc = rb_vm_ifunc_new(func, (void *)val, min_argc, max_argc);
686  return cfunc_proc_new(rb_cProc, (VALUE)ifunc, 1);
687 }
688 
689 static const char proc_without_block[] = "tried to create Proc object without a block";
690 
691 static VALUE
692 proc_new(VALUE klass, int8_t is_lambda)
693 {
694  VALUE procval;
695  rb_thread_t *th = GET_THREAD();
696  rb_control_frame_t *cfp = th->cfp;
697  VALUE block_handler;
698 
699  if ((block_handler = rb_vm_frame_block_handler(cfp)) == VM_BLOCK_HANDLER_NONE) {
700 #if !PROC_NEW_REQUIRES_BLOCK
702 
703  if ((block_handler = rb_vm_frame_block_handler(cfp)) != VM_BLOCK_HANDLER_NONE) {
704  const VALUE *lep = rb_vm_ep_local_ep(cfp->ep);
705 
706  if (VM_ENV_ESCAPED_P(lep)) {
707  procval = VM_ENV_PROCVAL(lep);
708  goto return_existing_proc;
709  }
710 
711  if (is_lambda) {
712  rb_warn(proc_without_block);
713  }
714  }
715 #else
716  if (0)
717 #endif
718  else {
719  rb_raise(rb_eArgError, proc_without_block);
720  }
721  }
722 
723  /* block is in cf */
724  switch (vm_block_handler_type(block_handler)) {
726  procval = VM_BH_TO_PROC(block_handler);
727 
728  return_existing_proc:
729  if (RBASIC_CLASS(procval) == klass) {
730  return procval;
731  }
732  else {
733  VALUE newprocval = proc_dup(procval);
734  RBASIC_SET_CLASS(newprocval, klass);
735  return newprocval;
736  }
737  break;
738 
740  return (klass != rb_cProc) ?
741  sym_proc_new(klass, VM_BH_TO_SYMBOL(block_handler)) :
742  rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
743  break;
744 
747  return rb_vm_make_proc_lambda(th, VM_BH_TO_CAPT_BLOCK(block_handler), klass, is_lambda);
748  }
750  return Qnil;
751 }
752 
753 /*
754  * call-seq:
755  * Proc.new {|...| block } -> a_proc
756  * Proc.new -> a_proc
757  *
758  * Creates a new <code>Proc</code> object, bound to the current
759  * context. <code>Proc::new</code> may be called without a block only
760  * within a method with an attached block, in which case that block is
761  * converted to the <code>Proc</code> object.
762  *
763  * def proc_from
764  * Proc.new
765  * end
766  * proc = proc_from { "hello" }
767  * proc.call #=> "hello"
768  */
769 
770 static VALUE
772 {
773  VALUE block = proc_new(klass, FALSE);
774 
775  rb_obj_call_init(block, argc, argv);
776  return block;
777 }
778 
779 /*
780  * call-seq:
781  * proc { |...| block } -> a_proc
782  *
783  * Equivalent to <code>Proc.new</code>.
784  */
785 
786 VALUE
788 {
789  return proc_new(rb_cProc, FALSE);
790 }
791 
792 /*
793  * call-seq:
794  * lambda { |...| block } -> a_proc
795  *
796  * Equivalent to <code>Proc.new</code>, except the resulting Proc objects
797  * check the number of parameters passed when called.
798  */
799 
800 VALUE
802 {
803  return proc_new(rb_cProc, TRUE);
804 }
805 
806 /* Document-method: ===
807  *
808  * call-seq:
809  * proc === obj -> result_of_proc
810  *
811  * Invokes the block with +obj+ as the proc's parameter like Proc#call. It
812  * is to allow a proc object to be a target of +when+ clause in a case
813  * statement.
814  */
815 
816 /* CHECKME: are the argument checking semantics correct? */
817 
818 /*
819  * Document-method: []
820  * Document-method: call
821  * Document-method: yield
822  *
823  * call-seq:
824  * prc.call(params,...) -> obj
825  * prc[params,...] -> obj
826  * prc.(params,...) -> obj
827  * prc.yield(params,...) -> obj
828  *
829  * Invokes the block, setting the block's parameters to the values in
830  * <i>params</i> using something close to method calling semantics.
831  * Returns the value of the last expression evaluated in the block.
832  *
833  * a_proc = Proc.new {|scalar, *values| values.map {|value| value*scalar } }
834  * a_proc.call(9, 1, 2, 3) #=> [9, 18, 27]
835  * a_proc[9, 1, 2, 3] #=> [9, 18, 27]
836  * a_proc.(9, 1, 2, 3) #=> [9, 18, 27]
837  * a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27]
838  *
839  * Note that <code>prc.()</code> invokes <code>prc.call()</code> with
840  * the parameters given. It's syntactic sugar to hide "call".
841  *
842  * For procs created using <code>lambda</code> or <code>->()</code> an error
843  * is generated if the wrong number of parameters are passed to the proc.
844  * For procs created using <code>Proc.new</code> or <code>Kernel.proc</code>,
845  * extra parameters are silently discarded and missing parameters are
846  * set to +nil+.
847  *
848  * a_proc = proc {|a,b| [a,b] }
849  * a_proc.call(1) #=> [1, nil]
850  *
851  * a_proc = lambda {|a,b| [a,b] }
852  * a_proc.call(1) # ArgumentError: wrong number of arguments (given 1, expected 2)
853  *
854  * See also Proc#lambda?.
855  */
856 #if 0
857 static VALUE
858 proc_call(int argc, VALUE *argv, VALUE procval)
859 {
860  /* removed */
861 }
862 #endif
863 
864 #if SIZEOF_LONG > SIZEOF_INT
865 static inline int
866 check_argc(long argc)
867 {
868  if (argc > INT_MAX || argc < 0) {
869  rb_raise(rb_eArgError, "too many arguments (%lu)",
870  (unsigned long)argc);
871  }
872  return (int)argc;
873 }
874 #else
875 #define check_argc(argc) (argc)
876 #endif
877 
878 VALUE
880 {
881  VALUE vret;
882  rb_proc_t *proc;
883  GetProcPtr(self, proc);
884  vret = rb_vm_invoke_proc(GET_THREAD(), proc,
885  check_argc(RARRAY_LEN(args)), RARRAY_CONST_PTR(args),
887  RB_GC_GUARD(self);
888  RB_GC_GUARD(args);
889  return vret;
890 }
891 
892 static VALUE
894 {
895  return NIL_P(procval) ? VM_BLOCK_HANDLER_NONE : procval;
896 }
897 
898 VALUE
899 rb_proc_call_with_block(VALUE self, int argc, const VALUE *argv, VALUE passed_procval)
900 {
901  rb_thread_t *th = GET_THREAD();
902  VALUE vret;
903  rb_proc_t *proc;
904  GetProcPtr(self, proc);
905  vret = rb_vm_invoke_proc(th, proc, argc, argv, proc_to_block_handler(passed_procval));
906  RB_GC_GUARD(self);
907  return vret;
908 }
909 
910 
911 /*
912  * call-seq:
913  * prc.arity -> integer
914  *
915  * Returns the number of mandatory arguments. If the block
916  * is declared to take no arguments, returns 0. If the block is known
917  * to take exactly n arguments, returns n.
918  * If the block has optional arguments, returns -n-1, where n is the
919  * number of mandatory arguments, with the exception for blocks that
920  * are not lambdas and have only a finite number of optional arguments;
921  * in this latter case, returns n.
922  * Keywords arguments will considered as a single additional argument,
923  * that argument being mandatory if any keyword argument is mandatory.
924  * A <code>proc</code> with no argument declarations
925  * is the same as a block declaring <code>||</code> as its arguments.
926  *
927  * proc {}.arity #=> 0
928  * proc { || }.arity #=> 0
929  * proc { |a| }.arity #=> 1
930  * proc { |a, b| }.arity #=> 2
931  * proc { |a, b, c| }.arity #=> 3
932  * proc { |*a| }.arity #=> -1
933  * proc { |a, *b| }.arity #=> -2
934  * proc { |a, *b, c| }.arity #=> -3
935  * proc { |x:, y:, z:0| }.arity #=> 1
936  * proc { |*a, x:, y:0| }.arity #=> -2
937  *
938  * proc { |x=0| }.arity #=> 0
939  * lambda { |x=0| }.arity #=> -1
940  * proc { |x=0, y| }.arity #=> 1
941  * lambda { |x=0, y| }.arity #=> -2
942  * proc { |x=0, y=0| }.arity #=> 0
943  * lambda { |x=0, y=0| }.arity #=> -1
944  * proc { |x, y=0| }.arity #=> 1
945  * lambda { |x, y=0| }.arity #=> -2
946  * proc { |(x, y), z=0| }.arity #=> 1
947  * lambda { |(x, y), z=0| }.arity #=> -2
948  * proc { |a, x:0, y:0| }.arity #=> 1
949  * lambda { |a, x:0, y:0| }.arity #=> -2
950  */
951 
952 static VALUE
954 {
955  int arity = rb_proc_arity(self);
956  return INT2FIX(arity);
957 }
958 
959 static inline int
961 {
962  *max = iseq->body->param.flags.has_rest == FALSE ?
963  iseq->body->param.lead_num + iseq->body->param.opt_num + iseq->body->param.post_num +
964  (iseq->body->param.flags.has_kw == TRUE || iseq->body->param.flags.has_kwrest == TRUE)
966  return iseq->body->param.lead_num + iseq->body->param.post_num + (iseq->body->param.flags.has_kw && iseq->body->param.keyword->required_num > 0);
967 }
968 
969 static int
970 rb_vm_block_min_max_arity(const struct rb_block *block, int *max)
971 {
972  switch (vm_block_type(block)) {
973  case block_type_iseq:
974  return rb_iseq_min_max_arity(block->as.captured.code.iseq, max);
975  case block_type_proc:
976  return rb_vm_block_min_max_arity(vm_proc_block(block->as.proc), max);
977  case block_type_ifunc:
978  {
979  const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
980  if (IS_METHOD_PROC_IFUNC(ifunc)) {
981  /* e.g. method(:foo).to_proc.arity */
982  return method_min_max_arity((VALUE)ifunc->data, max);
983  }
984  *max = ifunc->argc.max;
985  return ifunc->argc.min;
986  }
987  case block_type_symbol:
988  break;
989  }
990  *max = UNLIMITED_ARGUMENTS;
991  return 0;
992 }
993 
994 /*
995  * Returns the number of required parameters and stores the maximum
996  * number of parameters in max, or UNLIMITED_ARGUMENTS if no max.
997  * For non-lambda procs, the maximum is the number of non-ignored
998  * parameters even though there is no actual limit to the number of parameters
999  */
1000 static int
1002 {
1003  rb_proc_t *proc;
1004  GetProcPtr(self, proc);
1005  return rb_vm_block_min_max_arity(&proc->block, max);
1006 }
1007 
1008 int
1010 {
1011  rb_proc_t *proc;
1012  int max, min;
1013  GetProcPtr(self, proc);
1014  min = rb_vm_block_min_max_arity(&proc->block, &max);
1015  return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1;
1016 }
1017 
1018 static void
1019 block_setup(struct rb_block *block, VALUE block_handler)
1020 {
1021  switch (vm_block_handler_type(block_handler)) {
1023  block->type = block_type_iseq;
1024  block->as.captured = *VM_BH_TO_ISEQ_BLOCK(block_handler);
1025  break;
1027  block->type = block_type_ifunc;
1028  block->as.captured = *VM_BH_TO_IFUNC_BLOCK(block_handler);
1029  break;
1031  block->type = block_type_symbol;
1032  block->as.symbol = VM_BH_TO_SYMBOL(block_handler);
1033  break;
1035  block->type = block_type_proc;
1036  block->as.proc = VM_BH_TO_PROC(block_handler);
1037  }
1038 }
1039 
1040 int
1042 {
1043  int min, max;
1044  rb_thread_t *th = GET_THREAD();
1045  rb_control_frame_t *cfp = th->cfp;
1046  VALUE block_handler = rb_vm_frame_block_handler(cfp);
1047  struct rb_block block;
1048 
1049  if (block_handler == VM_BLOCK_HANDLER_NONE) {
1050  rb_raise(rb_eArgError, "no block given");
1051  }
1052 
1053  block_setup(&block, block_handler);
1054  min = rb_vm_block_min_max_arity(&block, &max);
1055 
1056  switch (vm_block_type(&block)) {
1058  return -1;
1059 
1061  {
1062  VALUE procval = block_handler;
1063  rb_proc_t *proc;
1064  GetProcPtr(procval, proc);
1065  return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1;
1066  /* fall through */
1067  }
1068 
1069  default:
1070  return max != UNLIMITED_ARGUMENTS ? min : -min-1;
1071  }
1072 }
1073 
1074 int
1076 {
1077  rb_thread_t *th = GET_THREAD();
1078  rb_control_frame_t *cfp = th->cfp;
1079  VALUE block_handler = rb_vm_frame_block_handler(cfp);
1080  struct rb_block block;
1081 
1082  if (block_handler == VM_BLOCK_HANDLER_NONE) {
1083  rb_raise(rb_eArgError, "no block given");
1084  }
1085 
1086  block_setup(&block, block_handler);
1087  return rb_vm_block_min_max_arity(&block, max);
1088 }
1089 
1090 const rb_iseq_t *
1091 rb_proc_get_iseq(VALUE self, int *is_proc)
1092 {
1093  const rb_proc_t *proc;
1094  const struct rb_block *block;
1095 
1096  GetProcPtr(self, proc);
1097  block = &proc->block;
1098  if (is_proc) *is_proc = !proc->is_lambda;
1099 
1100  switch (vm_block_type(block)) {
1101  case block_type_iseq:
1102  return rb_iseq_check(block->as.captured.code.iseq);
1103  case block_type_proc:
1104  return rb_proc_get_iseq(block->as.proc, is_proc);
1105  case block_type_ifunc:
1106  {
1107  const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
1108  if (IS_METHOD_PROC_IFUNC(ifunc)) {
1109  /* method(:foo).to_proc */
1110  if (is_proc) *is_proc = 0;
1111  return rb_method_iseq((VALUE)ifunc->data);
1112  }
1113  else {
1114  return NULL;
1115  }
1116  }
1117  case block_type_symbol:
1118  return NULL;
1119  }
1120 
1122  return NULL;
1123 }
1124 
1125 static VALUE
1127 {
1128  VALUE loc[2];
1129 
1130  if (!iseq) return Qnil;
1131  rb_iseq_check(iseq);
1132  loc[0] = iseq->body->location.path;
1133  loc[1] = iseq->body->location.first_lineno;
1134 
1135  return rb_ary_new4(2, loc);
1136 }
1137 
1138 /*
1139  * call-seq:
1140  * prc.source_location -> [String, Integer]
1141  *
1142  * Returns the Ruby source filename and line number containing this proc
1143  * or +nil+ if this proc was not defined in Ruby (i.e. native).
1144  */
1145 
1146 VALUE
1148 {
1149  return iseq_location(rb_proc_get_iseq(self, 0));
1150 }
1151 
1152 static VALUE
1154 {
1155  VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
1156  int n = (arity < 0) ? ~arity : arity;
1157  ID req, rest;
1158  CONST_ID(req, "req");
1159  a = rb_ary_new3(1, ID2SYM(req));
1160  OBJ_FREEZE(a);
1161  for (; n; --n) {
1162  rb_ary_push(param, a);
1163  }
1164  if (arity < 0) {
1165  CONST_ID(rest, "rest");
1166  rb_ary_store(param, ~arity, rb_ary_new3(1, ID2SYM(rest)));
1167  }
1168  return param;
1169 }
1170 
1171 /*
1172  * call-seq:
1173  * prc.parameters -> array
1174  *
1175  * Returns the parameter information of this proc.
1176  *
1177  * prc = lambda{|x, y=42, *other|}
1178  * prc.parameters #=> [[:req, :x], [:opt, :y], [:rest, :other]]
1179  */
1180 
1181 static VALUE
1183 {
1184  int is_proc;
1185  const rb_iseq_t *iseq = rb_proc_get_iseq(self, &is_proc);
1186  if (!iseq) {
1187  return unnamed_parameters(rb_proc_arity(self));
1188  }
1189  return rb_iseq_parameters(iseq, is_proc);
1190 }
1191 
1192 st_index_t
1194 {
1195  rb_proc_t *proc;
1196  GetProcPtr(prc, proc);
1197  hash = rb_hash_uint(hash, (st_index_t)proc->block.as.captured.code.val);
1198  hash = rb_hash_uint(hash, (st_index_t)proc->block.as.captured.self);
1199  return rb_hash_uint(hash, (st_index_t)proc->block.as.captured.ep >> 16);
1200 }
1201 
1202 VALUE
1204 {
1205  static VALUE sym_proc_cache = Qfalse;
1206  enum {SYM_PROC_CACHE_SIZE = 67};
1207  VALUE proc;
1208  long index;
1209  ID id;
1210  VALUE *aryp;
1211 
1212  if (!sym_proc_cache) {
1213  sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
1214  rb_gc_register_mark_object(sym_proc_cache);
1215  rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
1216  }
1217 
1218  id = SYM2ID(sym);
1219  index = (id % SYM_PROC_CACHE_SIZE) << 1;
1220 
1221  aryp = RARRAY_PTR(sym_proc_cache);
1222  if (aryp[index] == sym) {
1223  return aryp[index + 1];
1224  }
1225  else {
1226  proc = sym_proc_new(rb_cProc, ID2SYM(id));
1227  aryp[index] = sym;
1228  aryp[index + 1] = proc;
1229  return proc;
1230  }
1231 }
1232 
1233 /*
1234  * call-seq:
1235  * prc.hash -> integer
1236  *
1237  * Returns a hash value corresponding to proc body.
1238  *
1239  * See also Object#hash.
1240  */
1241 
1242 static VALUE
1244 {
1245  st_index_t hash;
1246  hash = rb_hash_start(0);
1247  hash = rb_hash_proc(hash, self);
1248  hash = rb_hash_end(hash);
1249  return ST2FIX(hash);
1250 }
1251 
1252 /*
1253  * call-seq:
1254  * prc.to_s -> string
1255  *
1256  * Returns the unique identifier for this proc, along with
1257  * an indication of where the proc was defined.
1258  */
1259 
1260 static VALUE
1262 {
1263  const rb_proc_t *proc;
1264  GetProcPtr(self, proc);
1265  return proc_to_s_(self, proc);
1266 }
1267 
1268 static VALUE
1269 proc_to_s_(VALUE self, const rb_proc_t *proc)
1270 {
1271  VALUE str = 0;
1272  const char *cname = rb_obj_classname(self);
1273  const struct rb_block *block;
1274  const char *is_lambda;
1275 
1276  block = &proc->block;
1277  is_lambda = proc->is_lambda ? " (lambda)" : "";
1278 
1279  again:
1280  switch (vm_block_type(block)) {
1281  case block_type_proc:
1282  block = vm_proc_block(block->as.proc);
1283  goto again;
1284  case block_type_iseq:
1285  {
1286  const rb_iseq_t *iseq = rb_iseq_check(block->as.captured.code.iseq);
1287  str = rb_sprintf("#<%s:%p@%"PRIsVALUE":%d%s>", cname, (void *)self,
1288  iseq->body->location.path,
1289  FIX2INT(iseq->body->location.first_lineno), is_lambda);
1290  }
1291  break;
1292  case block_type_symbol:
1293  str = rb_sprintf("#<%s:%p(&%+"PRIsVALUE")%s>", cname, (void *)self,
1294  block->as.symbol, is_lambda);
1295  break;
1296  case block_type_ifunc:
1297  str = rb_sprintf("#<%s:%p%s>", cname, proc->block.as.captured.code.ifunc,
1298  is_lambda);
1299  break;
1300  }
1301 
1302  if (OBJ_TAINTED(self)) {
1303  OBJ_TAINT(str);
1304  }
1305  return str;
1306 }
1307 
1308 /*
1309  * call-seq:
1310  * prc.to_proc -> proc
1311  *
1312  * Part of the protocol for converting objects to <code>Proc</code>
1313  * objects. Instances of class <code>Proc</code> simply return
1314  * themselves.
1315  */
1316 
1317 static VALUE
1319 {
1320  return self;
1321 }
1322 
1323 static void
1324 bm_mark(void *ptr)
1325 {
1326  struct METHOD *data = ptr;
1327  rb_gc_mark(data->recv);
1328  rb_gc_mark(data->klass);
1329  rb_gc_mark((VALUE)data->me);
1330 }
1331 
1332 static size_t
1333 bm_memsize(const void *ptr)
1334 {
1335  return sizeof(struct METHOD);
1336 }
1337 
1339  "method",
1340  {
1341  bm_mark,
1343  bm_memsize,
1344  },
1346 };
1347 
1348 VALUE
1350 {
1351  if (rb_typeddata_is_kind_of(m, &method_data_type)) {
1352  return Qtrue;
1353  }
1354  else {
1355  return Qfalse;
1356  }
1357 }
1358 
1359 static int
1361 {
1362  /* TODO: merge with obj_respond_to() */
1363  ID rmiss = idRespond_to_missing;
1364 
1365  if (obj == Qundef) return 0;
1366  if (rb_method_basic_definition_p(klass, rmiss)) return 0;
1367  return RTEST(rb_funcall(obj, rmiss, 2, sym, scope ? Qfalse : Qtrue));
1368 }
1369 
1370 
1371 static VALUE
1373 {
1374  struct METHOD *data;
1375  VALUE method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
1378 
1379  RB_OBJ_WRITE(method, &data->recv, obj);
1380  RB_OBJ_WRITE(method, &data->klass, klass);
1381 
1384  def->original_id = id;
1385 
1386  me = rb_method_entry_create(id, klass, METHOD_VISI_UNDEF, def);
1387 
1388  RB_OBJ_WRITE(method, &data->me, me);
1389 
1390  OBJ_INFECT(method, klass);
1391 
1392  return method;
1393 }
1394 
1395 static VALUE
1397  VALUE obj, ID id, VALUE mclass, int scope, int error)
1398 {
1399  struct METHOD *data;
1400  VALUE method;
1402 
1403  again:
1404  if (UNDEFINED_METHOD_ENTRY_P(me)) {
1405  if (respond_to_missing_p(klass, obj, ID2SYM(id), scope)) {
1406  return mnew_missing(klass, obj, id, mclass);
1407  }
1408  if (!error) return Qnil;
1409  rb_print_undef(klass, id, METHOD_VISI_UNDEF);
1410  }
1411  if (visi == METHOD_VISI_UNDEF) {
1412  visi = METHOD_ENTRY_VISI(me);
1413  if (scope && (visi != METHOD_VISI_PUBLIC)) {
1414  if (!error) return Qnil;
1415  rb_print_inaccessible(klass, id, visi);
1416  }
1417  }
1418  if (me->def->type == VM_METHOD_TYPE_ZSUPER) {
1419  if (me->defined_class) {
1421  id = me->def->original_id;
1423  }
1424  else {
1425  VALUE klass = RCLASS_SUPER(me->owner);
1426  id = me->def->original_id;
1427  me = rb_method_entry_without_refinements(klass, id);
1428  }
1429  goto again;
1430  }
1431 
1432  while (klass != me->owner && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
1433  klass = RCLASS_SUPER(klass);
1434  }
1435 
1436  method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
1437 
1438  RB_OBJ_WRITE(method, &data->recv, obj);
1439  RB_OBJ_WRITE(method, &data->klass, klass);
1440  RB_OBJ_WRITE(method, &data->me, me);
1441 
1442  OBJ_INFECT(method, klass);
1443  return method;
1444 }
1445 
1446 static VALUE
1448  VALUE obj, ID id, VALUE mclass, int scope)
1449 {
1450  return mnew_internal(me, klass, obj, id, mclass, scope, TRUE);
1451 }
1452 
1453 static VALUE
1454 mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
1455 {
1456  const rb_method_entry_t *me;
1457 
1458  if (obj == Qundef) { /* UnboundMethod */
1459  me = rb_method_entry_without_refinements(klass, id);
1460  }
1461  else {
1463  }
1464  return mnew_from_me(me, klass, obj, id, mclass, scope);
1465 }
1466 
1467 static inline VALUE
1469 {
1470  VALUE defined_class = me->defined_class;
1471  return defined_class ? defined_class : me->owner;
1472 }
1473 
1474 /**********************************************************************
1475  *
1476  * Document-class : Method
1477  *
1478  * Method objects are created by <code>Object#method</code>, and are
1479  * associated with a particular object (not just with a class). They
1480  * may be used to invoke the method within the object, and as a block
1481  * associated with an iterator. They may also be unbound from one
1482  * object (creating an <code>UnboundMethod</code>) and bound to
1483  * another.
1484  *
1485  * class Thing
1486  * def square(n)
1487  * n*n
1488  * end
1489  * end
1490  * thing = Thing.new
1491  * meth = thing.method(:square)
1492  *
1493  * meth.call(9) #=> 81
1494  * [ 1, 2, 3 ].collect(&meth) #=> [1, 4, 9]
1495  *
1496  */
1497 
1498 /*
1499  * call-seq:
1500  * meth.eql?(other_meth) -> true or false
1501  * meth == other_meth -> true or false
1502  *
1503  * Two method objects are equal if they are bound to the same
1504  * object and refer to the same method definition and their owners are the
1505  * same class or module.
1506  */
1507 
1508 static VALUE
1509 method_eq(VALUE method, VALUE other)
1510 {
1511  struct METHOD *m1, *m2;
1512  VALUE klass1, klass2;
1513 
1514  if (!rb_obj_is_method(other))
1515  return Qfalse;
1516  if (CLASS_OF(method) != CLASS_OF(other))
1517  return Qfalse;
1518 
1519  Check_TypedStruct(method, &method_data_type);
1520  m1 = (struct METHOD *)DATA_PTR(method);
1521  m2 = (struct METHOD *)DATA_PTR(other);
1522 
1523  klass1 = method_entry_defined_class(m1->me);
1524  klass2 = method_entry_defined_class(m2->me);
1525 
1526  if (!rb_method_entry_eq(m1->me, m2->me) ||
1527  klass1 != klass2 ||
1528  m1->klass != m2->klass ||
1529  m1->recv != m2->recv) {
1530  return Qfalse;
1531  }
1532 
1533  return Qtrue;
1534 }
1535 
1536 /*
1537  * call-seq:
1538  * meth.hash -> integer
1539  *
1540  * Returns a hash value corresponding to the method object.
1541  *
1542  * See also Object#hash.
1543  */
1544 
1545 static VALUE
1547 {
1548  struct METHOD *m;
1549  st_index_t hash;
1550 
1551  TypedData_Get_Struct(method, struct METHOD, &method_data_type, m);
1552  hash = rb_hash_start((st_index_t)m->recv);
1553  hash = rb_hash_method_entry(hash, m->me);
1554  hash = rb_hash_end(hash);
1555 
1556  return INT2FIX(hash);
1557 }
1558 
1559 /*
1560  * call-seq:
1561  * meth.unbind -> unbound_method
1562  *
1563  * Dissociates <i>meth</i> from its current receiver. The resulting
1564  * <code>UnboundMethod</code> can subsequently be bound to a new object
1565  * of the same class (see <code>UnboundMethod</code>).
1566  */
1567 
1568 static VALUE
1570 {
1571  VALUE method;
1572  struct METHOD *orig, *data;
1573 
1574  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig);
1576  &method_data_type, data);
1577  RB_OBJ_WRITE(method, &data->recv, Qundef);
1578  RB_OBJ_WRITE(method, &data->klass, orig->klass);
1579  RB_OBJ_WRITE(method, &data->me, rb_method_entry_clone(orig->me));
1580  OBJ_INFECT(method, obj);
1581 
1582  return method;
1583 }
1584 
1585 /*
1586  * call-seq:
1587  * meth.receiver -> object
1588  *
1589  * Returns the bound receiver of the method object.
1590  */
1591 
1592 static VALUE
1594 {
1595  struct METHOD *data;
1596 
1597  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
1598  return data->recv;
1599 }
1600 
1601 /*
1602  * call-seq:
1603  * meth.name -> symbol
1604  *
1605  * Returns the name of the method.
1606  */
1607 
1608 static VALUE
1610 {
1611  struct METHOD *data;
1612 
1613  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
1614  return ID2SYM(data->me->called_id);
1615 }
1616 
1617 /*
1618  * call-seq:
1619  * meth.original_name -> symbol
1620  *
1621  * Returns the original name of the method.
1622  */
1623 
1624 static VALUE
1626 {
1627  struct METHOD *data;
1628 
1629  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
1630  return ID2SYM(data->me->def->original_id);
1631 }
1632 
1633 /*
1634  * call-seq:
1635  * meth.owner -> class_or_module
1636  *
1637  * Returns the class or module that defines the method.
1638  */
1639 
1640 static VALUE
1642 {
1643  struct METHOD *data;
1644  TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
1645  return data->me->owner;
1646 }
1647 
1648 void
1650 {
1651 #define MSG(s) rb_fstring_cstr("undefined method `%1$s' for"s" `%2$s'")
1652  VALUE c = klass;
1653  VALUE s;
1654 
1655  if (FL_TEST(c, FL_SINGLETON)) {
1656  VALUE obj = rb_ivar_get(klass, attached);
1657 
1658  switch (BUILTIN_TYPE(obj)) {
1659  case T_MODULE:
1660  case T_CLASS:
1661  c = obj;
1662  s = MSG("");
1663  }
1664  goto normal_class;
1665  }
1666  else if (RB_TYPE_P(c, T_MODULE)) {
1667  s = MSG(" module");
1668  }
1669  else {
1670  normal_class:
1671  s = MSG(" class");
1672  }
1673  rb_name_err_raise_str(s, c, str);
1674 #undef MSG
1675 }
1676 
1677 static VALUE
1678 obj_method(VALUE obj, VALUE vid, int scope)
1679 {
1680  ID id = rb_check_id(&vid);
1681  const VALUE klass = CLASS_OF(obj);
1682  const VALUE mclass = rb_cMethod;
1683 
1684  if (!id) {
1685  if (respond_to_missing_p(klass, obj, vid, scope)) {
1686  id = rb_intern_str(vid);
1687  return mnew_missing(klass, obj, id, mclass);
1688  }
1689  rb_method_name_error(klass, vid);
1690  }
1691  return mnew(klass, obj, id, mclass, scope);
1692 }
1693 
1694 /*
1695  * call-seq:
1696  * obj.method(sym) -> method
1697  *
1698  * Looks up the named method as a receiver in <i>obj</i>, returning a
1699  * <code>Method</code> object (or raising <code>NameError</code>). The
1700  * <code>Method</code> object acts as a closure in <i>obj</i>'s object
1701  * instance, so instance variables and the value of <code>self</code>
1702  * remain available.
1703  *
1704  * class Demo
1705  * def initialize(n)
1706  * @iv = n
1707  * end
1708  * def hello()
1709  * "Hello, @iv = #{@iv}"
1710  * end
1711  * end
1712  *
1713  * k = Demo.new(99)
1714  * m = k.method(:hello)
1715  * m.call #=> "Hello, @iv = 99"
1716  *
1717  * l = Demo.new('Fred')
1718  * m = l.method("hello")
1719  * m.call #=> "Hello, @iv = Fred"
1720  */
1721 
1722 VALUE
1724 {
1725  return obj_method(obj, vid, FALSE);
1726 }
1727 
1728 /*
1729  * call-seq:
1730  * obj.public_method(sym) -> method
1731  *
1732  * Similar to _method_, searches public method only.
1733  */
1734 
1735 VALUE
1737 {
1738  return obj_method(obj, vid, TRUE);
1739 }
1740 
1741 /*
1742  * call-seq:
1743  * obj.singleton_method(sym) -> method
1744  *
1745  * Similar to _method_, searches singleton method only.
1746  *
1747  * class Demo
1748  * def initialize(n)
1749  * @iv = n
1750  * end
1751  * def hello()
1752  * "Hello, @iv = #{@iv}"
1753  * end
1754  * end
1755  *
1756  * k = Demo.new(99)
1757  * def k.hi
1758  * "Hi, @iv = #{@iv}"
1759  * end
1760  * m = k.singleton_method(:hi)
1761  * m.call #=> "Hi, @iv = 99"
1762  * m = k.singleton_method(:hello) #=> NameError
1763  */
1764 
1765 VALUE
1767 {
1768  const rb_method_entry_t *me;
1769  VALUE klass;
1770  ID id = rb_check_id(&vid);
1771 
1772  if (!id) {
1773  if (!NIL_P(klass = rb_singleton_class_get(obj)) &&
1774  respond_to_missing_p(klass, obj, vid, FALSE)) {
1775  id = rb_intern_str(vid);
1776  return mnew_missing(klass, obj, id, rb_cMethod);
1777  }
1778  undef:
1779  rb_name_err_raise("undefined singleton method `%1$s' for `%2$s'",
1780  obj, vid);
1781  }
1782  if (NIL_P(klass = rb_singleton_class_get(obj)) ||
1783  UNDEFINED_METHOD_ENTRY_P(me = rb_method_entry_at(klass, id)) ||
1785  vid = ID2SYM(id);
1786  goto undef;
1787  }
1788  return mnew_from_me(me, klass, obj, id, rb_cMethod, FALSE);
1789 }
1790 
1791 /*
1792  * call-seq:
1793  * mod.instance_method(symbol) -> unbound_method
1794  *
1795  * Returns an +UnboundMethod+ representing the given
1796  * instance method in _mod_.
1797  *
1798  * class Interpreter
1799  * def do_a() print "there, "; end
1800  * def do_d() print "Hello "; end
1801  * def do_e() print "!\n"; end
1802  * def do_v() print "Dave"; end
1803  * Dispatcher = {
1804  * "a" => instance_method(:do_a),
1805  * "d" => instance_method(:do_d),
1806  * "e" => instance_method(:do_e),
1807  * "v" => instance_method(:do_v)
1808  * }
1809  * def interpret(string)
1810  * string.each_char {|b| Dispatcher[b].bind(self).call }
1811  * end
1812  * end
1813  *
1814  * interpreter = Interpreter.new
1815  * interpreter.interpret('dave')
1816  *
1817  * <em>produces:</em>
1818  *
1819  * Hello there, Dave!
1820  */
1821 
1822 static VALUE
1824 {
1825  ID id = rb_check_id(&vid);
1826  if (!id) {
1827  rb_method_name_error(mod, vid);
1828  }
1829  return mnew(mod, Qundef, id, rb_cUnboundMethod, FALSE);
1830 }
1831 
1832 /*
1833  * call-seq:
1834  * mod.public_instance_method(symbol) -> unbound_method
1835  *
1836  * Similar to _instance_method_, searches public method only.
1837  */
1838 
1839 static VALUE
1841 {
1842  ID id = rb_check_id(&vid);
1843  if (!id) {
1844  rb_method_name_error(mod, vid);
1845  }
1846  return mnew(mod, Qundef, id, rb_cUnboundMethod, TRUE);
1847 }
1848 
1849 /*
1850  * call-seq:
1851  * define_method(symbol, method) -> symbol
1852  * define_method(symbol) { block } -> symbol
1853  *
1854  * Defines an instance method in the receiver. The _method_
1855  * parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
1856  * If a block is specified, it is used as the method body. This block
1857  * is evaluated using <code>instance_eval</code>, a point that is
1858  * tricky to demonstrate because <code>define_method</code> is private.
1859  * (This is why we resort to the +send+ hack in this example.)
1860  *
1861  * class A
1862  * def fred
1863  * puts "In Fred"
1864  * end
1865  * def create_method(name, &block)
1866  * self.class.send(:define_method, name, &block)
1867  * end
1868  * define_method(:wilma) { puts "Charge it!" }
1869  * end
1870  * class B < A
1871  * define_method(:barney, instance_method(:fred))
1872  * end
1873  * a = B.new
1874  * a.barney
1875  * a.wilma
1876  * a.create_method(:betty) { p self }
1877  * a.betty
1878  *
1879  * <em>produces:</em>
1880  *
1881  * In Fred
1882  * Charge it!
1883  * #<B:0x401b39e8>
1884  */
1885 
1886 static VALUE
1888 {
1889  ID id;
1890  VALUE body;
1891  VALUE name;
1892  const rb_cref_t *cref = rb_vm_cref_in_context(mod, mod);
1893  const rb_scope_visibility_t default_scope_visi = {METHOD_VISI_PUBLIC, FALSE};
1894  const rb_scope_visibility_t *scope_visi = &default_scope_visi;
1895  int is_method = FALSE;
1896 
1897  if (cref) {
1898  scope_visi = CREF_SCOPE_VISI(cref);
1899  }
1900 
1901  rb_check_arity(argc, 1, 2);
1902  name = argv[0];
1903  id = rb_check_id(&name);
1904  if (argc == 1) {
1905 #if PROC_NEW_REQUIRES_BLOCK
1906  body = rb_block_lambda();
1907 #else
1908  rb_thread_t *th = GET_THREAD();
1909  VALUE block_handler = rb_vm_frame_block_handler(th->cfp);
1910  if (block_handler == VM_BLOCK_HANDLER_NONE) rb_raise(rb_eArgError, proc_without_block);
1911 
1912  switch (vm_block_handler_type(block_handler)) {
1914  body = VM_BH_TO_PROC(block_handler);
1915  break;
1917  body = rb_sym_to_proc(VM_BH_TO_SYMBOL(block_handler));
1918  break;
1921  body = rb_vm_make_proc_lambda(th, VM_BH_TO_CAPT_BLOCK(block_handler), rb_cProc, TRUE);
1922  }
1923 #endif
1924  }
1925  else {
1926  body = argv[1];
1927 
1928  if (rb_obj_is_method(body)) {
1929  is_method = TRUE;
1930  }
1931  else if (rb_obj_is_proc(body)) {
1932  is_method = FALSE;
1933  }
1934  else {
1936  "wrong argument type %s (expected Proc/Method)",
1937  rb_obj_classname(body));
1938  }
1939  }
1940  if (!id) id = rb_to_id(name);
1941 
1942  if (is_method) {
1943  struct METHOD *method = (struct METHOD *)DATA_PTR(body);
1944  if (method->me->owner != mod && !RB_TYPE_P(method->me->owner, T_MODULE) &&
1945  !RTEST(rb_class_inherited_p(mod, method->me->owner))) {
1946  if (FL_TEST(method->me->owner, FL_SINGLETON)) {
1948  "can't bind singleton method to a different class");
1949  }
1950  else {
1952  "bind argument must be a subclass of % "PRIsVALUE,
1953  rb_class_name(method->me->owner));
1954  }
1955  }
1956  rb_method_entry_set(mod, id, method->me, scope_visi->method_visi);
1957  if (scope_visi->module_func) {
1959  }
1960  RB_GC_GUARD(body);
1961  }
1962  else {
1963  VALUE procval = proc_dup(body);
1964  if (vm_proc_iseq(procval) != NULL) {
1965  rb_proc_t *proc;
1966  GetProcPtr(procval, proc);
1967  proc->is_lambda = TRUE;
1968  proc->is_from_method = TRUE;
1969  }
1970  rb_add_method(mod, id, VM_METHOD_TYPE_BMETHOD, (void *)procval, scope_visi->method_visi);
1971  if (scope_visi->module_func) {
1973  }
1974  }
1975 
1976  return ID2SYM(id);
1977 }
1978 
1979 /*
1980  * call-seq:
1981  * define_singleton_method(symbol, method) -> symbol
1982  * define_singleton_method(symbol) { block } -> symbol
1983  *
1984  * Defines a singleton method in the receiver. The _method_
1985  * parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
1986  * If a block is specified, it is used as the method body.
1987  *
1988  * class A
1989  * class << self
1990  * def class_name
1991  * to_s
1992  * end
1993  * end
1994  * end
1995  * A.define_singleton_method(:who_am_i) do
1996  * "I am: #{class_name}"
1997  * end
1998  * A.who_am_i # ==> "I am: A"
1999  *
2000  * guy = "Bob"
2001  * guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
2002  * guy.hello #=> "Bob: Hello there!"
2003  */
2004 
2005 static VALUE
2006 rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
2007 {
2009 
2010  return rb_mod_define_method(argc, argv, klass);
2011 }
2012 
2013 /*
2014  * define_method(symbol, method) -> symbol
2015  * define_method(symbol) { block } -> symbol
2016  *
2017  * Defines a global function by _method_ or the block.
2018  */
2019 
2020 static VALUE
2021 top_define_method(int argc, VALUE *argv, VALUE obj)
2022 {
2023  rb_thread_t *th = GET_THREAD();
2024  VALUE klass;
2025 
2026  klass = th->top_wrapper;
2027  if (klass) {
2028  rb_warning("main.define_method in the wrapped load is effective only in wrapper module");
2029  }
2030  else {
2031  klass = rb_cObject;
2032  }
2033  return rb_mod_define_method(argc, argv, klass);
2034 }
2035 
2036 /*
2037  * call-seq:
2038  * method.clone -> new_method
2039  *
2040  * Returns a clone of this method.
2041  *
2042  * class A
2043  * def foo
2044  * return "bar"
2045  * end
2046  * end
2047  *
2048  * m = A.new.method(:foo)
2049  * m.call # => "bar"
2050  * n = m.clone.call # => "bar"
2051  */
2052 
2053 static VALUE
2055 {
2056  VALUE clone;
2057  struct METHOD *orig, *data;
2058 
2059  TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig);
2060  clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data);
2061  CLONESETUP(clone, self);
2062  RB_OBJ_WRITE(clone, &data->recv, orig->recv);
2063  RB_OBJ_WRITE(clone, &data->klass, orig->klass);
2064  RB_OBJ_WRITE(clone, &data->me, rb_method_entry_clone(orig->me));
2065  return clone;
2066 }
2067 
2068 /*
2069  * call-seq:
2070  * meth.call(args, ...) -> obj
2071  * meth[args, ...] -> obj
2072  *
2073  * Invokes the <i>meth</i> with the specified arguments, returning the
2074  * method's return value.
2075  *
2076  * m = 12.method("+")
2077  * m.call(3) #=> 15
2078  * m.call(20) #=> 32
2079  */
2080 
2081 VALUE
2082 rb_method_call(int argc, const VALUE *argv, VALUE method)
2083 {
2084  VALUE procval = rb_block_given_p() ? rb_block_proc() : Qnil;
2085  return rb_method_call_with_block(argc, argv, method, procval);
2086 }
2087 
2088 static const rb_callable_method_entry_t *
2090 {
2091  if (data->me->defined_class == 0) rb_bug("method_callable_method_entry: not callable.");
2092  return (const rb_callable_method_entry_t *)data->me;
2093 }
2094 
2095 static inline VALUE
2096 call_method_data(rb_thread_t *th, const struct METHOD *data,
2097  int argc, const VALUE *argv, VALUE passed_procval)
2098 {
2100  return rb_vm_call(th, data->recv, data->me->called_id, argc, argv,
2102 }
2103 
2104 static VALUE
2105 call_method_data_safe(rb_thread_t *th, const struct METHOD *data,
2106  int argc, const VALUE *argv, VALUE passed_procval,
2107  int safe)
2108 {
2109  VALUE result = Qnil; /* OK */
2110  int state;
2111 
2112  TH_PUSH_TAG(th);
2113  if ((state = TH_EXEC_TAG()) == 0) {
2114  /* result is used only if state == 0, no exceptions is caught. */
2115  /* otherwise it doesn't matter even if clobbered. */
2116  NO_CLOBBERED(result) = call_method_data(th, data, argc, argv, passed_procval);
2117  }
2118  TH_POP_TAG();
2120  if (state)
2121  TH_JUMP_TAG(th, state);
2122  return result;
2123 }
2124 
2125 VALUE
2126 rb_method_call_with_block(int argc, const VALUE *argv, VALUE method, VALUE passed_procval)
2127 {
2128  const struct METHOD *data;
2129  rb_thread_t *const th = GET_THREAD();
2130 
2131  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
2132  if (data->recv == Qundef) {
2133  rb_raise(rb_eTypeError, "can't call unbound method; bind first");
2134  }
2135  if (OBJ_TAINTED(method)) {
2136  const int safe_level_to_run = RUBY_SAFE_LEVEL_MAX;
2137  int safe = rb_safe_level();
2138  if (safe < safe_level_to_run) {
2139  rb_set_safe_level_force(safe_level_to_run);
2140  return call_method_data_safe(th, data, argc, argv, passed_procval, safe);
2141  }
2142  }
2143  return call_method_data(th, data, argc, argv, passed_procval);
2144 }
2145 
2146 /**********************************************************************
2147  *
2148  * Document-class: UnboundMethod
2149  *
2150  * Ruby supports two forms of objectified methods. Class
2151  * <code>Method</code> is used to represent methods that are associated
2152  * with a particular object: these method objects are bound to that
2153  * object. Bound method objects for an object can be created using
2154  * <code>Object#method</code>.
2155  *
2156  * Ruby also supports unbound methods; methods objects that are not
2157  * associated with a particular object. These can be created either by
2158  * calling <code>Module#instance_method</code> or by calling
2159  * <code>unbind</code> on a bound method object. The result of both of
2160  * these is an <code>UnboundMethod</code> object.
2161  *
2162  * Unbound methods can only be called after they are bound to an
2163  * object. That object must be a kind_of? the method's original
2164  * class.
2165  *
2166  * class Square
2167  * def area
2168  * @side * @side
2169  * end
2170  * def initialize(side)
2171  * @side = side
2172  * end
2173  * end
2174  *
2175  * area_un = Square.instance_method(:area)
2176  *
2177  * s = Square.new(12)
2178  * area = area_un.bind(s)
2179  * area.call #=> 144
2180  *
2181  * Unbound methods are a reference to the method at the time it was
2182  * objectified: subsequent changes to the underlying class will not
2183  * affect the unbound method.
2184  *
2185  * class Test
2186  * def test
2187  * :original
2188  * end
2189  * end
2190  * um = Test.instance_method(:test)
2191  * class Test
2192  * def test
2193  * :modified
2194  * end
2195  * end
2196  * t = Test.new
2197  * t.test #=> :modified
2198  * um.bind(t).call #=> :original
2199  *
2200  */
2201 
2202 /*
2203  * call-seq:
2204  * umeth.bind(obj) -> method
2205  *
2206  * Bind <i>umeth</i> to <i>obj</i>. If <code>Klass</code> was the class
2207  * from which <i>umeth</i> was obtained,
2208  * <code>obj.kind_of?(Klass)</code> must be true.
2209  *
2210  * class A
2211  * def test
2212  * puts "In test, class = #{self.class}"
2213  * end
2214  * end
2215  * class B < A
2216  * end
2217  * class C < B
2218  * end
2219  *
2220  *
2221  * um = B.instance_method(:test)
2222  * bm = um.bind(C.new)
2223  * bm.call
2224  * bm = um.bind(B.new)
2225  * bm.call
2226  * bm = um.bind(A.new)
2227  * bm.call
2228  *
2229  * <em>produces:</em>
2230  *
2231  * In test, class = C
2232  * In test, class = B
2233  * prog.rb:16:in `bind': bind argument must be an instance of B (TypeError)
2234  * from prog.rb:16
2235  */
2236 
2237 static VALUE
2239 {
2240  struct METHOD *data, *bound;
2241  VALUE methclass, klass;
2242 
2243  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
2244 
2245  methclass = data->me->owner;
2246 
2247  if (!RB_TYPE_P(methclass, T_MODULE) &&
2248  methclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, methclass)) {
2249  if (FL_TEST(methclass, FL_SINGLETON)) {
2251  "singleton method called for a different object");
2252  }
2253  else {
2254  rb_raise(rb_eTypeError, "bind argument must be an instance of % "PRIsVALUE,
2255  rb_class_name(methclass));
2256  }
2257  }
2258 
2259  klass = CLASS_OF(recv);
2260 
2261  method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
2262  RB_OBJ_WRITE(method, &bound->recv, recv);
2263  RB_OBJ_WRITE(method, &bound->klass, data->klass);
2264  RB_OBJ_WRITE(method, &bound->me, rb_method_entry_clone(data->me));
2265 
2266  if (RB_TYPE_P(bound->me->owner, T_MODULE)) {
2267  VALUE ic = rb_class_search_ancestor(klass, bound->me->owner);
2268  if (ic) {
2269  klass = ic;
2270  }
2271  else {
2272  klass = rb_include_class_new(methclass, klass);
2273  }
2274  RB_OBJ_WRITE(method, &bound->me, rb_method_entry_complement_defined_class(bound->me, bound->me->called_id, klass));
2275  }
2276 
2277  return method;
2278 }
2279 
2280 /*
2281  * Returns the number of required parameters and stores the maximum
2282  * number of parameters in max, or UNLIMITED_ARGUMENTS
2283  * if there is no maximum.
2284  */
2285 static int
2287 {
2288  const rb_method_definition_t *def = me->def;
2289 
2290  if (!def) return *max = 0;
2291  switch (def->type) {
2292  case VM_METHOD_TYPE_CFUNC:
2293  if (def->body.cfunc.argc < 0) {
2294  *max = UNLIMITED_ARGUMENTS;
2295  return 0;
2296  }
2297  return *max = check_argc(def->body.cfunc.argc);
2298  case VM_METHOD_TYPE_ZSUPER:
2299  *max = UNLIMITED_ARGUMENTS;
2300  return 0;
2302  return *max = 1;
2303  case VM_METHOD_TYPE_IVAR:
2304  return *max = 0;
2305  case VM_METHOD_TYPE_ALIAS:
2308  return rb_proc_min_max_arity(def->body.proc, max);
2309  case VM_METHOD_TYPE_ISEQ: {
2310  const rb_iseq_t *iseq = rb_iseq_check(def->body.iseq.iseqptr);
2311  return rb_iseq_min_max_arity(iseq, max);
2312  }
2313  case VM_METHOD_TYPE_UNDEF:
2315  return *max = 0;
2317  *max = UNLIMITED_ARGUMENTS;
2318  return 0;
2319  case VM_METHOD_TYPE_OPTIMIZED: {
2320  switch (def->body.optimize_type) {
2321  case OPTIMIZED_METHOD_TYPE_SEND:
2322  *max = UNLIMITED_ARGUMENTS;
2323  return 0;
2324  case OPTIMIZED_METHOD_TYPE_CALL:
2325  *max = UNLIMITED_ARGUMENTS;
2326  return 0;
2327  default:
2328  break;
2329  }
2330  break;
2331  }
2333  *max = UNLIMITED_ARGUMENTS;
2334  return 0;
2335  }
2336  rb_bug("rb_method_entry_min_max_arity: invalid method entry type (%d)", def->type);
2337  UNREACHABLE;
2338 }
2339 
2340 int
2342 {
2343  int max, min = rb_method_entry_min_max_arity(me, &max);
2344  return min == max ? min : -min-1;
2345 }
2346 
2347 /*
2348  * call-seq:
2349  * meth.arity -> integer
2350  *
2351  * Returns an indication of the number of arguments accepted by a
2352  * method. Returns a nonnegative integer for methods that take a fixed
2353  * number of arguments. For Ruby methods that take a variable number of
2354  * arguments, returns -n-1, where n is the number of required
2355  * arguments. For methods written in C, returns -1 if the call takes a
2356  * variable number of arguments.
2357  *
2358  * class C
2359  * def one; end
2360  * def two(a); end
2361  * def three(*a); end
2362  * def four(a, b); end
2363  * def five(a, b, *c); end
2364  * def six(a, b, *c, &d); end
2365  * end
2366  * c = C.new
2367  * c.method(:one).arity #=> 0
2368  * c.method(:two).arity #=> 1
2369  * c.method(:three).arity #=> -1
2370  * c.method(:four).arity #=> 2
2371  * c.method(:five).arity #=> -3
2372  * c.method(:six).arity #=> -3
2373  *
2374  * "cat".method(:size).arity #=> 0
2375  * "cat".method(:replace).arity #=> 1
2376  * "cat".method(:squeeze).arity #=> -1
2377  * "cat".method(:count).arity #=> -1
2378  */
2379 
2380 static VALUE
2382 {
2383  int n = method_arity(method);
2384  return INT2FIX(n);
2385 }
2386 
2387 static int
2389 {
2390  struct METHOD *data;
2391 
2392  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
2393  return rb_method_entry_arity(data->me);
2394 }
2395 
2396 static const rb_method_entry_t *
2398 {
2399  const rb_method_entry_t *me;
2400 
2401  while ((me = rb_method_entry(mod, id)) != 0) {
2402  const rb_method_definition_t *def = me->def;
2403  if (def->type != VM_METHOD_TYPE_ZSUPER) break;
2404  mod = RCLASS_SUPER(me->owner);
2405  id = def->original_id;
2406  }
2407  return me;
2408 }
2409 
2410 static int
2412 {
2413  const struct METHOD *data;
2414 
2415  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
2416  return rb_method_entry_min_max_arity(data->me, max);
2417 }
2418 
2419 int
2421 {
2422  const rb_method_entry_t *me = original_method_entry(mod, id);
2423  if (!me) return 0; /* should raise? */
2424  return rb_method_entry_arity(me);
2425 }
2426 
2427 int
2429 {
2430  return rb_mod_method_arity(CLASS_OF(obj), id);
2431 }
2432 
2433 static inline const rb_method_definition_t *
2435 {
2436  const struct METHOD *data;
2437 
2438  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
2439  return data->me->def;
2440 }
2441 
2442 static const rb_iseq_t *
2444 {
2445  switch (def->type) {
2446  case VM_METHOD_TYPE_ISEQ:
2447  return rb_iseq_check(def->body.iseq.iseqptr);
2449  return rb_proc_get_iseq(def->body.proc, 0);
2450  case VM_METHOD_TYPE_ALIAS:
2451  return method_def_iseq(def->body.alias.original_me->def);
2452  case VM_METHOD_TYPE_CFUNC:
2454  case VM_METHOD_TYPE_IVAR:
2455  case VM_METHOD_TYPE_ZSUPER:
2456  case VM_METHOD_TYPE_UNDEF:
2461  break;
2462  }
2463  return NULL;
2464 }
2465 
2466 const rb_iseq_t *
2468 {
2469  return method_def_iseq(method_def(method));
2470 }
2471 
2472 static const rb_cref_t *
2474 {
2475  const rb_method_definition_t *def = method_def(method);
2476 
2477  again:
2478  switch (def->type) {
2479  case VM_METHOD_TYPE_ISEQ:
2480  return def->body.iseq.cref;
2481  case VM_METHOD_TYPE_ALIAS:
2482  def = def->body.alias.original_me->def;
2483  goto again;
2484  default:
2485  return NULL;
2486  }
2487 }
2488 
2489 static VALUE
2491 {
2492  if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
2493  if (!def->body.attr.location)
2494  return Qnil;
2495  return rb_ary_dup(def->body.attr.location);
2496  }
2497  return iseq_location(method_def_iseq(def));
2498 }
2499 
2500 VALUE
2502 {
2503  if (!me) return Qnil;
2504  return method_def_location(me->def);
2505 }
2506 
2507 VALUE
2509 {
2510  const rb_method_entry_t *me = original_method_entry(mod, id);
2511  return rb_method_entry_location(me);
2512 }
2513 
2514 VALUE
2516 {
2517  return rb_mod_method_location(CLASS_OF(obj), id);
2518 }
2519 
2520 /*
2521  * call-seq:
2522  * meth.source_location -> [String, Integer]
2523  *
2524  * Returns the Ruby source filename and line number containing this method
2525  * or nil if this method was not defined in Ruby (i.e. native).
2526  */
2527 
2528 VALUE
2530 {
2531  return method_def_location(method_def(method));
2532 }
2533 
2534 /*
2535  * call-seq:
2536  * meth.parameters -> array
2537  *
2538  * Returns the parameter information of this method.
2539  *
2540  * def foo(bar); end
2541  * method(:foo).parameters #=> [[:req, :bar]]
2542  *
2543  * def foo(bar, baz, bat, &blk); end
2544  * method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:req, :bat], [:block, :blk]]
2545  *
2546  * def foo(bar, *args); end
2547  * method(:foo).parameters #=> [[:req, :bar], [:rest, :args]]
2548  *
2549  * def foo(bar, baz, *args, &blk); end
2550  * method(:foo).parameters #=> [[:req, :bar], [:req, :baz], [:rest, :args], [:block, :blk]]
2551  */
2552 
2553 static VALUE
2555 {
2556  const rb_iseq_t *iseq = rb_method_iseq(method);
2557  if (!iseq) {
2558  return unnamed_parameters(method_arity(method));
2559  }
2560  return rb_iseq_parameters(iseq, 0);
2561 }
2562 
2563 /*
2564  * call-seq:
2565  * meth.to_s -> string
2566  * meth.inspect -> string
2567  *
2568  * Returns the name of the underlying method.
2569  *
2570  * "cat".method(:count).inspect #=> "#<Method: String#count>"
2571  */
2572 
2573 static VALUE
2575 {
2576  struct METHOD *data;
2577  VALUE str;
2578  const char *s;
2579  const char *sharp = "#";
2580  VALUE mklass;
2581  VALUE defined_class;
2582 
2583  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
2584  str = rb_str_buf_new2("#<");
2585  s = rb_obj_classname(method);
2586  rb_str_buf_cat2(str, s);
2587  rb_str_buf_cat2(str, ": ");
2588 
2589  mklass = data->klass;
2590 
2591  if (data->me->def->type == VM_METHOD_TYPE_ALIAS) {
2592  defined_class = data->me->def->body.alias.original_me->owner;
2593  }
2594  else {
2595  defined_class = method_entry_defined_class(data->me);
2596  }
2597 
2598  if (RB_TYPE_P(defined_class, T_ICLASS)) {
2599  defined_class = RBASIC_CLASS(defined_class);
2600  }
2601 
2602  if (FL_TEST(mklass, FL_SINGLETON)) {
2603  VALUE v = rb_ivar_get(mklass, attached);
2604 
2605  if (data->recv == Qundef) {
2606  rb_str_buf_append(str, rb_inspect(mklass));
2607  }
2608  else if (data->recv == v) {
2609  rb_str_buf_append(str, rb_inspect(v));
2610  sharp = ".";
2611  }
2612  else {
2613  rb_str_buf_append(str, rb_inspect(data->recv));
2614  rb_str_buf_cat2(str, "(");
2615  rb_str_buf_append(str, rb_inspect(v));
2616  rb_str_buf_cat2(str, ")");
2617  sharp = ".";
2618  }
2619  }
2620  else {
2621  rb_str_buf_append(str, rb_class_name(mklass));
2622  if (defined_class != mklass) {
2623  rb_str_buf_cat2(str, "(");
2624  rb_str_buf_append(str, rb_class_name(defined_class));
2625  rb_str_buf_cat2(str, ")");
2626  }
2627  }
2628  rb_str_buf_cat2(str, sharp);
2629  rb_str_append(str, rb_id2str(data->me->called_id));
2630  if (data->me->called_id != data->me->def->original_id) {
2631  rb_str_catf(str, "(%"PRIsVALUE")",
2632  rb_id2str(data->me->def->original_id));
2633  }
2634  if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
2635  rb_str_buf_cat2(str, " (not-implemented)");
2636  }
2637  rb_str_buf_cat2(str, ">");
2638 
2639  return str;
2640 }
2641 
2642 static VALUE
2643 mproc(VALUE method)
2644 {
2645  return rb_funcallv(rb_mRubyVMFrozenCore, idProc, 0, 0);
2646 }
2647 
2648 static VALUE
2650 {
2651  return rb_funcallv(rb_mRubyVMFrozenCore, idLambda, 0, 0);
2652 }
2653 
2654 static VALUE
2655 bmcall(VALUE args, VALUE method, int argc, VALUE *argv, VALUE passed_proc)
2656 {
2657  return rb_method_call_with_block(argc, argv, method, passed_proc);
2658 }
2659 
2660 VALUE
2662  VALUE (*func)(ANYARGS), /* VALUE yieldarg[, VALUE procarg] */
2663  VALUE val)
2664 {
2665  VALUE procval = rb_iterate(mproc, 0, func, val);
2666  return procval;
2667 }
2668 
2669 /*
2670  * call-seq:
2671  * meth.to_proc -> proc
2672  *
2673  * Returns a <code>Proc</code> object corresponding to this method.
2674  */
2675 
2676 static VALUE
2678 {
2679  VALUE procval;
2680  rb_proc_t *proc;
2681 
2682  /*
2683  * class Method
2684  * def to_proc
2685  * lambda{|*args|
2686  * self.call(*args)
2687  * }
2688  * end
2689  * end
2690  */
2691  procval = rb_iterate(mlambda, 0, bmcall, method);
2692  GetProcPtr(procval, proc);
2693  proc->is_from_method = 1;
2694  return procval;
2695 }
2696 
2697 /*
2698  * call-seq:
2699  * meth.super_method -> method
2700  *
2701  * Returns a Method of superclass which would be called when super is used
2702  * or nil if there is no method on superclass.
2703  */
2704 
2705 static VALUE
2707 {
2708  const struct METHOD *data;
2709  VALUE super_class;
2710  const rb_method_entry_t *me;
2711 
2712  TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
2714  if (!super_class) return Qnil;
2716  if (!me) return Qnil;
2717  return mnew_internal(me, super_class, data->recv, data->me->called_id, rb_obj_class(method), FALSE, FALSE);
2718 }
2719 
2720 /*
2721  * call-seq:
2722  * local_jump_error.exit_value -> obj
2723  *
2724  * Returns the exit value associated with this +LocalJumpError+.
2725  */
2726 static VALUE
2728 {
2729  return rb_iv_get(exc, "@exit_value");
2730 }
2731 
2732 /*
2733  * call-seq:
2734  * local_jump_error.reason -> symbol
2735  *
2736  * The reason this block was terminated:
2737  * :break, :redo, :retry, :next, :return, or :noreason.
2738  */
2739 
2740 static VALUE
2742 {
2743  return rb_iv_get(exc, "@reason");
2744 }
2745 
2746 rb_cref_t *rb_vm_cref_new_toplevel(void); /* vm.c */
2747 
2748 static const rb_env_t *
2749 env_clone(const rb_env_t *env, const rb_cref_t *cref)
2750 {
2751  VALUE *new_ep;
2752  VALUE *new_body;
2753  const rb_env_t *new_env;
2754 
2755  VM_ASSERT(env->ep > env->env);
2756  VM_ASSERT(VM_ENV_ESCAPED_P(env->ep));
2757 
2758  if (cref == NULL) {
2759  cref = rb_vm_cref_new_toplevel();
2760  }
2761 
2762  new_body = ALLOC_N(VALUE, env->env_size);
2763  MEMCPY(new_body, env->env, VALUE, env->env_size);
2764  new_ep = &new_body[env->ep - env->env];
2765  new_env = vm_env_new(new_ep, new_body, env->env_size, env->iseq);
2766  RB_OBJ_WRITE(new_env, &new_ep[VM_ENV_DATA_INDEX_ME_CREF], (VALUE)cref);
2767  VM_ASSERT(VM_ENV_ESCAPED_P(new_ep));
2768  return new_env;
2769 }
2770 
2771 /*
2772  * call-seq:
2773  * prc.binding -> binding
2774  *
2775  * Returns the binding associated with <i>prc</i>. Note that
2776  * <code>Kernel#eval</code> accepts either a <code>Proc</code> or a
2777  * <code>Binding</code> object as its second parameter.
2778  *
2779  * def fred(param)
2780  * proc {}
2781  * end
2782  *
2783  * b = fred(99)
2784  * eval("param", b.binding) #=> 99
2785  */
2786 static VALUE
2788 {
2789  VALUE bindval, binding_self = Qundef;
2790  rb_binding_t *bind;
2791  const rb_proc_t *proc;
2792  const rb_iseq_t *iseq = NULL;
2793  const struct rb_block *block;
2794  const rb_env_t *env = NULL;
2795 
2796  GetProcPtr(self, proc);
2797  block = &proc->block;
2798 
2799  again:
2800  switch (vm_block_type(block)) {
2801  case block_type_iseq:
2802  iseq = block->as.captured.code.iseq;
2803  binding_self = block->as.captured.self;
2804  env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
2805  break;
2806  case block_type_proc:
2807  GetProcPtr(block->as.proc, proc);
2808  block = &proc->block;
2809  goto again;
2810  case block_type_symbol:
2811  goto error;
2812  case block_type_ifunc:
2813  {
2814  const struct vm_ifunc *ifunc = block->as.captured.code.ifunc;
2815  if (IS_METHOD_PROC_IFUNC(ifunc)) {
2816  VALUE method = (VALUE)ifunc->data;
2817  binding_self = method_receiver(method);
2818  iseq = rb_method_iseq(method);
2819  env = VM_ENV_ENVVAL_PTR(block->as.captured.ep);
2820  env = env_clone(env, method_cref(method));
2821  /* set empty iseq */
2822  RB_OBJ_WRITE(env, &env->iseq, rb_iseq_new(NULL, rb_str_new2("<empty iseq>"), rb_str_new2("<empty_iseq>"), Qnil, 0, ISEQ_TYPE_TOP));
2823  break;
2824  }
2825  else {
2826  error:
2827  rb_raise(rb_eArgError, "Can't create Binding from C level Proc");
2828  return Qnil;
2829  }
2830  }
2831  }
2832 
2833  bindval = rb_binding_alloc(rb_cBinding);
2834  GetBindingPtr(bindval, bind);
2835 
2836  bind->block.as.captured.self = binding_self;
2837  bind->block.as.captured.code.iseq = env->iseq;
2838  bind->block.as.captured.ep = env->ep;
2839 
2840  if (iseq) {
2841  rb_iseq_check(iseq);
2842  bind->path = iseq->body->location.path;
2843  bind->first_lineno = FIX2INT(rb_iseq_first_lineno(iseq));
2844  }
2845  else {
2846  bind->path = Qnil;
2847  bind->first_lineno = 0;
2848  }
2849 
2850  return bindval;
2851 }
2852 
2853 static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc);
2854 
2855 static VALUE
2856 make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
2857 {
2858  VALUE args = rb_ary_new3(3, proc, passed, arity);
2859  rb_proc_t *procp;
2860  int is_lambda;
2861 
2862  GetProcPtr(proc, procp);
2863  is_lambda = procp->is_lambda;
2864  rb_ary_freeze(passed);
2865  rb_ary_freeze(args);
2866  proc = rb_proc_new(curry, args);
2867  GetProcPtr(proc, procp);
2868  procp->is_lambda = is_lambda;
2869  return proc;
2870 }
2871 
2872 static VALUE
2873 curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
2874 {
2875  VALUE proc, passed, arity;
2876  proc = RARRAY_AREF(args, 0);
2877  passed = RARRAY_AREF(args, 1);
2878  arity = RARRAY_AREF(args, 2);
2879 
2880  passed = rb_ary_plus(passed, rb_ary_new4(argc, argv));
2881  rb_ary_freeze(passed);
2882 
2883  if (RARRAY_LEN(passed) < FIX2INT(arity)) {
2884  if (!NIL_P(passed_proc)) {
2885  rb_warn("given block not used");
2886  }
2887  arity = make_curry_proc(proc, passed, arity);
2888  return arity;
2889  }
2890  else {
2891  return rb_proc_call_with_block(proc, check_argc(RARRAY_LEN(passed)), RARRAY_CONST_PTR(passed), passed_proc);
2892  }
2893 }
2894 
2895  /*
2896  * call-seq:
2897  * prc.curry -> a_proc
2898  * prc.curry(arity) -> a_proc
2899  *
2900  * Returns a curried proc. If the optional <i>arity</i> argument is given,
2901  * it determines the number of arguments.
2902  * A curried proc receives some arguments. If a sufficient number of
2903  * arguments are supplied, it passes the supplied arguments to the original
2904  * proc and returns the result. Otherwise, returns another curried proc that
2905  * takes the rest of arguments.
2906  *
2907  * b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
2908  * p b.curry[1][2][3] #=> 6
2909  * p b.curry[1, 2][3, 4] #=> 6
2910  * p b.curry(5)[1][2][3][4][5] #=> 6
2911  * p b.curry(5)[1, 2][3, 4][5] #=> 6
2912  * p b.curry(1)[1] #=> 1
2913  *
2914  * b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
2915  * p b.curry[1][2][3] #=> 6
2916  * p b.curry[1, 2][3, 4] #=> 10
2917  * p b.curry(5)[1][2][3][4][5] #=> 15
2918  * p b.curry(5)[1, 2][3, 4][5] #=> 15
2919  * p b.curry(1)[1] #=> 1
2920  *
2921  * b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
2922  * p b.curry[1][2][3] #=> 6
2923  * p b.curry[1, 2][3, 4] #=> wrong number of arguments (given 4, expected 3)
2924  * p b.curry(5) #=> wrong number of arguments (given 5, expected 3)
2925  * p b.curry(1) #=> wrong number of arguments (given 1, expected 3)
2926  *
2927  * b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
2928  * p b.curry[1][2][3] #=> 6
2929  * p b.curry[1, 2][3, 4] #=> 10
2930  * p b.curry(5)[1][2][3][4][5] #=> 15
2931  * p b.curry(5)[1, 2][3, 4][5] #=> 15
2932  * p b.curry(1) #=> wrong number of arguments (given 1, expected 3)
2933  *
2934  * b = proc { :foo }
2935  * p b.curry[] #=> :foo
2936  */
2937 static VALUE
2938 proc_curry(int argc, const VALUE *argv, VALUE self)
2939 {
2940  int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity);
2941  VALUE arity;
2942 
2943  rb_scan_args(argc, argv, "01", &arity);
2944  if (NIL_P(arity)) {
2945  arity = INT2FIX(min_arity);
2946  }
2947  else {
2948  sarity = FIX2INT(arity);
2949  if (rb_proc_lambda_p(self)) {
2950  rb_check_arity(sarity, min_arity, max_arity);
2951  }
2952  }
2953 
2954  return make_curry_proc(self, rb_ary_new(), arity);
2955 }
2956 
2957 /*
2958  * call-seq:
2959  * meth.curry -> proc
2960  * meth.curry(arity) -> proc
2961  *
2962  * Returns a curried proc based on the method. When the proc is called with a number of
2963  * arguments that is lower than the method's arity, then another curried proc is returned.
2964  * Only when enough arguments have been supplied to satisfy the method signature, will the
2965  * method actually be called.
2966  *
2967  * The optional <i>arity</i> argument should be supplied when currying methods with
2968  * variable arguments to determine how many arguments are needed before the method is
2969  * called.
2970  *
2971  * def foo(a,b,c)
2972  * [a, b, c]
2973  * end
2974  *
2975  * proc = self.method(:foo).curry
2976  * proc2 = proc.call(1, 2) #=> #<Proc>
2977  * proc2.call(3) #=> [1,2,3]
2978  *
2979  * def vararg(*args)
2980  * args
2981  * end
2982  *
2983  * proc = self.method(:vararg).curry(4)
2984  * proc2 = proc.call(:x) #=> #<Proc>
2985  * proc3 = proc2.call(:y, :z) #=> #<Proc>
2986  * proc3.call(:a) #=> [:x, :y, :z, :a]
2987  */
2988 
2989 static VALUE
2990 rb_method_curry(int argc, const VALUE *argv, VALUE self)
2991 {
2992  VALUE proc = method_to_proc(self);
2993  return proc_curry(argc, argv, proc);
2994 }
2995 
2996 /*
2997  * Document-class: LocalJumpError
2998  *
2999  * Raised when Ruby can't yield as requested.
3000  *
3001  * A typical scenario is attempting to yield when no block is given:
3002  *
3003  * def call_block
3004  * yield 42
3005  * end
3006  * call_block
3007  *
3008  * <em>raises the exception:</em>
3009  *
3010  * LocalJumpError: no block given (yield)
3011  *
3012  * A more subtle example:
3013  *
3014  * def get_me_a_return
3015  * Proc.new { return 42 }
3016  * end
3017  * get_me_a_return.call
3018  *
3019  * <em>raises the exception:</em>
3020  *
3021  * LocalJumpError: unexpected return
3022  */
3023 
3024 /*
3025  * Document-class: SystemStackError
3026  *
3027  * Raised in case of a stack overflow.
3028  *
3029  * def me_myself_and_i
3030  * me_myself_and_i
3031  * end
3032  * me_myself_and_i
3033  *
3034  * <em>raises the exception:</em>
3035  *
3036  * SystemStackError: stack level too deep
3037  */
3038 
3039 /*
3040  * <code>Proc</code> objects are blocks of code that have been bound to
3041  * a set of local variables. Once bound, the code may be called in
3042  * different contexts and still access those variables.
3043  *
3044  * def gen_times(factor)
3045  * return Proc.new {|n| n*factor }
3046  * end
3047  *
3048  * times3 = gen_times(3)
3049  * times5 = gen_times(5)
3050  *
3051  * times3.call(12) #=> 36
3052  * times5.call(5) #=> 25
3053  * times3.call(times5.call(4)) #=> 60
3054  *
3055  */
3056 
3057 void
3059 {
3060  /* Proc */
3061  rb_cProc = rb_define_class("Proc", rb_cObject);
3064 
3066  (void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
3068  (void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
3070  (void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
3072  (void *)OPTIMIZED_METHOD_TYPE_CALL, METHOD_VISI_PUBLIC);
3073 
3074 #if 0 /* for RDoc */
3075  rb_define_method(rb_cProc, "call", proc_call, -1);
3076  rb_define_method(rb_cProc, "[]", proc_call, -1);
3077  rb_define_method(rb_cProc, "===", proc_call, -1);
3078  rb_define_method(rb_cProc, "yield", proc_call, -1);
3079 #endif
3080 
3081  rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0);
3082  rb_define_method(rb_cProc, "arity", proc_arity, 0);
3083  rb_define_method(rb_cProc, "clone", proc_clone, 0);
3084  rb_define_method(rb_cProc, "dup", proc_dup, 0);
3085  rb_define_method(rb_cProc, "hash", proc_hash, 0);
3086  rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
3087  rb_define_alias(rb_cProc, "inspect", "to_s");
3088  rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0);
3089  rb_define_method(rb_cProc, "binding", proc_binding, 0);
3090  rb_define_method(rb_cProc, "curry", proc_curry, -1);
3091  rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);
3092  rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0);
3093 
3094  /* Exceptions */
3098 
3099  rb_eSysStackError = rb_define_class("SystemStackError", rb_eException);
3101 
3102  /* utility functions */
3105 
3106  /* Method */
3107  rb_cMethod = rb_define_class("Method", rb_cObject);
3111  rb_define_method(rb_cMethod, "eql?", method_eq, 1);
3118  rb_define_method(rb_cMethod, "inspect", method_inspect, 0);
3120  rb_define_method(rb_cMethod, "to_proc", method_to_proc, 0);
3121  rb_define_method(rb_cMethod, "receiver", method_receiver, 0);
3123  rb_define_method(rb_cMethod, "original_name", method_original_name, 0);
3125  rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
3126  rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
3128  rb_define_method(rb_cMethod, "super_method", method_super_method, 0);
3129  rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
3130  rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
3131  rb_define_method(rb_mKernel, "singleton_method", rb_obj_singleton_method, 1);
3132 
3133  /* UnboundMethod */
3134  rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject);
3148  rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);
3151 
3152  /* Module#*_method */
3153  rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);
3154  rb_define_method(rb_cModule, "public_instance_method", rb_mod_public_instance_method, 1);
3156 
3157  /* Kernel */
3158  rb_define_method(rb_mKernel, "define_singleton_method", rb_obj_define_method, -1);
3159 
3161  "define_method", top_define_method, -1);
3162 }
3163 
3164 /*
3165  * Objects of class <code>Binding</code> encapsulate the execution
3166  * context at some particular place in the code and retain this context
3167  * for future use. The variables, methods, value of <code>self</code>,
3168  * and possibly an iterator block that can be accessed in this context
3169  * are all retained. Binding objects can be created using
3170  * <code>Kernel#binding</code>, and are made available to the callback
3171  * of <code>Kernel#set_trace_func</code>.
3172  *
3173  * These binding objects can be passed as the second argument of the
3174  * <code>Kernel#eval</code> method, establishing an environment for the
3175  * evaluation.
3176  *
3177  * class Demo
3178  * def initialize(n)
3179  * @secret = n
3180  * end
3181  * def get_binding
3182  * binding
3183  * end
3184  * end
3185  *
3186  * k1 = Demo.new(99)
3187  * b1 = k1.get_binding
3188  * k2 = Demo.new(-3)
3189  * b2 = k2.get_binding
3190  *
3191  * eval("@secret", b1) #=> 99
3192  * eval("@secret", b2) #=> -3
3193  * eval("@secret") #=> nil
3194  *
3195  * Binding objects have no class-specific methods.
3196  *
3197  */
3198 
3199 void
3201 {
3202  rb_cBinding = rb_define_class("Binding", rb_cObject);
3207  rb_define_method(rb_cBinding, "eval", bind_eval, -1);
3208  rb_define_method(rb_cBinding, "local_variables", bind_local_variables, 0);
3209  rb_define_method(rb_cBinding, "local_variable_get", bind_local_variable_get, 1);
3210  rb_define_method(rb_cBinding, "local_variable_set", bind_local_variable_set, 2);
3211  rb_define_method(rb_cBinding, "local_variable_defined?", bind_local_variable_defined_p, 1);
3212  rb_define_method(rb_cBinding, "receiver", bind_receiver, 0);
3213  rb_define_global_function("binding", rb_f_binding, 0);
3214 }
#define UNDEFINED_REFINED_METHOD_P(def)
Definition: method.h:172
static VALUE method_name(VALUE obj)
Definition: proc.c:1609
static VALUE bind_local_variables(VALUE bindval)
Definition: proc.c:468
static VALUE bind_local_variable_set(VALUE bindval, VALUE sym, VALUE val)
Definition: proc.c:544
rb_control_frame_t * cfp
Definition: vm_core.h:708
const rb_iseq_t * rb_method_iseq(VALUE method)
Definition: proc.c:2467
static int VM_ENV_ESCAPED_P(const VALUE *ep)
Definition: vm_core.h:1097
VALUE rb_eStandardError
Definition: error.c:760
VALUE rb_eLocalJumpError
Definition: eval.c:24
static int method_min_max_arity(VALUE, int *max)
Definition: proc.c:2411
static VALUE rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
Definition: proc.c:2006
static const rb_method_entry_t * original_method_entry(VALUE mod, ID id)
Definition: proc.c:2397
#define UNDEFINED_METHOD_ENTRY_P(me)
Definition: method.h:171
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:923
rb_iseq_t * rb_iseq_new(NODE *node, VALUE name, VALUE path, VALUE absolute_path, const rb_iseq_t *parent, enum iseq_type type)
Definition: iseq.c:436
const VALUE * ep
Definition: vm_core.h:636
static VALUE rb_method_curry(int argc, const VALUE *argv, VALUE self)
Definition: proc.c:2990
static size_t binding_memsize(const void *ptr)
Definition: proc.c:292
int rb_block_min_max_arity(int *max)
Definition: proc.c:1075
VALUE rb_proc_alloc(VALUE klass)
Definition: proc.c:110
VALUE rb_vm_call(rb_thread_t *th, VALUE recv, VALUE id, int argc, const VALUE *argv, const rb_callable_method_entry_t *me)
Definition: vm_eval.c:255
static VALUE method_arity_m(VALUE method)
Definition: proc.c:2381
static void vm_passed_block_handler_set(rb_thread_t *th, VALUE block_handler)
Definition: eval_intern.h:8
VALUE rb_obj_public_method(VALUE obj, VALUE vid)
Definition: proc.c:1736
#define RARRAY_LEN(a)
Definition: ruby.h:1026
void rb_bug(const char *fmt,...)
Definition: error.c:482
rb_method_type_t type
Definition: method.h:148
rb_method_entry_t * rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *, rb_method_visibility_t noex)
Definition: vm_method.c:666
static VALUE proc_to_proc(VALUE self)
Definition: proc.c:1318
#define FALSE
Definition: nkf.h:174
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1145
rb_method_attr_t attr
Definition: method.h:155
VALUE(* rb_block_call_func_t)(ANYARGS)
Definition: ruby.h:1837
static VALUE proc_hash(VALUE self)
Definition: proc.c:1243
rb_proc_t basic
Definition: proc.c:86
static const rb_data_type_t proc_data_type
Definition: proc.c:99
const VALUE klass
Definition: proc.c:31
void rb_print_undef(VALUE klass, ID id, rb_method_visibility_t visi)
Definition: eval_error.c:197
VALUE rb_ary_freeze(VALUE ary)
Definition: array.c:403
static VALUE bind_eval(int argc, VALUE *argv, VALUE bindval)
Definition: proc.c:385
static VALUE umethod_bind(VALUE method, VALUE recv)
Definition: proc.c:2238
static VALUE mnew_from_me(const rb_method_entry_t *me, VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
Definition: proc.c:1447
static VALUE method_original_name(VALUE obj)
Definition: proc.c:1625
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *me)
Definition: vm_method.c:402
static int max(int a, int b)
Definition: strftime.c:142
static unsigned int hash(str, len) register const char *str
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:681
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
#define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp)
Definition: vm_core.h:1172
#define GetProcPtr(obj, ptr)
Definition: vm_core.h:863
const VALUE location
Definition: method.h:135
int8_t safe_level
Definition: vm_core.h:868
#define CLASS_OF(v)
Definition: ruby.h:453
#define rb_name_err_raise_str(mesg, recv, name)
Definition: internal.h:1022
#define T_MODULE
Definition: ruby.h:494
rb_cref_t *const cref
Definition: method.h:124
int rb_mod_method_arity(VALUE mod, ID id)
Definition: proc.c:2420
const VALUE * env
Definition: vm_core.h:877
VALUE symbol
Definition: vm_core.h:625
#define Qtrue
Definition: ruby.h:437
static const rb_iseq_t * rb_iseq_check(const rb_iseq_t *iseq)
Definition: vm_core.h:416
static int rb_method_entry_min_max_arity(const rb_method_entry_t *me, int *max)
Definition: proc.c:2286
st_index_t rb_hash_end(st_index_t)
static VALUE proc_curry(int argc, const VALUE *argv, VALUE self)
Definition: proc.c:2938
static void proc_mark(void *ptr)
Definition: proc.c:78
#define rb_id2str(id)
Definition: vm_backtrace.c:29
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1190
static const VALUE * get_local_variable_ptr(const rb_env_t **envp, ID lid)
Definition: proc.c:395
const int id
Definition: nkf.c:209
static const rb_iseq_t * vm_proc_iseq(VALUE procval)
Definition: vm_core.h:1322
struct rb_iseq_constant_body::@196::@197 flags
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
static const rb_method_definition_t * method_def(VALUE method)
Definition: proc.c:2434
VALUE rb_iterate(VALUE(*)(VALUE), VALUE, VALUE(*)(ANYARGS), VALUE)
Definition: vm_eval.c:1202
void Init_Binding(void)
Definition: proc.c:3200
static VALUE localjump_reason(VALUE exc)
Definition: proc.c:2741
VALUE rb_eTypeError
Definition: error.c:762
#define TH_JUMP_TAG(th, st)
Definition: eval_intern.h:186
#define rb_check_arity
Definition: intern.h:303
VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, int argc, const VALUE *argv, VALUE passed_block_handler)
Definition: vm.c:1150
#define UNREACHABLE
Definition: ruby.h:46
const rb_iseq_t * iseq
Definition: vm_core.h:875
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
#define VM_BLOCK_HANDLER_NONE
Definition: vm_core.h:1070
VALUE rb_str_buf_new2(const char *)
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:54
#define SYM2ID(x)
Definition: ruby.h:384
static int rb_proc_min_max_arity(VALUE self, int *max)
Definition: proc.c:1001
const VALUE owner
Definition: method.h:55
rb_method_entry_t * rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, const rb_method_definition_t *def)
Definition: vm_method.c:393
static VALUE VM_BH_TO_SYMBOL(VALUE block_handler)
Definition: vm_core.h:1377
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:532
static VALUE method_to_proc(VALUE method)
Definition: proc.c:2677
struct rb_iseq_constant_body * body
Definition: vm_core.h:395
VALUE rb_cBinding
Definition: proc.c:38
static VALUE method_clone(VALUE self)
Definition: proc.c:2054
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
VALUE rb_proc_lambda_p(VALUE procval)
Definition: proc.c:255
#define RBASIC_SET_CLASS(obj, cls)
Definition: internal.h:1314
VALUE rb_iv_get(VALUE, const char *)
Definition: variable.c:3130
const VALUE * ep
Definition: vm_core.h:876
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1260
#define RUBY_MARK_LEAVE(msg)
Definition: gc.h:54
ID called_id
Definition: method.h:54
#define TH_EXEC_TAG()
Definition: eval_intern.h:180
struct rb_iseq_constant_body::@196 param
parameter information
#define RB_GC_GUARD(v)
Definition: ruby.h:552
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:690
VALUE rb_obj_method_location(VALUE obj, ID id)
Definition: proc.c:2515
static VALUE unnamed_parameters(int arity)
Definition: proc.c:1153
VALUE rb_cUnboundMethod
Definition: proc.c:36
#define DATA_PTR(dta)
Definition: ruby.h:1113
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4394
void rb_method_name_error(VALUE klass, VALUE str)
Definition: proc.c:1649
void Init_Proc(void)
Definition: proc.c:3058
static VALUE proc_clone(VALUE self)
Definition: proc.c:146
st_data_t st_index_t
Definition: st.h:50
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1745
static VALUE method_super_method(VALUE method)
Definition: proc.c:2706
VALUE rb_f_eval(int argc, const VALUE *argv, VALUE self)
Definition: vm_eval.c:1436
static const rb_cref_t * method_cref(VALUE method)
Definition: proc.c:2473
VALUE rb_method_call(int argc, const VALUE *argv, VALUE method)
Definition: proc.c:2082
static VALUE method_inspect(VALUE method)
Definition: proc.c:2574
static VALUE rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
Definition: proc.c:771
VALUE env[VM_ENV_DATA_SIZE+1]
Definition: proc.c:87
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1533
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:2802
static VALUE proc_dup(VALUE self)
Definition: proc.c:132
static size_t bm_memsize(const void *ptr)
Definition: proc.c:1333
int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
Definition: vm_method.c:1421
#define rb_name_err_raise(mesg, recv, name)
Definition: internal.h:1024
#define OBJ_TAINTED(x)
Definition: ruby.h:1298
#define VM_ENV_DATA_INDEX_ME_CREF
Definition: vm_core.h:990
VALUE rb_eRangeError
Definition: error.c:766
const char * rb_obj_classname(VALUE)
Definition: variable.c:458
#define rb_ary_new2
Definition: intern.h:90
rb_method_iseq_t iseq
Definition: method.h:153
union rb_block::@203 as
const VALUE * ep
Definition: vm_core.h:600
static int rb_vm_block_min_max_arity(const struct rb_block *block, int *max)
Definition: proc.c:970
#define GET_THREAD()
Definition: vm_core.h:1513
#define sym(x)
Definition: date_core.c:3721
VALUE rb_proc_new(VALUE(*func)(ANYARGS), VALUE val)
Definition: proc.c:2661
static void block_setup(struct rb_block *block, VALUE block_handler)
Definition: proc.c:1019
static VALUE method_hash(VALUE method)
Definition: proc.c:1546
static VALUE method_eq(VALUE method, VALUE other)
Definition: proc.c:1509
static VALUE call_method_data_safe(rb_thread_t *th, const struct METHOD *data, int argc, const VALUE *argv, VALUE passed_procval, int safe)
Definition: proc.c:2105
static const struct rb_captured_block * VM_BH_TO_CAPT_BLOCK(VALUE block_handler)
Definition: vm_core.h:1246
static int method_arity(VALUE)
Definition: proc.c:2388
#define FL_SINGLETON
Definition: ruby.h:1215
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
Definition: class.c:1689
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
#define attached
Definition: proc.c:45
static VALUE mnew_internal(const rb_method_entry_t *me, VALUE klass, VALUE obj, ID id, VALUE mclass, int scope, int error)
Definition: proc.c:1396
#define TH_POP_TAG()
Definition: eval_intern.h:137
struct rb_block block
Definition: vm_core.h:887
VALUE rb_proc_create(VALUE klass, const struct rb_block *block, int8_t safe_level, int8_t is_from_method, int8_t is_lambda)
Definition: vm.c:830
rb_method_cfunc_t cfunc
Definition: method.h:154
rb_cref_t * rb_vm_cref_new_toplevel(void)
Definition: vm.c:253
#define FL_TEST(x, f)
Definition: ruby.h:1284
#define RUBY_TYPED_WB_PROTECTED
Definition: ruby.h:1146
unsigned short first_lineno
Definition: vm_core.h:889
VALUE rb_obj_is_method(VALUE m)
Definition: proc.c:1349
#define rb_intern_str(string)
Definition: generator.h:16
VALUE rb_class_name(VALUE)
Definition: variable.c:443
static VALUE rb_mod_instance_method(VALUE mod, VALUE vid)
Definition: proc.c:1823
#define RUBY_SAFE_LEVEL_MAX
Definition: ruby.h:599
static int rb_obj_is_iseq(VALUE iseq)
Definition: vm_core.h:1043
#define ALLOC_N(type, n)
Definition: ruby.h:1587
int rb_block_given_p(void)
Definition: eval.c:797
const rb_env_t * rb_vm_env_prev_env(const rb_env_t *env)
Definition: vm.c:739
rb_method_alias_t alias
Definition: method.h:156
#define val
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
VALUE rb_eSysStackError
Definition: eval.c:25
Definition: proc.c:29
VALUE rb_vm_make_proc_lambda(rb_thread_t *th, const struct rb_captured_block *captured, VALUE klass, int8_t is_lambda)
Definition: vm.c:869
#define GetBindingPtr(obj, ptr)
Definition: vm_core.h:883
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:720
VALUE rb_ary_new(void)
Definition: array.c:493
VALUE rb_str_buf_cat2(VALUE, const char *)
RUBY_EXTERN VALUE rb_mKernel
Definition: ruby.h:1860
VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq)
Definition: iseq.c:694
#define RCLASS_ORIGIN(c)
Definition: internal.h:693
#define NIL_P(v)
Definition: ruby.h:451
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:646
#define NO_CLOBBERED(v)
Definition: proc.c:22
static VALUE proc_arity(VALUE self)
Definition: proc.c:953
struct vm_ifunc_argc argc
Definition: internal.h:805
rb_method_visibility_t
Definition: method.h:26
struct rb_method_definition_struct *const def
Definition: method.h:53
static VALUE sym_proc_new(VALUE klass, VALUE sym)
Definition: proc.c:635
void rb_print_inaccessible(VALUE klass, ID id, rb_method_visibility_t visi)
Definition: eval_error.c:225
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:799
static VALUE rb_proc_parameters(VALUE self)
Definition: proc.c:1182
static const struct rb_captured_block * VM_BH_TO_ISEQ_BLOCK(VALUE block_handler)
Definition: vm_core.h:1206
#define RUBY_MARK_ENTER(msg)
Definition: gc.h:53
static VALUE call_method_data(rb_thread_t *th, const struct METHOD *data, int argc, const VALUE *argv, VALUE passed_procval)
Definition: proc.c:2096
int argc
Definition: ruby.c:183
void rb_vm_register_special_exception(enum ruby_special_exceptions sp, VALUE cls, const char *mesg)
Definition: vm.c:2142
#define Qfalse
Definition: ruby.h:436
#define undefined
Definition: vm_method.c:36
const rb_data_type_t ruby_binding_data_type
Definition: proc.c:297
#define CLONESETUP(clone, obj)
Definition: ruby.h:756
VALUE rb_binding_new(void)
Definition: proc.c:340
static void vm_block_type_set(const struct rb_block *block, enum rb_block_type type)
Definition: vm_core.h:1304
static unsigned long VM_ENV_FLAGS(const VALUE *ep, long flag)
Definition: vm_core.h:1017
Definition: method.h:50
RUBY_EXTERN VALUE rb_cModule
Definition: ruby.h:1895
Definition: method.h:58
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:6154
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1661
static const rb_env_t * vm_env_new(VALUE *env_ep, VALUE *env_body, unsigned int env_size, const rb_iseq_t *iseq)
Definition: vm_core.h:1139
#define rb_ary_new4
Definition: intern.h:92
int8_t is_from_method
Definition: vm_core.h:869
#define rb_str_new2
Definition: intern.h:857
#define OBJ_FREEZE(x)
Definition: ruby.h:1308
const struct rb_iseq_constant_body::@196::rb_iseq_param_keyword * keyword
static VALUE rb_method_parameters(VALUE method)
Definition: proc.c:2554
VALUE rb_block_proc(void)
Definition: proc.c:787
static VALUE rb_f_binding(VALUE self)
Definition: proc.c:363
static const rb_env_t * VM_ENV_ENVVAL_PTR(const VALUE *ep)
Definition: vm_core.h:1123
Definition: util.c:833
#define VM_ENV_DATA_INDEX_SPECVAL
Definition: vm_core.h:991
const rb_method_entry_t *const me
Definition: proc.c:32
static VALUE rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
Definition: proc.c:1887
static VALUE proc_to_block_handler(VALUE procval)
Definition: proc.c:893
VALUE rb_block_lambda(void)
Definition: proc.c:801
static VALUE proc_to_s_(VALUE self, const rb_proc_t *proc)
Definition: proc.c:1269
static VALUE vm_block_self(const struct rb_block *block)
Definition: vm_core.h:1361
VALUE rb_mod_method_location(VALUE mod, ID id)
Definition: proc.c:2508
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1758
#define ZALLOC(type)
Definition: ruby.h:1590
static VALUE bmcall(VALUE, VALUE, int, VALUE *, VALUE)
Definition: proc.c:2655
#define RARRAY_CONST_PTR(a)
Definition: ruby.h:1028
static const struct rb_block * vm_proc_block(VALUE procval)
Definition: vm_core.h:1311
union rb_method_definition_struct::@144 body
#define TRUE
Definition: nkf.h:175
VALUE rb_func_proc_new(rb_block_call_func_t func, VALUE val)
Definition: proc.c:676
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
static void block_mark(const struct rb_block *block)
Definition: proc.c:54
static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
Definition: proc.c:2873
VALUE rb_include_class_new(VALUE module, VALUE super)
Definition: class.c:818
#define VM_ASSERT(expr)
Definition: vm_core.h:54
const rb_method_entry_t * rb_method_entry(VALUE klass, ID id)
Definition: vm_method.c:796
static const rb_iseq_t * method_def_iseq(const rb_method_definition_t *def)
Definition: proc.c:2443
static enum rb_block_handler_type vm_block_handler_type(VALUE block_handler)
Definition: vm_core.h:1254
static void binding_free(void *ptr)
Definition: proc.c:266
VALUE proc
Definition: vm_core.h:626
void ruby_xfree(void *x)
Definition: gc.c:8017
#define VM_ENV_DATA_SIZE
Definition: vm_core.h:988
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
static VALUE rb_mod_public_instance_method(VALUE mod, VALUE vid)
Definition: proc.c:1840
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
VALUE rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp)
Definition: vm.c:889
unsigned int env_size
Definition: vm_core.h:878
const VALUE * rb_binding_add_dynavars(rb_binding_t *bind, int dyncount, const ID *dynvars)
Definition: vm.c:920
static const VALUE * vm_block_ep(const struct rb_block *block)
Definition: vm_core.h:1348
#define Qnil
Definition: ruby.h:438
st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
Definition: vm_method.c:1520
static VALUE mnew_missing(VALUE klass, VALUE obj, ID id, VALUE mclass)
Definition: proc.c:1372
VALUE rb_cMethod
Definition: proc.c:37
#define METHOD_ENTRY_VISI(me)
Definition: method.h:66
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
int rb_obj_method_arity(VALUE obj, ID id)
Definition: proc.c:2428
#define OBJ_TAINT(x)
Definition: ruby.h:1300
unsigned long VALUE
Definition: ruby.h:85
static VALUE proc_binding(VALUE self)
Definition: proc.c:2787
const rb_cref_t * rb_vm_cref_in_context(VALUE self, VALUE cbase)
Definition: vm.c:1321
VALUE rb_vm_top_self(void)
Definition: vm.c:3151
static VALUE cfunc_proc_new(VALUE klass, VALUE ifunc, int8_t is_lambda)
Definition: proc.c:612
static VALUE binding_dup(VALUE self)
Definition: proc.c:318
static VALUE result
Definition: nkf.c:40
const VALUE defined_class
Definition: method.h:52
rb_method_visibility_t method_visi
Definition: method.h:36
#define FIX2INT(x)
Definition: ruby.h:686
static enum rb_block_type vm_block_type(const struct rb_block *block)
Definition: vm_core.h:1280
const struct rb_method_entry_struct *const original_me
Definition: method.h:139
#define VM_ENV_DATA_INDEX_FLAGS
Definition: vm_core.h:992
static struct vm_ifunc * rb_vm_ifunc_proc_new(VALUE(*func)(ANYARGS), const void *data)
Definition: internal.h:811
#define rb_ary_new3
Definition: intern.h:91
#define TH_PUSH_TAG(th)
Definition: eval_intern.h:131
static VALUE method_owner(VALUE obj)
Definition: proc.c:1641
const struct vm_ifunc * ifunc
Definition: vm_core.h:603
#define IS_METHOD_PROC_IFUNC(ifunc)
Definition: proc.c:49
static VALUE method_receiver(VALUE obj)
Definition: proc.c:1593
#define ST2FIX(h)
Definition: ruby.h:1579
const rb_method_entry_t * rb_method_entry_at(VALUE obj, ID id)
Definition: vm_method.c:714
#define MSG(s)
static VALUE make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
Definition: proc.c:2856
#define CHAR_BIT
Definition: ruby.h:196
struct rb_captured_block captured
Definition: vm_core.h:624
static VALUE mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
Definition: proc.c:1454
#define rb_funcallv
Definition: console.c:21
#define VM_ENV_DATA_INDEX_ENV
Definition: vm_core.h:993
const VALUE recv
Definition: proc.c:30
void rb_set_safe_level_force(int)
Definition: safe.c:41
const rb_callable_method_entry_t * rb_callable_method_entry_without_refinements(VALUE klass, ID id)
Definition: vm_method.c:887
static VALUE mlambda(VALUE method)
Definition: proc.c:2649
int8_t is_lambda
Definition: vm_core.h:870
VALUE rb_mRubyVMFrozenCore
Definition: vm.c:312
static VALUE VM_BH_TO_PROC(VALUE block_handler)
Definition: vm_core.h:1391
VALUE rb_vm_frame_block_handler(const rb_control_frame_t *cfp)
Definition: vm.c:82
static const rb_env_t * env_clone(const rb_env_t *env, const rb_cref_t *cref)
Definition: proc.c:2749
VALUE rb_proc_location(VALUE self)
Definition: proc.c:1147
#define INT2FIX(i)
Definition: ruby.h:232
VALUE top_wrapper
Definition: vm_core.h:727
#define UNLIMITED_ARGUMENTS
Definition: intern.h:44
const VALUE * rb_vm_ep_local_ep(const VALUE *ep)
Definition: vm.c:55
#define RCLASS_SUPER(c)
Definition: classext.h:16
st_index_t rb_hash_proc(st_index_t hash, VALUE prc)
Definition: proc.c:1193
const rb_callable_method_entry_t * rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
Definition: vm_method.c:411
int rb_safe_level(void)
Definition: safe.c:35
static void bm_mark(void *ptr)
Definition: proc.c:1324
static VALUE bind_local_variable_defined_p(VALUE bindval, VALUE sym)
Definition: proc.c:584
#define RARRAY_AREF(a, i)
Definition: ruby.h:1040
static int respond_to_missing_p(VALUE klass, VALUE obj, VALUE sym, int scope)
Definition: proc.c:1360
VALUE rb_method_location(VALUE method)
Definition: proc.c:2529
VALUE rb_ary_plus(VALUE x, VALUE y)
Definition: array.c:3621
#define RBASIC_CLASS(obj)
Definition: ruby.h:878
#define ANYARGS
Definition: defines.h:173
#define RUBY_FREE_LEAVE(msg)
Definition: gc.h:56
const rb_iseq_t * rb_proc_get_iseq(VALUE self, int *is_proc)
Definition: proc.c:1091
#define RARRAY_PTR(a)
Definition: ruby.h:1048
#define RUBY_FREE_ENTER(msg)
Definition: gc.h:55
static VALUE VM_ENV_PROCVAL(const VALUE *ep)
Definition: vm_core.h:1129
VALUE rb_func_lambda_new(rb_block_call_func_t func, VALUE val, int min_argc, int max_argc)
Definition: proc.c:683
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1480
VALUE rb_proc_call(VALUE self, VALUE args)
Definition: proc.c:879
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:1658
const struct rb_block block
Definition: vm_core.h:867
#define SIZEOF_VALUE
Definition: ruby.h:88
#define RTEST(v)
Definition: ruby.h:450
VALUE rb_obj_singleton_method(VALUE obj, VALUE vid)
Definition: proc.c:1766
rb_method_entry_t * rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *option, rb_method_visibility_t visi)
Definition: vm_method.c:631
#define OBJ_INFECT(x, s)
Definition: ruby.h:1304
VALUE rb_proc_call_with_block(VALUE self, int argc, const VALUE *argv, VALUE passed_procval)
Definition: proc.c:899
st_index_t rb_hash_uint(st_index_t, st_index_t)
VALUE rb_sym_to_proc(VALUE sym)
Definition: proc.c:1203
int rb_is_local_name(VALUE name)
Definition: symbol.c:1114
int rb_method_basic_definition_p(VALUE, ID)
Definition: vm_method.c:1880
#define RUBY_MARK_UNLESS_NULL(ptr)
Definition: gc.h:60
VALUE rb_method_call_with_block(int argc, const VALUE *argv, VALUE method, VALUE passed_procval)
Definition: proc.c:2126
struct vm_ifunc * rb_vm_ifunc_new(VALUE(*func)(ANYARGS), const void *data, int min_argc, int max_argc)
Definition: proc.c:647
void rb_obj_call_init(VALUE obj, int argc, const VALUE *argv)
Definition: eval.c:1422
#define VM_UNREACHABLE(func)
Definition: vm_core.h:55
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1182
VALUE rb_ary_dup(VALUE ary)
Definition: array.c:1927
int rb_is_local_id(ID id)
Definition: symbol.c:858
VALUE rb_obj_method(VALUE obj, VALUE vid)
Definition: proc.c:1723
const rb_iseq_t * iseq
Definition: vm_core.h:602
const void * data
Definition: internal.h:804
#define T_CLASS
Definition: ruby.h:492
static VALUE bind_receiver(VALUE bindval)
Definition: proc.c:604
static VALUE binding_clone(VALUE self)
Definition: proc.c:332
static VALUE proc_new(VALUE klass, int8_t is_lambda)
Definition: proc.c:692
static const char proc_without_block[]
Definition: proc.c:689
const char * name
Definition: nkf.c:208
#define ID2SYM(x)
Definition: ruby.h:383
static VALUE proc_to_s(VALUE self)
Definition: proc.c:1261
static const rb_data_type_t method_data_type
Definition: proc.c:1338
static ID check_local_id(VALUE bindval, volatile VALUE *pname)
Definition: proc.c:428
int rb_proc_arity(VALUE self)
Definition: proc.c:1009
static VALUE bind_local_variable_get(VALUE bindval, VALUE sym)
Definition: proc.c:496
enum rb_block_type type
Definition: vm_core.h:628
int rb_block_arity(void)
Definition: proc.c:1041
static VALUE obj_method(VALUE obj, VALUE vid, int scope)
Definition: proc.c:1678
VALUE rb_inspect(VALUE)
Definition: object.c:519
#define check_argc(argc)
Definition: proc.c:875
void rb_warning(const char *fmt,...)
Definition: error.c:250
static VALUE localjump_xvalue(VALUE exc)
Definition: proc.c:2727
static const rb_scope_visibility_t * CREF_SCOPE_VISI(const rb_cref_t *cref)
Definition: eval_intern.h:210
#define CONST_ID(var, str)
Definition: ruby.h:1743
static const rb_callable_method_entry_t * method_callable_method_entry(const struct METHOD *data)
Definition: proc.c:2089
VALUE rb_vm_env_local_variables(const rb_env_t *env)
Definition: vm.c:783
#define RUBY_TYPED_DEFAULT_FREE
Definition: ruby.h:1141
enum rb_method_definition_struct::@144::method_optimized_type optimize_type
const rb_iseq_t *const iseqptr
Definition: method.h:123
static VALUE method_def_location(const rb_method_definition_t *def)
Definition: proc.c:2490
#define rb_intern(str)
static VALUE top_define_method(int argc, VALUE *argv, VALUE obj)
Definition: proc.c:2021
#define mod(x, y)
Definition: date_strftime.c:28
static void binding_mark(void *ptr)
Definition: proc.c:278
VALUE path
Definition: vm_core.h:888
#define env
#define NULL
Definition: _sdbm.c:102
static VALUE VM_ENV_ENVVAL(const VALUE *ep)
Definition: vm_core.h:1114
VALUE rb_method_entry_location(const rb_method_entry_t *me)
Definition: proc.c:2501
#define Qundef
Definition: ruby.h:439
#define T_ICLASS
Definition: ruby.h:493
const rb_method_entry_t * rb_method_entry_without_refinements(VALUE klass, ID id)
Definition: vm_method.c:881
VALUE rb_class_search_ancestor(VALUE klass, VALUE super)
Definition: object.c:710
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
Definition: object.c:1593
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2818
static size_t proc_memsize(const void *ptr)
Definition: proc.c:91
static VALUE method_entry_defined_class(const rb_method_entry_t *me)
Definition: proc.c:1468
static int rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max)
Definition: proc.c:960
VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
Definition: iseq.c:2097
void rb_warn(const char *fmt,...)
Definition: error.c:221
#define Check_TypedStruct(v, t)
Definition: ruby.h:1138
ID rb_to_id(VALUE)
Definition: string.c:9979
static VALUE iseq_location(const rb_iseq_t *iseq)
Definition: proc.c:1126
VALUE rb_eArgError
Definition: error.c:763
VALUE rb_binding_alloc(VALUE klass)
Definition: proc.c:308
union rb_captured_block::@202 code
static VALUE mproc(VALUE method)
Definition: proc.c:2643
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1496
unsigned int module_func
Definition: method.h:37
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1437
VALUE rb_obj_is_proc(VALUE proc)
Definition: proc.c:117
int rb_method_entry_arity(const rb_method_entry_t *me)
Definition: proc.c:2341
char ** argv
Definition: ruby.c:184
VALUE rb_cProc
Definition: proc.c:39
rb_iseq_location_t location
Definition: vm_core.h:358
static VALUE method_unbind(VALUE obj)
Definition: proc.c:1569
static const struct rb_captured_block * VM_BH_TO_IFUNC_BLOCK(VALUE block_handler)
Definition: vm_core.h:1238
VALUE rb_eException
Definition: error.c:755
#define IFUNC_NEW(a, b, c)
Definition: internal.h:808
VALUE rb_obj_class(VALUE)
Definition: object.c:229