From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS43350 77.247.176.0/21 X-Spam-Status: No, score=-1.5 required=3.0 tests=BAYES_00,RCVD_IN_XBL shortcircuit=no autolearn=no version=3.3.2 X-Original-To: spew@80x24.org Received: from 80x24.org (chomsky.torservers.net [77.247.181.162]) by dcvr.yhbt.net (Postfix) with ESMTP id 71A8B20CA8 for ; Tue, 2 Jun 2015 20:14:58 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] variable.c: cleanup removal of special constant support (part2) Date: Tue, 2 Jun 2015 20:14:56 +0000 Message-Id: <1433276096-4270-1-git-send-email-e@80x24.org> List-Id: --- 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