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

ruby-changes:47442

From: ko1 <ko1@a...>
Date: Thu, 10 Aug 2017 11:58:49 +0900 (JST)
Subject: [ruby-changes:47442] ko1:r59558 (trunk): Fiber#to_s (#inspect) return richer information.

ko1	2017-08-10 11:58:36 +0900 (Thu, 10 Aug 2017)

  New Revision: 59558

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59558

  Log:
    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.

  Modified files:
    trunk/cont.c
    trunk/internal.h
    trunk/proc.c
    trunk/test/ruby/test_fiber.rb
Index: proc.c
===================================================================
--- proc.c	(revision 59557)
+++ proc.c	(revision 59558)
@@ -48,8 +48,6 @@ static int method_min_max_arity(VALUE, i https://github.com/ruby/ruby/blob/trunk/proc.c#L48
 
 #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) https://github.com/ruby/ruby/blob/trunk/proc.c#L1243
     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 * https://github.com/ruby/ruby/blob/trunk/proc.c#L1266
 	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
  *
Index: cont.c
===================================================================
--- cont.c	(revision 59557)
+++ cont.c	(revision 59558)
@@ -1717,6 +1717,26 @@ rb_fiber_s_current(VALUE klass) https://github.com/ruby/ruby/blob/trunk/cont.c#L1717
     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) https://github.com/ruby/ruby/blob/trunk/cont.c#L1774
     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
Index: internal.h
===================================================================
--- internal.h	(revision 59557)
+++ internal.h	(revision 59558)
@@ -1495,6 +1495,7 @@ int rb_block_arity(void); https://github.com/ruby/ruby/blob/trunk/internal.h#L1495
 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)
Index: test/ruby/test_fiber.rb
===================================================================
--- test/ruby/test_fiber.rb	(revision 59557)
+++ test/ruby/test_fiber.rb	(revision 59558)
@@ -352,5 +352,17 @@ class TestFiber < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_fiber.rb#L352
       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
 

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

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