mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
def83bff91
commit
f344841e30
3 changed files with 44 additions and 11 deletions
|
@ -1,3 +1,8 @@
|
|||
Mon Jul 15 13:15:37 2013 Masaki Matsushita <glass.saga@gmail.com>
|
||||
|
||||
* 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 <yugui@yugui.jp>
|
||||
|
||||
* lib/prime.rb (Prime::EratosthenesGenerator,
|
||||
|
|
|
@ -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)
|
||||
|
|
49
proc.c
49
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 *
|
||||
|
|
Loading…
Reference in a new issue