42993354cea15db62a99de936609c62b7403cf5e
[gitmo/Class-MOP.git] / mop.c
1 #include "mop.h"
2
3 void
4 mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
5 {
6     dSP;
7     PUSHMARK(mark);
8     (*subaddr)(aTHX_ cv);
9     PUTBACK;
10 }
11
12 #if PERL_BCDVERSION >= 0x5010000
13 UV
14 mop_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
49 UV
50 mop_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
59 SV *
60 mop_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
78 SV *
79 mop_call1 (pTHX_ SV *const self, SV *const method, SV* const arg1)
80 {
81     dSP;
82     SV *ret;
83
84     PUSHMARK(SP);
85     EXTEND(SP, 2);
86     PUSHs(self);
87     PUSHs(arg1);
88     PUTBACK;
89
90     call_sv(method, G_SCALAR | G_METHOD);
91
92     SPAGAIN;
93     ret = POPs;
94     PUTBACK;
95
96     return ret;
97 }
98
99
100 int
101 mop_get_code_info (SV *coderef, char **pkg, char **name)
102 {
103     if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
104         return 0;
105     }
106
107     coderef = SvRV(coderef);
108
109     /* sub is still being compiled */
110     if (!CvGV(coderef)) {
111         return 0;
112     }
113
114     /* I think this only gets triggered with a mangled coderef, but if
115        we hit it without the guard, we segfault. The slightly odd return
116        value strikes me as an improvement (mst)
117     */
118
119     if ( isGV_with_GP(CvGV(coderef)) ) {
120         GV *gv   = CvGV(coderef);
121         *pkg     = HvNAME( GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef) );
122         *name    = GvNAME( CvGV(coderef) );
123     } else {
124         *pkg     = "__UNKNOWN__";
125         *name    = "__ANON__";
126     }
127
128     return 1;
129 }
130
131 void
132 mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
133 {
134     dTHX;
135     HE *he;
136
137     (void)hv_iterinit(stash);
138
139     if (filter == TYPE_FILTER_NONE) {
140         while ( (he = hv_iternext(stash)) ) {
141             STRLEN keylen;
142             const char *key = HePV(he, keylen);
143             if (!cb(key, keylen, HeVAL(he), ud)) {
144                 return;
145             }
146         }
147         return;
148     }
149
150     while ( (he = hv_iternext(stash)) ) {
151         SV *const gv = HeVAL(he);
152         SV *sv = NULL;
153         char *key;
154         STRLEN keylen;
155         char *package;
156
157         switch( SvTYPE(gv) ) {
158 #ifndef SVt_RV
159             case SVt_RV:
160 #endif
161             case SVt_PV:
162             case SVt_IV:
163                 /* expand the gv into a real typeglob if it
164                  * contains stub functions and we were asked to
165                  * return CODE symbols */
166                 if (filter == TYPE_FILTER_CODE) {
167                     if (SvROK(gv)) {
168                         SV* fq;
169                         /* we don't really care about the length,
170                            but that's the API */
171                         key = HePV(he, keylen);
172                         package = HvNAME(stash);
173                         fq = sv_2mortal(newSVpvf("%s::%s", package, key));
174                         sv = (SV *)get_cv(SvPV_nolen(fq), 0);
175                         break;
176                     }
177
178                     key = HePV(he, keylen);
179                     gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
180                 }
181                 /* fall through */
182             case SVt_PVGV:
183                 switch (filter) {
184                     case TYPE_FILTER_CODE:   sv = (SV *)GvCVu(gv); break;
185                     case TYPE_FILTER_ARRAY:  sv = (SV *)GvAV(gv);  break;
186                     case TYPE_FILTER_IO:     sv = (SV *)GvIO(gv);  break;
187                     case TYPE_FILTER_HASH:   sv = (SV *)GvHV(gv);  break;
188                     case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv);  break;
189                     default:
190                         croak("Unknown type");
191                 }
192                 break;
193             default:
194                 continue;
195         }
196
197         if (sv) {
198             const char *key = HePV(he, keylen);
199             if (!cb(key, keylen, sv, ud)) {
200                 return;
201             }
202         }
203     }
204 }
205
206 static bool
207 collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
208 {
209     dTHX;
210     HV *hash = (HV *)ud;
211
212     if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
213         croak("failed to store symbol ref");
214     }
215
216     return TRUE;
217 }
218
219 HV *
220 mop_get_all_package_symbols (HV *stash, type_filter_t filter)
221 {
222     dTHX;
223     HV *ret = newHV ();
224     mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
225     return ret;
226 }
227
228
229 MAGIC*
230 mop_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags){
231     MAGIC* mg;
232
233     assert(sv != NULL);
234     for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
235         if(mg->mg_virtual == vtbl){
236             return mg;
237         }
238     }
239
240     if(flags & MOPf_DIE_ON_FAIL){
241         croak("mop_mg_find: no MAGIC found for %"SVf, sv_2mortal(newRV_inc(sv)));
242     }
243     return NULL;
244 }