Project

General

Profile

Feature #2366 » const_tbl.patch

mame (Yusuke Endoh), 11/15/2009 03:50 AM

View differences:

class.c
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;
......
CONST_ID(id, "__classid__");
st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0);
}
if (RCLASS_CONST_TBL(clone)) {
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;
......
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;
......
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) {
gc.c
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;
......
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));
}
include/ruby/ruby.h
typedef struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
} rb_classext_t;
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)
object.c
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));
}
variable.c
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;
......
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;
......
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);
......
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);
......
{
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);
......
if (tbl->num_entries == 0) {
n = autoload;
st_delete(RCLASS_IV_TBL(mod), &n, &val);
st_delete(RCLASS_CONST_TBL(mod), &n, &val);
}
}
......
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) {
......
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;
......
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));
......
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;
}
......
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;
......
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)) {
......
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
......
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
......
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
vm_insnhelper.c
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-1/3)