Reorganize flags values for classes; fix #3975

Renamed flag macro names as well:
`MRB_FLAG_IS_FROZEN` -> `MRB_FL_OBJ_FROZEN`
`MRB_FLAG_IS_PREPENDED` -> `MRB_FL_CLASS_IS_PREPENDED`
`MRB_FLAG_IS_ORIGIN` -> `MRB_FL_CLASS_IS_ORIGIN`
`MRB_FLAG_IS_INHERITED` -> `MRB_FL_CLASS_IS_INHERITED`
parent bfd11aab
...@@ -53,19 +53,25 @@ mrb_class(mrb_state *mrb, mrb_value v) ...@@ -53,19 +53,25 @@ mrb_class(mrb_state *mrb, mrb_value v)
} }
} }
/* TODO: figure out where to put user flags */ /* flags:
/* flags bits >= 18 is reserved */ 20: frozen
#define MRB_FLAG_IS_PREPENDED (1 << 19) 19: is_prepended
#define MRB_FLAG_IS_ORIGIN (1 << 20) 18: is_origin
17: is_inherited (used by method cache)
16: unused
0-15: instance type
*/
#define MRB_FL_CLASS_IS_PREPENDED (1 << 19)
#define MRB_FL_CLASS_IS_ORIGIN (1 << 18)
#define MRB_CLASS_ORIGIN(c) do {\ #define MRB_CLASS_ORIGIN(c) do {\
if (c->flags & MRB_FLAG_IS_PREPENDED) {\ if (c->flags & MRB_FL_CLASS_IS_PREPENDED) {\
c = c->super;\ c = c->super;\
while (!(c->flags & MRB_FLAG_IS_ORIGIN)) {\ while (!(c->flags & MRB_FL_CLASS_IS_ORIGIN)) {\
c = c->super;\ c = c->super;\
}\ }\
}\ }\
} while (0) } while (0)
#define MRB_FLAG_IS_INHERITED (1 << 21) #define MRB_FL_CLASS_IS_INHERITED (1 << 17)
#define MRB_INSTANCE_TT_MASK (0xFF) #define MRB_INSTANCE_TT_MASK (0xFF)
#define MRB_SET_INSTANCE_TT(c, tt) c->flags = ((c->flags & ~MRB_INSTANCE_TT_MASK) | (char)tt) #define MRB_SET_INSTANCE_TT(c, tt) c->flags = ((c->flags & ~MRB_INSTANCE_TT_MASK) | (char)tt)
#define MRB_INSTANCE_TT(c) (enum mrb_vtype)(c->flags & MRB_INSTANCE_TT_MASK) #define MRB_INSTANCE_TT(c) (enum mrb_vtype)(c->flags & MRB_INSTANCE_TT_MASK)
......
...@@ -22,11 +22,10 @@ struct RBasic { ...@@ -22,11 +22,10 @@ struct RBasic {
}; };
#define mrb_basic_ptr(v) ((struct RBasic*)(mrb_ptr(v))) #define mrb_basic_ptr(v) ((struct RBasic*)(mrb_ptr(v)))
/* flags bits >= 18 is reserved */ #define MRB_FL_OBJ_IS_FROZEN (1 << 20)
#define MRB_FLAG_IS_FROZEN (1 << 18) #define MRB_FROZEN_P(o) ((o)->flags & MRB_FL_OBJ_IS_FROZEN)
#define MRB_FROZEN_P(o) ((o)->flags & MRB_FLAG_IS_FROZEN) #define MRB_SET_FROZEN_FLAG(o) ((o)->flags |= MRB_FL_OBJ_IS_FROZEN)
#define MRB_SET_FROZEN_FLAG(o) ((o)->flags |= MRB_FLAG_IS_FROZEN) #define MRB_UNSET_FROZEN_FLAG(o) ((o)->flags &= ~MRB_FL_OBJ_IS_FROZEN)
#define MRB_UNSET_FROZEN_FLAG(o) ((o)->flags &= ~MRB_FLAG_IS_FROZEN)
struct RObject { struct RObject {
MRB_OBJECT_HEADER; MRB_OBJECT_HEADER;
......
...@@ -93,7 +93,7 @@ prepare_singleton_class(mrb_state *mrb, struct RBasic *o) ...@@ -93,7 +93,7 @@ prepare_singleton_class(mrb_state *mrb, struct RBasic *o)
if (o->c->tt == MRB_TT_SCLASS) return; if (o->c->tt == MRB_TT_SCLASS) return;
sc = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_SCLASS, mrb->class_class); sc = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_SCLASS, mrb->class_class);
sc->flags |= MRB_FLAG_IS_INHERITED; sc->flags |= MRB_FL_CLASS_IS_INHERITED;
sc->mt = kh_init(mt, mrb); sc->mt = kh_init(mt, mrb);
sc->iv = 0; sc->iv = 0;
if (o->tt == MRB_TT_CLASS) { if (o->tt == MRB_TT_CLASS) {
...@@ -275,7 +275,7 @@ mrb_class_inherited(mrb_state *mrb, struct RClass *super, struct RClass *klass) ...@@ -275,7 +275,7 @@ mrb_class_inherited(mrb_state *mrb, struct RClass *super, struct RClass *klass)
if (!super) if (!super)
super = mrb->object_class; super = mrb->object_class;
super->flags |= MRB_FLAG_IS_INHERITED; super->flags |= MRB_FL_CLASS_IS_INHERITED;
s = mrb_obj_value(super); s = mrb_obj_value(super);
mc_clear_by_class(mrb, klass); mc_clear_by_class(mrb, klass);
mid = mrb_intern_lit(mrb, "inherited"); mid = mrb_intern_lit(mrb, "inherited");
...@@ -1061,7 +1061,7 @@ include_module_at(mrb_state *mrb, struct RClass *c, struct RClass *ins_pos, stru ...@@ -1061,7 +1061,7 @@ include_module_at(mrb_state *mrb, struct RClass *c, struct RClass *ins_pos, stru
while (m) { while (m) {
int superclass_seen = 0; int superclass_seen = 0;
if (m->flags & MRB_FLAG_IS_PREPENDED) if (m->flags & MRB_FL_CLASS_IS_PREPENDED)
goto skip; goto skip;
if (klass_mt && klass_mt == m->mt) if (klass_mt && klass_mt == m->mt)
...@@ -1084,7 +1084,7 @@ include_module_at(mrb_state *mrb, struct RClass *c, struct RClass *ins_pos, stru ...@@ -1084,7 +1084,7 @@ include_module_at(mrb_state *mrb, struct RClass *c, struct RClass *ins_pos, stru
} }
ic = include_class_new(mrb, m, ins_pos->super); ic = include_class_new(mrb, m, ins_pos->super);
m->flags |= MRB_FLAG_IS_INHERITED; m->flags |= MRB_FL_CLASS_IS_INHERITED;
ins_pos->super = ic; ins_pos->super = ic;
mrb_field_write_barrier(mrb, (struct RBasic*)ins_pos, (struct RBasic*)ic); mrb_field_write_barrier(mrb, (struct RBasic*)ins_pos, (struct RBasic*)ic);
mc_clear_by_class(mrb, ins_pos); mc_clear_by_class(mrb, ins_pos);
...@@ -1111,15 +1111,15 @@ mrb_prepend_module(mrb_state *mrb, struct RClass *c, struct RClass *m) ...@@ -1111,15 +1111,15 @@ mrb_prepend_module(mrb_state *mrb, struct RClass *c, struct RClass *m)
struct RClass *origin; struct RClass *origin;
int changed = 0; int changed = 0;
if (!(c->flags & MRB_FLAG_IS_PREPENDED)) { if (!(c->flags & MRB_FL_CLASS_IS_PREPENDED)) {
origin = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_ICLASS, c); origin = (struct RClass*)mrb_obj_alloc(mrb, MRB_TT_ICLASS, c);
origin->flags |= MRB_FLAG_IS_ORIGIN | MRB_FLAG_IS_INHERITED; origin->flags |= MRB_FL_CLASS_IS_ORIGIN | MRB_FL_CLASS_IS_INHERITED;
origin->super = c->super; origin->super = c->super;
c->super = origin; c->super = origin;
origin->mt = c->mt; origin->mt = c->mt;
c->mt = kh_init(mt, mrb); c->mt = kh_init(mt, mrb);
mrb_field_write_barrier(mrb, (struct RBasic*)c, (struct RBasic*)origin); mrb_field_write_barrier(mrb, (struct RBasic*)c, (struct RBasic*)origin);
c->flags |= MRB_FLAG_IS_PREPENDED; c->flags |= MRB_FL_CLASS_IS_PREPENDED;
} }
changed = include_module_at(mrb, c, c, m, 0); changed = include_module_at(mrb, c, c, m, 0);
if (changed < 0) { if (changed < 0) {
...@@ -1196,7 +1196,7 @@ mrb_mod_ancestors(mrb_state *mrb, mrb_value self) ...@@ -1196,7 +1196,7 @@ mrb_mod_ancestors(mrb_state *mrb, mrb_value self)
if (c->tt == MRB_TT_ICLASS) { if (c->tt == MRB_TT_ICLASS) {
mrb_ary_push(mrb, result, mrb_obj_value(c->c)); mrb_ary_push(mrb, result, mrb_obj_value(c->c));
} }
else if (!(c->flags & MRB_FLAG_IS_PREPENDED)) { else if (!(c->flags & MRB_FL_CLASS_IS_PREPENDED)) {
mrb_ary_push(mrb, result, mrb_obj_value(c)); mrb_ary_push(mrb, result, mrb_obj_value(c));
} }
c = c->super; c = c->super;
...@@ -1365,9 +1365,9 @@ mc_clear_by_class(mrb_state *mrb, struct RClass *c) ...@@ -1365,9 +1365,9 @@ mc_clear_by_class(mrb_state *mrb, struct RClass *c)
struct mrb_cache_entry *mc = mrb->cache; struct mrb_cache_entry *mc = mrb->cache;
int i; int i;
if (c->flags & MRB_FLAG_IS_INHERITED) { if (c->flags & MRB_FL_CLASS_IS_INHERITED) {
mc_clear_all(mrb); mc_clear_all(mrb);
c->flags &= ~MRB_FLAG_IS_INHERITED; c->flags &= ~MRB_FL_CLASS_IS_INHERITED;
return; return;
} }
for (i=0; i<MRB_METHOD_CACHE_SIZE; i++) { for (i=0; i<MRB_METHOD_CACHE_SIZE; i++) {
...@@ -1381,9 +1381,9 @@ mc_clear_by_id(mrb_state *mrb, struct RClass *c, mrb_sym mid) ...@@ -1381,9 +1381,9 @@ mc_clear_by_id(mrb_state *mrb, struct RClass *c, mrb_sym mid)
struct mrb_cache_entry *mc = mrb->cache; struct mrb_cache_entry *mc = mrb->cache;
int i; int i;
if (c->flags & MRB_FLAG_IS_INHERITED) { if (c->flags & MRB_FL_CLASS_IS_INHERITED) {
mc_clear_all(mrb); mc_clear_all(mrb);
c->flags &= ~MRB_FLAG_IS_INHERITED; c->flags &= ~MRB_FL_CLASS_IS_INHERITED;
return; return;
} }
for (i=0; i<MRB_METHOD_CACHE_SIZE; i++) { for (i=0; i<MRB_METHOD_CACHE_SIZE; i++) {
......
...@@ -622,7 +622,7 @@ gc_mark_children(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj) ...@@ -622,7 +622,7 @@ gc_mark_children(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
case MRB_TT_ICLASS: case MRB_TT_ICLASS:
{ {
struct RClass *c = (struct RClass*)obj; struct RClass *c = (struct RClass*)obj;
if (MRB_FLAG_TEST(c, MRB_FLAG_IS_ORIGIN)) if (MRB_FLAG_TEST(c, MRB_FL_CLASS_IS_ORIGIN))
mrb_gc_mark_mt(mrb, c); mrb_gc_mark_mt(mrb, c);
mrb_gc_mark(mrb, (struct RBasic*)((struct RClass*)obj)->super); mrb_gc_mark(mrb, (struct RBasic*)((struct RClass*)obj)->super);
} }
...@@ -761,7 +761,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end) ...@@ -761,7 +761,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end)
mrb_gc_free_iv(mrb, (struct RObject*)obj); mrb_gc_free_iv(mrb, (struct RObject*)obj);
break; break;
case MRB_TT_ICLASS: case MRB_TT_ICLASS:
if (MRB_FLAG_TEST(obj, MRB_FLAG_IS_ORIGIN)) if (MRB_FLAG_TEST(obj, MRB_FL_CLASS_IS_ORIGIN))
mrb_gc_free_mt(mrb, (struct RClass*)obj); mrb_gc_free_mt(mrb, (struct RClass*)obj);
break; break;
case MRB_TT_ENV: case MRB_TT_ENV:
......
...@@ -252,18 +252,18 @@ copy_class(mrb_state *mrb, mrb_value dst, mrb_value src) ...@@ -252,18 +252,18 @@ copy_class(mrb_state *mrb, mrb_value dst, mrb_value src)
struct RClass *sc = mrb_class_ptr(src); struct RClass *sc = mrb_class_ptr(src);
/* if the origin is not the same as the class, then the origin and /* if the origin is not the same as the class, then the origin and
the current class need to be copied */ the current class need to be copied */
if (sc->flags & MRB_FLAG_IS_PREPENDED) { if (sc->flags & MRB_FL_CLASS_IS_PREPENDED) {
struct RClass *c0 = sc->super; struct RClass *c0 = sc->super;
struct RClass *c1 = dc; struct RClass *c1 = dc;
/* copy prepended iclasses */ /* copy prepended iclasses */
while (!(c0->flags & MRB_FLAG_IS_ORIGIN)) { while (!(c0->flags & MRB_FL_CLASS_IS_ORIGIN)) {
c1->super = mrb_class_ptr(mrb_obj_dup(mrb, mrb_obj_value(c0))); c1->super = mrb_class_ptr(mrb_obj_dup(mrb, mrb_obj_value(c0)));
c1 = c1->super; c1 = c1->super;
c0 = c0->super; c0 = c0->super;
} }
c1->super = mrb_class_ptr(mrb_obj_dup(mrb, mrb_obj_value(c0))); c1->super = mrb_class_ptr(mrb_obj_dup(mrb, mrb_obj_value(c0)));
c1->super->flags |= MRB_FLAG_IS_ORIGIN; c1->super->flags |= MRB_FL_CLASS_IS_ORIGIN;
} }
if (sc->mt) { if (sc->mt) {
dc->mt = kh_copy(mt, mrb, sc->mt); dc->mt = kh_copy(mt, mrb, sc->mt);
...@@ -348,7 +348,7 @@ mrb_obj_clone(mrb_state *mrb, mrb_value self) ...@@ -348,7 +348,7 @@ mrb_obj_clone(mrb_state *mrb, mrb_value self)
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)p->c); mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)p->c);
clone = mrb_obj_value(p); clone = mrb_obj_value(p);
init_copy(mrb, clone, self); init_copy(mrb, clone, self);
p->flags |= mrb_obj_ptr(self)->flags & MRB_FLAG_IS_FROZEN; p->flags |= mrb_obj_ptr(self)->flags & MRB_FL_OBJ_IS_FROZEN;
return clone; return clone;
} }
...@@ -706,7 +706,7 @@ mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* kl ...@@ -706,7 +706,7 @@ mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* kl
struct RClass* oldklass; struct RClass* oldklass;
khash_t(st)* set = kh_init(st, mrb); khash_t(st)* set = kh_init(st, mrb);
if (!recur && (klass->flags & MRB_FLAG_IS_PREPENDED)) { if (!recur && (klass->flags & MRB_FL_CLASS_IS_PREPENDED)) {
MRB_CLASS_ORIGIN(klass); MRB_CLASS_ORIGIN(klass);
prepended = TRUE; prepended = TRUE;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment