mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Hide internal IDs
* parse.y (internal_id): number the ID serial for internal use by counting down from the neary maximum value, not to accidentally match permanent IDs. [Bug #15768]
This commit is contained in:
parent
5689c46457
commit
54eac83b2a
Notes:
git
2019-08-31 04:40:14 +09:00
Fixed: [Bug #15786]
4 changed files with 40 additions and 7 deletions
3
parse.y
3
parse.y
|
@ -11812,8 +11812,9 @@ rb_init_parse(void)
|
||||||
static ID
|
static ID
|
||||||
internal_id(struct parser_params *p)
|
internal_id(struct parser_params *p)
|
||||||
{
|
{
|
||||||
|
const ID max_id = RB_ID_SERIAL_MAX & ~0xffff;
|
||||||
ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
|
ID id = (ID)vtable_size(p->lvtbl->args) + (ID)vtable_size(p->lvtbl->vars);
|
||||||
id += ((tLAST_TOKEN - ID_INTERNAL) >> ID_SCOPE_SHIFT) + 1;
|
id = max_id - id;
|
||||||
return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
|
return ID_STATIC_SYM | ID_INTERNAL | (id << ID_SCOPE_SHIFT);
|
||||||
}
|
}
|
||||||
#endif /* !RIPPER */
|
#endif /* !RIPPER */
|
||||||
|
|
36
symbol.c
36
symbol.c
|
@ -19,6 +19,9 @@
|
||||||
#ifndef SYMBOL_DEBUG
|
#ifndef SYMBOL_DEBUG
|
||||||
# define SYMBOL_DEBUG 0
|
# define SYMBOL_DEBUG 0
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef CHECK_ID_SERIAL
|
||||||
|
# define CHECK_ID_SERIAL SYMBOL_DEBUG
|
||||||
|
#endif
|
||||||
|
|
||||||
#define SYMBOL_PINNED_P(sym) (RSYMBOL(sym)->id&~ID_SCOPE_MASK)
|
#define SYMBOL_PINNED_P(sym) (RSYMBOL(sym)->id&~ID_SCOPE_MASK)
|
||||||
|
|
||||||
|
@ -370,20 +373,41 @@ set_id_entry(rb_id_serial_t num, VALUE str, VALUE sym)
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
get_id_entry(rb_id_serial_t num, const enum id_entry_type t)
|
get_id_serial_entry(rb_id_serial_t num, ID id, const enum id_entry_type t)
|
||||||
{
|
{
|
||||||
if (num && num <= global_symbols.last_id) {
|
if (num && num <= global_symbols.last_id) {
|
||||||
size_t idx = num / ID_ENTRY_UNIT;
|
size_t idx = num / ID_ENTRY_UNIT;
|
||||||
VALUE ids = global_symbols.ids;
|
VALUE ids = global_symbols.ids;
|
||||||
VALUE ary;
|
VALUE ary;
|
||||||
if (idx < (size_t)RARRAY_LEN(ids) && !NIL_P(ary = rb_ary_entry(ids, (long)idx))) {
|
if (idx < (size_t)RARRAY_LEN(ids) && !NIL_P(ary = rb_ary_entry(ids, (long)idx))) {
|
||||||
VALUE result = rb_ary_entry(ary, (long)(num % ID_ENTRY_UNIT) * ID_ENTRY_SIZE + t);
|
long pos = (long)(num % ID_ENTRY_UNIT) * ID_ENTRY_SIZE;
|
||||||
if (!NIL_P(result)) return result;
|
VALUE result = rb_ary_entry(ary, pos + t);
|
||||||
|
if (NIL_P(result)) return 0;
|
||||||
|
#if CHECK_ID_SERIAL
|
||||||
|
if (id) {
|
||||||
|
VALUE sym = result;
|
||||||
|
if (t != ID_ENTRY_SYM)
|
||||||
|
sym = rb_ary_entry(ary, pos + ID_ENTRY_SYM);
|
||||||
|
if (STATIC_SYM_P(sym)) {
|
||||||
|
if (STATIC_SYM2ID(sym) != id) return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (RSYMBOL(sym)->id != id) return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
get_id_entry(ID id, const enum id_entry_type t)
|
||||||
|
{
|
||||||
|
return get_id_serial_entry(rb_id_to_serial(id), id, t);
|
||||||
|
}
|
||||||
|
|
||||||
static inline ID
|
static inline ID
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
__attribute__((unused))
|
__attribute__((unused))
|
||||||
|
@ -391,7 +415,7 @@ __attribute__((unused))
|
||||||
rb_id_serial_to_id(rb_id_serial_t num)
|
rb_id_serial_to_id(rb_id_serial_t num)
|
||||||
{
|
{
|
||||||
if (is_notop_id((ID)num)) {
|
if (is_notop_id((ID)num)) {
|
||||||
VALUE sym = get_id_entry(num, ID_ENTRY_SYM);
|
VALUE sym = get_id_serial_entry(num, 0, ID_ENTRY_SYM);
|
||||||
return SYM2ID(sym);
|
return SYM2ID(sym);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -579,7 +603,7 @@ lookup_str_sym(const VALUE str)
|
||||||
static VALUE
|
static VALUE
|
||||||
lookup_id_str(ID id)
|
lookup_id_str(ID id)
|
||||||
{
|
{
|
||||||
return get_id_entry(rb_id_to_serial(id), ID_ENTRY_STR);
|
return get_id_entry(id, ID_ENTRY_STR);
|
||||||
}
|
}
|
||||||
|
|
||||||
ID
|
ID
|
||||||
|
@ -758,7 +782,7 @@ VALUE
|
||||||
rb_id2sym(ID x)
|
rb_id2sym(ID x)
|
||||||
{
|
{
|
||||||
if (!DYNAMIC_ID_P(x)) return STATIC_ID2SYM(x);
|
if (!DYNAMIC_ID_P(x)) return STATIC_ID2SYM(x);
|
||||||
return get_id_entry(rb_id_to_serial(x), ID_ENTRY_SYM);
|
return get_id_entry(x, ID_ENTRY_SYM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -615,6 +615,11 @@ class TestMethod < Test::Unit::TestCase
|
||||||
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], self.class.instance_method(:pmk7).parameters)
|
assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:keyrest, :o]], self.class.instance_method(:pmk7).parameters)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_hidden_parameters
|
||||||
|
instance_eval("def m((_)"+",(_)"*256+");end")
|
||||||
|
assert_empty(method(:m).parameters.map{|_,n|n}.compact)
|
||||||
|
end
|
||||||
|
|
||||||
def test_public_method_with_zsuper_method
|
def test_public_method_with_zsuper_method
|
||||||
c = Class.new
|
c = Class.new
|
||||||
c.class_eval do
|
c.class_eval do
|
||||||
|
|
|
@ -1137,6 +1137,9 @@ class TestProc < Test::Unit::TestCase
|
||||||
|
|
||||||
assert_equal([[:req]], method(:putc).parameters)
|
assert_equal([[:req]], method(:putc).parameters)
|
||||||
assert_equal([[:rest]], method(:p).parameters)
|
assert_equal([[:rest]], method(:p).parameters)
|
||||||
|
|
||||||
|
pr = eval("proc{|"+"(_),"*30+"|}")
|
||||||
|
assert_empty(pr.parameters.map{|_,n|n}.compact)
|
||||||
end
|
end
|
||||||
|
|
||||||
def pm0() end
|
def pm0() end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue