737a68c9cf6b6de929a797950ddb59067a86a41e
[gitmo/Class-MOP.git] / mop.h
1 #ifndef __MOP_H__
2 #define __MOP_H__
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #include "ppport.h"
10
11 #define MOP_CALL_BOOT(name) STMT_START {        \
12         EXTERN_C XS(CAT2(boot_, name));         \
13         PUSHMARK(SP);                           \
14         CALL_FPTR(CAT2(boot_, name))(aTHX_ cv); \
15     } STMT_END
16
17
18 #define MAKE_KEYSV(name) newSVpvn_share(#name, sizeof(#name)-1, 0U)
19
20 XS(mop_xs_simple_accessor);
21 XS(mop_xs_simple_reader);
22 XS(mop_xs_simple_writer);
23 XS(mop_xs_simple_predicate);
24 XS(mop_xs_simple_predicate_for_metaclass);
25 XS(mop_xs_simple_clearer);
26
27 extern SV *mop_method_metaclass;
28 extern SV *mop_associated_metaclass;
29 extern SV *mop_associated_attribute;
30 extern SV *mop_wrap;
31 extern SV *mop_methods;
32 extern SV *mop_name;
33 extern SV *mop_body;
34 extern SV *mop_package;
35 extern SV *mop_package_name;
36 extern SV *mop_package_cache_flag;
37 extern SV *mop_initialize;
38 extern SV *mop_can;
39 extern SV *mop_Class;
40 extern SV *mop_VERSION;
41 extern SV *mop_ISA;
42
43 /* MOP utilities */
44
45 UV mop_check_package_cache_flag(pTHX_ HV *stash);
46 int mop_get_code_info (SV *coderef, char **pkg, char **name);
47 SV *mop_call0(pTHX_ SV *const self, SV *const method);
48 SV *mop_call1(pTHX_ SV *const self, SV *const method, SV *const arg1);
49
50 #define mop_call0_pvs(o, m)    mop_call0(aTHX_ o, newSVpvs_flags(m, SVs_TEMP))
51 #define mop_call1_pvs(o, m, a) mop_call1(aTHX_ o, newSVpvs_flags(m, SVs_TEMP), a)
52
53 bool mop_is_class_loaded(pTHX_ SV*);
54 #define is_class_loaded(klass) mop_is_class_loaded(aTHX_ klass)
55
56 bool mop_is_instance_of(pTHX_ SV*, SV*);
57 #define is_instance_of(sv, klass)     mop_is_instance_of(aTHX_ sv, klass)
58 #define is_instance_of_pvs(sv, klass) mop_is_instance_of(aTHX_ sv, newSVpvs_flags(klass, SVs_TEMP))
59
60 typedef enum {
61     TYPE_FILTER_NONE,
62     TYPE_FILTER_CODE,
63     TYPE_FILTER_ARRAY,
64     TYPE_FILTER_IO,
65     TYPE_FILTER_HASH,
66     TYPE_FILTER_SCALAR,
67 } type_filter_t;
68
69 typedef bool (*get_package_symbols_cb_t) (const char *, STRLEN, SV *, void *);
70
71 void mop_get_package_symbols(HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud);
72 HV  *mop_get_all_package_symbols (HV *stash, type_filter_t filter);
73
74
75 #define MOPf_DIE_ON_FAIL 0x01
76 MAGIC* mop_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags);
77
78 #ifdef DEBUGGING
79 #define MOP_av_at(av, ix)  *mop_av_at_safe(aTHX_ (av) , (ix))
80 SV** mop_av_at_safe(pTHX_ AV* const mi, I32 const ix);
81 #else
82 #define MOP_av_at(av, ix)  AvARRAY(av)[ix]
83 #endif
84
85 #define IsObject(sv) (SvROK(sv) && SvOBJECT(SvRV(sv)))
86
87 #define newSVsv_share(sv) mop_newSVsv_share(aTHX_ sv)
88 SV* mop_newSVsv_share(pTHX_ SV*);
89
90 SV* mop_class_of(pTHX_ SV* const sv);
91
92 /* Class::MOP::Class */
93
94 AV* mop_class_get_all_attributes(pTHX_ SV* const metaclass);
95
96 /* Class::MOP Magic stuff */
97
98 /* All the MOP_mg_* macros require MAGIC* mg for the first argument */
99
100
101 /* Class::MOP::Instance stuff */
102
103 /* SV* mi (meta instance) may not be used */
104 typedef struct {
105     SV*  (*create_instance)(pTHX_ SV* const mi, HV* const stash);
106     SV*  (*clone_instance) (pTHX_ SV* const mi, SV* const instance);
107     bool (*has_slot)       (pTHX_ SV* const mi, SV* const instance, SV* const slot);
108     SV*  (*get_slot)       (pTHX_ SV* const mi, SV* const instance, SV* const slot);
109     SV*  (*set_slot)       (pTHX_ SV* const mi, SV* const instance, SV* const slot, SV* const value);
110     SV*  (*delete_slot)    (pTHX_ SV* const mi, SV* const instance, SV* const slot);
111     void (*weaken_slot)    (pTHX_ SV* const mi, SV* const instance, SV* const slot);
112 } mop_instance_vtbl;
113
114 SV*  mop_instance_create     (pTHX_ SV* const mi, HV* const stash);
115 SV*  mop_instance_clone      (pTHX_ SV* const mi, SV* const instance);
116 bool mop_instance_has_slot   (pTHX_ SV* const mi, SV* const instance, SV* const slot);
117 SV*  mop_instance_get_slot   (pTHX_ SV* const mi, SV* const instance, SV* const slot);
118 SV*  mop_instance_set_slot   (pTHX_ SV* const mi, SV* const instance, SV* const slot, SV* const value);
119 SV*  mop_instance_delete_slot(pTHX_ SV* const mi, SV* const instance, SV* const slot);
120 void mop_instance_weaken_slot(pTHX_ SV* const mi, SV* const instance, SV* const slot);
121
122 const mop_instance_vtbl* mop_get_default_instance_vtbl(pTHX);
123
124 #define MOP_mg_obj(mg)   ((mg)->mg_obj)
125 #define MOP_mg_ptr(mg)   ((mg)->mg_ptr)
126 #define MOP_mg_vtbl(mg)  ((const mop_instance_vtbl*)MOP_mg_ptr(mg))
127 #define MOP_mg_flags(mg) ((mg)->mg_private)
128 #define MOP_mg_virtual(mg) ((mg)->mg_virtual)
129
130 #define MOP_mg_obj_refcounted_on(mg)    (void)((mg)->mg_flags |= MGf_REFCOUNTED);
131
132 #define MOP_mg_create_instance(mg, stash) MOP_mg_vtbl(mg)->create_instance (aTHX_ NULL, (stash))
133 #define MOP_mg_clone_instance(mg, o)      MOP_mg_vtbl(mg)->clone_instance  (aTHX_ NULL, (o))
134 #define MOP_mg_has_slot(mg, o, slot)      MOP_mg_vtbl(mg)->has_slot        (aTHX_ NULL, (o), (slot))
135 #define MOP_mg_get_slot(mg, o, slot)      MOP_mg_vtbl(mg)->get_slot        (aTHX_ NULL, (o), (slot))
136 #define MOP_mg_set_slot(mg, o, slot, v)   MOP_mg_vtbl(mg)->set_slot        (aTHX_ NULL, (o), (slot), (v))
137 #define MOP_mg_delete_slot(mg, o, slot)   MOP_mg_vtbl(mg)->delete_slot     (aTHX_ NULL, (o), (slot))
138 #define MOP_mg_weaken_slot(mg, o, slot)   MOP_mg_vtbl(mg)->weaken_slot     (aTHX_ NULL, (o), (slot))
139
140 /* Class::MOP::Attribute stuff */
141
142
143 #define MOP_attr_slot(meta)          MOP_av_at(meta, MOP_ATTR_SLOT)
144 #define MOP_attr_init_arg(meta)      MOP_av_at(meta, MOP_ATTR_INIT_ARG)
145 #define MOP_attr_default(meta)       MOP_av_at(meta, MOP_ATTR_DEFAULT)
146 #define MOP_attr_builder(meta)       MOP_av_at(meta, MOP_ATTR_BUILDER)
147
148 enum mop_attr_ix_t{
149     MOP_ATTR_SLOT,
150
151     MOP_ATTR_INIT_ARG,
152     MOP_ATTR_DEFAULT,
153     MOP_ATTR_BUILDER,
154
155     MOP_ATTR_last,
156 };
157
158 enum mop_attr_flags_t{ /* keep 16 bits */
159     MOP_ATTRf_HAS_INIT_ARG         = 0x0001,
160     MOP_ATTRf_HAS_DEFAULT          = 0x0002,
161     MOP_ATTRf_IS_DEFAULT_A_CODEREF = 0x0004,
162     MOP_ATTRf_HAS_BUILDER          = 0x0008,
163     MOP_ATTRf_HAS_INITIALIZER      = 0x0010,
164
165     MOP_ATTRf_DEBUG                = 0x8000
166 };
167
168 MAGIC* mop_attr_mg(pTHX_ SV* const attr, SV* const instance);
169 void   mop_attr_initialize_instance_slot(pTHX_ SV* const attr, const mop_instance_vtbl* const vtbl, SV* const instance, HV* const args);
170
171 /* Class::MOP::Method::Accessor stuff */
172
173 #define dMOP_self      SV* const self = mop_accessor_get_self(aTHX_ ax, items, cv)
174 #define dMOP_mg(xsub)  MAGIC* mg      = (MAGIC*)CvXSUBANY(xsub).any_ptr
175 #define dMOP_METHOD_COMMON  dMOP_self; dMOP_mg(cv)
176
177
178 SV*    mop_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv);
179
180 CV*    mop_install_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl, const mop_instance_vtbl* vtbl);
181 CV*    mop_instantiate_xs_accessor(pTHX_ SV* const accessor, XSUBADDR_t const accessor_impl, mop_instance_vtbl* const vtbl);
182
183 #define INSTALL_SIMPLE_READER(klass, name)                  INSTALL_SIMPLE_READER_WITH_KEY(klass, name, name)
184 #define INSTALL_SIMPLE_READER_WITH_KEY(klass, name, key)    (void)mop_install_accessor(aTHX_ "Class::MOP::" #klass "::" #name, #key, sizeof(#key)-1, mop_xs_simple_reader, NULL)
185
186 #define INSTALL_SIMPLE_WRITER(klass, name)                  INSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, name)
187 #define INSTALL_SIMPLE_WRITER_WITH_KEY(klass, name, key)    (void)mop_install_accessor(aTHX_ "Class::MOP::" #klass "::" #name, #key, sizeof(#key)-1, mop_xs_simple_writer, NULL)
188
189 #define INSTALL_SIMPLE_PREDICATE(klass, name)                INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, name)
190 #define INSTALL_SIMPLE_PREDICATE_WITH_KEY(klass, name, key) (void)mop_install_accessor(aTHX_ "Class::MOP::" #klass "::has_" #name, #key, sizeof(#key)-1, mop_xs_simple_predicate_for_metaclass, NULL)
191
192 #endif