diff --git a/ChangeLog b/ChangeLog index 585a6c8dda..93abe653ae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Wed Oct 27 02:12:10 2010 Yusuke Endoh + + * 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 * class.c, constant.h, gc.c, method.h, object.c, variable.c, diff --git a/constant.h b/constant.h index 5c04b01f41..d347ea19f5 100644 --- a/constant.h +++ b/constant.h @@ -21,6 +21,8 @@ typedef struct rb_const_entry_struct { 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 */ diff --git a/object.c b/object.c index 30f308bf60..252af17681 100644 --- a/object.c +++ b/object.c @@ -2648,6 +2648,8 @@ Init_Object(void) 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); diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index e5f1e69749..87a4905e3c 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -939,4 +939,24 @@ class TestModule < Test::Unit::TestCase 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 diff --git a/variable.c b/variable.c index bc7a692f12..b372eb2591 100644 --- a/variable.c +++ b/variable.c @@ -1586,7 +1586,11 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse) 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_global_const(const char *name, VALUE val) 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) {