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