From f344841e30618373889d13f5eb43cf8add194f10 Mon Sep 17 00:00:00 2001 From: glass Date: Mon, 15 Jul 2013 04:26:58 +0000 Subject: * proc.c (rb_block_arity): create internal API rb_block_arity(). it return arity of given block. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41983 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ internal.h | 1 + proc.c | 49 ++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index f1130c5612..1bb12f7c20 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Jul 15 13:15:37 2013 Masaki Matsushita + + * proc.c (rb_block_arity): create internal API rb_block_arity(). + it returns arity of given block. + Mon Jul 15 13:07:27 2013 Yuki Yugui Sonoda * lib/prime.rb (Prime::EratosthenesGenerator, diff --git a/internal.h b/internal.h index feb51cf8a8..05a59253a5 100644 --- a/internal.h +++ b/internal.h @@ -356,6 +356,7 @@ void rb_gc_mark_symbols(void); /* proc.c */ VALUE rb_proc_location(VALUE self); st_index_t rb_hash_proc(st_index_t hash, VALUE proc); +int rb_block_arity(void); /* process.c */ #define RB_MAX_GROUPS (65536) diff --git a/proc.c b/proc.c index f83e833877..63bed7257e 100644 --- a/proc.c +++ b/proc.c @@ -621,6 +621,7 @@ rb_proc_call_with_block(VALUE self, int argc, VALUE *argv, VALUE pass_procval) return vret; } + /* * call-seq: * prc.arity -> fixnum @@ -669,19 +670,10 @@ rb_iseq_min_max_arity(const rb_iseq_t *iseq, int *max) return iseq->argc + iseq->arg_post_len; } -/* - * Returns the number of required parameters and stores the maximum - * number of parameters in max, or UNLIMITED_ARGUMENTS if no max. - * For non-lambda procs, the maximum is the number of non-ignored - * parameters even though there is no actual limit to the number of parameters - */ static int -rb_proc_min_max_arity(VALUE self, int *max) +rb_block_min_max_arity(rb_block_t *block, int *max) { - rb_proc_t *proc; - rb_iseq_t *iseq; - GetProcPtr(self, proc); - iseq = proc->block.iseq; + rb_iseq_t *iseq = block->iseq; if (iseq) { if (BUILTIN_TYPE(iseq) != T_NODE) { return rb_iseq_min_max_arity(iseq, max); @@ -698,6 +690,22 @@ rb_proc_min_max_arity(VALUE self, int *max) return 0; } +/* + * Returns the number of required parameters and stores the maximum + * number of parameters in max, or UNLIMITED_ARGUMENTS if no max. + * For non-lambda procs, the maximum is the number of non-ignored + * parameters even though there is no actual limit to the number of parameters + */ +static int +rb_proc_min_max_arity(VALUE self, int *max) +{ + rb_proc_t *proc; + rb_block_t *block; + GetProcPtr(self, proc); + block = &proc->block; + return rb_block_min_max_arity(block, max); +} + int rb_proc_arity(VALUE self) { @@ -707,6 +715,25 @@ rb_proc_arity(VALUE self) return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1; } +int +rb_block_arity(void) +{ + int min, max; + rb_thread_t *th = GET_THREAD(); + rb_control_frame_t *cfp = th->cfp; + rb_block_t *block = rb_vm_control_frame_block_ptr(cfp); + VALUE proc_value = block->proc; + + min = rb_block_min_max_arity(block, &max); + if (proc_value) { + rb_proc_t *proc; + GetProcPtr(proc_value, proc); + if (proc) + return (proc->is_lambda ? min == max : max != UNLIMITED_ARGUMENTS) ? min : -min-1; + } + return max != UNLIMITED_ARGUMENTS ? min : -min-1; +} + #define get_proc_iseq rb_proc_get_iseq rb_iseq_t * -- cgit v1.2.3