Merge branch 'topic/unified-method-generation' into topic/unified-method-generation...
[gitmo/Class-MOP.git] / mop.c
CommitLineData
d846ade3 1#include "mop.h"
2
3void
1be56175 4mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
d846ade3 5{
e3dcef7f 6 dSP;
7 PUSHMARK(mark);
8 (*subaddr)(aTHX_ cv);
9 PUTBACK;
d846ade3 10}
11
8a2e4cdb 12#if PERL_BCDVERSION >= 0x5010000
d846ade3 13UV
14mop_check_package_cache_flag (pTHX_ HV *stash)
15{
16 assert(SvTYPE(stash) == SVt_PVHV);
17
18 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
19 * however the perl core doesn't make it easy for us. It doesn't provide an
20 * api that just does what we want.
21 *
22 * However, we know that the information we want is, inside the core,
23 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
24 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
25 * which is not public and only available inside the core, as the mro
26 * interface as well as the structure returned by mro_meta_init isn't
27 * considered to be stable yet.
28 *
29 * Perl_mro_meta_init isn't declared static, so we could just define it
30 * ourselfs if perls headers don't do that for us, except that won't work
31 * on platforms where symbols need to be explicitly exported when linking
32 * shared libraries.
33 *
34 * So our, hopefully temporary, solution is to be even more evil and
35 * basically reimplement HvMROMETA in a very fragile way that'll blow up
36 * when the relevant parts of the mro implementation in core change.
37 *
38 * :-(
39 *
40 */
41
42 return HvAUX(stash)->xhv_mro_meta
43 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
44 : 0;
45}
46
47#else /* pre 5.10.0 */
48
49UV
50mop_check_package_cache_flag (pTHX_ HV *stash)
51{
52 PERL_UNUSED_ARG(stash);
53 assert(SvTYPE(stash) == SVt_PVHV);
54
55 return PL_sub_generation;
56}
57#endif
58
59SV *
60mop_call0 (pTHX_ SV *const self, SV *const method)
61{
62 dSP;
63 SV *ret;
64
65 PUSHMARK(SP);
66 XPUSHs(self);
67 PUTBACK;
68
69 call_sv(method, G_SCALAR | G_METHOD);
70
71 SPAGAIN;
72 ret = POPs;
73 PUTBACK;
74
75 return ret;
76}
77
78int
e1f52a8a 79mop_get_code_info (SV *coderef, char **pkg, char **name)
d846ade3 80{
81 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
82 return 0;
83 }
84
85 coderef = SvRV(coderef);
caa6b5cd 86
87 /* sub is still being compiled */
88 if (!CvGV(coderef)) {
89 return 0;
90 }
91
d846ade3 92 /* I think this only gets triggered with a mangled coderef, but if
93 we hit it without the guard, we segfault. The slightly odd return
94 value strikes me as an improvement (mst)
95 */
9b52bbf1 96
d846ade3 97 if ( isGV_with_GP(CvGV(coderef)) ) {
2087a201 98 GV *gv = CvGV(coderef);
99 *pkg = HvNAME( GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef) );
d846ade3 100 *name = GvNAME( CvGV(coderef) );
d846ade3 101 } else {
102 *pkg = "__UNKNOWN__";
103 *name = "__ANON__";
104 }
d846ade3 105
106 return 1;
107}
108
109void
e1f52a8a 110mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
d846ade3 111{
112 HE *he;
113
114 (void)hv_iterinit(stash);
115
116 if (filter == TYPE_FILTER_NONE) {
117 while ( (he = hv_iternext(stash)) ) {
118 STRLEN keylen;
119 const char *key = HePV(he, keylen);
120 if (!cb(key, keylen, HeVAL(he), ud)) {
121 return;
122 }
123 }
124 return;
125 }
126
127 while ( (he = hv_iternext(stash)) ) {
128 SV *const gv = HeVAL(he);
129 SV *sv = NULL;
130 char *key;
131 STRLEN keylen;
132 char *package;
d846ade3 133
134 switch( SvTYPE(gv) ) {
135#ifndef SVt_RV
136 case SVt_RV:
137#endif
138 case SVt_PV:
139 case SVt_IV:
140 /* expand the gv into a real typeglob if it
141 * contains stub functions and we were asked to
142 * return CODE symbols */
143 if (filter == TYPE_FILTER_CODE) {
144 if (SvROK(gv)) {
8a2e4cdb 145 SV* fq;
d846ade3 146 /* we don't really care about the length,
147 but that's the API */
148 key = HePV(he, keylen);
149 package = HvNAME(stash);
efc98200 150 fq = sv_2mortal(newSVpvf("%s::%s", package, key));
d846ade3 151 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
152 break;
153 }
154
155 key = HePV(he, keylen);
156 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
157 }
158 /* fall through */
159 case SVt_PVGV:
160 switch (filter) {
161 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
162 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
163 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
164 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
165 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
166 default:
167 croak("Unknown type");
168 }
169 break;
170 default:
171 continue;
172 }
173
174 if (sv) {
175 const char *key = HePV(he, keylen);
176 if (!cb(key, keylen, sv, ud)) {
177 return;
178 }
179 }
180 }
181}
182
183static bool
184collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
185{
186 HV *hash = (HV *)ud;
187
188 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
189 croak("failed to store symbol ref");
190 }
191
192 return TRUE;
193}
194
195HV *
e1f52a8a 196mop_get_all_package_symbols (HV *stash, type_filter_t filter)
d846ade3 197{
198 HV *ret = newHV ();
e1f52a8a 199 mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
d846ade3 200 return ret;
201}
22932438 202
8a2e4cdb 203static MGVTBL mop_accessor_vtbl; /* the MAGIC identity */
22932438 204
a69b9501 205CV*
206mop_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSPROTO(accessor_impl)){
207 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
208 SV* const keysv = newSVpvn_share(key, keylen, 0U);
22932438 209
8a2e4cdb 210 sv_magicext((SV*)xsub, keysv, PERL_MAGIC_ext, &mop_accessor_vtbl, NULL, 0);
211 SvREFCNT_dec(keysv); /* sv_magicext() increases refcnt in mg_obj */
a69b9501 212 return xsub;
22932438 213}
214
8a2e4cdb 215static MAGIC*
a69b9501 216mop_mg_find_by_vtbl(pTHX_ SV* const sv, const MGVTBL* const vtbl){
8a2e4cdb 217 MAGIC* mg;
218
219 assert(sv != NULL);
220 for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
221 if(mg->mg_virtual == vtbl){
222 break;
223 }
22932438 224 }
8a2e4cdb 225 return mg;
22932438 226}
227
a69b9501 228static SV*
229mop_fetch_attr(pTHX_ SV* const self, SV* const key, I32 const create, CV* const cv){
230 HE* he;
231 if (!SvROK(self)) {
232 croak("can't call %s as a class method", GvNAME(CvGV(cv)));
233 }
234 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
235 croak("object is not a hashref");
236 }
237 if((he = hv_fetch_ent((HV*)SvRV(self), key, create, 0U))){
238 return HeVAL(he);
239 }
240 return NULL;
241}
242static SV*
243mop_delete_attr(pTHX_ SV* const self, SV* const key, CV* const cv){
244 SV* sv;
245 if (!SvROK(self)) {
246 croak("can't call %s as a class method", GvNAME(CvGV(cv)));
247 }
248 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
249 croak("object is not a hashref");
250 }
251 if((sv = hv_delete_ent((HV*)SvRV(self), key, 0, 0U))){
252 return sv;
253 }
254 return NULL;
255}
256
257XS(mop_xs_simple_accessor)
22932438 258{
a69b9501 259 dVAR; dXSARGS;
260 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
261 SV* const key = mg->mg_obj;
262 SV* attr;
263 if(items == 1){ /* reader */
264 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
265 }
266 else if (items == 2){ /* writer */
267 attr = mop_fetch_attr(aTHX_ ST(0), key, TRUE, cv);
268 sv_setsv(attr, ST(1));
22932438 269 }
a69b9501 270 else{
271 croak("expected exactly one or two argument");
272 }
273 ST(0) = attr ? attr : &PL_sv_undef;
274 XSRETURN(1);
22932438 275}
7ec7b950 276
a69b9501 277
384bb6c9 278XS(mop_xs_simple_reader)
7ec7b950 279{
384bb6c9 280 dVAR; dXSARGS;
a69b9501 281 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
8a2e4cdb 282 SV* const key = mg->mg_obj;
a69b9501 283 SV* attr;
384bb6c9 284
285 if (items != 1) {
286 croak("expected exactly one argument");
287 }
288
a69b9501 289 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
290 ST(0) = attr ? attr : &PL_sv_undef;
291 XSRETURN(1);
292}
7ec7b950 293
a69b9501 294XS(mop_xs_simple_writer)
295{
296 dVAR; dXSARGS;
297 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
298 SV* const key = mg->mg_obj;
299 SV* attr;
7ec7b950 300
a69b9501 301 if (items != 2) {
302 croak("expected exactly two argument");
7ec7b950 303 }
304
a69b9501 305 attr = mop_fetch_attr(aTHX_ ST(0), key, TRUE, cv);
306 sv_setsv(attr, ST(1));
307 ST(0) = attr;
308 XSRETURN(1);
309}
310
311XS(mop_xs_simple_clearer)
312{
313 dVAR; dXSARGS;
314 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
315 SV* const key = mg->mg_obj;
316 SV* attr;
317
318 if (items != 1) {
319 croak("expected exactly one argument");
7ec7b950 320 }
7ec7b950 321
a69b9501 322 attr = mop_delete_attr(aTHX_ ST(0), key, cv);
323 ST(0) = attr ? attr : &PL_sv_undef;
324 XSRETURN(1);
325}
326
327
328XS(mop_xs_simple_predicate)
329{
330 dVAR; dXSARGS;
331 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
332 SV* const key = mg->mg_obj;
333 SV* attr;
334
335 if (items != 1) {
336 croak("expected exactly one argument");
7ec7b950 337 }
338
a69b9501 339 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
340 ST(0) = boolSV(attr); /* exists */
7ec7b950 341 XSRETURN(1);
342}
343
a69b9501 344
345XS(mop_xs_simple_predicate_for_metaclass)
346{
347 dVAR; dXSARGS;
348 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
349 SV* const key = mg->mg_obj;
350 SV* attr;
351
352 if (items != 1) {
353 croak("expected exactly one argument");
7ec7b950 354 }
355
a69b9501 356 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
357 ST(0) = boolSV(attr && SvOK(attr)); /* defined */
7ec7b950 358 XSRETURN(1);
359}