1 /**
2 ** @file mruby/class.h - Class class
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #ifndef MRUBY_CLASS_H
8 #define MRUBY_CLASS_H
9
10 #include "common.h"
11
12 /**
13 * Class class
14 */
15 MRB_BEGIN_DECL
16
17 struct RClass {
18 MRB_OBJECT_HEADER;
19 struct iv_tbl *iv;
20 struct kh_mt *mt;
21 struct RClass *super;
22 };
23
24 #define mrb_class_ptr(v) ((struct RClass*)(mrb_ptr(v)))
25
26 MRB_INLINE struct RClass*
mrb_class(mrb_state * mrb,mrb_value v)27 mrb_class(mrb_state *mrb, mrb_value v)
28 {
29 switch (mrb_type(v)) {
30 case MRB_TT_FALSE:
31 if (mrb_fixnum(v))
32 return mrb->false_class;
33 return mrb->nil_class;
34 case MRB_TT_TRUE:
35 return mrb->true_class;
36 case MRB_TT_SYMBOL:
37 return mrb->symbol_class;
38 case MRB_TT_FIXNUM:
39 return mrb->fixnum_class;
40 #ifndef MRB_WITHOUT_FLOAT
41 case MRB_TT_FLOAT:
42 return mrb->float_class;
43 #endif
44 case MRB_TT_CPTR:
45 return mrb->object_class;
46 case MRB_TT_ENV:
47 return NULL;
48 default:
49 return mrb_obj_ptr(v)->c;
50 }
51 }
52
53 /* flags:
54 20: frozen
55 19: is_prepended
56 18: is_origin
57 17: is_inherited (used by method cache)
58 16: unused
59 0-15: instance type
60 */
61 #define MRB_FL_CLASS_IS_PREPENDED (1 << 19)
62 #define MRB_FL_CLASS_IS_ORIGIN (1 << 18)
63 #define MRB_CLASS_ORIGIN(c) do {\
64 if ((c)->flags & MRB_FL_CLASS_IS_PREPENDED) {\
65 (c) = (c)->super;\
66 while (!((c)->flags & MRB_FL_CLASS_IS_ORIGIN)) {\
67 (c) = (c)->super;\
68 }\
69 }\
70 } while (0)
71 #define MRB_FL_CLASS_IS_INHERITED (1 << 17)
72 #define MRB_INSTANCE_TT_MASK (0xFF)
73 #define MRB_SET_INSTANCE_TT(c, tt) ((c)->flags = (((c)->flags & ~MRB_INSTANCE_TT_MASK) | (char)(tt)))
74 #define MRB_INSTANCE_TT(c) (enum mrb_vtype)((c)->flags & MRB_INSTANCE_TT_MASK)
75
76 MRB_API struct RClass* mrb_define_class_id(mrb_state*, mrb_sym, struct RClass*);
77 MRB_API struct RClass* mrb_define_module_id(mrb_state*, mrb_sym);
78 struct RClass *mrb_vm_define_class(mrb_state*, mrb_value, mrb_value, mrb_sym);
79 struct RClass *mrb_vm_define_module(mrb_state*, mrb_value, mrb_sym);
80 MRB_API void mrb_define_method_raw(mrb_state*, struct RClass*, mrb_sym, mrb_method_t);
81 MRB_API void mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, mrb_aspec aspec);
82 MRB_API void mrb_alias_method(mrb_state*, struct RClass *c, mrb_sym a, mrb_sym b);
83
84 MRB_API mrb_method_t mrb_method_search_vm(mrb_state*, struct RClass**, mrb_sym);
85 MRB_API mrb_method_t mrb_method_search(mrb_state*, struct RClass*, mrb_sym);
86
87 MRB_API struct RClass* mrb_class_real(struct RClass* cl);
88 mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
89
90 void mrb_class_name_class(mrb_state*, struct RClass*, struct RClass*, mrb_sym);
91 mrb_bool mrb_const_name_p(mrb_state*, const char*, mrb_int);
92 mrb_value mrb_class_find_path(mrb_state*, struct RClass*);
93 mrb_value mrb_mod_to_s(mrb_state*, mrb_value);
94 void mrb_gc_mark_mt(mrb_state*, struct RClass*);
95 size_t mrb_gc_mark_mt_size(mrb_state*, struct RClass*);
96 void mrb_gc_free_mt(mrb_state*, struct RClass*);
97
98 #ifdef MRB_METHOD_CACHE
99 void mrb_mc_clear_by_class(mrb_state *mrb, struct RClass* c);
100 #else
101 #define mrb_mc_clear_by_class(mrb,c)
102 #endif
103
104 MRB_END_DECL
105
106 #endif /* MRUBY_CLASS_H */
107