* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2010-10-26 17:27:44 +00:00
Родитель a115768161
Коммит b88c9aa1fe
5 изменённых файлов: 89 добавлений и 1 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Wed Oct 27 02:12:10 2010 Yusuke Endoh <mame@tsg.ne.jp>
* 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@tsg.ne.jp>
* class.c, constant.h, gc.c, method.h, object.c, variable.c,

Просмотреть файл

@ -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 */

Просмотреть файл

@ -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);

Просмотреть файл

@ -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

Просмотреть файл

@ -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)
{