Commit | Line | Data |
d846ade3 |
1 | #include "mop.h" |
2 | |
3 | void |
1be56175 |
4 | mop_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 |
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 | int |
e1f52a8a |
79 | mop_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 | */ |
96 | #ifdef isGV_with_GP |
97 | if ( isGV_with_GP(CvGV(coderef)) ) { |
98 | #endif |
99 | *pkg = HvNAME( GvSTASH(CvGV(coderef)) ); |
100 | *name = GvNAME( CvGV(coderef) ); |
101 | #ifdef isGV_with_GP |
102 | } else { |
103 | *pkg = "__UNKNOWN__"; |
104 | *name = "__ANON__"; |
105 | } |
106 | #endif |
107 | |
108 | return 1; |
109 | } |
110 | |
111 | void |
e1f52a8a |
112 | mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud) |
d846ade3 |
113 | { |
114 | HE *he; |
115 | |
116 | (void)hv_iterinit(stash); |
117 | |
118 | if (filter == TYPE_FILTER_NONE) { |
119 | while ( (he = hv_iternext(stash)) ) { |
120 | STRLEN keylen; |
121 | const char *key = HePV(he, keylen); |
122 | if (!cb(key, keylen, HeVAL(he), ud)) { |
123 | return; |
124 | } |
125 | } |
126 | return; |
127 | } |
128 | |
129 | while ( (he = hv_iternext(stash)) ) { |
130 | SV *const gv = HeVAL(he); |
131 | SV *sv = NULL; |
132 | char *key; |
133 | STRLEN keylen; |
134 | char *package; |
d846ade3 |
135 | |
136 | switch( SvTYPE(gv) ) { |
137 | #ifndef SVt_RV |
138 | case SVt_RV: |
139 | #endif |
140 | case SVt_PV: |
141 | case SVt_IV: |
142 | /* expand the gv into a real typeglob if it |
143 | * contains stub functions and we were asked to |
144 | * return CODE symbols */ |
145 | if (filter == TYPE_FILTER_CODE) { |
146 | if (SvROK(gv)) { |
8a2e4cdb |
147 | SV* fq; |
d846ade3 |
148 | /* we don't really care about the length, |
149 | but that's the API */ |
150 | key = HePV(he, keylen); |
151 | package = HvNAME(stash); |
efc98200 |
152 | fq = sv_2mortal(newSVpvf("%s::%s", package, key)); |
d846ade3 |
153 | sv = (SV *)get_cv(SvPV_nolen(fq), 0); |
154 | break; |
155 | } |
156 | |
157 | key = HePV(he, keylen); |
158 | gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI); |
159 | } |
160 | /* fall through */ |
161 | case SVt_PVGV: |
162 | switch (filter) { |
163 | case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break; |
164 | case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break; |
165 | case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break; |
166 | case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break; |
167 | case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break; |
168 | default: |
169 | croak("Unknown type"); |
170 | } |
171 | break; |
172 | default: |
173 | continue; |
174 | } |
175 | |
176 | if (sv) { |
177 | const char *key = HePV(he, keylen); |
178 | if (!cb(key, keylen, sv, ud)) { |
179 | return; |
180 | } |
181 | } |
182 | } |
183 | } |
184 | |
185 | static bool |
186 | collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud) |
187 | { |
188 | HV *hash = (HV *)ud; |
189 | |
190 | if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) { |
191 | croak("failed to store symbol ref"); |
192 | } |
193 | |
194 | return TRUE; |
195 | } |
196 | |
197 | HV * |
e1f52a8a |
198 | mop_get_all_package_symbols (HV *stash, type_filter_t filter) |
d846ade3 |
199 | { |
200 | HV *ret = newHV (); |
e1f52a8a |
201 | mop_get_package_symbols (stash, filter, collect_all_symbols, ret); |
d846ade3 |
202 | return ret; |
203 | } |
22932438 |
204 | |
8a2e4cdb |
205 | static MGVTBL mop_accessor_vtbl; /* the MAGIC identity */ |
22932438 |
206 | |
8a2e4cdb |
207 | void |
208 | mop_install_simple_reader(const char* const fq_name, const char* const key, const int accessor_type){ |
209 | CV* const xsub = newXS((char*)fq_name, mop_xs_simple_reader, __FILE__); |
210 | SV* const keysv = newSVpvn_share(key, strlen(key), 0U); |
22932438 |
211 | |
8a2e4cdb |
212 | sv_magicext((SV*)xsub, keysv, PERL_MAGIC_ext, &mop_accessor_vtbl, NULL, 0); |
213 | SvREFCNT_dec(keysv); /* sv_magicext() increases refcnt in mg_obj */ |
214 | |
215 | CvXSUBANY(xsub).any_i32 = accessor_type; |
22932438 |
216 | } |
217 | |
8a2e4cdb |
218 | static MAGIC* |
219 | mop_mg_find_by_vtbl(SV* const sv, const MGVTBL* const vtbl){ |
220 | MAGIC* mg; |
221 | |
222 | assert(sv != NULL); |
223 | for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){ |
224 | if(mg->mg_virtual == vtbl){ |
225 | break; |
226 | } |
22932438 |
227 | } |
8a2e4cdb |
228 | return mg; |
22932438 |
229 | } |
7ec7b950 |
230 | |
384bb6c9 |
231 | XS(mop_xs_simple_reader) |
7ec7b950 |
232 | { |
384bb6c9 |
233 | dVAR; dXSARGS; |
8a2e4cdb |
234 | MAGIC* const mg = mop_mg_find_by_vtbl((SV*)cv, &mop_accessor_vtbl); |
235 | SV* const key = mg->mg_obj; |
7ec7b950 |
236 | register HE *he; |
384bb6c9 |
237 | SV *self; |
8a2e4cdb |
238 | SV *retval; |
384bb6c9 |
239 | |
240 | if (items != 1) { |
241 | croak("expected exactly one argument"); |
242 | } |
243 | |
244 | self = ST(0); |
7ec7b950 |
245 | |
246 | if (!SvROK(self)) { |
8a2e4cdb |
247 | croak("can't call %s as a class method", GvNAME(CvGV(cv))); |
7ec7b950 |
248 | } |
249 | |
250 | if (SvTYPE(SvRV(self)) != SVt_PVHV) { |
251 | croak("object is not a hashref"); |
252 | } |
253 | |
8a2e4cdb |
254 | if ((he = hv_fetch_ent((HV *)SvRV(self), key, 0, 0U))) { |
255 | switch(XSANY.any_i32){ |
256 | case SIMPLE_READER: |
257 | retval = HeVAL(he); |
258 | break; |
259 | case SIMPLE_PREDICATE: |
260 | retval = boolSV(SvOK(HeVAL(he))); |
261 | break; |
262 | default: |
263 | croak("panic: not reached"); |
264 | retval = NULL; /* -W */ |
265 | } |
7ec7b950 |
266 | } |
384bb6c9 |
267 | else { |
8a2e4cdb |
268 | retval = &PL_sv_undef; |
7ec7b950 |
269 | } |
270 | |
8a2e4cdb |
271 | ST(0) = retval; |
7ec7b950 |
272 | XSRETURN(1); |
273 | } |