Ruby 1.9.3p327(2012-11-10revision37606)
proc.c
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   proc.c - Proc, Binding, Env
00004 
00005   $Author: naruse $
00006   created at: Wed Jan 17 12:13:14 2007
00007 
00008   Copyright (C) 2004-2007 Koichi Sasada
00009 
00010 **********************************************************************/
00011 
00012 #include "eval_intern.h"
00013 #include "internal.h"
00014 #include "gc.h"
00015 #include "iseq.h"
00016 
00017 struct METHOD {
00018     VALUE recv;
00019     VALUE rclass;
00020     ID id;
00021     rb_method_entry_t *me;
00022     struct unlinked_method_entry_list_entry *ume;
00023 };
00024 
00025 VALUE rb_cUnboundMethod;
00026 VALUE rb_cMethod;
00027 VALUE rb_cBinding;
00028 VALUE rb_cProc;
00029 
00030 static VALUE bmcall(VALUE, VALUE);
00031 static int method_arity(VALUE);
00032 
00033 /* Proc */
00034 
00035 #define IS_METHOD_PROC_NODE(node) (nd_type(node) == NODE_IFUNC && (node)->nd_cfnc == bmcall)
00036 
00037 static void
00038 proc_free(void *ptr)
00039 {
00040     RUBY_FREE_ENTER("proc");
00041     if (ptr) {
00042         ruby_xfree(ptr);
00043     }
00044     RUBY_FREE_LEAVE("proc");
00045 }
00046 
00047 static void
00048 proc_mark(void *ptr)
00049 {
00050     rb_proc_t *proc;
00051     RUBY_MARK_ENTER("proc");
00052     if (ptr) {
00053         proc = ptr;
00054         RUBY_MARK_UNLESS_NULL(proc->envval);
00055         RUBY_MARK_UNLESS_NULL(proc->blockprocval);
00056         RUBY_MARK_UNLESS_NULL(proc->block.proc);
00057         RUBY_MARK_UNLESS_NULL(proc->block.self);
00058         if (proc->block.iseq && RUBY_VM_IFUNC_P(proc->block.iseq)) {
00059             RUBY_MARK_UNLESS_NULL((VALUE)(proc->block.iseq));
00060         }
00061     }
00062     RUBY_MARK_LEAVE("proc");
00063 }
00064 
00065 static size_t
00066 proc_memsize(const void *ptr)
00067 {
00068     return ptr ? sizeof(rb_proc_t) : 0;
00069 }
00070 
00071 static const rb_data_type_t proc_data_type = {
00072     "proc",
00073     {
00074         proc_mark,
00075         proc_free,
00076         proc_memsize,
00077     },
00078 };
00079 
00080 VALUE
00081 rb_proc_alloc(VALUE klass)
00082 {
00083     rb_proc_t *proc;
00084     return TypedData_Make_Struct(klass, rb_proc_t, &proc_data_type, proc);
00085 }
00086 
00087 VALUE
00088 rb_obj_is_proc(VALUE proc)
00089 {
00090     if (rb_typeddata_is_kind_of(proc, &proc_data_type)) {
00091         return Qtrue;
00092     }
00093     else {
00094         return Qfalse;
00095     }
00096 }
00097 
00098 /* :nodoc: */
00099 static VALUE
00100 proc_dup(VALUE self)
00101 {
00102     VALUE procval = rb_proc_alloc(rb_cProc);
00103     rb_proc_t *src, *dst;
00104     GetProcPtr(self, src);
00105     GetProcPtr(procval, dst);
00106 
00107     dst->block = src->block;
00108     dst->block.proc = procval;
00109     dst->blockprocval = src->blockprocval;
00110     dst->envval = src->envval;
00111     dst->safe_level = src->safe_level;
00112     dst->is_lambda = src->is_lambda;
00113 
00114     return procval;
00115 }
00116 
00117 /* :nodoc: */
00118 static VALUE
00119 proc_clone(VALUE self)
00120 {
00121     VALUE procval = proc_dup(self);
00122     CLONESETUP(procval, self);
00123     return procval;
00124 }
00125 
00126 /*
00127  * call-seq:
00128  *   prc.lambda? -> true or false
00129  *
00130  * Returns +true+ for a Proc object for which argument handling is rigid.
00131  * Such procs are typically generated by +lambda+.
00132  *
00133  * A Proc object generated by +proc+ ignores extra arguments.
00134  *
00135  *   proc {|a,b| [a,b] }.call(1,2,3)    #=> [1,2]
00136  *
00137  * It provides +nil+ for missing arguments.
00138  *
00139  *   proc {|a,b| [a,b] }.call(1)        #=> [1,nil]
00140  *
00141  * It expands a single array argument.
00142  *
00143  *   proc {|a,b| [a,b] }.call([1,2])    #=> [1,2]
00144  *
00145  * A Proc object generated by +lambda+ doesn't have such tricks.
00146  *
00147  *   lambda {|a,b| [a,b] }.call(1,2,3)  #=> ArgumentError
00148  *   lambda {|a,b| [a,b] }.call(1)      #=> ArgumentError
00149  *   lambda {|a,b| [a,b] }.call([1,2])  #=> ArgumentError
00150  *
00151  * Proc#lambda? is a predicate for the tricks.
00152  * It returns +true+ if no tricks apply.
00153  *
00154  *   lambda {}.lambda?            #=> true
00155  *   proc {}.lambda?              #=> false
00156  *
00157  * Proc.new is the same as +proc+.
00158  *
00159  *   Proc.new {}.lambda?          #=> false
00160  *
00161  * +lambda+, +proc+ and Proc.new preserve the tricks of
00162  * a Proc object given by <code>&</code> argument.
00163  *
00164  *   lambda(&lambda {}).lambda?   #=> true
00165  *   proc(&lambda {}).lambda?     #=> true
00166  *   Proc.new(&lambda {}).lambda? #=> true
00167  *
00168  *   lambda(&proc {}).lambda?     #=> false
00169  *   proc(&proc {}).lambda?       #=> false
00170  *   Proc.new(&proc {}).lambda?   #=> false
00171  *
00172  * A Proc object generated by <code>&</code> argument has the tricks
00173  *
00174  *   def n(&b) b.lambda? end
00175  *   n {}                         #=> false
00176  *
00177  * The <code>&</code> argument preserves the tricks if a Proc object
00178  * is given by <code>&</code> argument.
00179  *
00180  *   n(&lambda {})                #=> true
00181  *   n(&proc {})                  #=> false
00182  *   n(&Proc.new {})              #=> false
00183  *
00184  * A Proc object converted from a method has no tricks.
00185  *
00186  *   def m() end
00187  *   method(:m).to_proc.lambda?   #=> true
00188  *
00189  *   n(&method(:m))               #=> true
00190  *   n(&method(:m).to_proc)       #=> true
00191  *
00192  * +define_method+ is treated the same as method definition.
00193  * The defined method has no tricks.
00194  *
00195  *   class C
00196  *     define_method(:d) {}
00197  *   end
00198  *   C.new.d(1,2)       #=> ArgumentError
00199  *   C.new.method(:d).to_proc.lambda?   #=> true
00200  *
00201  * +define_method+ always defines a method without the tricks,
00202  * even if a non-lambda Proc object is given.
00203  * This is the only exception for which the tricks are not preserved.
00204  *
00205  *   class C
00206  *     define_method(:e, &proc {})
00207  *   end
00208  *   C.new.e(1,2)       #=> ArgumentError
00209  *   C.new.method(:e).to_proc.lambda?   #=> true
00210  *
00211  * This exception insures that methods never have tricks
00212  * and makes it easy to have wrappers to define methods that behave as usual.
00213  *
00214  *   class C
00215  *     def self.def2(name, &body)
00216  *       define_method(name, &body)
00217  *     end
00218  *
00219  *     def2(:f) {}
00220  *   end
00221  *   C.new.f(1,2)       #=> ArgumentError
00222  *
00223  * The wrapper <i>def2</i> defines a method which has no tricks.
00224  *
00225  */
00226 
00227 VALUE
00228 rb_proc_lambda_p(VALUE procval)
00229 {
00230     rb_proc_t *proc;
00231     GetProcPtr(procval, proc);
00232 
00233     return proc->is_lambda ? Qtrue : Qfalse;
00234 }
00235 
00236 /* Binding */
00237 
00238 static void
00239 binding_free(void *ptr)
00240 {
00241     rb_binding_t *bind;
00242     RUBY_FREE_ENTER("binding");
00243     if (ptr) {
00244         bind = ptr;
00245         ruby_xfree(ptr);
00246     }
00247     RUBY_FREE_LEAVE("binding");
00248 }
00249 
00250 static void
00251 binding_mark(void *ptr)
00252 {
00253     rb_binding_t *bind;
00254     RUBY_MARK_ENTER("binding");
00255     if (ptr) {
00256         bind = ptr;
00257         RUBY_MARK_UNLESS_NULL(bind->env);
00258         RUBY_MARK_UNLESS_NULL(bind->filename);
00259     }
00260     RUBY_MARK_LEAVE("binding");
00261 }
00262 
00263 static size_t
00264 binding_memsize(const void *ptr)
00265 {
00266     return ptr ? sizeof(rb_binding_t) : 0;
00267 }
00268 
00269 static const rb_data_type_t binding_data_type = {
00270     "binding",
00271     {
00272         binding_mark,
00273         binding_free,
00274         binding_memsize,
00275     },
00276 };
00277 
00278 static VALUE
00279 binding_alloc(VALUE klass)
00280 {
00281     VALUE obj;
00282     rb_binding_t *bind;
00283     obj = TypedData_Make_Struct(klass, rb_binding_t, &binding_data_type, bind);
00284     return obj;
00285 }
00286 
00287 /* :nodoc: */
00288 static VALUE
00289 binding_dup(VALUE self)
00290 {
00291     VALUE bindval = binding_alloc(rb_cBinding);
00292     rb_binding_t *src, *dst;
00293     GetBindingPtr(self, src);
00294     GetBindingPtr(bindval, dst);
00295     dst->env = src->env;
00296     dst->filename = src->filename;
00297     dst->line_no = src->line_no;
00298     return bindval;
00299 }
00300 
00301 /* :nodoc: */
00302 static VALUE
00303 binding_clone(VALUE self)
00304 {
00305     VALUE bindval = binding_dup(self);
00306     CLONESETUP(bindval, self);
00307     return bindval;
00308 }
00309 
00310 VALUE
00311 rb_binding_new(void)
00312 {
00313     rb_thread_t *th = GET_THREAD();
00314     rb_control_frame_t *cfp = rb_vm_get_ruby_level_next_cfp(th, th->cfp);
00315     VALUE bindval = binding_alloc(rb_cBinding);
00316     rb_binding_t *bind;
00317 
00318     if (cfp == 0) {
00319         rb_raise(rb_eRuntimeError, "Can't create Binding Object on top of Fiber.");
00320     }
00321 
00322     GetBindingPtr(bindval, bind);
00323     bind->env = rb_vm_make_env_object(th, cfp);
00324     bind->filename = cfp->iseq->filename;
00325     bind->line_no = rb_vm_get_sourceline(cfp);
00326     return bindval;
00327 }
00328 
00329 /*
00330  *  call-seq:
00331  *     binding -> a_binding
00332  *
00333  *  Returns a +Binding+ object, describing the variable and
00334  *  method bindings at the point of call. This object can be used when
00335  *  calling +eval+ to execute the evaluated command in this
00336  *  environment. See also the description of class +Binding+.
00337  *
00338  *     def get_binding(param)
00339  *       return binding
00340  *     end
00341  *     b = get_binding("hello")
00342  *     eval("param", b)   #=> "hello"
00343  */
00344 
00345 static VALUE
00346 rb_f_binding(VALUE self)
00347 {
00348     return rb_binding_new();
00349 }
00350 
00351 /*
00352  *  call-seq:
00353  *     binding.eval(string [, filename [,lineno]])  -> obj
00354  *
00355  *  Evaluates the Ruby expression(s) in <em>string</em>, in the
00356  *  <em>binding</em>'s context.  If the optional <em>filename</em> and
00357  *  <em>lineno</em> parameters are present, they will be used when
00358  *  reporting syntax errors.
00359  *
00360  *     def get_binding(param)
00361  *       return binding
00362  *     end
00363  *     b = get_binding("hello")
00364  *     b.eval("param")   #=> "hello"
00365  */
00366 
00367 static VALUE
00368 bind_eval(int argc, VALUE *argv, VALUE bindval)
00369 {
00370     VALUE args[4];
00371 
00372     rb_scan_args(argc, argv, "12", &args[0], &args[2], &args[3]);
00373     args[1] = bindval;
00374     return rb_f_eval(argc+1, args, Qnil /* self will be searched in eval */);
00375 }
00376 
00377 static VALUE
00378 proc_new(VALUE klass, int is_lambda)
00379 {
00380     VALUE procval = Qnil;
00381     rb_thread_t *th = GET_THREAD();
00382     rb_control_frame_t *cfp = th->cfp;
00383     rb_block_t *block;
00384 
00385     if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0) {
00386 
00387         block = GC_GUARDED_PTR_REF(cfp->lfp[0]);
00388     }
00389     else {
00390         cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
00391 
00392         if ((GC_GUARDED_PTR_REF(cfp->lfp[0])) != 0) {
00393 
00394             block = GC_GUARDED_PTR_REF(cfp->lfp[0]);
00395 
00396             if (is_lambda) {
00397                 rb_warn("tried to create Proc object without a block");
00398             }
00399         }
00400         else {
00401             rb_raise(rb_eArgError,
00402                      "tried to create Proc object without a block");
00403         }
00404     }
00405 
00406     procval = block->proc;
00407 
00408     if (procval) {
00409         if (RBASIC(procval)->klass == klass) {
00410             return procval;
00411         }
00412         else {
00413             VALUE newprocval = proc_dup(procval);
00414             RBASIC(newprocval)->klass = klass;
00415             return newprocval;
00416         }
00417     }
00418 
00419     procval = rb_vm_make_proc(th, block, klass);
00420 
00421     if (is_lambda) {
00422         rb_proc_t *proc;
00423         GetProcPtr(procval, proc);
00424         proc->is_lambda = TRUE;
00425     }
00426     return procval;
00427 }
00428 
00429 /*
00430  *  call-seq:
00431  *     Proc.new {|...| block } -> a_proc
00432  *     Proc.new                -> a_proc
00433  *
00434  *  Creates a new <code>Proc</code> object, bound to the current
00435  *  context. <code>Proc::new</code> may be called without a block only
00436  *  within a method with an attached block, in which case that block is
00437  *  converted to the <code>Proc</code> object.
00438  *
00439  *     def proc_from
00440  *       Proc.new
00441  *     end
00442  *     proc = proc_from { "hello" }
00443  *     proc.call   #=> "hello"
00444  */
00445 
00446 static VALUE
00447 rb_proc_s_new(int argc, VALUE *argv, VALUE klass)
00448 {
00449     VALUE block = proc_new(klass, FALSE);
00450 
00451     rb_obj_call_init(block, argc, argv);
00452     return block;
00453 }
00454 
00455 /*
00456  * call-seq:
00457  *   proc   { |...| block }  -> a_proc
00458  *
00459  * Equivalent to <code>Proc.new</code>.
00460  */
00461 
00462 VALUE
00463 rb_block_proc(void)
00464 {
00465     return proc_new(rb_cProc, FALSE);
00466 }
00467 
00468 VALUE
00469 rb_block_lambda(void)
00470 {
00471     return proc_new(rb_cProc, TRUE);
00472 }
00473 
00474 VALUE
00475 rb_f_lambda(void)
00476 {
00477     rb_warn("rb_f_lambda() is deprecated; use rb_block_proc() instead");
00478     return rb_block_lambda();
00479 }
00480 
00481 /*
00482  * call-seq:
00483  *   lambda { |...| block }  -> a_proc
00484  *
00485  * Equivalent to <code>Proc.new</code>, except the resulting Proc objects
00486  * check the number of parameters passed when called.
00487  */
00488 
00489 static VALUE
00490 proc_lambda(void)
00491 {
00492     return rb_block_lambda();
00493 }
00494 
00495 /*  Document-method: ===
00496  *
00497  *  call-seq:
00498  *     proc === obj   -> result_of_proc
00499  *
00500  *  Invokes the block with +obj+ as the proc's parameter like Proc#call.  It
00501  *  is to allow a proc object to be a target of +when+ clause in a case
00502  *  statement.
00503  */
00504 
00505 /* CHECKME: are the argument checking semantics correct? */
00506 
00507 /*
00508  *  call-seq:
00509  *     prc.call(params,...)   -> obj
00510  *     prc[params,...]        -> obj
00511  *     prc.(params,...)       -> obj
00512  *
00513  *  Invokes the block, setting the block's parameters to the values in
00514  *  <i>params</i> using something close to method calling semantics.
00515  *  Generates a warning if multiple values are passed to a proc that
00516  *  expects just one (previously this silently converted the parameters
00517  *  to an array).  Note that prc.() invokes prc.call() with the parameters
00518  *  given.  It's a syntax sugar to hide "call".
00519  *
00520  *  For procs created using <code>lambda</code> or <code>->()</code> an error
00521  *  is generated if the wrong number of parameters are passed to a Proc with
00522  *  multiple parameters.  For procs created using <code>Proc.new</code> or
00523  *  <code>Kernel.proc</code>, extra parameters are silently discarded.
00524  *
00525  *  Returns the value of the last expression evaluated in the block. See
00526  *  also <code>Proc#yield</code>.
00527  *
00528  *     a_proc = Proc.new {|a, *b| b.collect {|i| i*a }}
00529  *     a_proc.call(9, 1, 2, 3)   #=> [9, 18, 27]
00530  *     a_proc[9, 1, 2, 3]        #=> [9, 18, 27]
00531  *     a_proc = lambda {|a,b| a}
00532  *     a_proc.call(1,2,3)
00533  *
00534  *  <em>produces:</em>
00535  *
00536  *     prog.rb:4:in `block in <main>': wrong number of arguments (3 for 2) (ArgumentError)
00537  *      from prog.rb:5:in `call'
00538  *      from prog.rb:5:in `<main>'
00539  *
00540  */
00541 
00542 static VALUE
00543 proc_call(int argc, VALUE *argv, VALUE procval)
00544 {
00545     rb_proc_t *proc;
00546     rb_block_t *blockptr = 0;
00547     rb_iseq_t *iseq;
00548     VALUE passed_procval;
00549     GetProcPtr(procval, proc);
00550 
00551     iseq = proc->block.iseq;
00552     if (BUILTIN_TYPE(iseq) == T_NODE || iseq->arg_block != -1) {
00553         if (rb_block_given_p()) {
00554             rb_proc_t *passed_proc;
00555             RB_GC_GUARD(passed_procval) = rb_block_proc();
00556             GetProcPtr(passed_procval, passed_proc);
00557             blockptr = &passed_proc->block;
00558         }
00559     }
00560 
00561     return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
00562                              argc, argv, blockptr);
00563 }
00564 
00565 #if SIZEOF_LONG > SIZEOF_INT
00566 static inline int
00567 check_argc(long argc)
00568 {
00569     if (argc > INT_MAX || argc < 0) {
00570         rb_raise(rb_eArgError, "too many arguments (%lu)",
00571                  (unsigned long)argc);
00572     }
00573     return (int)argc;
00574 }
00575 #else
00576 #define check_argc(argc) (argc)
00577 #endif
00578 
00579 VALUE
00580 rb_proc_call(VALUE self, VALUE args)
00581 {
00582     rb_proc_t *proc;
00583     GetProcPtr(self, proc);
00584     return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
00585                              check_argc(RARRAY_LEN(args)), RARRAY_PTR(args), 0);
00586 }
00587 
00588 VALUE
00589 rb_proc_call_with_block(VALUE self, int argc, VALUE *argv, VALUE pass_procval)
00590 {
00591     rb_proc_t *proc;
00592     rb_block_t *block = 0;
00593     GetProcPtr(self, proc);
00594 
00595     if (!NIL_P(pass_procval)) {
00596         rb_proc_t *pass_proc;
00597         GetProcPtr(pass_procval, pass_proc);
00598         block = &pass_proc->block;
00599     }
00600 
00601     return rb_vm_invoke_proc(GET_THREAD(), proc, proc->block.self,
00602                              argc, argv, block);
00603 }
00604 
00605 /*
00606  *  call-seq:
00607  *     prc.arity -> fixnum
00608  *
00609  *  Returns the number of arguments that would not be ignored. If the block
00610  *  is declared to take no arguments, returns 0. If the block is known
00611  *  to take exactly n arguments, returns n. If the block has optional
00612  *  arguments, return -n-1, where n is the number of mandatory
00613  *  arguments. A <code>proc</code> with no argument declarations
00614  *  is the same a block declaring <code>||</code> as its arguments.
00615  *
00616  *     Proc.new {}.arity          #=>  0
00617  *     Proc.new {||}.arity        #=>  0
00618  *     Proc.new {|a|}.arity       #=>  1
00619  *     Proc.new {|a,b|}.arity     #=>  2
00620  *     Proc.new {|a,b,c|}.arity   #=>  3
00621  *     Proc.new {|*a|}.arity      #=> -1
00622  *     Proc.new {|a,*b|}.arity    #=> -2
00623  *     Proc.new {|a,*b, c|}.arity    #=> -3
00624  */
00625 
00626 static VALUE
00627 proc_arity(VALUE self)
00628 {
00629     int arity = rb_proc_arity(self);
00630     return INT2FIX(arity);
00631 }
00632 
00633 int
00634 rb_proc_arity(VALUE self)
00635 {
00636     rb_proc_t *proc;
00637     rb_iseq_t *iseq;
00638     GetProcPtr(self, proc);
00639     iseq = proc->block.iseq;
00640     if (iseq) {
00641         if (BUILTIN_TYPE(iseq) != T_NODE) {
00642             if (iseq->arg_rest < 0) {
00643                 return iseq->argc;
00644             }
00645             else {
00646                 return -(iseq->argc + 1 + iseq->arg_post_len);
00647             }
00648         }
00649         else {
00650             NODE *node = (NODE *)iseq;
00651             if (IS_METHOD_PROC_NODE(node)) {
00652                 /* method(:foo).to_proc.arity */
00653                 return method_arity(node->nd_tval);
00654             }
00655         }
00656     }
00657     return -1;
00658 }
00659 
00660 #define get_proc_iseq rb_proc_get_iseq
00661 
00662 rb_iseq_t *
00663 rb_proc_get_iseq(VALUE self, int *is_proc)
00664 {
00665     rb_proc_t *proc;
00666     rb_iseq_t *iseq;
00667 
00668     GetProcPtr(self, proc);
00669     iseq = proc->block.iseq;
00670     if (is_proc) *is_proc = !proc->is_lambda;
00671     if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) {
00672         NODE *node = (NODE *)iseq;
00673         iseq = 0;
00674         if (IS_METHOD_PROC_NODE(node)) {
00675             /* method(:foo).to_proc */
00676             iseq = rb_method_get_iseq(node->nd_tval);
00677             if (is_proc) *is_proc = 0;
00678         }
00679     }
00680     return iseq;
00681 }
00682 
00683 static VALUE
00684 iseq_location(rb_iseq_t *iseq)
00685 {
00686     VALUE loc[2];
00687 
00688     if (!iseq) return Qnil;
00689     loc[0] = iseq->filename;
00690     if (iseq->insn_info_table) {
00691         loc[1] = INT2FIX(rb_iseq_first_lineno(iseq));
00692     }
00693     else {
00694         loc[1] = Qnil;
00695     }
00696     return rb_ary_new4(2, loc);
00697 }
00698 
00699 /*
00700  * call-seq:
00701  *    prc.source_location  -> [String, Fixnum]
00702  *
00703  * Returns the Ruby source filename and line number containing this proc
00704  * or +nil+ if this proc was not defined in Ruby (i.e. native)
00705  */
00706 
00707 VALUE
00708 rb_proc_location(VALUE self)
00709 {
00710     return iseq_location(get_proc_iseq(self, 0));
00711 }
00712 
00713 static VALUE
00714 unnamed_parameters(int arity)
00715 {
00716     VALUE a, param = rb_ary_new2((arity < 0) ? -arity : arity);
00717     int n = (arity < 0) ? ~arity : arity;
00718     ID req, rest;
00719     CONST_ID(req, "req");
00720     a = rb_ary_new3(1, ID2SYM(req));
00721     OBJ_FREEZE(a);
00722     for (; n; --n) {
00723         rb_ary_push(param, a);
00724     }
00725     if (arity < 0) {
00726         CONST_ID(rest, "rest");
00727         rb_ary_store(param, ~arity, rb_ary_new3(1, ID2SYM(rest)));
00728     }
00729     return param;
00730 }
00731 
00732 /*
00733  * call-seq:
00734  *    prc.parameters  -> array
00735  *
00736  * Returns the parameter information of this proc.
00737  *
00738  *    prc = lambda{|x, y=42, *other|}
00739  *    prc.parameters  #=> [[:req, :x], [:opt, :y], [:rest, :other]]
00740  */
00741 
00742 static VALUE
00743 rb_proc_parameters(VALUE self)
00744 {
00745     int is_proc;
00746     rb_iseq_t *iseq = get_proc_iseq(self, &is_proc);
00747     if (!iseq) {
00748         return unnamed_parameters(rb_proc_arity(self));
00749     }
00750     return rb_iseq_parameters(iseq, is_proc);
00751 }
00752 
00753 /*
00754  * call-seq:
00755  *   prc == other_proc   ->  true or false
00756  *
00757  * Returns <code>true</code> if <i>prc</i> is the same object as
00758  * <i>other_proc</i>, or if they are both procs with the same body.
00759  */
00760 
00761 static VALUE
00762 proc_eq(VALUE self, VALUE other)
00763 {
00764     if (self == other) {
00765         return Qtrue;
00766     }
00767     else {
00768         if (rb_obj_is_proc(other)) {
00769             rb_proc_t *p1, *p2;
00770             GetProcPtr(self, p1);
00771             GetProcPtr(other, p2);
00772             if (p1->envval == p2->envval &&
00773                 p1->block.iseq->iseq_size == p2->block.iseq->iseq_size &&
00774                 p1->block.iseq->local_size == p2->block.iseq->local_size &&
00775                 MEMCMP(p1->block.iseq->iseq, p2->block.iseq->iseq, VALUE,
00776                        p1->block.iseq->iseq_size) == 0) {
00777                 return Qtrue;
00778             }
00779         }
00780     }
00781     return Qfalse;
00782 }
00783 
00784 /*
00785  * call-seq:
00786  *   prc.hash   ->  integer
00787  *
00788  * Returns a hash value corresponding to proc body.
00789  */
00790 
00791 static VALUE
00792 proc_hash(VALUE self)
00793 {
00794     st_index_t hash;
00795     rb_proc_t *proc;
00796     GetProcPtr(self, proc);
00797     hash = rb_hash_start((st_index_t)proc->block.iseq);
00798     hash = rb_hash_uint(hash, (st_index_t)proc->envval);
00799     hash = rb_hash_uint(hash, (st_index_t)proc->block.lfp >> 16);
00800     hash = rb_hash_end(hash);
00801     return LONG2FIX(hash);
00802 }
00803 
00804 /*
00805  * call-seq:
00806  *   prc.to_s   -> string
00807  *
00808  * Returns the unique identifier for this proc, along with
00809  * an indication of where the proc was defined.
00810  */
00811 
00812 static VALUE
00813 proc_to_s(VALUE self)
00814 {
00815     VALUE str = 0;
00816     rb_proc_t *proc;
00817     const char *cname = rb_obj_classname(self);
00818     rb_iseq_t *iseq;
00819     const char *is_lambda;
00820 
00821     GetProcPtr(self, proc);
00822     iseq = proc->block.iseq;
00823     is_lambda = proc->is_lambda ? " (lambda)" : "";
00824 
00825     if (RUBY_VM_NORMAL_ISEQ_P(iseq)) {
00826         int line_no = 0;
00827 
00828         if (iseq->insn_info_table) {
00829             line_no = rb_iseq_first_lineno(iseq);
00830         }
00831         str = rb_sprintf("#<%s:%p@%s:%d%s>", cname, (void *)self,
00832                          RSTRING_PTR(iseq->filename),
00833                          line_no, is_lambda);
00834     }
00835     else {
00836         str = rb_sprintf("#<%s:%p%s>", cname, (void *)proc->block.iseq,
00837                          is_lambda);
00838     }
00839 
00840     if (OBJ_TAINTED(self)) {
00841         OBJ_TAINT(str);
00842     }
00843     return str;
00844 }
00845 
00846 /*
00847  *  call-seq:
00848  *     prc.to_proc -> prc
00849  *
00850  *  Part of the protocol for converting objects to <code>Proc</code>
00851  *  objects. Instances of class <code>Proc</code> simply return
00852  *  themselves.
00853  */
00854 
00855 static VALUE
00856 proc_to_proc(VALUE self)
00857 {
00858     return self;
00859 }
00860 
00861 static void
00862 bm_mark(void *ptr)
00863 {
00864     struct METHOD *data = ptr;
00865     rb_gc_mark(data->rclass);
00866     rb_gc_mark(data->recv);
00867     if (data->me) rb_mark_method_entry(data->me);
00868 }
00869 
00870 static void
00871 bm_free(void *ptr)
00872 {
00873     struct METHOD *data = ptr;
00874     struct unlinked_method_entry_list_entry *ume = data->ume;
00875     ume->me = data->me;
00876     ume->next = GET_VM()->unlinked_method_entry_list;
00877     GET_VM()->unlinked_method_entry_list = ume;
00878     xfree(ptr);
00879 }
00880 
00881 static size_t
00882 bm_memsize(const void *ptr)
00883 {
00884     return ptr ? sizeof(struct METHOD) : 0;
00885 }
00886 
00887 static const rb_data_type_t method_data_type = {
00888     "method",
00889     {
00890         bm_mark,
00891         bm_free,
00892         bm_memsize,
00893     },
00894 };
00895 
00896 VALUE
00897 rb_obj_is_method(VALUE m)
00898 {
00899     if (rb_typeddata_is_kind_of(m, &method_data_type)) {
00900         return Qtrue;
00901     }
00902     else {
00903         return Qfalse;
00904     }
00905 }
00906 
00907 static VALUE
00908 mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
00909 {
00910     VALUE method;
00911     VALUE rclass = klass;
00912     ID rid = id;
00913     struct METHOD *data;
00914     rb_method_entry_t *me, meb;
00915     rb_method_definition_t *def = 0;
00916     rb_method_flag_t flag = NOEX_UNDEF;
00917 
00918   again:
00919     me = rb_method_entry(klass, id);
00920     if (UNDEFINED_METHOD_ENTRY_P(me)) {
00921         ID rmiss = rb_intern("respond_to_missing?");
00922         VALUE sym = ID2SYM(id);
00923 
00924         if (obj != Qundef && !rb_method_basic_definition_p(klass, rmiss)) {
00925             if (RTEST(rb_funcall(obj, rmiss, 2, sym, scope ? Qfalse : Qtrue))) {
00926                 def = ALLOC(rb_method_definition_t);
00927                 def->type = VM_METHOD_TYPE_MISSING;
00928                 def->original_id = id;
00929                 def->alias_count = 0;
00930 
00931                 meb.flag = 0;
00932                 meb.mark = 0;
00933                 meb.called_id = id;
00934                 meb.klass = klass;
00935                 meb.def = def;
00936                 me = &meb;
00937                 def = 0;
00938 
00939                 goto gen_method;
00940             }
00941         }
00942         rb_print_undef(klass, id, 0);
00943     }
00944     def = me->def;
00945     if (flag == NOEX_UNDEF) {
00946         flag = me->flag;
00947         if (scope && (flag & NOEX_MASK) != NOEX_PUBLIC) {
00948             const char *v = "";
00949             switch (flag & NOEX_MASK) {
00950                 case NOEX_PRIVATE: v = "private"; break;
00951                 case NOEX_PROTECTED: v = "protected"; break;
00952             }
00953             rb_name_error(id, "method `%s' for %s `%s' is %s",
00954                           rb_id2name(id),
00955                           (TYPE(klass) == T_MODULE) ? "module" : "class",
00956                           rb_class2name(klass),
00957                           v);
00958         }
00959     }
00960     if (def && def->type == VM_METHOD_TYPE_ZSUPER) {
00961         klass = RCLASS_SUPER(me->klass);
00962         id = def->original_id;
00963         goto again;
00964     }
00965 
00966     klass = me->klass;
00967 
00968     while (rclass != klass &&
00969            (FL_TEST(rclass, FL_SINGLETON) || TYPE(rclass) == T_ICLASS)) {
00970         rclass = RCLASS_SUPER(rclass);
00971     }
00972 
00973     if (TYPE(klass) == T_ICLASS) {
00974         klass = RBASIC(klass)->klass;
00975     }
00976 
00977   gen_method:
00978     method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data);
00979 
00980     data->recv = obj;
00981     data->rclass = rclass;
00982     data->id = rid;
00983     data->me = ALLOC(rb_method_entry_t);
00984     *data->me = *me;
00985     data->me->def->alias_count++;
00986     data->ume = ALLOC(struct unlinked_method_entry_list_entry);
00987 
00988     OBJ_INFECT(method, klass);
00989 
00990     return method;
00991 }
00992 
00993 
00994 /**********************************************************************
00995  *
00996  * Document-class : Method
00997  *
00998  *  Method objects are created by <code>Object#method</code>, and are
00999  *  associated with a particular object (not just with a class). They
01000  *  may be used to invoke the method within the object, and as a block
01001  *  associated with an iterator. They may also be unbound from one
01002  *  object (creating an <code>UnboundMethod</code>) and bound to
01003  *  another.
01004  *
01005  *     class Thing
01006  *       def square(n)
01007  *         n*n
01008  *       end
01009  *     end
01010  *     thing = Thing.new
01011  *     meth  = thing.method(:square)
01012  *
01013  *     meth.call(9)                 #=> 81
01014  *     [ 1, 2, 3 ].collect(&meth)   #=> [1, 4, 9]
01015  *
01016  */
01017 
01018 /*
01019  * call-seq:
01020  *   meth == other_meth  -> true or false
01021  *
01022  * Two method objects are equal if they are bound to the same
01023  * object and refer to the same method definition.
01024  */
01025 
01026 static VALUE
01027 method_eq(VALUE method, VALUE other)
01028 {
01029     struct METHOD *m1, *m2;
01030 
01031     if (!rb_obj_is_method(other))
01032         return Qfalse;
01033     if (CLASS_OF(method) != CLASS_OF(other))
01034         return Qfalse;
01035 
01036     Check_TypedStruct(method, &method_data_type);
01037     m1 = (struct METHOD *)DATA_PTR(method);
01038     m2 = (struct METHOD *)DATA_PTR(other);
01039 
01040     if (!rb_method_entry_eq(m1->me, m2->me) ||
01041         m1->rclass != m2->rclass ||
01042         m1->recv != m2->recv) {
01043         return Qfalse;
01044     }
01045 
01046     return Qtrue;
01047 }
01048 
01049 /*
01050  * call-seq:
01051  *    meth.hash   -> integer
01052  *
01053  * Returns a hash value corresponding to the method object.
01054  */
01055 
01056 static VALUE
01057 method_hash(VALUE method)
01058 {
01059     struct METHOD *m;
01060     st_index_t hash;
01061 
01062     TypedData_Get_Struct(method, struct METHOD, &method_data_type, m);
01063     hash = rb_hash_start((st_index_t)m->rclass);
01064     hash = rb_hash_uint(hash, (st_index_t)m->recv);
01065     hash = rb_hash_uint(hash, (st_index_t)m->me->def);
01066     hash = rb_hash_end(hash);
01067 
01068     return INT2FIX(hash);
01069 }
01070 
01071 /*
01072  *  call-seq:
01073  *     meth.unbind    -> unbound_method
01074  *
01075  *  Dissociates <i>meth</i> from its current receiver. The resulting
01076  *  <code>UnboundMethod</code> can subsequently be bound to a new object
01077  *  of the same class (see <code>UnboundMethod</code>).
01078  */
01079 
01080 static VALUE
01081 method_unbind(VALUE obj)
01082 {
01083     VALUE method;
01084     struct METHOD *orig, *data;
01085 
01086     TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig);
01087     method = TypedData_Make_Struct(rb_cUnboundMethod, struct METHOD,
01088                                    &method_data_type, data);
01089     data->recv = Qundef;
01090     data->id = orig->id;
01091     data->me = ALLOC(rb_method_entry_t);
01092     *data->me = *orig->me;
01093     if (orig->me->def) orig->me->def->alias_count++;
01094     data->rclass = orig->rclass;
01095     data->ume = ALLOC(struct unlinked_method_entry_list_entry);
01096     OBJ_INFECT(method, obj);
01097 
01098     return method;
01099 }
01100 
01101 /*
01102  *  call-seq:
01103  *     meth.receiver    -> object
01104  *
01105  *  Returns the bound receiver of the method object.
01106  */
01107 
01108 static VALUE
01109 method_receiver(VALUE obj)
01110 {
01111     struct METHOD *data;
01112 
01113     TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
01114     return data->recv;
01115 }
01116 
01117 /*
01118  *  call-seq:
01119  *     meth.name    -> symbol
01120  *
01121  *  Returns the name of the method.
01122  */
01123 
01124 static VALUE
01125 method_name(VALUE obj)
01126 {
01127     struct METHOD *data;
01128 
01129     TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
01130     return ID2SYM(data->id);
01131 }
01132 
01133 /*
01134  *  call-seq:
01135  *     meth.owner    -> class_or_module
01136  *
01137  *  Returns the class or module that defines the method.
01138  */
01139 
01140 static VALUE
01141 method_owner(VALUE obj)
01142 {
01143     struct METHOD *data;
01144 
01145     TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data);
01146     return data->me->klass;
01147 }
01148 
01149 /*
01150  *  call-seq:
01151  *     obj.method(sym)    -> method
01152  *
01153  *  Looks up the named method as a receiver in <i>obj</i>, returning a
01154  *  <code>Method</code> object (or raising <code>NameError</code>). The
01155  *  <code>Method</code> object acts as a closure in <i>obj</i>'s object
01156  *  instance, so instance variables and the value of <code>self</code>
01157  *  remain available.
01158  *
01159  *     class Demo
01160  *       def initialize(n)
01161  *         @iv = n
01162  *       end
01163  *       def hello()
01164  *         "Hello, @iv = #{@iv}"
01165  *       end
01166  *     end
01167  *
01168  *     k = Demo.new(99)
01169  *     m = k.method(:hello)
01170  *     m.call   #=> "Hello, @iv = 99"
01171  *
01172  *     l = Demo.new('Fred')
01173  *     m = l.method("hello")
01174  *     m.call   #=> "Hello, @iv = Fred"
01175  */
01176 
01177 VALUE
01178 rb_obj_method(VALUE obj, VALUE vid)
01179 {
01180     return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, FALSE);
01181 }
01182 
01183 /*
01184  *  call-seq:
01185  *     obj.public_method(sym)    -> method
01186  *
01187  *  Similar to _method_, searches public method only.
01188  */
01189 
01190 VALUE
01191 rb_obj_public_method(VALUE obj, VALUE vid)
01192 {
01193     return mnew(CLASS_OF(obj), obj, rb_to_id(vid), rb_cMethod, TRUE);
01194 }
01195 
01196 /*
01197  *  call-seq:
01198  *     mod.instance_method(symbol)   -> unbound_method
01199  *
01200  *  Returns an +UnboundMethod+ representing the given
01201  *  instance method in _mod_.
01202  *
01203  *     class Interpreter
01204  *       def do_a() print "there, "; end
01205  *       def do_d() print "Hello ";  end
01206  *       def do_e() print "!\n";     end
01207  *       def do_v() print "Dave";    end
01208  *       Dispatcher = {
01209  *         "a" => instance_method(:do_a),
01210  *         "d" => instance_method(:do_d),
01211  *         "e" => instance_method(:do_e),
01212  *         "v" => instance_method(:do_v)
01213  *       }
01214  *       def interpret(string)
01215  *         string.each_char {|b| Dispatcher[b].bind(self).call }
01216  *       end
01217  *     end
01218  *
01219  *     interpreter = Interpreter.new
01220  *     interpreter.interpret('dave')
01221  *
01222  *  <em>produces:</em>
01223  *
01224  *     Hello there, Dave!
01225  */
01226 
01227 static VALUE
01228 rb_mod_instance_method(VALUE mod, VALUE vid)
01229 {
01230     return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, FALSE);
01231 }
01232 
01233 /*
01234  *  call-seq:
01235  *     mod.public_instance_method(symbol)   -> unbound_method
01236  *
01237  *  Similar to _instance_method_, searches public method only.
01238  */
01239 
01240 static VALUE
01241 rb_mod_public_instance_method(VALUE mod, VALUE vid)
01242 {
01243     return mnew(mod, Qundef, rb_to_id(vid), rb_cUnboundMethod, TRUE);
01244 }
01245 
01246 /*
01247  *  call-seq:
01248  *     define_method(symbol, method)     -> new_method
01249  *     define_method(symbol) { block }   -> proc
01250  *
01251  *  Defines an instance method in the receiver. The _method_
01252  *  parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
01253  *  If a block is specified, it is used as the method body. This block
01254  *  is evaluated using <code>instance_eval</code>, a point that is
01255  *  tricky to demonstrate because <code>define_method</code> is private.
01256  *  (This is why we resort to the +send+ hack in this example.)
01257  *
01258  *     class A
01259  *       def fred
01260  *         puts "In Fred"
01261  *       end
01262  *       def create_method(name, &block)
01263  *         self.class.send(:define_method, name, &block)
01264  *       end
01265  *       define_method(:wilma) { puts "Charge it!" }
01266  *     end
01267  *     class B < A
01268  *       define_method(:barney, instance_method(:fred))
01269  *     end
01270  *     a = B.new
01271  *     a.barney
01272  *     a.wilma
01273  *     a.create_method(:betty) { p self }
01274  *     a.betty
01275  *
01276  *  <em>produces:</em>
01277  *
01278  *     In Fred
01279  *     Charge it!
01280  *     #<B:0x401b39e8>
01281  */
01282 
01283 static VALUE
01284 rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
01285 {
01286     ID id;
01287     VALUE body;
01288     int noex = NOEX_PUBLIC;
01289 
01290     if (argc == 1) {
01291         id = rb_to_id(argv[0]);
01292         body = rb_block_lambda();
01293     }
01294     else if (argc == 2) {
01295         id = rb_to_id(argv[0]);
01296         body = argv[1];
01297         if (!rb_obj_is_method(body) && !rb_obj_is_proc(body)) {
01298             rb_raise(rb_eTypeError,
01299                      "wrong argument type %s (expected Proc/Method)",
01300                      rb_obj_classname(body));
01301         }
01302     }
01303     else {
01304         rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
01305     }
01306 
01307     if (rb_obj_is_method(body)) {
01308         struct METHOD *method = (struct METHOD *)DATA_PTR(body);
01309         VALUE rclass = method->rclass;
01310         if (rclass != mod && !RTEST(rb_class_inherited_p(mod, rclass))) {
01311             if (FL_TEST(rclass, FL_SINGLETON)) {
01312                 rb_raise(rb_eTypeError,
01313                          "can't bind singleton method to a different class");
01314             }
01315             else {
01316                 rb_raise(rb_eTypeError,
01317                          "bind argument must be a subclass of %s",
01318                          rb_class2name(rclass));
01319             }
01320         }
01321         rb_method_entry_set(mod, id, method->me, noex);
01322     }
01323     else if (rb_obj_is_proc(body)) {
01324         rb_proc_t *proc;
01325         body = proc_dup(body);
01326         GetProcPtr(body, proc);
01327         if (BUILTIN_TYPE(proc->block.iseq) != T_NODE) {
01328             proc->block.iseq->defined_method_id = id;
01329             proc->block.iseq->klass = mod;
01330             proc->is_lambda = TRUE;
01331             proc->is_from_method = TRUE;
01332         }
01333         rb_add_method(mod, id, VM_METHOD_TYPE_BMETHOD, (void *)body, noex);
01334     }
01335     else {
01336         /* type error */
01337         rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)");
01338     }
01339 
01340     return body;
01341 }
01342 
01343 /*
01344  *  call-seq:
01345  *     define_singleton_method(symbol, method) -> new_method
01346  *     define_singleton_method(symbol) { block } -> proc
01347  *
01348  *  Defines a singleton method in the receiver. The _method_
01349  *  parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
01350  *  If a block is specified, it is used as the method body.
01351  *
01352  *     class A
01353  *       class << self
01354  *         def class_name
01355  *           to_s
01356  *         end
01357  *       end
01358  *     end
01359  *     A.define_singleton_method(:who_am_i) do
01360  *       "I am: #{class_name}"
01361  *     end
01362  *     A.who_am_i   # ==> "I am: A"
01363  *
01364  *     guy = "Bob"
01365  *     guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
01366  *     guy.hello    #=>  "Bob: Hello there!"
01367  */
01368 
01369 static VALUE
01370 rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
01371 {
01372     VALUE klass = rb_singleton_class(obj);
01373 
01374     return rb_mod_define_method(argc, argv, klass);
01375 }
01376 
01377 
01378 /*
01379  * MISSING: documentation
01380  */
01381 
01382 static VALUE
01383 method_clone(VALUE self)
01384 {
01385     VALUE clone;
01386     struct METHOD *orig, *data;
01387 
01388     TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig);
01389     clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data);
01390     CLONESETUP(clone, self);
01391     *data = *orig;
01392     data->me = ALLOC(rb_method_entry_t);
01393     *data->me = *orig->me;
01394     if (data->me->def) data->me->def->alias_count++;
01395     data->ume = ALLOC(struct unlinked_method_entry_list_entry);
01396 
01397     return clone;
01398 }
01399 
01400 /*
01401  *  call-seq:
01402  *     meth.call(args, ...)    -> obj
01403  *     meth[args, ...]         -> obj
01404  *
01405  *  Invokes the <i>meth</i> with the specified arguments, returning the
01406  *  method's return value.
01407  *
01408  *     m = 12.method("+")
01409  *     m.call(3)    #=> 15
01410  *     m.call(20)   #=> 32
01411  */
01412 
01413 VALUE
01414 rb_method_call(int argc, VALUE *argv, VALUE method)
01415 {
01416     VALUE result = Qnil;        /* OK */
01417     struct METHOD *data;
01418     int state;
01419     volatile int safe = -1;
01420 
01421     TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01422     if (data->recv == Qundef) {
01423         rb_raise(rb_eTypeError, "can't call unbound method; bind first");
01424     }
01425     PUSH_TAG();
01426     if (OBJ_TAINTED(method)) {
01427         safe = rb_safe_level();
01428         if (rb_safe_level() < 4) {
01429             rb_set_safe_level_force(4);
01430         }
01431     }
01432     if ((state = EXEC_TAG()) == 0) {
01433         rb_thread_t *th = GET_THREAD();
01434 
01435         PASS_PASSED_BLOCK_TH(th);
01436         result = rb_vm_call(th, data->recv, data->id,  argc, argv, data->me);
01437     }
01438     POP_TAG();
01439     if (safe >= 0)
01440         rb_set_safe_level_force(safe);
01441     if (state)
01442         JUMP_TAG(state);
01443     return result;
01444 }
01445 
01446 /**********************************************************************
01447  *
01448  * Document-class: UnboundMethod
01449  *
01450  *  Ruby supports two forms of objectified methods. Class
01451  *  <code>Method</code> is used to represent methods that are associated
01452  *  with a particular object: these method objects are bound to that
01453  *  object. Bound method objects for an object can be created using
01454  *  <code>Object#method</code>.
01455  *
01456  *  Ruby also supports unbound methods; methods objects that are not
01457  *  associated with a particular object. These can be created either by
01458  *  calling <code>Module#instance_method</code> or by calling
01459  *  <code>unbind</code> on a bound method object. The result of both of
01460  *  these is an <code>UnboundMethod</code> object.
01461  *
01462  *  Unbound methods can only be called after they are bound to an
01463  *  object. That object must be be a kind_of? the method's original
01464  *  class.
01465  *
01466  *     class Square
01467  *       def area
01468  *         @side * @side
01469  *       end
01470  *       def initialize(side)
01471  *         @side = side
01472  *       end
01473  *     end
01474  *
01475  *     area_un = Square.instance_method(:area)
01476  *
01477  *     s = Square.new(12)
01478  *     area = area_un.bind(s)
01479  *     area.call   #=> 144
01480  *
01481  *  Unbound methods are a reference to the method at the time it was
01482  *  objectified: subsequent changes to the underlying class will not
01483  *  affect the unbound method.
01484  *
01485  *     class Test
01486  *       def test
01487  *         :original
01488  *       end
01489  *     end
01490  *     um = Test.instance_method(:test)
01491  *     class Test
01492  *       def test
01493  *         :modified
01494  *       end
01495  *     end
01496  *     t = Test.new
01497  *     t.test            #=> :modified
01498  *     um.bind(t).call   #=> :original
01499  *
01500  */
01501 
01502 /*
01503  *  call-seq:
01504  *     umeth.bind(obj) -> method
01505  *
01506  *  Bind <i>umeth</i> to <i>obj</i>. If <code>Klass</code> was the class
01507  *  from which <i>umeth</i> was obtained,
01508  *  <code>obj.kind_of?(Klass)</code> must be true.
01509  *
01510  *     class A
01511  *       def test
01512  *         puts "In test, class = #{self.class}"
01513  *       end
01514  *     end
01515  *     class B < A
01516  *     end
01517  *     class C < B
01518  *     end
01519  *
01520  *
01521  *     um = B.instance_method(:test)
01522  *     bm = um.bind(C.new)
01523  *     bm.call
01524  *     bm = um.bind(B.new)
01525  *     bm.call
01526  *     bm = um.bind(A.new)
01527  *     bm.call
01528  *
01529  *  <em>produces:</em>
01530  *
01531  *     In test, class = C
01532  *     In test, class = B
01533  *     prog.rb:16:in `bind': bind argument must be an instance of B (TypeError)
01534  *      from prog.rb:16
01535  */
01536 
01537 static VALUE
01538 umethod_bind(VALUE method, VALUE recv)
01539 {
01540     struct METHOD *data, *bound;
01541 
01542     TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01543 
01544     if (data->rclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, data->rclass)) {
01545         if (FL_TEST(data->rclass, FL_SINGLETON)) {
01546             rb_raise(rb_eTypeError,
01547                      "singleton method called for a different object");
01548         }
01549         else {
01550             rb_raise(rb_eTypeError, "bind argument must be an instance of %s",
01551                      rb_class2name(data->rclass));
01552         }
01553     }
01554 
01555     method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound);
01556     *bound = *data;
01557     bound->me = ALLOC(rb_method_entry_t);
01558     *bound->me = *data->me;
01559     if (bound->me->def) bound->me->def->alias_count++;
01560     bound->recv = recv;
01561     bound->rclass = CLASS_OF(recv);
01562     data->ume = ALLOC(struct unlinked_method_entry_list_entry);
01563 
01564     return method;
01565 }
01566 
01567 int
01568 rb_method_entry_arity(const rb_method_entry_t *me)
01569 {
01570     const rb_method_definition_t *def = me->def;
01571     if (!def) return 0;
01572     switch (def->type) {
01573       case VM_METHOD_TYPE_CFUNC:
01574         if (def->body.cfunc.argc < 0)
01575             return -1;
01576         return check_argc(def->body.cfunc.argc);
01577       case VM_METHOD_TYPE_ZSUPER:
01578         return -1;
01579       case VM_METHOD_TYPE_ATTRSET:
01580         return 1;
01581       case VM_METHOD_TYPE_IVAR:
01582         return 0;
01583       case VM_METHOD_TYPE_BMETHOD:
01584         return rb_proc_arity(def->body.proc);
01585       case VM_METHOD_TYPE_ISEQ: {
01586         rb_iseq_t *iseq = def->body.iseq;
01587         if (iseq->arg_rest == -1 && iseq->arg_opts == 0) {
01588             return iseq->argc;
01589         }
01590         else {
01591             return -(iseq->argc + 1 + iseq->arg_post_len);
01592         }
01593       }
01594       case VM_METHOD_TYPE_UNDEF:
01595       case VM_METHOD_TYPE_NOTIMPLEMENTED:
01596         return 0;
01597       case VM_METHOD_TYPE_MISSING:
01598         return -1;
01599       case VM_METHOD_TYPE_OPTIMIZED: {
01600         switch (def->body.optimize_type) {
01601           case OPTIMIZED_METHOD_TYPE_SEND:
01602             return -1;
01603           default:
01604             break;
01605         }
01606       }
01607     }
01608     rb_bug("rb_method_entry_arity: invalid method entry type (%d)", def->type);
01609 }
01610 
01611 /*
01612  *  call-seq:
01613  *     meth.arity    -> fixnum
01614  *
01615  *  Returns an indication of the number of arguments accepted by a
01616  *  method. Returns a nonnegative integer for methods that take a fixed
01617  *  number of arguments. For Ruby methods that take a variable number of
01618  *  arguments, returns -n-1, where n is the number of required
01619  *  arguments. For methods written in C, returns -1 if the call takes a
01620  *  variable number of arguments.
01621  *
01622  *     class C
01623  *       def one;    end
01624  *       def two(a); end
01625  *       def three(*a);  end
01626  *       def four(a, b); end
01627  *       def five(a, b, *c);    end
01628  *       def six(a, b, *c, &d); end
01629  *     end
01630  *     c = C.new
01631  *     c.method(:one).arity     #=> 0
01632  *     c.method(:two).arity     #=> 1
01633  *     c.method(:three).arity   #=> -1
01634  *     c.method(:four).arity    #=> 2
01635  *     c.method(:five).arity    #=> -3
01636  *     c.method(:six).arity     #=> -3
01637  *
01638  *     "cat".method(:size).arity      #=> 0
01639  *     "cat".method(:replace).arity   #=> 1
01640  *     "cat".method(:squeeze).arity   #=> -1
01641  *     "cat".method(:count).arity     #=> -1
01642  */
01643 
01644 static VALUE
01645 method_arity_m(VALUE method)
01646 {
01647     int n = method_arity(method);
01648     return INT2FIX(n);
01649 }
01650 
01651 static int
01652 method_arity(VALUE method)
01653 {
01654     struct METHOD *data;
01655 
01656     TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01657     return rb_method_entry_arity(data->me);
01658 }
01659 
01660 int
01661 rb_mod_method_arity(VALUE mod, ID id)
01662 {
01663     rb_method_entry_t *me = rb_method_entry(mod, id);
01664     return rb_method_entry_arity(me);
01665 }
01666 
01667 int
01668 rb_obj_method_arity(VALUE obj, ID id)
01669 {
01670     return rb_mod_method_arity(CLASS_OF(obj), id);
01671 }
01672 
01673 static inline rb_method_definition_t *
01674 method_get_def(VALUE method)
01675 {
01676     struct METHOD *data;
01677 
01678     TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01679     return data->me->def;
01680 }
01681 
01682 static rb_iseq_t *
01683 method_get_iseq(rb_method_definition_t *def)
01684 {
01685     switch (def->type) {
01686       case VM_METHOD_TYPE_BMETHOD:
01687         return get_proc_iseq(def->body.proc, 0);
01688       case VM_METHOD_TYPE_ISEQ:
01689         return def->body.iseq;
01690       default:
01691         return 0;
01692     }
01693 }
01694 
01695 rb_iseq_t *
01696 rb_method_get_iseq(VALUE method)
01697 {
01698     return method_get_iseq(method_get_def(method));
01699 }
01700 
01701 /*
01702  * call-seq:
01703  *    meth.source_location  -> [String, Fixnum]
01704  *
01705  * Returns the Ruby source filename and line number containing this method
01706  * or nil if this method was not defined in Ruby (i.e. native)
01707  */
01708 
01709 VALUE
01710 rb_method_location(VALUE method)
01711 {
01712     rb_method_definition_t *def = method_get_def(method);
01713     if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) {
01714         if (!def->body.attr.location)
01715             return Qnil;
01716         return rb_ary_dup(def->body.attr.location);
01717     }
01718     return iseq_location(method_get_iseq(def));
01719 }
01720 
01721 /*
01722  * call-seq:
01723  *    meth.parameters  -> array
01724  *
01725  * Returns the parameter information of this method.
01726  */
01727 
01728 static VALUE
01729 rb_method_parameters(VALUE method)
01730 {
01731     rb_iseq_t *iseq = rb_method_get_iseq(method);
01732     if (!iseq) {
01733         return unnamed_parameters(method_arity(method));
01734     }
01735     return rb_iseq_parameters(iseq, 0);
01736 }
01737 
01738 /*
01739  *  call-seq:
01740  *   meth.to_s      ->  string
01741  *   meth.inspect   ->  string
01742  *
01743  *  Returns the name of the underlying method.
01744  *
01745  *    "cat".method(:count).inspect   #=> "#<Method: String#count>"
01746  */
01747 
01748 static VALUE
01749 method_inspect(VALUE method)
01750 {
01751     struct METHOD *data;
01752     VALUE str;
01753     const char *s;
01754     const char *sharp = "#";
01755 
01756     TypedData_Get_Struct(method, struct METHOD, &method_data_type, data);
01757     str = rb_str_buf_new2("#<");
01758     s = rb_obj_classname(method);
01759     rb_str_buf_cat2(str, s);
01760     rb_str_buf_cat2(str, ": ");
01761 
01762     if (FL_TEST(data->me->klass, FL_SINGLETON)) {
01763         VALUE v = rb_iv_get(data->me->klass, "__attached__");
01764 
01765         if (data->recv == Qundef) {
01766             rb_str_buf_append(str, rb_inspect(data->me->klass));
01767         }
01768         else if (data->recv == v) {
01769             rb_str_buf_append(str, rb_inspect(v));
01770             sharp = ".";
01771         }
01772         else {
01773             rb_str_buf_append(str, rb_inspect(data->recv));
01774             rb_str_buf_cat2(str, "(");
01775             rb_str_buf_append(str, rb_inspect(v));
01776             rb_str_buf_cat2(str, ")");
01777             sharp = ".";
01778         }
01779     }
01780     else {
01781         rb_str_buf_cat2(str, rb_class2name(data->rclass));
01782         if (data->rclass != data->me->klass) {
01783             rb_str_buf_cat2(str, "(");
01784             rb_str_buf_cat2(str, rb_class2name(data->me->klass));
01785             rb_str_buf_cat2(str, ")");
01786         }
01787     }
01788     rb_str_buf_cat2(str, sharp);
01789     rb_str_append(str, rb_id2str(data->me->def->original_id));
01790     if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
01791         rb_str_buf_cat2(str, " (not-implemented)");
01792     }
01793     rb_str_buf_cat2(str, ">");
01794 
01795     return str;
01796 }
01797 
01798 static VALUE
01799 mproc(VALUE method)
01800 {
01801     return rb_funcall(Qnil, rb_intern("proc"), 0);
01802 }
01803 
01804 static VALUE
01805 mlambda(VALUE method)
01806 {
01807     return rb_funcall(Qnil, rb_intern("lambda"), 0);
01808 }
01809 
01810 static VALUE
01811 bmcall(VALUE args, VALUE method)
01812 {
01813     volatile VALUE a;
01814     VALUE ret;
01815     int argc;
01816 
01817     if (CLASS_OF(args) != rb_cArray) {
01818         args = rb_ary_new3(1, args);
01819         argc = 1;
01820     }
01821     else {
01822         argc = check_argc(RARRAY_LEN(args));
01823     }
01824     ret = rb_method_call(argc, RARRAY_PTR(args), method);
01825     RB_GC_GUARD(a) = args;
01826     return ret;
01827 }
01828 
01829 VALUE
01830 rb_proc_new(
01831     VALUE (*func)(ANYARGS), /* VALUE yieldarg[, VALUE procarg] */
01832     VALUE val)
01833 {
01834     VALUE procval = rb_iterate(mproc, 0, func, val);
01835     return procval;
01836 }
01837 
01838 /*
01839  *  call-seq:
01840  *     meth.to_proc    -> prc
01841  *
01842  *  Returns a <code>Proc</code> object corresponding to this method.
01843  */
01844 
01845 static VALUE
01846 method_proc(VALUE method)
01847 {
01848     VALUE procval;
01849     rb_proc_t *proc;
01850     /*
01851      * class Method
01852      *   def to_proc
01853      *     proc{|*args|
01854      *       self.call(*args)
01855      *     }
01856      *   end
01857      * end
01858      */
01859     procval = rb_iterate(mlambda, 0, bmcall, method);
01860     GetProcPtr(procval, proc);
01861     proc->is_from_method = 1;
01862     return procval;
01863 }
01864 
01865 /*
01866  * call_seq:
01867  *   local_jump_error.exit_value  -> obj
01868  *
01869  * Returns the exit value associated with this +LocalJumpError+.
01870  */
01871 static VALUE
01872 localjump_xvalue(VALUE exc)
01873 {
01874     return rb_iv_get(exc, "@exit_value");
01875 }
01876 
01877 /*
01878  * call-seq:
01879  *    local_jump_error.reason   -> symbol
01880  *
01881  * The reason this block was terminated:
01882  * :break, :redo, :retry, :next, :return, or :noreason.
01883  */
01884 
01885 static VALUE
01886 localjump_reason(VALUE exc)
01887 {
01888     return rb_iv_get(exc, "@reason");
01889 }
01890 
01891 /*
01892  *  call-seq:
01893  *     prc.binding    -> binding
01894  *
01895  *  Returns the binding associated with <i>prc</i>. Note that
01896  *  <code>Kernel#eval</code> accepts either a <code>Proc</code> or a
01897  *  <code>Binding</code> object as its second parameter.
01898  *
01899  *     def fred(param)
01900  *       proc {}
01901  *     end
01902  *
01903  *     b = fred(99)
01904  *     eval("param", b.binding)   #=> 99
01905  */
01906 static VALUE
01907 proc_binding(VALUE self)
01908 {
01909     rb_proc_t *proc;
01910     VALUE bindval;
01911     rb_binding_t *bind;
01912 
01913     GetProcPtr(self, proc);
01914     if (TYPE(proc->block.iseq) == T_NODE) {
01915         if (!IS_METHOD_PROC_NODE((NODE *)proc->block.iseq)) {
01916             rb_raise(rb_eArgError, "Can't create Binding from C level Proc");
01917         }
01918     }
01919 
01920     bindval = binding_alloc(rb_cBinding);
01921     GetBindingPtr(bindval, bind);
01922     bind->env = proc->envval;
01923     if (RUBY_VM_NORMAL_ISEQ_P(proc->block.iseq)) {
01924         bind->filename = proc->block.iseq->filename;
01925         bind->line_no = rb_iseq_first_lineno(proc->block.iseq);
01926     }
01927     else {
01928         bind->filename = Qnil;
01929         bind->line_no = 0;
01930     }
01931     return bindval;
01932 }
01933 
01934 static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc);
01935 
01936 static VALUE
01937 make_curry_proc(VALUE proc, VALUE passed, VALUE arity)
01938 {
01939     VALUE args = rb_ary_new3(3, proc, passed, arity);
01940     rb_proc_t *procp;
01941     int is_lambda;
01942 
01943     GetProcPtr(proc, procp);
01944     is_lambda = procp->is_lambda;
01945     rb_ary_freeze(passed);
01946     rb_ary_freeze(args);
01947     proc = rb_proc_new(curry, args);
01948     GetProcPtr(proc, procp);
01949     procp->is_lambda = is_lambda;
01950     return proc;
01951 }
01952 
01953 static VALUE
01954 curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc)
01955 {
01956     VALUE proc, passed, arity;
01957     proc = RARRAY_PTR(args)[0];
01958     passed = RARRAY_PTR(args)[1];
01959     arity = RARRAY_PTR(args)[2];
01960 
01961     passed = rb_ary_plus(passed, rb_ary_new4(argc, argv));
01962     rb_ary_freeze(passed);
01963 
01964     if (RARRAY_LEN(passed) < FIX2INT(arity)) {
01965         if (!NIL_P(passed_proc)) {
01966             rb_warn("given block not used");
01967         }
01968         arity = make_curry_proc(proc, passed, arity);
01969         return arity;
01970     }
01971     else {
01972         return rb_proc_call_with_block(proc, check_argc(RARRAY_LEN(passed)),
01973                                        RARRAY_PTR(passed), passed_proc);
01974     }
01975 }
01976 
01977  /*
01978   *  call-seq:
01979   *     prc.curry         -> a_proc
01980   *     prc.curry(arity)  -> a_proc
01981   *
01982   *  Returns a curried proc. If the optional <i>arity</i> argument is given,
01983   *  it determines the number of arguments.
01984   *  A curried proc receives some arguments. If a sufficient number of
01985   *  arguments are supplied, it passes the supplied arguments to the original
01986   *  proc and returns the result. Otherwise, returns another curried proc that
01987   *  takes the rest of arguments.
01988   *
01989   *     b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
01990   *     p b.curry[1][2][3]           #=> 6
01991   *     p b.curry[1, 2][3, 4]        #=> 6
01992   *     p b.curry(5)[1][2][3][4][5]  #=> 6
01993   *     p b.curry(5)[1, 2][3, 4][5]  #=> 6
01994   *     p b.curry(1)[1]              #=> 1
01995   *
01996   *     b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
01997   *     p b.curry[1][2][3]           #=> 6
01998   *     p b.curry[1, 2][3, 4]        #=> 10
01999   *     p b.curry(5)[1][2][3][4][5]  #=> 15
02000   *     p b.curry(5)[1, 2][3, 4][5]  #=> 15
02001   *     p b.curry(1)[1]              #=> 1
02002   *
02003   *     b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
02004   *     p b.curry[1][2][3]           #=> 6
02005   *     p b.curry[1, 2][3, 4]        #=> wrong number of arguments (4 for 3)
02006   *     p b.curry(5)                 #=> wrong number of arguments (5 for 3)
02007   *     p b.curry(1)                 #=> wrong number of arguments (1 for 3)
02008   *
02009   *     b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
02010   *     p b.curry[1][2][3]           #=> 6
02011   *     p b.curry[1, 2][3, 4]        #=> 10
02012   *     p b.curry(5)[1][2][3][4][5]  #=> 15
02013   *     p b.curry(5)[1, 2][3, 4][5]  #=> 15
02014   *     p b.curry(1)                 #=> wrong number of arguments (1 for 3)
02015   *
02016   *     b = proc { :foo }
02017   *     p b.curry[]                  #=> :foo
02018   */
02019 static VALUE
02020 proc_curry(int argc, VALUE *argv, VALUE self)
02021 {
02022     int sarity, marity = rb_proc_arity(self);
02023     VALUE arity, opt = Qfalse;
02024 
02025     if (marity < 0) {
02026         marity = -marity - 1;
02027         opt = Qtrue;
02028     }
02029 
02030     rb_scan_args(argc, argv, "01", &arity);
02031     if (NIL_P(arity)) {
02032         arity = INT2FIX(marity);
02033     }
02034     else {
02035         sarity = FIX2INT(arity);
02036         if (rb_proc_lambda_p(self) && (sarity < marity || (sarity > marity && !opt))) {
02037             rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", sarity, marity);
02038         }
02039     }
02040 
02041     return make_curry_proc(self, rb_ary_new(), arity);
02042 }
02043 
02044 /*
02045  *  Document-class: LocalJumpError
02046  *
02047  *  Raised when Ruby can't yield as requested.
02048  *
02049  *  A typical scenario is attempting to yield when no block is given:
02050  *
02051  *     def call_block
02052  *       yield 42
02053  *     end
02054  *     call_block
02055  *
02056  *  <em>raises the exception:</em>
02057  *
02058  *     LocalJumpError: no block given (yield)
02059  *
02060  *  A more subtle example:
02061  *
02062  *     def get_me_a_return
02063  *       Proc.new { return 42 }
02064  *     end
02065  *     get_me_a_return.call
02066  *
02067  *  <em>raises the exception:</em>
02068  *
02069  *     LocalJumpError: unexpected return
02070  */
02071 
02072 /*
02073  *  Document-class: SystemStackError
02074  *
02075  *  Raised in case of a stack overflow.
02076  *
02077  *     def me_myself_and_i
02078  *       me_myself_and_i
02079  *     end
02080  *     me_myself_and_i
02081  *
02082  *  <em>raises the exception:</em>
02083  *
02084  *    SystemStackError: stack level too deep
02085  */
02086 
02087 /*
02088  *  <code>Proc</code> objects are blocks of code that have been bound to
02089  *  a set of local variables. Once bound, the code may be called in
02090  *  different contexts and still access those variables.
02091  *
02092  *     def gen_times(factor)
02093  *       return Proc.new {|n| n*factor }
02094  *     end
02095  *
02096  *     times3 = gen_times(3)
02097  *     times5 = gen_times(5)
02098  *
02099  *     times3.call(12)               #=> 36
02100  *     times5.call(5)                #=> 25
02101  *     times3.call(times5.call(4))   #=> 60
02102  *
02103  */
02104 
02105 void
02106 Init_Proc(void)
02107 {
02108     /* Proc */
02109     rb_cProc = rb_define_class("Proc", rb_cObject);
02110     rb_undef_alloc_func(rb_cProc);
02111     rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, -1);
02112 
02113 #if 0 /* incomplete. */
02114     rb_add_method(rb_cProc, rb_intern("call"), VM_METHOD_TYPE_OPTIMIZED,
02115                   (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02116     rb_add_method(rb_cProc, rb_intern("[]"), VM_METHOD_TYPE_OPTIMIZED,
02117                   (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02118     rb_add_method(rb_cProc, rb_intern("==="), VM_METHOD_TYPE_OPTIMIZED,
02119                   (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02120     rb_add_method(rb_cProc, rb_intern("yield"), VM_METHOD_TYPE_OPTIMIZED,
02121                   (void *)OPTIMIZED_METHOD_TYPE_CALL, 0);
02122 #else
02123     rb_define_method(rb_cProc, "call", proc_call, -1);
02124     rb_define_method(rb_cProc, "[]", proc_call, -1);
02125     rb_define_method(rb_cProc, "===", proc_call, -1);
02126     rb_define_method(rb_cProc, "yield", proc_call, -1);
02127 #endif
02128     rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0);
02129     rb_define_method(rb_cProc, "arity", proc_arity, 0);
02130     rb_define_method(rb_cProc, "clone", proc_clone, 0);
02131     rb_define_method(rb_cProc, "dup", proc_dup, 0);
02132     rb_define_method(rb_cProc, "==", proc_eq, 1);
02133     rb_define_method(rb_cProc, "eql?", proc_eq, 1);
02134     rb_define_method(rb_cProc, "hash", proc_hash, 0);
02135     rb_define_method(rb_cProc, "to_s", proc_to_s, 0);
02136     rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0);
02137     rb_define_method(rb_cProc, "binding", proc_binding, 0);
02138     rb_define_method(rb_cProc, "curry", proc_curry, -1);
02139     rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);
02140     rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0);
02141 
02142     /* Exceptions */
02143     rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError);
02144     rb_define_method(rb_eLocalJumpError, "exit_value", localjump_xvalue, 0);
02145     rb_define_method(rb_eLocalJumpError, "reason", localjump_reason, 0);
02146 
02147     rb_eSysStackError = rb_define_class("SystemStackError", rb_eException);
02148     sysstack_error = rb_exc_new3(rb_eSysStackError,
02149                                  rb_obj_freeze(rb_str_new2("stack level too deep")));
02150     OBJ_TAINT(sysstack_error);
02151 
02152     /* utility functions */
02153     rb_define_global_function("proc", rb_block_proc, 0);
02154     rb_define_global_function("lambda", proc_lambda, 0);
02155 
02156     /* Method */
02157     rb_cMethod = rb_define_class("Method", rb_cObject);
02158     rb_undef_alloc_func(rb_cMethod);
02159     rb_undef_method(CLASS_OF(rb_cMethod), "new");
02160     rb_define_method(rb_cMethod, "==", method_eq, 1);
02161     rb_define_method(rb_cMethod, "eql?", method_eq, 1);
02162     rb_define_method(rb_cMethod, "hash", method_hash, 0);
02163     rb_define_method(rb_cMethod, "clone", method_clone, 0);
02164     rb_define_method(rb_cMethod, "call", rb_method_call, -1);
02165     rb_define_method(rb_cMethod, "[]", rb_method_call, -1);
02166     rb_define_method(rb_cMethod, "arity", method_arity_m, 0);
02167     rb_define_method(rb_cMethod, "inspect", method_inspect, 0);
02168     rb_define_method(rb_cMethod, "to_s", method_inspect, 0);
02169     rb_define_method(rb_cMethod, "to_proc", method_proc, 0);
02170     rb_define_method(rb_cMethod, "receiver", method_receiver, 0);
02171     rb_define_method(rb_cMethod, "name", method_name, 0);
02172     rb_define_method(rb_cMethod, "owner", method_owner, 0);
02173     rb_define_method(rb_cMethod, "unbind", method_unbind, 0);
02174     rb_define_method(rb_cMethod, "source_location", rb_method_location, 0);
02175     rb_define_method(rb_cMethod, "parameters", rb_method_parameters, 0);
02176     rb_define_method(rb_mKernel, "method", rb_obj_method, 1);
02177     rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1);
02178 
02179     /* UnboundMethod */
02180     rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject);
02181     rb_undef_alloc_func(rb_cUnboundMethod);
02182     rb_undef_method(CLASS_OF(rb_cUnboundMethod), "new");
02183     rb_define_method(rb_cUnboundMethod, "==", method_eq, 1);
02184     rb_define_method(rb_cUnboundMethod, "eql?", method_eq, 1);
02185     rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0);
02186     rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0);
02187     rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0);
02188     rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0);
02189     rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0);
02190     rb_define_method(rb_cUnboundMethod, "name", method_name, 0);
02191     rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0);
02192     rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
02193     rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0);
02194     rb_define_method(rb_cUnboundMethod, "parameters", rb_method_parameters, 0);
02195 
02196     /* Module#*_method */
02197     rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1);
02198     rb_define_method(rb_cModule, "public_instance_method", rb_mod_public_instance_method, 1);
02199     rb_define_private_method(rb_cModule, "define_method", rb_mod_define_method, -1);
02200 
02201     /* Kernel */
02202     rb_define_method(rb_mKernel, "define_singleton_method", rb_obj_define_method, -1);
02203 }
02204 
02205 /*
02206  *  Objects of class <code>Binding</code> encapsulate the execution
02207  *  context at some particular place in the code and retain this context
02208  *  for future use. The variables, methods, value of <code>self</code>,
02209  *  and possibly an iterator block that can be accessed in this context
02210  *  are all retained. Binding objects can be created using
02211  *  <code>Kernel#binding</code>, and are made available to the callback
02212  *  of <code>Kernel#set_trace_func</code>.
02213  *
02214  *  These binding objects can be passed as the second argument of the
02215  *  <code>Kernel#eval</code> method, establishing an environment for the
02216  *  evaluation.
02217  *
02218  *     class Demo
02219  *       def initialize(n)
02220  *         @secret = n
02221  *       end
02222  *       def get_binding
02223  *         return binding()
02224  *       end
02225  *     end
02226  *
02227  *     k1 = Demo.new(99)
02228  *     b1 = k1.get_binding
02229  *     k2 = Demo.new(-3)
02230  *     b2 = k2.get_binding
02231  *
02232  *     eval("@secret", b1)   #=> 99
02233  *     eval("@secret", b2)   #=> -3
02234  *     eval("@secret")       #=> nil
02235  *
02236  *  Binding objects have no class-specific methods.
02237  *
02238  */
02239 
02240 void
02241 Init_Binding(void)
02242 {
02243     rb_cBinding = rb_define_class("Binding", rb_cObject);
02244     rb_undef_alloc_func(rb_cBinding);
02245     rb_undef_method(CLASS_OF(rb_cBinding), "new");
02246     rb_define_method(rb_cBinding, "clone", binding_clone, 0);
02247     rb_define_method(rb_cBinding, "dup", binding_dup, 0);
02248     rb_define_method(rb_cBinding, "eval", bind_eval, -1);
02249     rb_define_global_function("binding", rb_f_binding, 0);
02250 }
02251 
02252