dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] variable.c: cleanup removal of special constant support (part2)
@ 2015-06-02 20:14 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-06-02 20:14 UTC (permalink / raw)
  To: spew

---
 test/ruby/test_variable.rb | 18 ++++++++++++++++++
 variable.c                 | 45 ++++++++++++++++-----------------------------
 2 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/test/ruby/test_variable.rb b/test/ruby/test_variable.rb
index 8f5329b..f04e358 100644
--- a/test/ruby/test_variable.rb
+++ b/test/ruby/test_variable.rb
@@ -118,4 +118,22 @@ class TestVariable < Test::Unit::TestCase
       }
     }
   end
+
+  def test_special_constant_ivars
+    [ true, false, :symbol, "dsym#{rand(9999)}".to_sym, 1, 1.0 ].each do |v|
+      assert_empty v.instance_variables
+      msg = "can't modify frozen #{v.class}"
+
+      assert_raise_with_message(RuntimeError, msg) do
+        v.instance_variable_set(:@foo, :bar)
+      end
+
+      assert_nil v.instance_variable_get(:@foo)
+      refute v.instance_variable_defined?(:@foo)
+
+      assert_raise_with_message(RuntimeError, msg) do
+        v.remove_instance_variable(:@foo)
+      end
+    end
+  end
 end
diff --git a/variable.c b/variable.c
index 827ceb7..e130ee1 100644
--- a/variable.c
+++ b/variable.c
@@ -1231,7 +1231,7 @@ rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
     long len;
     st_data_t index;
 
-    if (SPECIAL_CONST_P(obj)) goto generic;
+    if (SPECIAL_CONST_P(obj)) return undef;
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         len = ROBJECT_NUMIV(obj);
@@ -1250,8 +1250,7 @@ rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
 	    return (VALUE)index;
 	break;
       default:
-      generic:
-	if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj))
+	if (FL_TEST(obj, FL_EXIVAR))
 	    return generic_ivar_get(obj, id, undef);
 	break;
     }
@@ -1285,7 +1284,7 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
     long len;
     st_data_t index;
 
-    if (SPECIAL_CONST_P(obj)) goto generic;
+    rb_check_frozen(obj);
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         len = ROBJECT_NUMIV(obj);
@@ -1305,8 +1304,7 @@ rb_ivar_delete(VALUE obj, ID id, VALUE undef)
 	    return (VALUE)index;
 	break;
       default:
-      generic:
-	if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj))
+	if (FL_TEST(obj, FL_EXIVAR))
 	    return generic_ivar_delete(obj, id, undef);
 	break;
     }
@@ -1351,11 +1349,6 @@ generic_ivar_set(VALUE obj, ID id, VALUE val)
 {
     struct ivar_update ivup;
 
-    if (rb_special_const_p(obj)) {
-	if (rb_obj_frozen_p(obj)) rb_error_frozen("object");
-	rb_bug("non-frozen special constant");
-    }
-
     ivup.extended = 0;
     ivup.u.iv_index_tbl = iv_index_tbl_make(obj);
     iv_index_tbl_extend(&ivup, id);
@@ -1374,7 +1367,7 @@ rb_ivar_set(VALUE obj, ID id, VALUE val)
     long i, len;
 
     rb_check_frozen(obj);
-    if (SPECIAL_CONST_P(obj)) goto generic;
+
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         ivup.extended = 0;
@@ -1418,7 +1411,6 @@ rb_ivar_set(VALUE obj, ID id, VALUE val)
 	rb_st_insert_id_and_value(obj, RCLASS_IV_TBL(obj), (st_data_t)id, val);
         break;
       default:
-      generic:
 	generic_ivar_set(obj, id, val);
 	break;
     }
@@ -1431,7 +1423,8 @@ rb_ivar_defined(VALUE obj, ID id)
     VALUE val;
     struct st_table *iv_index_tbl;
     st_data_t index;
-    if (SPECIAL_CONST_P(obj)) goto generic;
+
+    if (SPECIAL_CONST_P(obj)) return Qfalse;
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
@@ -1448,8 +1441,7 @@ rb_ivar_defined(VALUE obj, ID id)
 	    return Qtrue;
 	break;
       default:
-      generic:
-	if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj))
+	if (FL_TEST(obj, FL_EXIVAR))
 	    return generic_ivar_defined(obj, id);
 	break;
     }
@@ -1559,10 +1551,7 @@ rb_copy_generic_ivar(VALUE clone, VALUE obj)
 {
     struct gen_ivtbl *ivtbl;
 
-    if (rb_special_const_p(clone)) {
-	if (rb_obj_frozen_p(clone)) rb_error_frozen("object");
-	rb_bug("non-frozen special constant");
-    }
+    rb_check_frozen(clone);
 
     if (!FL_TEST(obj, FL_EXIVAR)) {
       clear:
@@ -1602,7 +1591,7 @@ rb_copy_generic_ivar(VALUE clone, VALUE obj)
 void
 rb_ivar_foreach(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
 {
-    if (SPECIAL_CONST_P(obj)) goto generic;
+    if (SPECIAL_CONST_P(obj)) return;
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         obj_ivar_each(obj, func, arg);
@@ -1614,8 +1603,7 @@ rb_ivar_foreach(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
 	}
 	break;
       default:
-      generic:
-	if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) {
+	if (FL_TEST(obj, FL_EXIVAR)) {
 	    gen_ivar_each(obj, func, arg);
 	}
 	break;
@@ -1626,7 +1614,9 @@ st_index_t
 rb_ivar_count(VALUE obj)
 {
     st_table *tbl;
-    if (SPECIAL_CONST_P(obj)) goto generic;
+
+    if (SPECIAL_CONST_P(obj)) return 0;
+
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
 	if ((tbl = ROBJECT_IV_INDEX_TBL(obj)) != 0) {
@@ -1647,8 +1637,7 @@ rb_ivar_count(VALUE obj)
 	}
 	break;
       default:
-      generic:
-	if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) {
+	if (FL_TEST(obj, FL_EXIVAR)) {
 	    struct gen_ivtbl *ivtbl;
 
 	    if (gen_ivtbl_get(obj, &ivtbl)) {
@@ -1746,7 +1735,6 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
 		      QUOTE_ID(id));
     }
 
-    if (SPECIAL_CONST_P(obj)) goto generic;
     switch (BUILTIN_TYPE(obj)) {
       case T_OBJECT:
         iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
@@ -1767,8 +1755,7 @@ rb_obj_remove_instance_variable(VALUE obj, VALUE name)
 	}
 	break;
       default:
-      generic:
-	if (FL_TEST(obj, FL_EXIVAR) || rb_special_const_p(obj)) {
+	if (FL_TEST(obj, FL_EXIVAR)) {
 	    v = val;
 	    if (generic_ivar_remove(obj, (st_data_t)id, &v)) {
 		return (VALUE)v;
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-06-02 20:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-02 20:14 [PATCH] variable.c: cleanup removal of special constant support (part2) Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).