summaryrefslogtreecommitdiff
path: root/variable.c
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-28 17:57:27 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-01-28 17:57:27 +0000
commitf0483c494fe092a35ba2022702c554eac68efddf (patch)
treed387def29992cd701cd4890a5a81b28e94ead84b /variable.c
parenta79ff4c7d8989cca323f397fe437e6e4a6415c8a (diff)
* constant.h, variable.c: to ensure compatibility, rb_const_get_* must
not raise an exception even when the constant is private. Instead, rb_public_const_get_* and rb_public_const_defined_* are introduced, which raise an exception when the referring constant is private. see [ruby-core:32912]. * vm_insnhelper.c (vm_get_ev_const): use rb_public_const_get_* instead of rb_const_get_* to follow the constant visibility when user code refers a constant. * test/ruby/test_marshal.rb (test_marshal_private_class): add a test. This test had failed because of incompatibility of rb_const_get. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30713 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'variable.c')
-rw-r--r--variable.c60
1 files changed, 50 insertions, 10 deletions
diff --git a/variable.c b/variable.c
index f737973878..1f4cf76fae 100644
--- a/variable.c
+++ b/variable.c
@@ -1575,7 +1575,7 @@ rb_autoload_p(VALUE mod, ID id)
}
static VALUE
-rb_const_get_0(VALUE klass, ID id, int exclude, int recurse)
+rb_const_get_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
{
VALUE value, tmp;
int mod_retry = 0;
@@ -1587,7 +1587,7 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse)
st_data_t data;
while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &data)) {
rb_const_entry_t *ce = (rb_const_entry_t *)data;
- if (ce->flag == CONST_PRIVATE) {
+ if (visibility && ce->flag == CONST_PRIVATE) {
rb_name_error(id, "private constant %s::%s referenced", rb_class2name(klass), rb_id2name(id));
}
value = ce->value;
@@ -1620,19 +1620,37 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse)
VALUE
rb_const_get_from(VALUE klass, ID id)
{
- return rb_const_get_0(klass, id, TRUE, TRUE);
+ return rb_const_get_0(klass, id, TRUE, TRUE, FALSE);
}
VALUE
rb_const_get(VALUE klass, ID id)
{
- return rb_const_get_0(klass, id, FALSE, TRUE);
+ return rb_const_get_0(klass, id, FALSE, TRUE, FALSE);
}
VALUE
rb_const_get_at(VALUE klass, ID id)
{
- return rb_const_get_0(klass, id, TRUE, FALSE);
+ return rb_const_get_0(klass, id, TRUE, FALSE, FALSE);
+}
+
+VALUE
+rb_public_const_get_from(VALUE klass, ID id)
+{
+ return rb_const_get_0(klass, id, TRUE, TRUE, TRUE);
+}
+
+VALUE
+rb_public_const_get(VALUE klass, ID id)
+{
+ return rb_const_get_0(klass, id, FALSE, TRUE, TRUE);
+}
+
+VALUE
+rb_public_const_get_at(VALUE klass, ID id)
+{
+ return rb_const_get_0(klass, id, TRUE, FALSE, TRUE);
}
/*
@@ -1781,7 +1799,7 @@ rb_mod_constants(int argc, VALUE *argv, VALUE mod)
}
static int
-rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse)
+rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse, int visibility)
{
st_data_t value;
VALUE tmp;
@@ -1791,7 +1809,11 @@ rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse)
retry:
while (tmp) {
if (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &value)) {
- if (((rb_const_entry_t*)value)->value == Qundef && !autoload_node((VALUE)klass, id, 0))
+ rb_const_entry_t *ce = (rb_const_entry_t *)value;
+ if (visibility && ce->flag == CONST_PRIVATE) {
+ return (int)Qfalse;
+ }
+ if (ce->value == Qundef && !autoload_node((VALUE)klass, id, 0))
return (int)Qfalse;
return (int)Qtrue;
}
@@ -1809,19 +1831,37 @@ rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse)
int
rb_const_defined_from(VALUE klass, ID id)
{
- return rb_const_defined_0(klass, id, TRUE, TRUE);
+ return rb_const_defined_0(klass, id, TRUE, TRUE, FALSE);
}
int
rb_const_defined(VALUE klass, ID id)
{
- return rb_const_defined_0(klass, id, FALSE, TRUE);
+ return rb_const_defined_0(klass, id, FALSE, TRUE, FALSE);
}
int
rb_const_defined_at(VALUE klass, ID id)
{
- return rb_const_defined_0(klass, id, TRUE, FALSE);
+ return rb_const_defined_0(klass, id, TRUE, FALSE, FALSE);
+}
+
+int
+rb_public_const_defined_from(VALUE klass, ID id)
+{
+ return rb_const_defined_0(klass, id, TRUE, TRUE, TRUE);
+}
+
+int
+rb_public_const_defined(VALUE klass, ID id)
+{
+ return rb_const_defined_0(klass, id, FALSE, TRUE, TRUE);
+}
+
+int
+rb_public_const_defined_at(VALUE klass, ID id)
+{
+ return rb_const_defined_0(klass, id, TRUE, FALSE, TRUE);
}
void