diff options
author | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-15 04:26:58 +0000 |
---|---|---|
committer | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-07-15 04:26:58 +0000 |
commit | f344841e30618373889d13f5eb43cf8add194f10 (patch) | |
tree | 13a6c1c4ff794a1d549af59ad8b7c4995344594d /proc.c | |
parent | def83bff91d59fa5fdd1898f903f9000ad1dbe13 (diff) | |
download | ruby-f344841e30618373889d13f5eb43cf8add194f10.tar.gz |
* 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
Diffstat (limited to 'proc.c')
-rw-r--r-- | proc.c | 49 |
1 files changed, 38 insertions, 11 deletions
@@ -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 * |