diff options
author | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-08-10 02:58:36 +0000 |
---|---|---|
committer | ko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-08-10 02:58:36 +0000 |
commit | 43384ae9788be243746849ee54d9f5c8f7e0412b (patch) | |
tree | e696a34c00d2d333a01e85e91e964e2f8a40e707 | |
parent | 57199f2125ce8e2043420af6ccc3354e1d411c0f (diff) |
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
-rw-r--r-- | cont.c | 22 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | proc.c | 43 | ||||
-rw-r--r-- | test/ruby/test_fiber.rb | 12 |
4 files changed, 55 insertions, 23 deletions
@@ -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 diff --git a/internal.h b/internal.h index 90758f86a4..895b482d82 100644 --- a/internal.h +++ b/internal.h @@ -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) @@ -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,17 +1266,33 @@ 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 * diff --git a/test/ruby/test_fiber.rb b/test/ruby/test_fiber.rb index ffcb02ce51..8fe2cf5be8 100644 --- a/test/ruby/test_fiber.rb +++ b/test/ruby/test_fiber.rb @@ -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 |