diff options
-rw-r--r-- | spec/ruby/language/constants_spec.rb | 12 | ||||
-rw-r--r-- | test/ruby/test_module.rb | 15 | ||||
-rw-r--r-- | variable.c | 13 | ||||
-rw-r--r-- | vm.c | 1 | ||||
-rw-r--r-- | vm_core.h | 1 |
5 files changed, 39 insertions, 3 deletions
diff --git a/spec/ruby/language/constants_spec.rb b/spec/ruby/language/constants_spec.rb index 4f9429c126..49ff8844af 100644 --- a/spec/ruby/language/constants_spec.rb +++ b/spec/ruby/language/constants_spec.rb @@ -458,6 +458,18 @@ describe "Module#private_constant marked constants" do lambda {mod::Foo}.should raise_error(NameError) end + it "sends #const_missing to the original class or module" do + mod = Module.new + mod.const_set :Foo, true + mod.send :private_constant, :Foo + def mod.const_missing(name) + @const_missing_arg = name + name == :Foo ? name : super + end + + mod::Foo.should == :Foo + end + describe "in a module" do it "cannot be accessed from outside the module" do lambda do diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 78b285157f..08254605cf 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1422,6 +1422,21 @@ class TestModule < Test::Unit::TestCase RUBY end + def test_private_constant_const_missing + c = Class.new + c.const_set(:FOO, "foo") + c.private_constant(:FOO) + class << c + attr_reader :const_missing_arg + def const_missing(name) + @const_missing_arg = name + name == :FOO ? const_get(:FOO) : super + end + end + assert_equal("foo", c::FOO) + assert_equal(:FOO, c.const_missing_arg) + end + class PrivateClass end private_constant :PrivateClass diff --git a/variable.c b/variable.c index ee842e5842..9afed854ae 100644 --- a/variable.c +++ b/variable.c @@ -1795,7 +1795,12 @@ rb_const_missing(VALUE klass, VALUE name) VALUE rb_mod_const_missing(VALUE klass, VALUE name) { + VALUE ref = GET_EC()->private_const_reference; rb_vm_pop_cfunc_frame(); + if (ref) { + rb_name_err_raise("private constant %2$s::%1$s referenced", + ref, name); + } uninitialized_constant(klass, name); UNREACHABLE; @@ -2363,8 +2368,8 @@ rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility) while ((ce = rb_const_lookup(tmp, id))) { if (visibility && RB_CONST_PRIVATE_P(ce)) { if (BUILTIN_TYPE(tmp) == T_ICLASS) tmp = RBASIC(tmp)->klass; - rb_name_err_raise("private constant %2$s::%1$s referenced", - tmp, ID2SYM(id)); + GET_EC()->private_const_reference = tmp; + return Qundef; } rb_const_warn_if_deprecated(ce, tmp, id); value = ce->value; @@ -2376,7 +2381,7 @@ rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility) continue; } if (exclude && tmp == rb_cObject && klass != rb_cObject) { - return Qundef; + goto not_found; } return value; } @@ -2389,6 +2394,8 @@ rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility) goto retry; } + not_found: + GET_EC()->private_const_reference = 0; return Qundef; } @@ -2418,6 +2418,7 @@ rb_execution_context_mark(const rb_execution_context_t *ec) rb_mark_tbl(ec->local_storage); RUBY_MARK_UNLESS_NULL(ec->local_storage_recursive_hash); RUBY_MARK_UNLESS_NULL(ec->local_storage_recursive_hash_for_trace); + RUBY_MARK_UNLESS_NULL(ec->private_const_reference); } void rb_fiber_mark_self(rb_fiber_t *fib); @@ -836,6 +836,7 @@ typedef struct rb_execution_context_struct { VALUE passed_block_handler; /* for rb_iterate */ const rb_callable_method_entry_t *passed_bmethod_me; /* for bmethod */ enum method_missing_reason method_missing_reason; + VALUE private_const_reference; /* for GC */ struct { |