From a7927cfd50dbbf708536e908325e0638f6102e86 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Fri, 17 Sep 2010 22:53:58 +0900 Subject: [PATCH 1/3] separate RCLASS_CONST_TBL from RCLASS_IV_TBL RCLASS_IV_TBL contained not only instance variable table but also constant table. Now the two table are separated to RCLASS_CONST_TBL and RCLASS_IV_TBL. --- class.c | 14 +++++++++ gc.c | 4 ++ include/ruby/ruby.h | 3 ++ object.c | 4 ++ variable.c | 77 ++++++++++++++++++++++++++------------------------ vm_insnhelper.c | 4 +- 6 files changed, 67 insertions(+), 39 deletions(-) diff --git a/class.c b/class.c index 1d4695b..5225ffa 100644 --- a/class.c +++ b/class.c @@ -52,6 +52,7 @@ class_alloc(VALUE flags, VALUE klass) OBJSETUP(obj, klass, flags); obj->ptr = ext; RCLASS_IV_TBL(obj) = 0; + RCLASS_CONST_TBL(obj) = 0; RCLASS_M_TBL(obj) = 0; RCLASS_SUPER(obj) = 0; RCLASS_IV_INDEX_TBL(obj) = 0; @@ -161,6 +162,12 @@ rb_mod_init_copy(VALUE clone, VALUE orig) CONST_ID(id, "__classid__"); st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0); } + if (RCLASS_CONST_TBL(orig)) { + if (RCLASS_CONST_TBL(clone)) { + st_free_table(RCLASS_CONST_TBL(clone)); + } + RCLASS_CONST_TBL(clone) = st_copy(RCLASS_CONST_TBL(orig)); + } if (RCLASS_M_TBL(orig)) { struct clone_method_data data; @@ -216,6 +223,9 @@ rb_singleton_class_clone(VALUE obj) if (RCLASS_IV_TBL(klass)) { RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(klass)); } + if (RCLASS_CONST_TBL(klass)) { + RCLASS_CONST_TBL(clone) = st_copy(RCLASS_CONST_TBL(klass)); + } RCLASS_M_TBL(clone) = st_init_numtable(); data.tbl = RCLASS_M_TBL(clone); data.klass = (VALUE)clone; @@ -605,7 +615,11 @@ include_class_new(VALUE module, VALUE super) if (!RCLASS_IV_TBL(module)) { RCLASS_IV_TBL(module) = st_init_numtable(); } + if (!RCLASS_CONST_TBL(module)) { + RCLASS_CONST_TBL(module) = st_init_numtable(); + } RCLASS_IV_TBL(klass) = RCLASS_IV_TBL(module); + RCLASS_CONST_TBL(klass) = RCLASS_CONST_TBL(module); RCLASS_M_TBL(klass) = RCLASS_M_TBL(module); RCLASS_SUPER(klass) = super; if (TYPE(module) == T_ICLASS) { diff --git a/gc.c b/gc.c index 9bde487..ee5b256 100644 --- a/gc.c +++ b/gc.c @@ -1717,6 +1717,7 @@ gc_mark_children(rb_objspace_t *objspace, VALUE ptr, int lev) case T_MODULE: mark_m_tbl(objspace, RCLASS_M_TBL(obj), lev); mark_tbl(objspace, RCLASS_IV_TBL(obj), lev); + mark_tbl(objspace, RCLASS_CONST_TBL(obj), lev); ptr = RCLASS_SUPER(obj); goto again; @@ -2164,6 +2165,9 @@ obj_free(rb_objspace_t *objspace, VALUE obj) if (RCLASS_IV_TBL(obj)) { st_free_table(RCLASS_IV_TBL(obj)); } + if (RCLASS_CONST_TBL(obj)) { + st_free_table(RCLASS_CONST_TBL(obj)); + } if (RCLASS_IV_INDEX_TBL(obj)) { st_free_table(RCLASS_IV_INDEX_TBL(obj)); } diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 028f4e9..1bf0379 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -606,6 +606,7 @@ struct RObject { typedef struct { VALUE super; struct st_table *iv_tbl; + struct st_table *const_tbl; } rb_classext_t; struct RClass { @@ -615,10 +616,12 @@ struct RClass { struct st_table *iv_index_tbl; }; #define RCLASS_IV_TBL(c) (RCLASS(c)->ptr->iv_tbl) +#define RCLASS_CONST_TBL(c) (RCLASS(c)->ptr->const_tbl) #define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl) #define RCLASS_SUPER(c) (RCLASS(c)->ptr->super) #define RCLASS_IV_INDEX_TBL(c) (RCLASS(c)->iv_index_tbl) #define RMODULE_IV_TBL(m) RCLASS_IV_TBL(m) +#define RMODULE_CONST_TBL(m) RCLASS_CONST_TBL(m) #define RMODULE_M_TBL(m) RCLASS_M_TBL(m) #define RMODULE_SUPER(m) RCLASS_SUPER(m) diff --git a/object.c b/object.c index 5ad5680..b1bbeb2 100644 --- a/object.c +++ b/object.c @@ -221,6 +221,10 @@ init_copy(VALUE dest, VALUE obj) st_free_table(RCLASS_IV_TBL(dest)); RCLASS_IV_TBL(dest) = 0; } + if (RCLASS_CONST_TBL(dest)) { + st_free_table(RCLASS_CONST_TBL(dest)); + RCLASS_CONST_TBL(dest) = 0; + } if (RCLASS_IV_TBL(obj)) { RCLASS_IV_TBL(dest) = st_copy(RCLASS_IV_TBL(obj)); } diff --git a/variable.c b/variable.c index 6c653ca..ab62deb 100644 --- a/variable.c +++ b/variable.c @@ -82,7 +82,7 @@ fc_i(ID key, VALUE value, struct fc_result *res) switch (TYPE(value)) { case T_MODULE: case T_CLASS: - if (!RCLASS_IV_TBL(value)) return ST_CONTINUE; + if (!RCLASS_CONST_TBL(value)) return ST_CONTINUE; else { struct fc_result arg; struct fc_result *list; @@ -98,7 +98,7 @@ fc_i(ID key, VALUE value, struct fc_result *res) arg.klass = res->klass; arg.track = value; arg.prev = res; - st_foreach(RCLASS_IV_TBL(value), fc_i, (st_data_t)&arg); + st_foreach(RCLASS_CONST_TBL(value), fc_i, (st_data_t)&arg); if (arg.path) { res->path = arg.path; return ST_STOP; @@ -122,8 +122,8 @@ find_class_path(VALUE klass) arg.klass = klass; arg.track = rb_cObject; arg.prev = 0; - if (RCLASS_IV_TBL(rb_cObject)) { - st_foreach_safe(RCLASS_IV_TBL(rb_cObject), fc_i, (st_data_t)&arg); + if (RCLASS_CONST_TBL(rb_cObject)) { + st_foreach_safe(RCLASS_CONST_TBL(rb_cObject), fc_i, (st_data_t)&arg); } if (arg.path == 0) { st_foreach_safe(rb_class_tbl, fc_i, (st_data_t)&arg); @@ -1438,7 +1438,7 @@ rb_autoload(VALUE mod, ID id, const char *file) rb_raise(rb_eArgError, "empty file name"); } - if ((tbl = RCLASS_IV_TBL(mod)) && st_lookup(tbl, (st_data_t)id, &av) && (VALUE)av != Qundef) + if ((tbl = RCLASS_CONST_TBL(mod)) && st_lookup(tbl, (st_data_t)id, &av) && (VALUE)av != Qundef) return; rb_const_set(mod, id, Qundef); @@ -1462,7 +1462,7 @@ autoload_delete(VALUE mod, ID id) { st_data_t val, load = 0, n = id; - st_delete(RCLASS_IV_TBL(mod), &n, 0); + st_delete(RCLASS_CONST_TBL(mod), &n, 0); if (st_lookup(RCLASS_IV_TBL(mod), (st_data_t)autoload, &val)) { struct st_table *tbl = check_autoload_table((VALUE)val); @@ -1470,7 +1470,7 @@ autoload_delete(VALUE mod, ID id) if (tbl->num_entries == 0) { n = autoload; - st_delete(RCLASS_IV_TBL(mod), &n, &val); + st_delete(RCLASS_CONST_TBL(mod), &n, &val); } } @@ -1527,7 +1527,7 @@ autoload_node(VALUE mod, ID id, const char **loadingpath) static int autoload_node_id(VALUE mod, ID id) { - struct st_table *tbl = RCLASS_IV_TBL(mod); + struct st_table *tbl = RCLASS_CONST_TBL(mod); st_data_t val; if (!tbl || !st_lookup(tbl, (st_data_t)id, &val) || (VALUE)val != Qundef) { @@ -1575,7 +1575,7 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse) retry: while (RTEST(tmp)) { VALUE am = 0; - while (RCLASS_IV_TBL(tmp) && st_lookup(RCLASS_IV_TBL(tmp), (st_data_t)id, &value)) { + while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &value)) { if (value == Qundef) { if (am == tmp) break; am = tmp; @@ -1650,7 +1650,7 @@ rb_const_remove(VALUE mod, ID id) rb_raise(rb_eSecurityError, "Insecure: can't remove constant"); if (OBJ_FROZEN(mod)) rb_error_frozen("class/module"); - if (!RCLASS_IV_TBL(mod) || !st_delete(RCLASS_IV_TBL(mod), &n, &v)) { + if (!RCLASS_CONST_TBL(mod) || !st_delete(RCLASS_CONST_TBL(mod), &n, &v)) { if (rb_const_defined_at(mod, id)) { rb_name_error(id, "cannot remove %s::%s", rb_class2name(mod), rb_id2name(id)); @@ -1687,8 +1687,8 @@ rb_mod_const_at(VALUE mod, void *data) if (!tbl) { tbl = st_init_numtable(); } - if (RCLASS_IV_TBL(mod)) { - st_foreach_safe(RCLASS_IV_TBL(mod), sv_i, (st_data_t)tbl); + if (RCLASS_CONST_TBL(mod)) { + st_foreach_safe(RCLASS_CONST_TBL(mod), sv_i, (st_data_t)tbl); } return tbl; } @@ -1773,7 +1773,7 @@ rb_const_defined_0(VALUE klass, ID id, int exclude, int recurse) tmp = klass; retry: while (tmp) { - if (RCLASS_IV_TBL(tmp) && st_lookup(RCLASS_IV_TBL(tmp), (st_data_t)id, &value)) { + if (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &value)) { if ((VALUE)value == Qundef && !autoload_node((VALUE)klass, id, 0)) return (int)Qfalse; return (int)Qtrue; @@ -1807,11 +1807,9 @@ rb_const_defined_at(VALUE klass, ID id) return rb_const_defined_0(klass, id, TRUE, FALSE); } -static void -mod_av_set(VALUE klass, ID id, VALUE val, int isconst) +void +check_before_mod_set(VALUE klass, ID id, VALUE val, const char *dest) { - const char *dest = isconst ? "constant" : "class variable"; - if (!OBJ_UNTRUSTED(klass) && rb_safe_level() >= 4) rb_raise(rb_eSecurityError, "Insecure: can't set %s", dest); if (OBJ_FROZEN(klass)) { @@ -1822,24 +1820,6 @@ mod_av_set(VALUE klass, ID id, VALUE val, int isconst) rb_error_frozen("class"); } } - if (!RCLASS_IV_TBL(klass)) { - RCLASS_IV_TBL(klass) = st_init_numtable(); - } - else if (isconst) { - st_data_t value; - - if (st_lookup(RCLASS_IV_TBL(klass), (st_data_t)id, &value)) { - if ((VALUE)value == Qundef) - autoload_delete(klass, id); - else - rb_warn("already initialized %s %s", dest, rb_id2name(id)); - } - } - - if (isconst){ - rb_vm_change_state(); - } - st_insert(RCLASS_IV_TBL(klass), (st_data_t)id, (st_data_t)val); } void @@ -1849,7 +1829,24 @@ rb_const_set(VALUE klass, ID id, VALUE val) rb_raise(rb_eTypeError, "no class/module to define constant %s", rb_id2name(id)); } - mod_av_set(klass, id, val, TRUE); + + check_before_mod_set(klass, id, val, "constant"); + if (!RCLASS_CONST_TBL(klass)) { + RCLASS_CONST_TBL(klass) = st_init_numtable(); + } + else { + st_data_t value; + + if (st_lookup(RCLASS_CONST_TBL(klass), (st_data_t)id, &value)) { + if ((VALUE)value == Qundef) + autoload_delete(klass, id); + else + rb_warn("already initialized constant %s", rb_id2name(id)); + } + } + + rb_vm_change_state(); + st_insert(RCLASS_CONST_TBL(klass), (st_data_t)id, (st_data_t)val); } void @@ -1931,7 +1928,13 @@ rb_cvar_set(VALUE klass, ID id, VALUE val) else { target = tmp; } - mod_av_set(target, id, val, FALSE); + + check_before_mod_set(target, id, val, "class variable"); + if (!RCLASS_IV_TBL(target)) { + RCLASS_IV_TBL(target) = st_init_numtable(); + } + + st_insert(RCLASS_IV_TBL(target), (st_data_t)id, (st_data_t)val); } VALUE diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 46326d1..e6e8db2 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1168,8 +1168,8 @@ vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq, if (!NIL_P(klass)) { VALUE am = 0; search_continue: - if (RCLASS_IV_TBL(klass) && - st_lookup(RCLASS_IV_TBL(klass), id, &val)) { + if (RCLASS_CONST_TBL(klass) && + st_lookup(RCLASS_CONST_TBL(klass), id, &val)) { if (val == Qundef) { if (am == klass) break; am = klass; -- 1.7.2.1