[前][次][番号順一覧][スレッド一覧]

ruby-changes:29931

From: glass <ko1@a...>
Date: Mon, 15 Jul 2013 13:27:09 +0900 (JST)
Subject: [ruby-changes:29931] glass:r41983 (trunk): * proc.c (rb_block_arity): create internal API rb_block_arity().

glass	2013-07-15 13:26:58 +0900 (Mon, 15 Jul 2013)

  New Revision: 41983

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41983

  Log:
    * proc.c (rb_block_arity): create internal API rb_block_arity().
      it return arity of given block.

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/proc.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41982)
+++ ChangeLog	(revision 41983)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jul 15 13:15:37 2013  Masaki Matsushita  <glass.saga@g...>
+
+	* 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@y...>
 
 	* lib/prime.rb (Prime::EratosthenesGenerator,
Index: proc.c
===================================================================
--- proc.c	(revision 41982)
+++ proc.c	(revision 41983)
@@ -621,6 +621,7 @@ rb_proc_call_with_block(VALUE self, int https://github.com/ruby/ruby/blob/trunk/proc.c#L621
     return vret;
 }
 
+
 /*
  *  call-seq:
  *     prc.arity -> fixnum
@@ -669,19 +670,10 @@ rb_iseq_min_max_arity(const rb_iseq_t *i https://github.com/ruby/ruby/blob/trunk/proc.c#L670
     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 *m https://github.com/ruby/ruby/blob/trunk/proc.c#L690
     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) https://github.com/ruby/ruby/blob/trunk/proc.c#L715
     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 *
Index: internal.h
===================================================================
--- internal.h	(revision 41982)
+++ internal.h	(revision 41983)
@@ -356,6 +356,7 @@ void rb_gc_mark_symbols(void); https://github.com/ruby/ruby/blob/trunk/internal.h#L356
 /* 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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]