mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
const_missing on private constants
* variable.c (rb_const_search): call #const_missing method on private constants, as well as uninitialized constants. [Feature #14328] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63871 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5e7167f8fb
commit
7387c08373
5 changed files with 39 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
13
variable.c
13
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;
|
||||
}
|
||||
|
||||
|
|
1
vm.c
1
vm.c
|
@ -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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue