1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* class.c, gc.c, object.c, variable.c, vm_insnhelper.c,

include/ruby/ruby.h: separate RCLASS_CONST_TBL from RCLASS_IV_TBL.
  RCLASS_IV_TBL has contained not only instance variable table but
  also constant table.  Now the two table are separated to
  RCLASS_CONST_TBL and RCLASS_IV_TBL.  This is a preparation for
  private constant (see [ruby-dev:39685][ruby-core:32698]).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29600 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2010-10-26 17:27:21 +00:00
parent 6ca3ad34a0
commit 2285a8d813
7 changed files with 78 additions and 40 deletions

View file

@ -1,3 +1,12 @@
Wed Oct 27 01:56:34 2010 Yusuke Endoh <mame@tsg.ne.jp>
* class.c, gc.c, object.c, variable.c, vm_insnhelper.c,
include/ruby/ruby.h: separate RCLASS_CONST_TBL from RCLASS_IV_TBL.
RCLASS_IV_TBL has contained not only instance variable table but
also constant table. Now the two table are separated to
RCLASS_CONST_TBL and RCLASS_IV_TBL. This is a preparation for
private constant (see [ruby-dev:39685][ruby-core:32698]).
Tue Oct 26 18:51:00 2010 NARUSE, Yui <naruse@ruby-lang.org>
* lib/scanf.rb (extract_float): allow 2.e+2 style.

14
class.c
View file

@ -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), &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;
@ -607,7 +617,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) {

4
gc.c
View file

@ -1718,6 +1718,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;
@ -2179,6 +2180,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));
}

View file

@ -608,6 +608,7 @@ struct RObject {
typedef struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
} rb_classext_t;
struct RClass {
@ -617,10 +618,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)

View file

@ -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));
}

View file

@ -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);
@ -1439,15 +1439,16 @@ 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);
tbl = RCLASS_IV_TBL(mod);
if (st_lookup(tbl, (st_data_t)autoload, &av)) {
if (tbl && st_lookup(tbl, (st_data_t)autoload, &av)) {
tbl = check_autoload_table((VALUE)av);
}
else {
if (!tbl) tbl = RCLASS_IV_TBL(mod) = st_init_numtable();
av = (st_data_t)TypedData_Wrap_Struct(0, &autoload_data_type, 0);
st_add_direct(tbl, (st_data_t)autoload, av);
DATA_PTR(av) = tbl = st_init_numtable();
@ -1463,7 +1464,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);
@ -1471,7 +1472,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);
}
}
@ -1528,7 +1529,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) {
@ -1577,7 +1578,7 @@ rb_const_get_0(VALUE klass, ID id, int exclude, int recurse)
while (RTEST(tmp)) {
VALUE am = 0;
st_data_t data;
while (RCLASS_IV_TBL(tmp) && st_lookup(RCLASS_IV_TBL(tmp), (st_data_t)id, &data)) {
while (RCLASS_CONST_TBL(tmp) && st_lookup(RCLASS_CONST_TBL(tmp), (st_data_t)id, &data)) {
value = (VALUE)data;
if (value == Qundef) {
if (am == tmp) break;
@ -1654,7 +1655,7 @@ rb_const_remove(VALUE mod, ID id)
if (!OBJ_UNTRUSTED(mod) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: can't remove constant");
rb_check_frozen(mod);
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));
@ -1691,8 +1692,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;
}
@ -1777,7 +1778,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;
@ -1811,32 +1812,12 @@ 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);
rb_check_frozen(klass);
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
@ -1846,7 +1827,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
@ -1928,7 +1926,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

View file

@ -1169,8 +1169,8 @@ vm_get_ev_const(rb_thread_t *th, const rb_iseq_t *iseq,
VALUE am = 0;
st_data_t data;
search_continue:
if (RCLASS_IV_TBL(klass) &&
st_lookup(RCLASS_IV_TBL(klass), id, &data)) {
if (RCLASS_CONST_TBL(klass) &&
st_lookup(RCLASS_CONST_TBL(klass), id, &data)) {
val = (st_data_t)data;
if (val == Qundef) {
if (am == klass) break;