mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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`
This commit is contained in:
+13
-7
@@ -53,19 +53,25 @@ mrb_class(mrb_state *mrb, mrb_value v)
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: figure out where to put user flags */
|
||||
/* flags bits >= 18 is reserved */
|
||||
#define MRB_FLAG_IS_PREPENDED (1 << 19)
|
||||
#define MRB_FLAG_IS_ORIGIN (1 << 20)
|
||||
/* flags:
|
||||
20: frozen
|
||||
19: is_prepended
|
||||
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 {\
|
||||
if (c->flags & MRB_FLAG_IS_PREPENDED) {\
|
||||
if (c->flags & MRB_FL_CLASS_IS_PREPENDED) {\
|
||||
c = c->super;\
|
||||
while (!(c->flags & MRB_FLAG_IS_ORIGIN)) {\
|
||||
while (!(c->flags & MRB_FL_CLASS_IS_ORIGIN)) {\
|
||||
c = c->super;\
|
||||
}\
|
||||
}\
|
||||
} 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_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)
|
||||
|
||||
@@ -22,11 +22,10 @@ struct RBasic {
|
||||
};
|
||||
#define mrb_basic_ptr(v) ((struct RBasic*)(mrb_ptr(v)))
|
||||
|
||||
/* flags bits >= 18 is reserved */
|
||||
#define MRB_FLAG_IS_FROZEN (1 << 18)
|
||||
#define MRB_FROZEN_P(o) ((o)->flags & MRB_FLAG_IS_FROZEN)
|
||||
#define MRB_SET_FROZEN_FLAG(o) ((o)->flags |= MRB_FLAG_IS_FROZEN)
|
||||
#define MRB_UNSET_FROZEN_FLAG(o) ((o)->flags &= ~MRB_FLAG_IS_FROZEN)
|
||||
#define MRB_FL_OBJ_IS_FROZEN (1 << 20)
|
||||
#define MRB_FROZEN_P(o) ((o)->flags & MRB_FL_OBJ_IS_FROZEN)
|
||||
#define MRB_SET_FROZEN_FLAG(o) ((o)->flags |= MRB_FL_OBJ_IS_FROZEN)
|
||||
#define MRB_UNSET_FROZEN_FLAG(o) ((o)->flags &= ~MRB_FL_OBJ_IS_FROZEN)
|
||||
|
||||
struct RObject {
|
||||
MRB_OBJECT_HEADER;
|
||||
|
||||
+12
-12
@@ -93,7 +93,7 @@ prepare_singleton_class(mrb_state *mrb, struct RBasic *o)
|
||||
|
||||
if (o->c->tt == MRB_TT_SCLASS) return;
|
||||
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->iv = 0;
|
||||
if (o->tt == MRB_TT_CLASS) {
|
||||
@@ -275,7 +275,7 @@ mrb_class_inherited(mrb_state *mrb, struct RClass *super, struct RClass *klass)
|
||||
|
||||
if (!super)
|
||||
super = mrb->object_class;
|
||||
super->flags |= MRB_FLAG_IS_INHERITED;
|
||||
super->flags |= MRB_FL_CLASS_IS_INHERITED;
|
||||
s = mrb_obj_value(super);
|
||||
mc_clear_by_class(mrb, klass);
|
||||
mid = mrb_intern_lit(mrb, "inherited");
|
||||
@@ -1061,7 +1061,7 @@ include_module_at(mrb_state *mrb, struct RClass *c, struct RClass *ins_pos, stru
|
||||
while (m) {
|
||||
int superclass_seen = 0;
|
||||
|
||||
if (m->flags & MRB_FLAG_IS_PREPENDED)
|
||||
if (m->flags & MRB_FL_CLASS_IS_PREPENDED)
|
||||
goto skip;
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)ins_pos, (struct RBasic*)ic);
|
||||
mc_clear_by_class(mrb, ins_pos);
|
||||
@@ -1111,15 +1111,15 @@ mrb_prepend_module(mrb_state *mrb, struct RClass *c, struct RClass *m)
|
||||
struct RClass *origin;
|
||||
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->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;
|
||||
c->super = origin;
|
||||
origin->mt = c->mt;
|
||||
c->mt = kh_init(mt, mrb);
|
||||
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);
|
||||
if (changed < 0) {
|
||||
@@ -1196,7 +1196,7 @@ mrb_mod_ancestors(mrb_state *mrb, mrb_value self)
|
||||
if (c->tt == MRB_TT_ICLASS) {
|
||||
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));
|
||||
}
|
||||
c = c->super;
|
||||
@@ -1365,9 +1365,9 @@ mc_clear_by_class(mrb_state *mrb, struct RClass *c)
|
||||
struct mrb_cache_entry *mc = mrb->cache;
|
||||
int i;
|
||||
|
||||
if (c->flags & MRB_FLAG_IS_INHERITED) {
|
||||
if (c->flags & MRB_FL_CLASS_IS_INHERITED) {
|
||||
mc_clear_all(mrb);
|
||||
c->flags &= ~MRB_FLAG_IS_INHERITED;
|
||||
c->flags &= ~MRB_FL_CLASS_IS_INHERITED;
|
||||
return;
|
||||
}
|
||||
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)
|
||||
struct mrb_cache_entry *mc = mrb->cache;
|
||||
int i;
|
||||
|
||||
if (c->flags & MRB_FLAG_IS_INHERITED) {
|
||||
if (c->flags & MRB_FL_CLASS_IS_INHERITED) {
|
||||
mc_clear_all(mrb);
|
||||
c->flags &= ~MRB_FLAG_IS_INHERITED;
|
||||
c->flags &= ~MRB_FL_CLASS_IS_INHERITED;
|
||||
return;
|
||||
}
|
||||
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)
|
||||
case MRB_TT_ICLASS:
|
||||
{
|
||||
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(mrb, (struct RBasic*)((struct RClass*)obj)->super);
|
||||
}
|
||||
@@ -761,7 +761,7 @@ obj_free(mrb_state *mrb, struct RBasic *obj, int end)
|
||||
mrb_gc_free_iv(mrb, (struct RObject*)obj);
|
||||
break;
|
||||
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);
|
||||
break;
|
||||
case MRB_TT_ENV:
|
||||
|
||||
+5
-5
@@ -252,18 +252,18 @@ copy_class(mrb_state *mrb, mrb_value dst, mrb_value src)
|
||||
struct RClass *sc = mrb_class_ptr(src);
|
||||
/* if the origin is not the same as the class, then the origin and
|
||||
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 *c1 = dc;
|
||||
|
||||
/* 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 = c1->super;
|
||||
c0 = c0->super;
|
||||
}
|
||||
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) {
|
||||
dc->mt = kh_copy(mt, mrb, sc->mt);
|
||||
@@ -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);
|
||||
clone = mrb_obj_value(p);
|
||||
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;
|
||||
}
|
||||
@@ -706,7 +706,7 @@ mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* kl
|
||||
struct RClass* oldklass;
|
||||
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);
|
||||
prepended = TRUE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user