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