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

ruby-changes:62600

From: Nobuyoshi <ko1@a...>
Date: Mon, 17 Aug 2020 23:07:37 +0900 (JST)
Subject: [ruby-changes:62600] b52a501ca7 (master): Ensure the shortcut cached in the class

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

From b52a501ca786a54fdaadf1a60fef517c55dd6ca3 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 17 Aug 2020 22:57:40 +0900
Subject: Ensure the shortcut cached in the class

As well as the other places using RCLASS_IV_INDEX_TBL.
`IO#reopen` seems the only case that the class of an object can be
changed.

diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 8c7fe69..82f1224 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -2489,6 +2489,17 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L2489
     end
   end
 
+  def test_reopen_ivar
+    assert_ruby_status([], "#{<<~"begin;"}\n#{<<~'end;'}")
+    begin;
+      f = File.open(IO::NULL)
+      f.instance_variable_set(:@foo, 42)
+      f.reopen(STDIN)
+      f.instance_variable_defined?(:@foo)
+      f.instance_variable_get(:@foo)
+    end;
+  end
+
   def test_foreach
     a = []
     IO.foreach("|" + EnvUtil.rubybin + " -e 'puts :foo; puts :bar; puts :baz'") {|x| a << x }
diff --git a/variable.c b/variable.c
index 5634082..4f66ad3 100644
--- a/variable.c
+++ b/variable.c
@@ -885,7 +885,7 @@ generic_ivar_delete(VALUE obj, ID id, VALUE undef) https://github.com/ruby/ruby/blob/trunk/variable.c#L885
 	st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
 	st_data_t index;
 
-	if (st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
+	if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
 	    if (index < ivtbl->numiv) {
 		VALUE ret = ivtbl->ivptr[index];
 
@@ -906,7 +906,7 @@ generic_ivar_get(VALUE obj, ID id, VALUE undef) https://github.com/ruby/ruby/blob/trunk/variable.c#L906
 	st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(rb_obj_class(obj));
 	st_data_t index;
 
-	if (st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
+	if (iv_index_tbl && st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
 	    if (index < ivtbl->numiv) {
 		VALUE ret = ivtbl->ivptr[index];
 
-- 
cgit v0.10.2


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

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