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

ruby-changes:71304

From: Peter <ko1@a...>
Date: Wed, 2 Mar 2022 00:56:03 +0900 (JST)
Subject: [ruby-changes:71304] fb724a887a (master): Show embed status of array when len is 0 in objspace dump

https://git.ruby-lang.org/ruby.git/commit/?id=fb724a887a

From fb724a887aa239de2fb1920f769ea097bee37b01 Mon Sep 17 00:00:00 2001
From: Peter Zhu <peter@p...>
Date: Mon, 28 Feb 2022 09:39:16 -0500
Subject: Show embed status of array when len is 0 in objspace dump

---
 ext/objspace/objspace_dump.c   |  2 +-
 test/objspace/test_objspace.rb | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/ext/objspace/objspace_dump.c b/ext/objspace/objspace_dump.c
index e958a831c5..b570acbd95 100644
--- a/ext/objspace/objspace_dump.c
+++ b/ext/objspace/objspace_dump.c
@@ -419,7 +419,7 @@ dump_object(VALUE obj, struct dump_config *dc) https://github.com/ruby/ruby/blob/trunk/ext/objspace/objspace_dump.c#L419
         dump_append_ld(dc, RARRAY_LEN(obj));
         if (RARRAY_LEN(obj) > 0 && FL_TEST(obj, ELTS_SHARED))
             dump_append(dc, ", \"shared\":true");
-        if (RARRAY_LEN(obj) > 0 && FL_TEST(obj, RARRAY_EMBED_FLAG))
+        if (FL_TEST(obj, RARRAY_EMBED_FLAG))
             dump_append(dc, ", \"embedded\":true");
         break;
 
diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb
index 781fb5b3c2..c9ed62f6f6 100644
--- a/test/objspace/test_objspace.rb
+++ b/test/objspace/test_objspace.rb
@@ -307,6 +307,28 @@ class TestObjSpace < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/objspace/test_objspace.rb#L307
     JSON.parse(info) if defined?(JSON)
   end
 
+  def test_dump_array
+    # Empty array
+    info = ObjectSpace.dump([])
+    assert_include(info, '"length":0, "embedded":true')
+    assert_not_include(info, '"shared":true')
+
+    # Non-embed array
+    arr = (1..10).to_a
+    info = ObjectSpace.dump(arr)
+    assert_include(info, '"length":10')
+    assert_not_include(info, '"embedded":true')
+    assert_not_include(info, '"shared":true')
+
+    # Shared array
+    arr1 = (1..10).to_a
+    arr = []
+    arr.replace(arr1)
+    info = ObjectSpace.dump(arr)
+    assert_include(info, '"length":10, "shared":true')
+    assert_not_include(info, '"embedded":true')
+  end
+
   def test_dump_control_char
     assert_include(ObjectSpace.dump("\x0f"), '"value":"\u000f"')
     assert_include(ObjectSpace.dump("\C-?"), '"value":"\u007f"')
-- 
cgit v1.2.1


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

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