Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# proc.c

Go to the documentation of this file.

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