ruby-changes:17598
From: mame <ko1@a...>
Date: Wed, 27 Oct 2010 02:27:57 +0900 (JST)
Subject: [ruby-changes:17598] Ruby:r29603 (trunk): * object.c (Init_Object), constant.h, variable.c
mame 2010-10-27 02:27:44 +0900 (Wed, 27 Oct 2010) New Revision: 29603 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29603 Log: * object.c (Init_Object), constant.h, variable.c (rb_mod_private_constant, rb_mod_public_constant, set_const_visibility, rb_const_get_0): add Module#public_constant and private_constant. [ruby-dev:39685][ruby-core:32698] * test/ruby/test_module.rb: add tests for above. Modified files: trunk/ChangeLog trunk/constant.h trunk/object.c trunk/test/ruby/test_module.rb trunk/variable.c Index: ChangeLog =================================================================== --- ChangeLog (revision 29602) +++ ChangeLog (revision 29603) @@ -1,3 +1,12 @@ +Wed Oct 27 02:12:10 2010 Yusuke Endoh <mame@t...> + + * object.c (Init_Object), constant.h, variable.c + (rb_mod_private_constant, rb_mod_public_constant, + set_const_visibility, rb_const_get_0): add Module#public_constant + and private_constant. [ruby-dev:39685][ruby-core:32698] + + * test/ruby/test_module.rb: add tests for above. + Wed Oct 27 02:02:54 2010 Yusuke Endoh <mame@t...> * class.c, constant.h, gc.c, method.h, object.c, variable.c, Index: variable.c =================================================================== --- variable.c (revision 29602) +++ variable.c (revision 29603) @@ -1586,7 +1586,11 @@ VALUE am = 0; st_data_t data; while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &data)) { - value = ((rb_const_entry_t*)data)->value; + rb_const_entry_t *ce = (rb_const_entry_t *)data; + if (ce->flag == CONST_PRIVATE) { + rb_name_error(id, "private constant %s::%s referenced", rb_class2name(klass), rb_id2name(id)); + } + value = ce->value; if (value == Qundef) { if (am == tmp) break; am = tmp; @@ -1882,6 +1886,57 @@ rb_define_const(rb_cObject, name, val); } +static void +set_const_visibility(VALUE mod, int argc, VALUE *argv, rb_const_flag_t flag) +{ + int i; + st_data_t v; + ID id; + + if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(mod)) { + rb_raise(rb_eSecurityError, + "Insecure: can't change method visibility"); + } + + for (i = 0; i < argc; i++) { + id = rb_to_id(argv[i]); + if (RCLASS_CONST_TBL(mod) && st_lookup(RCLASS_CONST_TBL(mod), (st_data_t)id, &v)) { + ((rb_const_entry_t*)v)->flag = flag; + return; + } + rb_name_error(id, "constant %s::%s not defined", rb_class2name(mod), rb_id2name(id)); + } + rb_clear_cache_by_class(mod); +} + +/* + * call-seq: + * mod.private_constant(symbol, ...) => mod + * + * Makes a list of existing constants private. + */ + +VALUE +rb_mod_private_constant(int argc, VALUE *argv, VALUE obj) +{ + set_const_visibility(obj, argc, argv, CONST_PRIVATE); + return obj; +} + +/* + * call-seq: + * mod.public_constant(symbol, ...) => mod + * + * Makes a list of existing constants public. + */ + +VALUE +rb_mod_public_constant(int argc, VALUE *argv, VALUE obj) +{ + set_const_visibility(obj, argc, argv, CONST_PUBLIC); + return obj; +} + static VALUE original_module(VALUE c) { Index: object.c =================================================================== --- object.c (revision 29602) +++ object.c (revision 29603) @@ -2648,6 +2648,8 @@ rb_define_method(rb_cModule, "class_variable_get", rb_mod_cvar_get, 1); rb_define_method(rb_cModule, "class_variable_set", rb_mod_cvar_set, 2); rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1); + rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); + rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0); rb_define_method(rb_cClass, "new", rb_class_new_instance, -1); Index: constant.h =================================================================== --- constant.h (revision 29602) +++ constant.h (revision 29603) @@ -21,6 +21,8 @@ VALUE value; /* should be mark */ } rb_const_entry_t; +VALUE rb_mod_private_constant(int argc, VALUE *argv, VALUE obj); +VALUE rb_mod_public_constant(int argc, VALUE *argv, VALUE obj); void rb_free_const_table(st_table *tbl); #endif /* CONSTANT_H */ Index: test/ruby/test_module.rb =================================================================== --- test/ruby/test_module.rb (revision 29602) +++ test/ruby/test_module.rb (revision 29603) @@ -939,4 +939,24 @@ assert_nothing_raised(bug3406) {c.x = 1} assert_equal(1, c.x, bug3406) end + + def test_private_constant + c = Class.new + c.const_set(:FOO, "foo") + assert_equal("foo", c::FOO) + c.private_constant(:FOO) + assert_raise(NameError) { c::FOO } + assert_equal("foo", c.class_eval("FOO")) + end + + def test_public_constant + c = Class.new + c.const_set(:FOO, "foo") + assert_equal("foo", c::FOO) + c.private_constant(:FOO) + assert_raise(NameError) { c::FOO } + assert_equal("foo", c.class_eval("FOO")) + c.public_constant(:FOO) + assert_equal("foo", c::FOO) + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/