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