summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-15 04:26:58 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-15 04:26:58 +0000
commitf344841e30618373889d13f5eb43cf8add194f10 (patch)
tree13a6c1c4ff794a1d549af59ad8b7c4995344594d
parentdef83bff91d59fa5fdd1898f903f9000ad1dbe13 (diff)
* 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
-rw-r--r--ChangeLog5
-rw-r--r--internal.h1
-rw-r--r--proc.c49
3 files changed, 44 insertions, 11 deletions
diff --git a/ChangeLog b/ChangeLog
index f1130c56129..1bb12f7c208 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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,
diff --git a/internal.h b/internal.h
index feb51cf8a8f..05a59253a53 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 f83e8338772..63bed7257ef 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 *