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
@ -458,6 +458,18 @@ describe "Module#private_constant marked constants" do
|
|||||||
lambda {mod::Foo}.should raise_error(NameError)
|
lambda {mod::Foo}.should raise_error(NameError)
|
||||||
end
|
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
|
describe "in a module" do
|
||||||
it "cannot be accessed from outside the module" do
|
it "cannot be accessed from outside the module" do
|
||||||
lambda do
|
lambda do
|
||||||
|
@ -1422,6 +1422,21 @@ class TestModule < Test::Unit::TestCase
|
|||||||
RUBY
|
RUBY
|
||||||
end
|
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
|
class PrivateClass
|
||||||
end
|
end
|
||||||
private_constant :PrivateClass
|
private_constant :PrivateClass
|
||||||
|
13
variable.c
13
variable.c
@ -1795,7 +1795,12 @@ rb_const_missing(VALUE klass, VALUE name)
|
|||||||
VALUE
|
VALUE
|
||||||
rb_mod_const_missing(VALUE klass, VALUE name)
|
rb_mod_const_missing(VALUE klass, VALUE name)
|
||||||
{
|
{
|
||||||
|
VALUE ref = GET_EC()->private_const_reference;
|
||||||
rb_vm_pop_cfunc_frame();
|
rb_vm_pop_cfunc_frame();
|
||||||
|
if (ref) {
|
||||||
|
rb_name_err_raise("private constant %2$s::%1$s referenced",
|
||||||
|
ref, name);
|
||||||
|
}
|
||||||
uninitialized_constant(klass, name);
|
uninitialized_constant(klass, name);
|
||||||
|
|
||||||
UNREACHABLE;
|
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))) {
|
while ((ce = rb_const_lookup(tmp, id))) {
|
||||||
if (visibility && RB_CONST_PRIVATE_P(ce)) {
|
if (visibility && RB_CONST_PRIVATE_P(ce)) {
|
||||||
if (BUILTIN_TYPE(tmp) == T_ICLASS) tmp = RBASIC(tmp)->klass;
|
if (BUILTIN_TYPE(tmp) == T_ICLASS) tmp = RBASIC(tmp)->klass;
|
||||||
rb_name_err_raise("private constant %2$s::%1$s referenced",
|
GET_EC()->private_const_reference = tmp;
|
||||||
tmp, ID2SYM(id));
|
return Qundef;
|
||||||
}
|
}
|
||||||
rb_const_warn_if_deprecated(ce, tmp, id);
|
rb_const_warn_if_deprecated(ce, tmp, id);
|
||||||
value = ce->value;
|
value = ce->value;
|
||||||
@ -2376,7 +2381,7 @@ rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (exclude && tmp == rb_cObject && klass != rb_cObject) {
|
if (exclude && tmp == rb_cObject && klass != rb_cObject) {
|
||||||
return Qundef;
|
goto not_found;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@ -2389,6 +2394,8 @@ rb_const_search(VALUE klass, ID id, int exclude, int recurse, int visibility)
|
|||||||
goto retry;
|
goto retry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
not_found:
|
||||||
|
GET_EC()->private_const_reference = 0;
|
||||||
return Qundef;
|
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);
|
rb_mark_tbl(ec->local_storage);
|
||||||
RUBY_MARK_UNLESS_NULL(ec->local_storage_recursive_hash);
|
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->local_storage_recursive_hash_for_trace);
|
||||||
|
RUBY_MARK_UNLESS_NULL(ec->private_const_reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rb_fiber_mark_self(rb_fiber_t *fib);
|
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 */
|
VALUE passed_block_handler; /* for rb_iterate */
|
||||||
const rb_callable_method_entry_t *passed_bmethod_me; /* for bmethod */
|
const rb_callable_method_entry_t *passed_bmethod_me; /* for bmethod */
|
||||||
enum method_missing_reason method_missing_reason;
|
enum method_missing_reason method_missing_reason;
|
||||||
|
VALUE private_const_reference;
|
||||||
|
|
||||||
/* for GC */
|
/* for GC */
|
||||||
struct {
|
struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user