mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	Fiber#to_s (#inspect) return richer information.
* cont.c (fiber_to_s): return with block and status information. * proc.c (proc_to_s_): removed and introduce rb_block_to_s() function to return block information string. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59558 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									57199f2125
								
							
						
					
					
						commit
						43384ae978
					
				
					 4 changed files with 55 additions and 23 deletions
				
			
		
							
								
								
									
										22
									
								
								cont.c
									
										
									
									
									
								
							
							
						
						
									
										22
									
								
								cont.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1717,6 +1717,26 @@ rb_fiber_s_current(VALUE klass)
 | 
			
		|||
    return rb_fiber_current();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * call-seq:
 | 
			
		||||
 *   fiber.to_s   -> string
 | 
			
		||||
 *
 | 
			
		||||
 * Returns fiber information string.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
static VALUE
 | 
			
		||||
fiber_to_s(VALUE fibval)
 | 
			
		||||
{
 | 
			
		||||
    const rb_fiber_t *fib;
 | 
			
		||||
    const rb_proc_t *proc;
 | 
			
		||||
    char status_info[0x10];
 | 
			
		||||
 | 
			
		||||
    GetFiberPtr(fibval, fib);
 | 
			
		||||
    GetProcPtr(fib->first_proc, proc);
 | 
			
		||||
    snprintf(status_info, 0x10, " (%s)", fiber_status_name(fib->status));
 | 
			
		||||
    return rb_block_to_s(fibval, &proc->block, status_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
| 
						 | 
				
			
			@ -1754,6 +1774,8 @@ Init_Cont(void)
 | 
			
		|||
    rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
 | 
			
		||||
    rb_define_method(rb_cFiber, "initialize", rb_fiber_init, 0);
 | 
			
		||||
    rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
 | 
			
		||||
    rb_define_method(rb_cFiber, "to_s", fiber_to_s, 0);
 | 
			
		||||
    rb_define_alias(rb_cFiber, "inspect", "to_s");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RUBY_SYMBOL_EXPORT_BEGIN
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1495,6 +1495,7 @@ int rb_block_arity(void);
 | 
			
		|||
int rb_block_min_max_arity(int *max);
 | 
			
		||||
VALUE rb_func_proc_new(rb_block_call_func_t func, VALUE val);
 | 
			
		||||
VALUE rb_func_lambda_new(rb_block_call_func_t func, VALUE val, int min_argc, int max_argc);
 | 
			
		||||
VALUE rb_block_to_s(VALUE self, const struct rb_block *block, const char *additional_info);
 | 
			
		||||
 | 
			
		||||
/* process.c */
 | 
			
		||||
#define RB_MAX_GROUPS (65536)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										43
									
								
								proc.c
									
										
									
									
									
								
							
							
						
						
									
										43
									
								
								proc.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -48,8 +48,6 @@ static int method_min_max_arity(VALUE, int *max);
 | 
			
		|||
 | 
			
		||||
#define IS_METHOD_PROC_IFUNC(ifunc) ((ifunc)->func == bmcall)
 | 
			
		||||
 | 
			
		||||
static VALUE proc_to_s_(VALUE self, const rb_proc_t *proc);
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
block_mark(const struct rb_block *block)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -1245,27 +1243,10 @@ proc_hash(VALUE self)
 | 
			
		|||
    return ST2FIX(hash);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * call-seq:
 | 
			
		||||
 *   prc.to_s   -> string
 | 
			
		||||
 *
 | 
			
		||||
 * Returns the unique identifier for this proc, along with
 | 
			
		||||
 * an indication of where the proc was defined.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
static VALUE
 | 
			
		||||
proc_to_s(VALUE self)
 | 
			
		||||
{
 | 
			
		||||
    const rb_proc_t *proc;
 | 
			
		||||
    GetProcPtr(self, proc);
 | 
			
		||||
    return proc_to_s_(self, proc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static VALUE
 | 
			
		||||
proc_to_s_(VALUE self, const rb_proc_t *proc)
 | 
			
		||||
VALUE
 | 
			
		||||
rb_block_to_s(VALUE self, const struct rb_block *block, const char *additional_info)
 | 
			
		||||
{
 | 
			
		||||
    VALUE cname = rb_obj_class(self);
 | 
			
		||||
    const struct rb_block *block = &proc->block;
 | 
			
		||||
    VALUE str = rb_sprintf("#<%"PRIsVALUE":", cname);
 | 
			
		||||
 | 
			
		||||
  again:
 | 
			
		||||
| 
						 | 
				
			
			@ -1285,16 +1266,32 @@ proc_to_s_(VALUE self, const rb_proc_t *proc)
 | 
			
		|||
	rb_str_catf(str, "%p(&%+"PRIsVALUE")", (void *)self, block->as.symbol);
 | 
			
		||||
	break;
 | 
			
		||||
      case block_type_ifunc:
 | 
			
		||||
	rb_str_catf(str, "%p", proc->block.as.captured.code.ifunc);
 | 
			
		||||
	rb_str_catf(str, "%p", block->as.captured.code.ifunc);
 | 
			
		||||
	break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (proc->is_lambda) rb_str_cat_cstr(str, " (lambda)");
 | 
			
		||||
    if (additional_info) rb_str_cat_cstr(str, additional_info);
 | 
			
		||||
    rb_str_cat_cstr(str, ">");
 | 
			
		||||
    OBJ_INFECT_RAW(str, self);
 | 
			
		||||
    return str;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * call-seq:
 | 
			
		||||
 *   prc.to_s   -> string
 | 
			
		||||
 *
 | 
			
		||||
 * Returns the unique identifier for this proc, along with
 | 
			
		||||
 * an indication of where the proc was defined.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
static VALUE
 | 
			
		||||
proc_to_s(VALUE self)
 | 
			
		||||
{
 | 
			
		||||
    const rb_proc_t *proc;
 | 
			
		||||
    GetProcPtr(self, proc);
 | 
			
		||||
    return rb_block_to_s(self, &proc->block, proc->is_lambda ? " (lambda)" : NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  call-seq:
 | 
			
		||||
 *     prc.to_proc -> proc
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -352,5 +352,17 @@ class TestFiber < Test::Unit::TestCase
 | 
			
		|||
      exit("1" == Fiber.new(&:to_s).resume(1))
 | 
			
		||||
    end;
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_to_s
 | 
			
		||||
    f = Fiber.new do
 | 
			
		||||
      assert_match(/resumed/, f.to_s)
 | 
			
		||||
      Fiber.yield
 | 
			
		||||
    end
 | 
			
		||||
    assert_match(/created/, f.to_s)
 | 
			
		||||
    f.resume
 | 
			
		||||
    assert_match(/suspended/, f.to_s)
 | 
			
		||||
    f.resume
 | 
			
		||||
    assert_match(/terminated/, f.to_s)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue