From 26719f9deb5112556d6a67ca88558740d584573f Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Fri, 17 Sep 2010 22:54:30 +0900 Subject: [PATCH 3/3] add Module#public_constant and private_constant --- object.c | 2 ++ variable.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletions(-) diff --git a/object.c b/object.c index 2da4b94..adaeddc 100644 --- a/object.c +++ b/object.c @@ -2656,6 +2656,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/variable.c b/variable.c index fb960f7..51c9cd5 100644 --- a/variable.c +++ b/variable.c @@ -1585,7 +1585,8 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse) while (RTEST(tmp)) { VALUE am = 0; while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &v)) { - value = ((rb_const_entry_t*)v)->value; + rb_const_entry_t *ce = (rb_const_entry_t *)v; + value = ce->value; if (value == Qundef) { if (am == tmp) break; am = tmp; @@ -1596,6 +1597,9 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse) rb_warn("toplevel constant %s referenced by %s::%s", rb_id2name(id), rb_class2name(klass), rb_id2name(id)); } + if (ce->flag == CONST_PRIVATE) { + rb_name_error(id, "private constant %s::%s referenced", rb_class2name(klass), rb_id2name(id)); + } return value; } if (!recurse && klass != rb_cObject) break; @@ -1887,6 +1891,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) { -- 1.7.2.1