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