Ruby
2.0.0p247(2013-06-27revision41674)
|
00001 /********************************************************************** 00002 00003 proc.c - Proc, Binding, Env 00004 00005 $Author: nagachika $ 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 00959 meb.flag = 0; 00960 meb.mark = 0; 00961 meb.called_id = id; 00962 meb.klass = klass; 00963 meb.def = def; 00964 me = &meb; 00965 def = 0; 00966 00967 goto gen_method; 00968 } 00969 } 00970 rb_print_undef(klass, id, 0); 00971 } 00972 def = me->def; 00973 if (flag == NOEX_UNDEF) { 00974 flag = me->flag; 00975 if (scope && (flag & NOEX_MASK) != NOEX_PUBLIC) { 00976 const char *v = ""; 00977 switch (flag & NOEX_MASK) { 00978 case NOEX_PRIVATE: v = "private"; break; 00979 case NOEX_PROTECTED: v = "protected"; break; 00980 } 00981 rb_name_error(id, "method `%s' for %s `%s' is %s", 00982 rb_id2name(id), 00983 (RB_TYPE_P(klass, T_MODULE)) ? "module" : "class", 00984 rb_class2name(klass), 00985 v); 00986 } 00987 } 00988 if (def && def->type == VM_METHOD_TYPE_ZSUPER) { 00989 klass = RCLASS_SUPER(defined_class); 00990 id = def->original_id; 00991 goto again; 00992 } 00993 00994 klass = defined_class; 00995 00996 while (rclass != klass && 00997 (FL_TEST(rclass, FL_SINGLETON) || RB_TYPE_P(rclass, T_ICLASS))) { 00998 rclass = RCLASS_SUPER(rclass); 00999 } 01000 01001 if (RB_TYPE_P(klass, T_ICLASS)) { 01002 klass = RBASIC(klass)->klass; 01003 } 01004 01005 gen_method: 01006 method = TypedData_Make_Struct(mclass, struct METHOD, &method_data_type, data); 01007 01008 data->recv = obj; 01009 data->rclass = rclass; 01010 data->defined_class = defined_class; 01011 data->id = rid; 01012 data->me = ALLOC(rb_method_entry_t); 01013 *data->me = *me; 01014 data->me->def->alias_count++; 01015 data->ume = ALLOC(struct unlinked_method_entry_list_entry); 01016 01017 OBJ_INFECT(method, klass); 01018 01019 return method; 01020 } 01021 01022 01023 /********************************************************************** 01024 * 01025 * Document-class : Method 01026 * 01027 * Method objects are created by <code>Object#method</code>, and are 01028 * associated with a particular object (not just with a class). They 01029 * may be used to invoke the method within the object, and as a block 01030 * associated with an iterator. They may also be unbound from one 01031 * object (creating an <code>UnboundMethod</code>) and bound to 01032 * another. 01033 * 01034 * class Thing 01035 * def square(n) 01036 * n*n 01037 * end 01038 * end 01039 * thing = Thing.new 01040 * meth = thing.method(:square) 01041 * 01042 * meth.call(9) #=> 81 01043 * [ 1, 2, 3 ].collect(&meth) #=> [1, 4, 9] 01044 * 01045 */ 01046 01047 /* 01048 * call-seq: 01049 * meth == other_meth -> true or false 01050 * 01051 * Two method objects are equal if they are bound to the same 01052 * object and refer to the same method definition and their owners are the 01053 * same class or module. 01054 */ 01055 01056 static VALUE 01057 method_eq(VALUE method, VALUE other) 01058 { 01059 struct METHOD *m1, *m2; 01060 01061 if (!rb_obj_is_method(other)) 01062 return Qfalse; 01063 if (CLASS_OF(method) != CLASS_OF(other)) 01064 return Qfalse; 01065 01066 Check_TypedStruct(method, &method_data_type); 01067 m1 = (struct METHOD *)DATA_PTR(method); 01068 m2 = (struct METHOD *)DATA_PTR(other); 01069 01070 if (!rb_method_entry_eq(m1->me, m2->me) || 01071 m1->rclass != m2->rclass || 01072 m1->recv != m2->recv) { 01073 return Qfalse; 01074 } 01075 01076 return Qtrue; 01077 } 01078 01079 /* 01080 * call-seq: 01081 * meth.hash -> integer 01082 * 01083 * Returns a hash value corresponding to the method object. 01084 */ 01085 01086 static VALUE 01087 method_hash(VALUE method) 01088 { 01089 struct METHOD *m; 01090 st_index_t hash; 01091 01092 TypedData_Get_Struct(method, struct METHOD, &method_data_type, m); 01093 hash = rb_hash_start((st_index_t)m->rclass); 01094 hash = rb_hash_uint(hash, (st_index_t)m->recv); 01095 hash = rb_hash_method_entry(hash, m->me); 01096 hash = rb_hash_end(hash); 01097 01098 return INT2FIX(hash); 01099 } 01100 01101 /* 01102 * call-seq: 01103 * meth.unbind -> unbound_method 01104 * 01105 * Dissociates <i>meth</i> from its current receiver. The resulting 01106 * <code>UnboundMethod</code> can subsequently be bound to a new object 01107 * of the same class (see <code>UnboundMethod</code>). 01108 */ 01109 01110 static VALUE 01111 method_unbind(VALUE obj) 01112 { 01113 VALUE method; 01114 struct METHOD *orig, *data; 01115 01116 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, orig); 01117 method = TypedData_Make_Struct(rb_cUnboundMethod, struct METHOD, 01118 &method_data_type, data); 01119 data->recv = Qundef; 01120 data->id = orig->id; 01121 data->me = ALLOC(rb_method_entry_t); 01122 *data->me = *orig->me; 01123 if (orig->me->def) orig->me->def->alias_count++; 01124 data->rclass = orig->rclass; 01125 data->defined_class = orig->defined_class; 01126 data->ume = ALLOC(struct unlinked_method_entry_list_entry); 01127 OBJ_INFECT(method, obj); 01128 01129 return method; 01130 } 01131 01132 /* 01133 * call-seq: 01134 * meth.receiver -> object 01135 * 01136 * Returns the bound receiver of the method object. 01137 */ 01138 01139 static VALUE 01140 method_receiver(VALUE obj) 01141 { 01142 struct METHOD *data; 01143 01144 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data); 01145 return data->recv; 01146 } 01147 01148 /* 01149 * call-seq: 01150 * meth.name -> symbol 01151 * 01152 * Returns the name of the method. 01153 */ 01154 01155 static VALUE 01156 method_name(VALUE obj) 01157 { 01158 struct METHOD *data; 01159 01160 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data); 01161 return ID2SYM(data->id); 01162 } 01163 01164 /* 01165 * call-seq: 01166 * meth.owner -> class_or_module 01167 * 01168 * Returns the class or module that defines the method. 01169 */ 01170 01171 static VALUE 01172 method_owner(VALUE obj) 01173 { 01174 struct METHOD *data; 01175 01176 TypedData_Get_Struct(obj, struct METHOD, &method_data_type, data); 01177 return data->me->klass; 01178 } 01179 01180 void 01181 rb_method_name_error(VALUE klass, VALUE str) 01182 { 01183 const char *s0 = " class"; 01184 VALUE c = klass; 01185 01186 if (FL_TEST(c, FL_SINGLETON)) { 01187 VALUE obj = rb_ivar_get(klass, attached); 01188 01189 switch (TYPE(obj)) { 01190 case T_MODULE: 01191 case T_CLASS: 01192 c = obj; 01193 s0 = ""; 01194 } 01195 } 01196 else if (RB_TYPE_P(c, T_MODULE)) { 01197 s0 = " module"; 01198 } 01199 rb_name_error_str(str, "undefined method `%"PRIsVALUE"' for%s `%"PRIsVALUE"'", 01200 QUOTE(str), s0, rb_class_name(c)); 01201 } 01202 01203 /* 01204 * call-seq: 01205 * obj.method(sym) -> method 01206 * 01207 * Looks up the named method as a receiver in <i>obj</i>, returning a 01208 * <code>Method</code> object (or raising <code>NameError</code>). The 01209 * <code>Method</code> object acts as a closure in <i>obj</i>'s object 01210 * instance, so instance variables and the value of <code>self</code> 01211 * remain available. 01212 * 01213 * class Demo 01214 * def initialize(n) 01215 * @iv = n 01216 * end 01217 * def hello() 01218 * "Hello, @iv = #{@iv}" 01219 * end 01220 * end 01221 * 01222 * k = Demo.new(99) 01223 * m = k.method(:hello) 01224 * m.call #=> "Hello, @iv = 99" 01225 * 01226 * l = Demo.new('Fred') 01227 * m = l.method("hello") 01228 * m.call #=> "Hello, @iv = Fred" 01229 */ 01230 01231 VALUE 01232 rb_obj_method(VALUE obj, VALUE vid) 01233 { 01234 ID id = rb_check_id(&vid); 01235 if (!id) { 01236 rb_method_name_error(CLASS_OF(obj), vid); 01237 } 01238 return mnew(CLASS_OF(obj), obj, id, rb_cMethod, FALSE); 01239 } 01240 01241 /* 01242 * call-seq: 01243 * obj.public_method(sym) -> method 01244 * 01245 * Similar to _method_, searches public method only. 01246 */ 01247 01248 VALUE 01249 rb_obj_public_method(VALUE obj, VALUE vid) 01250 { 01251 ID id = rb_check_id(&vid); 01252 if (!id) { 01253 rb_method_name_error(CLASS_OF(obj), vid); 01254 } 01255 return mnew(CLASS_OF(obj), obj, id, rb_cMethod, TRUE); 01256 } 01257 01258 /* 01259 * call-seq: 01260 * mod.instance_method(symbol) -> unbound_method 01261 * 01262 * Returns an +UnboundMethod+ representing the given 01263 * instance method in _mod_. 01264 * 01265 * class Interpreter 01266 * def do_a() print "there, "; end 01267 * def do_d() print "Hello "; end 01268 * def do_e() print "!\n"; end 01269 * def do_v() print "Dave"; end 01270 * Dispatcher = { 01271 * "a" => instance_method(:do_a), 01272 * "d" => instance_method(:do_d), 01273 * "e" => instance_method(:do_e), 01274 * "v" => instance_method(:do_v) 01275 * } 01276 * def interpret(string) 01277 * string.each_char {|b| Dispatcher[b].bind(self).call } 01278 * end 01279 * end 01280 * 01281 * interpreter = Interpreter.new 01282 * interpreter.interpret('dave') 01283 * 01284 * <em>produces:</em> 01285 * 01286 * Hello there, Dave! 01287 */ 01288 01289 static VALUE 01290 rb_mod_instance_method(VALUE mod, VALUE vid) 01291 { 01292 ID id = rb_check_id(&vid); 01293 if (!id) { 01294 rb_method_name_error(mod, vid); 01295 } 01296 return mnew(mod, Qundef, id, rb_cUnboundMethod, FALSE); 01297 } 01298 01299 /* 01300 * call-seq: 01301 * mod.public_instance_method(symbol) -> unbound_method 01302 * 01303 * Similar to _instance_method_, searches public method only. 01304 */ 01305 01306 static VALUE 01307 rb_mod_public_instance_method(VALUE mod, VALUE vid) 01308 { 01309 ID id = rb_check_id(&vid); 01310 if (!id) { 01311 rb_method_name_error(mod, vid); 01312 } 01313 return mnew(mod, Qundef, id, rb_cUnboundMethod, TRUE); 01314 } 01315 01316 /* 01317 * call-seq: 01318 * define_method(symbol, method) -> new_method 01319 * define_method(symbol) { block } -> proc 01320 * 01321 * Defines an instance method in the receiver. The _method_ 01322 * parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object. 01323 * If a block is specified, it is used as the method body. This block 01324 * is evaluated using <code>instance_eval</code>, a point that is 01325 * tricky to demonstrate because <code>define_method</code> is private. 01326 * (This is why we resort to the +send+ hack in this example.) 01327 * 01328 * class A 01329 * def fred 01330 * puts "In Fred" 01331 * end 01332 * def create_method(name, &block) 01333 * self.class.send(:define_method, name, &block) 01334 * end 01335 * define_method(:wilma) { puts "Charge it!" } 01336 * end 01337 * class B < A 01338 * define_method(:barney, instance_method(:fred)) 01339 * end 01340 * a = B.new 01341 * a.barney 01342 * a.wilma 01343 * a.create_method(:betty) { p self } 01344 * a.betty 01345 * 01346 * <em>produces:</em> 01347 * 01348 * In Fred 01349 * Charge it! 01350 * #<B:0x401b39e8> 01351 */ 01352 01353 static VALUE 01354 rb_mod_define_method(int argc, VALUE *argv, VALUE mod) 01355 { 01356 ID id; 01357 VALUE body; 01358 int noex = NOEX_PUBLIC; 01359 01360 if (argc == 1) { 01361 id = rb_to_id(argv[0]); 01362 body = rb_block_lambda(); 01363 } 01364 else { 01365 rb_check_arity(argc, 1, 2); 01366 id = rb_to_id(argv[0]); 01367 body = argv[1]; 01368 if (!rb_obj_is_method(body) && !rb_obj_is_proc(body)) { 01369 rb_raise(rb_eTypeError, 01370 "wrong argument type %s (expected Proc/Method)", 01371 rb_obj_classname(body)); 01372 } 01373 } 01374 01375 if (rb_obj_is_method(body)) { 01376 struct METHOD *method = (struct METHOD *)DATA_PTR(body); 01377 VALUE rclass = method->rclass; 01378 if (rclass != mod && !RB_TYPE_P(rclass, T_MODULE) && 01379 !RTEST(rb_class_inherited_p(mod, rclass))) { 01380 if (FL_TEST(rclass, FL_SINGLETON)) { 01381 rb_raise(rb_eTypeError, 01382 "can't bind singleton method to a different class"); 01383 } 01384 else { 01385 rb_raise(rb_eTypeError, 01386 "bind argument must be a subclass of %s", 01387 rb_class2name(rclass)); 01388 } 01389 } 01390 rb_method_entry_set(mod, id, method->me, noex); 01391 } 01392 else if (rb_obj_is_proc(body)) { 01393 rb_proc_t *proc; 01394 body = proc_dup(body); 01395 GetProcPtr(body, proc); 01396 if (BUILTIN_TYPE(proc->block.iseq) != T_NODE) { 01397 proc->block.iseq->defined_method_id = id; 01398 proc->block.iseq->klass = mod; 01399 proc->is_lambda = TRUE; 01400 proc->is_from_method = TRUE; 01401 proc->block.klass = mod; 01402 } 01403 rb_add_method(mod, id, VM_METHOD_TYPE_BMETHOD, (void *)body, noex); 01404 } 01405 else { 01406 /* type error */ 01407 rb_raise(rb_eTypeError, "wrong argument type (expected Proc/Method)"); 01408 } 01409 01410 return body; 01411 } 01412 01413 /* 01414 * call-seq: 01415 * define_singleton_method(symbol, method) -> new_method 01416 * define_singleton_method(symbol) { block } -> proc 01417 * 01418 * Defines a singleton method in the receiver. The _method_ 01419 * parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object. 01420 * If a block is specified, it is used as the method body. 01421 * 01422 * class A 01423 * class << self 01424 * def class_name 01425 * to_s 01426 * end 01427 * end 01428 * end 01429 * A.define_singleton_method(:who_am_i) do 01430 * "I am: #{class_name}" 01431 * end 01432 * A.who_am_i # ==> "I am: A" 01433 * 01434 * guy = "Bob" 01435 * guy.define_singleton_method(:hello) { "#{self}: Hello there!" } 01436 * guy.hello #=> "Bob: Hello there!" 01437 */ 01438 01439 static VALUE 01440 rb_obj_define_method(int argc, VALUE *argv, VALUE obj) 01441 { 01442 VALUE klass = rb_singleton_class(obj); 01443 01444 return rb_mod_define_method(argc, argv, klass); 01445 } 01446 01447 /* 01448 * define_method(symbol, method) -> new_method 01449 * define_method(symbol) { block } -> proc 01450 * 01451 * Defines a global function by _method_ or the block. 01452 */ 01453 01454 static VALUE 01455 top_define_method(int argc, VALUE *argv, VALUE obj) 01456 { 01457 rb_thread_t *th = GET_THREAD(); 01458 VALUE klass; 01459 01460 rb_secure(4); 01461 klass = th->top_wrapper; 01462 if (klass) { 01463 rb_warning("main.define_method in the wrapped load is effective only in wrapper module"); 01464 } 01465 else { 01466 klass = rb_cObject; 01467 } 01468 return rb_mod_define_method(argc, argv, klass); 01469 } 01470 01471 /* 01472 * call-seq: 01473 * method.clone -> new_method 01474 * 01475 * Returns a clone of this method. 01476 * 01477 * class A 01478 * def foo 01479 * return "bar" 01480 * end 01481 * end 01482 * 01483 * m = A.new.method(:foo) 01484 * m.call # => "bar" 01485 * n = m.clone.call # => "bar" 01486 */ 01487 01488 static VALUE 01489 method_clone(VALUE self) 01490 { 01491 VALUE clone; 01492 struct METHOD *orig, *data; 01493 01494 TypedData_Get_Struct(self, struct METHOD, &method_data_type, orig); 01495 clone = TypedData_Make_Struct(CLASS_OF(self), struct METHOD, &method_data_type, data); 01496 CLONESETUP(clone, self); 01497 *data = *orig; 01498 data->me = ALLOC(rb_method_entry_t); 01499 *data->me = *orig->me; 01500 if (data->me->def) data->me->def->alias_count++; 01501 data->ume = ALLOC(struct unlinked_method_entry_list_entry); 01502 01503 return clone; 01504 } 01505 01506 /* 01507 * call-seq: 01508 * meth.call(args, ...) -> obj 01509 * meth[args, ...] -> obj 01510 * 01511 * Invokes the <i>meth</i> with the specified arguments, returning the 01512 * method's return value. 01513 * 01514 * m = 12.method("+") 01515 * m.call(3) #=> 15 01516 * m.call(20) #=> 32 01517 */ 01518 01519 VALUE 01520 rb_method_call(int argc, VALUE *argv, VALUE method) 01521 { 01522 VALUE proc = rb_block_given_p() ? rb_block_proc() : Qnil; 01523 return rb_method_call_with_block(argc, argv, method, proc); 01524 } 01525 01526 VALUE 01527 rb_method_call_with_block(int argc, VALUE *argv, VALUE method, VALUE pass_procval) 01528 { 01529 VALUE result = Qnil; /* OK */ 01530 struct METHOD *data; 01531 int state; 01532 volatile int safe = -1; 01533 01534 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data); 01535 if (data->recv == Qundef) { 01536 rb_raise(rb_eTypeError, "can't call unbound method; bind first"); 01537 } 01538 PUSH_TAG(); 01539 if (OBJ_TAINTED(method)) { 01540 const int safe_level_to_run = 4 /*SAFE_LEVEL_MAX*/; 01541 safe = rb_safe_level(); 01542 if (rb_safe_level() < safe_level_to_run) { 01543 rb_set_safe_level_force(safe_level_to_run); 01544 } 01545 } 01546 if ((state = EXEC_TAG()) == 0) { 01547 rb_thread_t *th = GET_THREAD(); 01548 rb_block_t *block = 0; 01549 01550 if (!NIL_P(pass_procval)) { 01551 rb_proc_t *pass_proc; 01552 GetProcPtr(pass_procval, pass_proc); 01553 block = &pass_proc->block; 01554 } 01555 01556 th->passed_block = block; 01557 result = rb_vm_call(th, data->recv, data->id, argc, argv, data->me, data->defined_class); 01558 } 01559 POP_TAG(); 01560 if (safe >= 0) 01561 rb_set_safe_level_force(safe); 01562 if (state) 01563 JUMP_TAG(state); 01564 return result; 01565 } 01566 01567 /********************************************************************** 01568 * 01569 * Document-class: UnboundMethod 01570 * 01571 * Ruby supports two forms of objectified methods. Class 01572 * <code>Method</code> is used to represent methods that are associated 01573 * with a particular object: these method objects are bound to that 01574 * object. Bound method objects for an object can be created using 01575 * <code>Object#method</code>. 01576 * 01577 * Ruby also supports unbound methods; methods objects that are not 01578 * associated with a particular object. These can be created either by 01579 * calling <code>Module#instance_method</code> or by calling 01580 * <code>unbind</code> on a bound method object. The result of both of 01581 * these is an <code>UnboundMethod</code> object. 01582 * 01583 * Unbound methods can only be called after they are bound to an 01584 * object. That object must be be a kind_of? the method's original 01585 * class. 01586 * 01587 * class Square 01588 * def area 01589 * @side * @side 01590 * end 01591 * def initialize(side) 01592 * @side = side 01593 * end 01594 * end 01595 * 01596 * area_un = Square.instance_method(:area) 01597 * 01598 * s = Square.new(12) 01599 * area = area_un.bind(s) 01600 * area.call #=> 144 01601 * 01602 * Unbound methods are a reference to the method at the time it was 01603 * objectified: subsequent changes to the underlying class will not 01604 * affect the unbound method. 01605 * 01606 * class Test 01607 * def test 01608 * :original 01609 * end 01610 * end 01611 * um = Test.instance_method(:test) 01612 * class Test 01613 * def test 01614 * :modified 01615 * end 01616 * end 01617 * t = Test.new 01618 * t.test #=> :modified 01619 * um.bind(t).call #=> :original 01620 * 01621 */ 01622 01623 /* 01624 * call-seq: 01625 * umeth.bind(obj) -> method 01626 * 01627 * Bind <i>umeth</i> to <i>obj</i>. If <code>Klass</code> was the class 01628 * from which <i>umeth</i> was obtained, 01629 * <code>obj.kind_of?(Klass)</code> must be true. 01630 * 01631 * class A 01632 * def test 01633 * puts "In test, class = #{self.class}" 01634 * end 01635 * end 01636 * class B < A 01637 * end 01638 * class C < B 01639 * end 01640 * 01641 * 01642 * um = B.instance_method(:test) 01643 * bm = um.bind(C.new) 01644 * bm.call 01645 * bm = um.bind(B.new) 01646 * bm.call 01647 * bm = um.bind(A.new) 01648 * bm.call 01649 * 01650 * <em>produces:</em> 01651 * 01652 * In test, class = C 01653 * In test, class = B 01654 * prog.rb:16:in `bind': bind argument must be an instance of B (TypeError) 01655 * from prog.rb:16 01656 */ 01657 01658 static VALUE 01659 umethod_bind(VALUE method, VALUE recv) 01660 { 01661 struct METHOD *data, *bound; 01662 01663 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data); 01664 01665 if (!RB_TYPE_P(data->rclass, T_MODULE) && 01666 data->rclass != CLASS_OF(recv) && !rb_obj_is_kind_of(recv, data->rclass)) { 01667 if (FL_TEST(data->rclass, FL_SINGLETON)) { 01668 rb_raise(rb_eTypeError, 01669 "singleton method called for a different object"); 01670 } 01671 else { 01672 rb_raise(rb_eTypeError, "bind argument must be an instance of %s", 01673 rb_class2name(data->rclass)); 01674 } 01675 } 01676 01677 method = TypedData_Make_Struct(rb_cMethod, struct METHOD, &method_data_type, bound); 01678 *bound = *data; 01679 bound->me = ALLOC(rb_method_entry_t); 01680 *bound->me = *data->me; 01681 if (bound->me->def) bound->me->def->alias_count++; 01682 bound->recv = recv; 01683 bound->rclass = CLASS_OF(recv); 01684 data->ume = ALLOC(struct unlinked_method_entry_list_entry); 01685 01686 return method; 01687 } 01688 01689 /* 01690 * Returns the number of required parameters and stores the maximum 01691 * number of parameters in max, or UNLIMITED_ARGUMENTS 01692 * if there is no maximum. 01693 */ 01694 static int 01695 rb_method_entry_min_max_arity(const rb_method_entry_t *me, int *max) 01696 { 01697 const rb_method_definition_t *def = me->def; 01698 if (!def) return *max = 0; 01699 switch (def->type) { 01700 case VM_METHOD_TYPE_CFUNC: 01701 if (def->body.cfunc.argc < 0) { 01702 *max = UNLIMITED_ARGUMENTS; 01703 return 0; 01704 } 01705 return *max = check_argc(def->body.cfunc.argc); 01706 case VM_METHOD_TYPE_ZSUPER: 01707 *max = UNLIMITED_ARGUMENTS; 01708 return 0; 01709 case VM_METHOD_TYPE_ATTRSET: 01710 return *max = 1; 01711 case VM_METHOD_TYPE_IVAR: 01712 return *max = 0; 01713 case VM_METHOD_TYPE_BMETHOD: 01714 return rb_proc_min_max_arity(def->body.proc, max); 01715 case VM_METHOD_TYPE_ISEQ: { 01716 rb_iseq_t *iseq = def->body.iseq; 01717 return rb_iseq_min_max_arity(iseq, max); 01718 } 01719 case VM_METHOD_TYPE_UNDEF: 01720 case VM_METHOD_TYPE_NOTIMPLEMENTED: 01721 return *max = 0; 01722 case VM_METHOD_TYPE_MISSING: 01723 *max = UNLIMITED_ARGUMENTS; 01724 return 0; 01725 case VM_METHOD_TYPE_OPTIMIZED: { 01726 switch (def->body.optimize_type) { 01727 case OPTIMIZED_METHOD_TYPE_SEND: 01728 *max = UNLIMITED_ARGUMENTS; 01729 return 0; 01730 default: 01731 break; 01732 } 01733 } 01734 case VM_METHOD_TYPE_REFINED: 01735 *max = UNLIMITED_ARGUMENTS; 01736 return 0; 01737 } 01738 rb_bug("rb_method_entry_min_max_arity: invalid method entry type (%d)", def->type); 01739 UNREACHABLE; 01740 } 01741 01742 int 01743 rb_method_entry_arity(const rb_method_entry_t *me) 01744 { 01745 int max, min = rb_method_entry_min_max_arity(me, &max); 01746 return min == max ? min : -min-1; 01747 } 01748 01749 /* 01750 * call-seq: 01751 * meth.arity -> fixnum 01752 * 01753 * Returns an indication of the number of arguments accepted by a 01754 * method. Returns a nonnegative integer for methods that take a fixed 01755 * number of arguments. For Ruby methods that take a variable number of 01756 * arguments, returns -n-1, where n is the number of required 01757 * arguments. For methods written in C, returns -1 if the call takes a 01758 * variable number of arguments. 01759 * 01760 * class C 01761 * def one; end 01762 * def two(a); end 01763 * def three(*a); end 01764 * def four(a, b); end 01765 * def five(a, b, *c); end 01766 * def six(a, b, *c, &d); end 01767 * end 01768 * c = C.new 01769 * c.method(:one).arity #=> 0 01770 * c.method(:two).arity #=> 1 01771 * c.method(:three).arity #=> -1 01772 * c.method(:four).arity #=> 2 01773 * c.method(:five).arity #=> -3 01774 * c.method(:six).arity #=> -3 01775 * 01776 * "cat".method(:size).arity #=> 0 01777 * "cat".method(:replace).arity #=> 1 01778 * "cat".method(:squeeze).arity #=> -1 01779 * "cat".method(:count).arity #=> -1 01780 */ 01781 01782 static VALUE 01783 method_arity_m(VALUE method) 01784 { 01785 int n = method_arity(method); 01786 return INT2FIX(n); 01787 } 01788 01789 static int 01790 method_arity(VALUE method) 01791 { 01792 struct METHOD *data; 01793 01794 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data); 01795 return rb_method_entry_arity(data->me); 01796 } 01797 01798 static rb_method_entry_t * 01799 original_method_entry(VALUE mod, ID id) 01800 { 01801 VALUE rclass; 01802 rb_method_entry_t *me; 01803 while ((me = rb_method_entry(mod, id, &rclass)) != 0) { 01804 rb_method_definition_t *def = me->def; 01805 if (!def) break; 01806 if (def->type != VM_METHOD_TYPE_ZSUPER) break; 01807 mod = RCLASS_SUPER(rclass); 01808 id = def->original_id; 01809 } 01810 return me; 01811 } 01812 01813 static int 01814 method_min_max_arity(VALUE method, int *max) 01815 { 01816 struct METHOD *data; 01817 01818 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data); 01819 return rb_method_entry_min_max_arity(data->me, max); 01820 } 01821 01822 int 01823 rb_mod_method_arity(VALUE mod, ID id) 01824 { 01825 rb_method_entry_t *me = original_method_entry(mod, id); 01826 if (!me) return 0; /* should raise? */ 01827 return rb_method_entry_arity(me); 01828 } 01829 01830 int 01831 rb_obj_method_arity(VALUE obj, ID id) 01832 { 01833 return rb_mod_method_arity(CLASS_OF(obj), id); 01834 } 01835 01836 static inline rb_method_definition_t * 01837 method_get_def(VALUE method) 01838 { 01839 struct METHOD *data; 01840 01841 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data); 01842 return data->me->def; 01843 } 01844 01845 static rb_iseq_t * 01846 method_get_iseq(rb_method_definition_t *def) 01847 { 01848 switch (def->type) { 01849 case VM_METHOD_TYPE_BMETHOD: 01850 return get_proc_iseq(def->body.proc, 0); 01851 case VM_METHOD_TYPE_ISEQ: 01852 return def->body.iseq; 01853 default: 01854 return 0; 01855 } 01856 } 01857 01858 rb_iseq_t * 01859 rb_method_get_iseq(VALUE method) 01860 { 01861 return method_get_iseq(method_get_def(method)); 01862 } 01863 01864 static VALUE 01865 method_def_location(rb_method_definition_t *def) 01866 { 01867 if (def->type == VM_METHOD_TYPE_ATTRSET || def->type == VM_METHOD_TYPE_IVAR) { 01868 if (!def->body.attr.location) 01869 return Qnil; 01870 return rb_ary_dup(def->body.attr.location); 01871 } 01872 return iseq_location(method_get_iseq(def)); 01873 } 01874 01875 VALUE 01876 rb_method_entry_location(rb_method_entry_t *me) 01877 { 01878 if (!me || !me->def) return Qnil; 01879 return method_def_location(me->def); 01880 } 01881 01882 VALUE 01883 rb_mod_method_location(VALUE mod, ID id) 01884 { 01885 rb_method_entry_t *me = original_method_entry(mod, id); 01886 return rb_method_entry_location(me); 01887 } 01888 01889 VALUE 01890 rb_obj_method_location(VALUE obj, ID id) 01891 { 01892 return rb_mod_method_location(CLASS_OF(obj), id); 01893 } 01894 01895 /* 01896 * call-seq: 01897 * meth.source_location -> [String, Fixnum] 01898 * 01899 * Returns the Ruby source filename and line number containing this method 01900 * or nil if this method was not defined in Ruby (i.e. native) 01901 */ 01902 01903 VALUE 01904 rb_method_location(VALUE method) 01905 { 01906 rb_method_definition_t *def = method_get_def(method); 01907 return method_def_location(def); 01908 } 01909 01910 /* 01911 * call-seq: 01912 * meth.parameters -> array 01913 * 01914 * Returns the parameter information of this method. 01915 */ 01916 01917 static VALUE 01918 rb_method_parameters(VALUE method) 01919 { 01920 rb_iseq_t *iseq = rb_method_get_iseq(method); 01921 if (!iseq) { 01922 return unnamed_parameters(method_arity(method)); 01923 } 01924 return rb_iseq_parameters(iseq, 0); 01925 } 01926 01927 /* 01928 * call-seq: 01929 * meth.to_s -> string 01930 * meth.inspect -> string 01931 * 01932 * Returns the name of the underlying method. 01933 * 01934 * "cat".method(:count).inspect #=> "#<Method: String#count>" 01935 */ 01936 01937 static VALUE 01938 method_inspect(VALUE method) 01939 { 01940 struct METHOD *data; 01941 VALUE str; 01942 const char *s; 01943 const char *sharp = "#"; 01944 01945 TypedData_Get_Struct(method, struct METHOD, &method_data_type, data); 01946 str = rb_str_buf_new2("#<"); 01947 s = rb_obj_classname(method); 01948 rb_str_buf_cat2(str, s); 01949 rb_str_buf_cat2(str, ": "); 01950 01951 if (FL_TEST(data->me->klass, FL_SINGLETON)) { 01952 VALUE v = rb_ivar_get(data->me->klass, attached); 01953 01954 if (data->recv == Qundef) { 01955 rb_str_buf_append(str, rb_inspect(data->me->klass)); 01956 } 01957 else if (data->recv == v) { 01958 rb_str_buf_append(str, rb_inspect(v)); 01959 sharp = "."; 01960 } 01961 else { 01962 rb_str_buf_append(str, rb_inspect(data->recv)); 01963 rb_str_buf_cat2(str, "("); 01964 rb_str_buf_append(str, rb_inspect(v)); 01965 rb_str_buf_cat2(str, ")"); 01966 sharp = "."; 01967 } 01968 } 01969 else { 01970 rb_str_buf_cat2(str, rb_class2name(data->rclass)); 01971 if (data->rclass != data->me->klass) { 01972 rb_str_buf_cat2(str, "("); 01973 rb_str_buf_cat2(str, rb_class2name(data->me->klass)); 01974 rb_str_buf_cat2(str, ")"); 01975 } 01976 } 01977 rb_str_buf_cat2(str, sharp); 01978 rb_str_append(str, rb_id2str(data->me->def->original_id)); 01979 if (data->me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) { 01980 rb_str_buf_cat2(str, " (not-implemented)"); 01981 } 01982 rb_str_buf_cat2(str, ">"); 01983 01984 return str; 01985 } 01986 01987 static VALUE 01988 mproc(VALUE method) 01989 { 01990 return rb_funcall2(rb_mRubyVMFrozenCore, idProc, 0, 0); 01991 } 01992 01993 static VALUE 01994 mlambda(VALUE method) 01995 { 01996 return rb_funcall(rb_mRubyVMFrozenCore, idLambda, 0, 0); 01997 } 01998 01999 static VALUE 02000 bmcall(VALUE args, VALUE method, int argc, VALUE *argv, VALUE passed_proc) 02001 { 02002 volatile VALUE a; 02003 VALUE ret; 02004 02005 if (CLASS_OF(args) != rb_cArray) { 02006 args = rb_ary_new3(1, args); 02007 argc = 1; 02008 } 02009 else { 02010 argc = check_argc(RARRAY_LEN(args)); 02011 } 02012 ret = rb_method_call_with_block(argc, RARRAY_PTR(args), method, passed_proc); 02013 RB_GC_GUARD(a) = args; 02014 return ret; 02015 } 02016 02017 VALUE 02018 rb_proc_new( 02019 VALUE (*func)(ANYARGS), /* VALUE yieldarg[, VALUE procarg] */ 02020 VALUE val) 02021 { 02022 VALUE procval = rb_iterate(mproc, 0, func, val); 02023 return procval; 02024 } 02025 02026 /* 02027 * call-seq: 02028 * meth.to_proc -> prc 02029 * 02030 * Returns a <code>Proc</code> object corresponding to this method. 02031 */ 02032 02033 static VALUE 02034 method_proc(VALUE method) 02035 { 02036 VALUE procval; 02037 rb_proc_t *proc; 02038 /* 02039 * class Method 02040 * def to_proc 02041 * proc{|*args| 02042 * self.call(*args) 02043 * } 02044 * end 02045 * end 02046 */ 02047 procval = rb_iterate(mlambda, 0, bmcall, method); 02048 GetProcPtr(procval, proc); 02049 proc->is_from_method = 1; 02050 return procval; 02051 } 02052 02053 /* 02054 * call_seq: 02055 * local_jump_error.exit_value -> obj 02056 * 02057 * Returns the exit value associated with this +LocalJumpError+. 02058 */ 02059 static VALUE 02060 localjump_xvalue(VALUE exc) 02061 { 02062 return rb_iv_get(exc, "@exit_value"); 02063 } 02064 02065 /* 02066 * call-seq: 02067 * local_jump_error.reason -> symbol 02068 * 02069 * The reason this block was terminated: 02070 * :break, :redo, :retry, :next, :return, or :noreason. 02071 */ 02072 02073 static VALUE 02074 localjump_reason(VALUE exc) 02075 { 02076 return rb_iv_get(exc, "@reason"); 02077 } 02078 02079 /* 02080 * call-seq: 02081 * prc.binding -> binding 02082 * 02083 * Returns the binding associated with <i>prc</i>. Note that 02084 * <code>Kernel#eval</code> accepts either a <code>Proc</code> or a 02085 * <code>Binding</code> object as its second parameter. 02086 * 02087 * def fred(param) 02088 * proc {} 02089 * end 02090 * 02091 * b = fred(99) 02092 * eval("param", b.binding) #=> 99 02093 */ 02094 static VALUE 02095 proc_binding(VALUE self) 02096 { 02097 rb_proc_t *proc; 02098 VALUE bindval; 02099 rb_binding_t *bind; 02100 02101 GetProcPtr(self, proc); 02102 if (RB_TYPE_P((VALUE)proc->block.iseq, T_NODE)) { 02103 if (!IS_METHOD_PROC_NODE((NODE *)proc->block.iseq)) { 02104 rb_raise(rb_eArgError, "Can't create Binding from C level Proc"); 02105 } 02106 } 02107 02108 bindval = binding_alloc(rb_cBinding); 02109 GetBindingPtr(bindval, bind); 02110 bind->env = proc->envval; 02111 if (RUBY_VM_NORMAL_ISEQ_P(proc->block.iseq)) { 02112 bind->path = proc->block.iseq->location.path; 02113 bind->first_lineno = rb_iseq_first_lineno(proc->block.iseq); 02114 } 02115 else { 02116 bind->path = Qnil; 02117 bind->first_lineno = 0; 02118 } 02119 return bindval; 02120 } 02121 02122 static VALUE curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc); 02123 02124 static VALUE 02125 make_curry_proc(VALUE proc, VALUE passed, VALUE arity) 02126 { 02127 VALUE args = rb_ary_new3(3, proc, passed, arity); 02128 rb_proc_t *procp; 02129 int is_lambda; 02130 02131 GetProcPtr(proc, procp); 02132 is_lambda = procp->is_lambda; 02133 rb_ary_freeze(passed); 02134 rb_ary_freeze(args); 02135 proc = rb_proc_new(curry, args); 02136 GetProcPtr(proc, procp); 02137 procp->is_lambda = is_lambda; 02138 return proc; 02139 } 02140 02141 static VALUE 02142 curry(VALUE dummy, VALUE args, int argc, VALUE *argv, VALUE passed_proc) 02143 { 02144 VALUE proc, passed, arity; 02145 proc = RARRAY_PTR(args)[0]; 02146 passed = RARRAY_PTR(args)[1]; 02147 arity = RARRAY_PTR(args)[2]; 02148 02149 passed = rb_ary_plus(passed, rb_ary_new4(argc, argv)); 02150 rb_ary_freeze(passed); 02151 02152 if (RARRAY_LEN(passed) < FIX2INT(arity)) { 02153 if (!NIL_P(passed_proc)) { 02154 rb_warn("given block not used"); 02155 } 02156 arity = make_curry_proc(proc, passed, arity); 02157 return arity; 02158 } 02159 else { 02160 return rb_proc_call_with_block(proc, check_argc(RARRAY_LEN(passed)), 02161 RARRAY_PTR(passed), passed_proc); 02162 } 02163 } 02164 02165 /* 02166 * call-seq: 02167 * prc.curry -> a_proc 02168 * prc.curry(arity) -> a_proc 02169 * 02170 * Returns a curried proc. If the optional <i>arity</i> argument is given, 02171 * it determines the number of arguments. 02172 * A curried proc receives some arguments. If a sufficient number of 02173 * arguments are supplied, it passes the supplied arguments to the original 02174 * proc and returns the result. Otherwise, returns another curried proc that 02175 * takes the rest of arguments. 02176 * 02177 * b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } 02178 * p b.curry[1][2][3] #=> 6 02179 * p b.curry[1, 2][3, 4] #=> 6 02180 * p b.curry(5)[1][2][3][4][5] #=> 6 02181 * p b.curry(5)[1, 2][3, 4][5] #=> 6 02182 * p b.curry(1)[1] #=> 1 02183 * 02184 * b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } 02185 * p b.curry[1][2][3] #=> 6 02186 * p b.curry[1, 2][3, 4] #=> 10 02187 * p b.curry(5)[1][2][3][4][5] #=> 15 02188 * p b.curry(5)[1, 2][3, 4][5] #=> 15 02189 * p b.curry(1)[1] #=> 1 02190 * 02191 * b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } 02192 * p b.curry[1][2][3] #=> 6 02193 * p b.curry[1, 2][3, 4] #=> wrong number of arguments (4 for 3) 02194 * p b.curry(5) #=> wrong number of arguments (5 for 3) 02195 * p b.curry(1) #=> wrong number of arguments (1 for 3) 02196 * 02197 * b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) } 02198 * p b.curry[1][2][3] #=> 6 02199 * p b.curry[1, 2][3, 4] #=> 10 02200 * p b.curry(5)[1][2][3][4][5] #=> 15 02201 * p b.curry(5)[1, 2][3, 4][5] #=> 15 02202 * p b.curry(1) #=> wrong number of arguments (1 for 3) 02203 * 02204 * b = proc { :foo } 02205 * p b.curry[] #=> :foo 02206 */ 02207 static VALUE 02208 proc_curry(int argc, VALUE *argv, VALUE self) 02209 { 02210 int sarity, max_arity, min_arity = rb_proc_min_max_arity(self, &max_arity); 02211 VALUE arity; 02212 02213 rb_scan_args(argc, argv, "01", &arity); 02214 if (NIL_P(arity)) { 02215 arity = INT2FIX(min_arity); 02216 } 02217 else { 02218 sarity = FIX2INT(arity); 02219 if (rb_proc_lambda_p(self)) { 02220 rb_check_arity(sarity, min_arity, max_arity); 02221 } 02222 } 02223 02224 return make_curry_proc(self, rb_ary_new(), arity); 02225 } 02226 02227 /* 02228 * Document-class: LocalJumpError 02229 * 02230 * Raised when Ruby can't yield as requested. 02231 * 02232 * A typical scenario is attempting to yield when no block is given: 02233 * 02234 * def call_block 02235 * yield 42 02236 * end 02237 * call_block 02238 * 02239 * <em>raises the exception:</em> 02240 * 02241 * LocalJumpError: no block given (yield) 02242 * 02243 * A more subtle example: 02244 * 02245 * def get_me_a_return 02246 * Proc.new { return 42 } 02247 * end 02248 * get_me_a_return.call 02249 * 02250 * <em>raises the exception:</em> 02251 * 02252 * LocalJumpError: unexpected return 02253 */ 02254 02255 /* 02256 * Document-class: SystemStackError 02257 * 02258 * Raised in case of a stack overflow. 02259 * 02260 * def me_myself_and_i 02261 * me_myself_and_i 02262 * end 02263 * me_myself_and_i 02264 * 02265 * <em>raises the exception:</em> 02266 * 02267 * SystemStackError: stack level too deep 02268 */ 02269 02270 /* 02271 * <code>Proc</code> objects are blocks of code that have been bound to 02272 * a set of local variables. Once bound, the code may be called in 02273 * different contexts and still access those variables. 02274 * 02275 * def gen_times(factor) 02276 * return Proc.new {|n| n*factor } 02277 * end 02278 * 02279 * times3 = gen_times(3) 02280 * times5 = gen_times(5) 02281 * 02282 * times3.call(12) #=> 36 02283 * times5.call(5) #=> 25 02284 * times3.call(times5.call(4)) #=> 60 02285 * 02286 */ 02287 02288 void 02289 Init_Proc(void) 02290 { 02291 /* Proc */ 02292 rb_cProc = rb_define_class("Proc", rb_cObject); 02293 rb_undef_alloc_func(rb_cProc); 02294 rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, -1); 02295 02296 #if 0 /* incomplete. */ 02297 rb_add_method(rb_cProc, rb_intern("call"), VM_METHOD_TYPE_OPTIMIZED, 02298 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0); 02299 rb_add_method(rb_cProc, rb_intern("[]"), VM_METHOD_TYPE_OPTIMIZED, 02300 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0); 02301 rb_add_method(rb_cProc, rb_intern("==="), VM_METHOD_TYPE_OPTIMIZED, 02302 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0); 02303 rb_add_method(rb_cProc, rb_intern("yield"), VM_METHOD_TYPE_OPTIMIZED, 02304 (void *)OPTIMIZED_METHOD_TYPE_CALL, 0); 02305 #else 02306 rb_define_method(rb_cProc, "call", proc_call, -1); 02307 rb_define_method(rb_cProc, "[]", proc_call, -1); 02308 rb_define_method(rb_cProc, "===", proc_call, -1); 02309 rb_define_method(rb_cProc, "yield", proc_call, -1); 02310 #endif 02311 rb_define_method(rb_cProc, "to_proc", proc_to_proc, 0); 02312 rb_define_method(rb_cProc, "arity", proc_arity, 0); 02313 rb_define_method(rb_cProc, "clone", proc_clone, 0); 02314 rb_define_method(rb_cProc, "dup", proc_dup, 0); 02315 rb_define_method(rb_cProc, "hash", proc_hash, 0); 02316 rb_define_method(rb_cProc, "to_s", proc_to_s, 0); 02317 rb_define_alias(rb_cProc, "inspect", "to_s"); 02318 rb_define_method(rb_cProc, "lambda?", rb_proc_lambda_p, 0); 02319 rb_define_method(rb_cProc, "binding", proc_binding, 0); 02320 rb_define_method(rb_cProc, "curry", proc_curry, -1); 02321 rb_define_method(rb_cProc, "source_location", rb_proc_location, 0); 02322 rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0); 02323 02324 /* Exceptions */ 02325 rb_eLocalJumpError = rb_define_class("LocalJumpError", rb_eStandardError); 02326 rb_define_method(rb_eLocalJumpError, "exit_value", localjump_xvalue, 0); 02327 rb_define_method(rb_eLocalJumpError, "reason", localjump_reason, 0); 02328 02329 rb_eSysStackError = rb_define_class("SystemStackError", rb_eException); 02330 sysstack_error = rb_exc_new3(rb_eSysStackError, 02331 rb_obj_freeze(rb_str_new2("stack level too deep"))); 02332 OBJ_TAINT(sysstack_error); 02333 02334 /* utility functions */ 02335 rb_define_global_function("proc", rb_block_proc, 0); 02336 rb_define_global_function("lambda", rb_block_lambda, 0); 02337 02338 /* Method */ 02339 rb_cMethod = rb_define_class("Method", rb_cObject); 02340 rb_undef_alloc_func(rb_cMethod); 02341 rb_undef_method(CLASS_OF(rb_cMethod), "new"); 02342 rb_define_method(rb_cMethod, "==", method_eq, 1); 02343 rb_define_method(rb_cMethod, "eql?", method_eq, 1); 02344 rb_define_method(rb_cMethod, "hash", method_hash, 0); 02345 rb_define_method(rb_cMethod, "clone", method_clone, 0); 02346 rb_define_method(rb_cMethod, "call", rb_method_call, -1); 02347 rb_define_method(rb_cMethod, "[]", rb_method_call, -1); 02348 rb_define_method(rb_cMethod, "arity", method_arity_m, 0); 02349 rb_define_method(rb_cMethod, "inspect", method_inspect, 0); 02350 rb_define_method(rb_cMethod, "to_s", method_inspect, 0); 02351 rb_define_method(rb_cMethod, "to_proc", method_proc, 0); 02352 rb_define_method(rb_cMethod, "receiver", method_receiver, 0); 02353 rb_define_method(rb_cMethod, "name", method_name, 0); 02354 rb_define_method(rb_cMethod, "owner", method_owner, 0); 02355 rb_define_method(rb_cMethod, "unbind", method_unbind, 0); 02356 rb_define_method(rb_cMethod, "source_location", rb_method_location, 0); 02357 rb_define_method(rb_cMethod, "parameters", rb_method_parameters, 0); 02358 rb_define_method(rb_mKernel, "method", rb_obj_method, 1); 02359 rb_define_method(rb_mKernel, "public_method", rb_obj_public_method, 1); 02360 02361 /* UnboundMethod */ 02362 rb_cUnboundMethod = rb_define_class("UnboundMethod", rb_cObject); 02363 rb_undef_alloc_func(rb_cUnboundMethod); 02364 rb_undef_method(CLASS_OF(rb_cUnboundMethod), "new"); 02365 rb_define_method(rb_cUnboundMethod, "==", method_eq, 1); 02366 rb_define_method(rb_cUnboundMethod, "eql?", method_eq, 1); 02367 rb_define_method(rb_cUnboundMethod, "hash", method_hash, 0); 02368 rb_define_method(rb_cUnboundMethod, "clone", method_clone, 0); 02369 rb_define_method(rb_cUnboundMethod, "arity", method_arity_m, 0); 02370 rb_define_method(rb_cUnboundMethod, "inspect", method_inspect, 0); 02371 rb_define_method(rb_cUnboundMethod, "to_s", method_inspect, 0); 02372 rb_define_method(rb_cUnboundMethod, "name", method_name, 0); 02373 rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0); 02374 rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1); 02375 rb_define_method(rb_cUnboundMethod, "source_location", rb_method_location, 0); 02376 rb_define_method(rb_cUnboundMethod, "parameters", rb_method_parameters, 0); 02377 02378 /* Module#*_method */ 02379 rb_define_method(rb_cModule, "instance_method", rb_mod_instance_method, 1); 02380 rb_define_method(rb_cModule, "public_instance_method", rb_mod_public_instance_method, 1); 02381 rb_define_private_method(rb_cModule, "define_method", rb_mod_define_method, -1); 02382 02383 /* Kernel */ 02384 rb_define_method(rb_mKernel, "define_singleton_method", rb_obj_define_method, -1); 02385 02386 rb_define_private_method(rb_singleton_class(rb_vm_top_self()), 02387 "define_method", top_define_method, -1); 02388 } 02389 02390 /* 02391 * Objects of class <code>Binding</code> encapsulate the execution 02392 * context at some particular place in the code and retain this context 02393 * for future use. The variables, methods, value of <code>self</code>, 02394 * and possibly an iterator block that can be accessed in this context 02395 * are all retained. Binding objects can be created using 02396 * <code>Kernel#binding</code>, and are made available to the callback 02397 * of <code>Kernel#set_trace_func</code>. 02398 * 02399 * These binding objects can be passed as the second argument of the 02400 * <code>Kernel#eval</code> method, establishing an environment for the 02401 * evaluation. 02402 * 02403 * class Demo 02404 * def initialize(n) 02405 * @secret = n 02406 * end 02407 * def get_binding 02408 * return binding() 02409 * end 02410 * end 02411 * 02412 * k1 = Demo.new(99) 02413 * b1 = k1.get_binding 02414 * k2 = Demo.new(-3) 02415 * b2 = k2.get_binding 02416 * 02417 * eval("@secret", b1) #=> 99 02418 * eval("@secret", b2) #=> -3 02419 * eval("@secret") #=> nil 02420 * 02421 * Binding objects have no class-specific methods. 02422 * 02423 */ 02424 02425 void 02426 Init_Binding(void) 02427 { 02428 rb_cBinding = rb_define_class("Binding", rb_cObject); 02429 rb_undef_alloc_func(rb_cBinding); 02430 rb_undef_method(CLASS_OF(rb_cBinding), "new"); 02431 rb_define_method(rb_cBinding, "clone", binding_clone, 0); 02432 rb_define_method(rb_cBinding, "dup", binding_dup, 0); 02433 rb_define_method(rb_cBinding, "eval", bind_eval, -1); 02434 rb_define_global_function("binding", rb_f_binding, 0); 02435 attached = rb_intern("__attached__"); 02436 } 02437 02438