diff options
author | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-10-26 17:27:44 +0000 |
---|---|---|
committer | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-10-26 17:27:44 +0000 |
commit | b88c9aa1fe0ec07db14544939f9001d66de1bd0a (patch) | |
tree | 1707c28259164f4ce98cd2c00a0e7c1a0da7a94e /variable.c | |
parent | a11576816197d75fa17d959e74cabb612ba8b8be (diff) |
* 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
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 57 |
1 files changed, 56 insertions, 1 deletions
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) { |