Commit d192a52e authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge branch 'assign-anon-class-name' of https://github.com/dearblue/mruby...

Merge branch 'assign-anon-class-name' of https://github.com/dearblue/mruby into dearblue-assign-anon-class-name
parents 3a9caad8 5e6cd86f
...@@ -110,6 +110,9 @@ ...@@ -110,6 +110,9 @@
/* fixed size state atexit stack */ /* fixed size state atexit stack */
//#define MRB_FIXED_STATE_ATEXIT_STACK //#define MRB_FIXED_STATE_ATEXIT_STACK
/* improve meta programming support */
//#define MRB_IMPROVE_META_PROGRAMMING
/* -DMRB_DISABLE_XXXX to drop following features */ /* -DMRB_DISABLE_XXXX to drop following features */
//#define MRB_DISABLE_STDIO /* use of stdio */ //#define MRB_DISABLE_STDIO /* use of stdio */
......
...@@ -65,7 +65,7 @@ mrb_class_name_class(mrb_state *mrb, struct RClass *outer, struct RClass *c, mrb ...@@ -65,7 +65,7 @@ mrb_class_name_class(mrb_state *mrb, struct RClass *outer, struct RClass *c, mrb
else { else {
name = mrb_class_path(mrb, outer); name = mrb_class_path(mrb, outer);
if (mrb_nil_p(name)) { /* unnamed outer class */ if (mrb_nil_p(name)) { /* unnamed outer class */
if (outer != mrb->object_class) { if (outer != mrb->object_class && outer != c) {
mrb_obj_iv_set(mrb, (struct RObject*)c, mrb_intern_lit(mrb, "__outer__"), mrb_obj_iv_set(mrb, (struct RObject*)c, mrb_intern_lit(mrb, "__outer__"),
mrb_obj_value(outer)); mrb_obj_value(outer));
} }
......
...@@ -344,6 +344,10 @@ mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym) ...@@ -344,6 +344,10 @@ mrb_iv_get(mrb_state *mrb, mrb_value obj, mrb_sym sym)
return mrb_nil_value(); return mrb_nil_value();
} }
#ifdef MRB_IMPROVE_META_PROGRAMMING
static inline void assign_class_name(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v);
#endif
MRB_API void MRB_API void
mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v) mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
{ {
...@@ -352,6 +356,9 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v) ...@@ -352,6 +356,9 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
if (MRB_FROZEN_P(obj)) { if (MRB_FROZEN_P(obj)) {
mrb_raisef(mrb, E_FROZEN_ERROR, "can't modify frozen %S", mrb_obj_value(obj)); mrb_raisef(mrb, E_FROZEN_ERROR, "can't modify frozen %S", mrb_obj_value(obj));
} }
#ifdef MRB_IMPROVE_META_PROGRAMMING
assign_class_name(mrb, obj, sym, v);
#endif
if (!obj->iv) { if (!obj->iv) {
obj->iv = iv_new(mrb); obj->iv = iv_new(mrb);
} }
...@@ -360,6 +367,40 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v) ...@@ -360,6 +367,40 @@ mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
mrb_write_barrier(mrb, (struct RBasic*)obj); mrb_write_barrier(mrb, (struct RBasic*)obj);
} }
#ifdef MRB_IMPROVE_META_PROGRAMMING
static inline mrb_bool
is_namespace(enum mrb_vtype tt)
{
return tt == MRB_TT_CLASS || tt == MRB_TT_MODULE ? TRUE : FALSE;
}
static inline void
assign_class_name(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v)
{
if (is_namespace(obj->tt) && is_namespace(mrb_type(v))) {
struct RObject *c = mrb_obj_ptr(v);
if (obj != c && ISUPPER(mrb_sym2name(mrb, sym)[0])) {
mrb_sym id_classname = mrb_intern_lit(mrb, "__classname__");
mrb_value o = mrb_obj_iv_get(mrb, c, id_classname);
if (mrb_nil_p(o)) {
mrb_sym id_outer = mrb_intern_lit(mrb, "__outer__");
o = mrb_obj_iv_get(mrb, c, id_outer);
if (mrb_nil_p(o)) {
if ((struct RClass *)obj == mrb->object_class) {
mrb_obj_iv_set(mrb, c, id_classname, mrb_symbol_value(sym));
}
else {
mrb_obj_iv_set(mrb, c, id_outer, mrb_obj_value(obj));
}
}
}
}
}
}
#endif
MRB_API void MRB_API void
mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v) mrb_iv_set(mrb_state *mrb, mrb_value obj, mrb_sym sym, mrb_value v)
{ {
...@@ -1069,8 +1110,14 @@ mrb_class_find_path(mrb_state *mrb, struct RClass *c) ...@@ -1069,8 +1110,14 @@ mrb_class_find_path(mrb_state *mrb, struct RClass *c)
str = mrb_sym2name_len(mrb, name, &len); str = mrb_sym2name_len(mrb, name, &len);
mrb_str_cat(mrb, path, str, len); mrb_str_cat(mrb, path, str, len);
iv_del(mrb, c->iv, mrb_intern_lit(mrb, "__outer__"), NULL); #ifdef MRB_IMPROVE_META_PROGRAMMING
iv_put(mrb, c->iv, mrb_intern_lit(mrb, "__classname__"), path); if (RSTRING_PTR(path)[0] != '#') {
mrb_field_write_barrier_value(mrb, (struct RBasic*)c, path); #endif
iv_del(mrb, c->iv, mrb_intern_lit(mrb, "__outer__"), NULL);
iv_put(mrb, c->iv, mrb_intern_lit(mrb, "__classname__"), path);
mrb_field_write_barrier_value(mrb, (struct RBasic*)c, path);
#ifdef MRB_IMPROVE_META_PROGRAMMING
}
#endif
return path; return path;
} }
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