mop.c

filter: Statementstmt 79.7% 75 / 94 Branchbran 51.2% 42 / 82 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.4% 117 / 176 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/ToInstance.xs ↑ Summary xs/MOP.xs »
1 #include "mop.h"
2 #include "ppport.h"
3  
4 void
5 5508 mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
6 {
7 5508 dSP;
8 5508 ▶ PUSHMARK(mark);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(mark); 0 5508 5508
9 5508 (*subaddr)(aTHX_ cv);
10 5508 PUTBACK;
11 5508 }
12  
13 #if PERL_VERSION_GE(5,10,0)
14 UV
15 497770 mop_check_package_cache_flag (pTHX_ HV *stash)
16 {
17 assert(SvTYPE(stash) == SVt_PVHV);
18  
19 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
20 * however the perl core doesn't make it easy for us. It doesn't provide an
21 * api that just does what we want.
22 *
23 * However, we know that the information we want is, inside the core,
24 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
25 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
26 * which is not public and only available inside the core, as the mro
27 * interface as well as the structure returned by mro_meta_init isn't
28 * considered to be stable yet.
29 *
30 * Perl_mro_meta_init isn't declared static, so we could just define it
31 * ourselfs if perls headers don't do that for us, except that won't work
32 * on platforms where symbols need to be explicitly exported when linking
33 * shared libraries.
34 *
35 * So our, hopefully temporary, solution is to be even more evil and
36 * basically reimplement HvMROMETA in a very fragile way that'll blow up
37 * when the relevant parts of the mro implementation in core change.
38 *
39 * :-(
40 *
41 */
42  
43 497770 return HvAUX(stash)->xhv_mro_meta
44 497695 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
45 497770 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
: 0; 497695 75 497770
46 }
47  
48 #else /* pre 5.10.0 */
49  
50 UV
51 mop_check_package_cache_flag (pTHX_ HV *stash)
52 {
53 PERL_UNUSED_ARG(stash);
54 assert(SvTYPE(stash) == SVt_PVHV);
55  
56 return PL_sub_generation;
57 }
58 #endif
59  
60 SV *
61 44333 mop_call0 (pTHX_ SV *const self, SV *const method)
62 {
63 44333 dSP;
64 SV *ret;
65  
66 44333 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 44333 44333
67 44333 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 44333 44333
68 44333 PUTBACK;
69  
70 44333 call_sv(method, G_SCALAR | G_METHOD);
71  
72 44333 SPAGAIN;
73 44333 ret = POPs;
74 44333 PUTBACK;
75  
76 44333 return ret;
77 }
78  
79 int
80 238188 mop_get_code_info (SV *coderef, char **pkg, char **name)
81 {
82 238188 ▶ if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 238188 0 238188
if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 238188 0 238188
if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 0 238188 238188
83 0 return 0;
84 }
85  
86 238188 coderef = SvRV(coderef);
87  
88 /* sub is still being compiled */
89 238188 ▶ if (!CvGV(coderef)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!CvGV(coderef)) { 0 238188 238188
90 0 return 0;
91 }
92  
93 /* I think this only gets triggered with a mangled coderef, but if
94 we hit it without the guard, we segfault. The slightly odd return
95 value strikes me as an improvement (mst)
96 */
97  
98 238188 ▶ if ( isGV_with_GP(CvGV(coderef)) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( isGV_with_GP(CvGV(coderef)) ) { 238188 0 238188
if ( isGV_with_GP(CvGV(coderef)) ) { 0 238188 238188
if ( isGV_with_GP(CvGV(coderef)) ) { 0 0 0
99 238188 GV *gv = CvGV(coderef);
100 238188 ▶ HV *stash = GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef);
Branch: 1 branch
BranchTrueFalseTotal
HV *stash = GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef); 238187 1 238188
101  
102 238188 ▶ *pkg = stash ? HvNAME(stash) : "__UNKNOWN__";
Branch: 7 branches
BranchTrueFalseTotal
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__"; 238187 1 238188
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__"; 238187 0 238187
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__"; 238187 0 238187
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__"; 0 238187 238187
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__"; 0 0 0
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__"; 238187 0 238187
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__"; 0 238187 238187
103 238188 *name = GvNAME( CvGV(coderef) );
104 } else {
105 0 *pkg = "__UNKNOWN__";
106 0 *name = "__ANON__";
107 }
108  
109 238188 return 1;
110 }
111  
112 /* XXX: eventually this should just use the implementation in Package::Stash */
113 void
114 44585 mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
115 {
116 HE *he;
117  
118 44585 (void)hv_iterinit(stash);
119  
120 44585 ▶ if (filter == TYPE_FILTER_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (filter == TYPE_FILTER_NONE) { 0 44585 44585
121 0 ▶ while ( (he = hv_iternext(stash)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (he = hv_iternext(stash)) ) { 0 0 0
122 STRLEN keylen;
123 0 ▶ const char *key = HePV(he, keylen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, keylen); 0 0 0
124 0 ▶ if (!cb(key, keylen, HeVAL(he), ud)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cb(key, keylen, HeVAL(he), ud)) { 0 0 0
125 0 return;
126 }
127 }
128 0 return;
129 }
130  
131 794489 ▶ while ( (he = hv_iternext(stash)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (he = hv_iternext(stash)) ) { 749904 44585 794489
132 749904 GV * const gv = (GV*)HeVAL(he);
133 STRLEN keylen;
134 749904 ▶ const char * const key = HePV(he, keylen);
Branch: 1 branch
BranchTrueFalseTotal
const char * const key = HePV(he, keylen); 0 749904 749904
135 749904 SV *sv = NULL;
136  
137 749904 ▶ if(isGV(gv)){
Branch: 1 branch
BranchTrueFalseTotal
if(isGV(gv)){ 748641 1263 749904
138 748641 switch (filter) {
139 748641 ▶ case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
Branch: 1 branch
BranchTrueFalseTotal
case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break; 678681 69960 748641
140 0 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
141 0 ▶ case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
Branch: 4 branches
BranchTrueFalseTotal
case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break; 0 0 0
case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break; 0 0 0
case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break; 0 0 0
case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break; 0 0 0
142 0 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
143 0 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
144 0 default:
145 0 croak("Unknown type");
146 }
147 }
148 /* expand the gv into a real typeglob if it
149 * contains stub functions or constants and we
150 * were asked to return CODE references */
151 1263 ▶ else if (filter == TYPE_FILTER_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (filter == TYPE_FILTER_CODE) { 1263 0 1263
152 1263 gv_init(gv, stash, key, keylen, GV_ADDMULTI);
153 1263 sv = (SV *)GvCV(gv);
154 }
155  
156 749904 ▶ if (sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv) { 510488 239416 749904
157 510488 ▶ if (!cb(key, keylen, sv, ud)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cb(key, keylen, sv, ud)) { 0 510488 510488
158 0 return;
159 }
160 }
161 }
162 }
163  
164 static bool
165 510488 collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
166 {
167 510488 HV *hash = (HV *)ud;
168  
169 510488 ▶ if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) { 0 510488 510488
170 0 croak("failed to store symbol ref");
171 }
172  
173 510488 return TRUE;
174 }
175  
176 HV *
177 44585 mop_get_all_package_symbols (HV *stash, type_filter_t filter)
178 {
179 44585 HV *ret = newHV ();
180 44585 mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
181 44585 return ret;
182 }
183  
184 #define DECLARE_KEY(name) { #name, #name, NULL, 0 }
185 #define DECLARE_KEY_WITH_VALUE(name, value) { #name, value, NULL, 0 }
186  
187 /* the order of these has to match with those in mop.h */
188 static struct {
189 const char *name;
190 const char *value;
191 SV *key;
192 U32 hash;
193 } prehashed_keys[key_last] = {
194 DECLARE_KEY(_expected_method_class),
195 DECLARE_KEY(ISA),
196 DECLARE_KEY(VERSION),
197 DECLARE_KEY(accessor),
198 DECLARE_KEY(associated_class),
199 DECLARE_KEY(associated_metaclass),
200 DECLARE_KEY(associated_methods),
201 DECLARE_KEY(attribute_metaclass),
202 DECLARE_KEY(attributes),
203 DECLARE_KEY(body),
204 DECLARE_KEY(builder),
205 DECLARE_KEY(clearer),
206 DECLARE_KEY(constructor_class),
207 DECLARE_KEY(constructor_name),
208 DECLARE_KEY(definition_context),
209 DECLARE_KEY(destructor_class),
210 DECLARE_KEY(immutable_trait),
211 DECLARE_KEY(init_arg),
212 DECLARE_KEY(initializer),
213 DECLARE_KEY(insertion_order),
214 DECLARE_KEY(instance_metaclass),
215 DECLARE_KEY(is_inline),
216 DECLARE_KEY(method_metaclass),
217 DECLARE_KEY(methods),
218 DECLARE_KEY(name),
219 DECLARE_KEY(package),
220 DECLARE_KEY(package_name),
221 DECLARE_KEY(predicate),
222 DECLARE_KEY(reader),
223 DECLARE_KEY(wrapped_method_metaclass),
224 DECLARE_KEY(writer),
225 DECLARE_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag"),
226 DECLARE_KEY_WITH_VALUE(_version, "-version"),
227 DECLARE_KEY(operator)
228 };
229  
230 SV *
231 1404299 mop_prehashed_key_for (mop_prehashed_key_t key)
232 {
233 1404299 return prehashed_keys[key].key;
234 }
235  
236 U32
237 1359966 mop_prehashed_hash_for (mop_prehashed_key_t key)
238 {
239 1359966 return prehashed_keys[key].hash;
240 }
241  
242 void
243 459 mop_prehash_keys ()
244 {
245 int i;
246 16065 ▶ for (i = 0; i < key_last; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < key_last; i++) { 15606 459 16065
247 15606 const char *value = prehashed_keys[i].value;
248 15606 prehashed_keys[i].key = newSVpv(value, strlen(value));
249 15606 ▶ PERL_HASH(prehashed_keys[i].hash, value, strlen(value));
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(prehashed_keys[i].hash, value, strlen(value)); 15606 0 15606
250 }
251 459 }
252  
253 6211257 XS_EXTERNAL(mop_xs_simple_reader)
254 {
255 dVAR;
256 6211257 dXSARGS;
257 register HE *he;
258 6211257 mop_prehashed_key_t key = (mop_prehashed_key_t)CvXSUBANY(cv).any_i32;
259 SV *self;
260  
261 6211257 ▶ if (items != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) { 0 6211257 6211257
262 0 croak("expected exactly one argument");
263 }
264  
265 6211257 self = ST(0);
266  
267 6211257 ▶ if (!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(self)) { 18 6211239 6211257
268 18 croak("can't call %s as a class method", prehashed_keys[key].name);
269 }
270  
271 6211239 ▶ if (SvTYPE(SvRV(self)) != SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(self)) != SVt_PVHV) { 0 6211239 6211239
272 0 croak("object is not a hashref");
273 }
274  
275 6211239 ▶ if ((he = hv_fetch_ent((HV *)SvRV(self), prehashed_keys[key].key, 0, prehashed_keys[key].hash))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((he = hv_fetch_ent((HV *)SvRV(self), prehashed_keys[key].key, 0, prehashed_keys[key].hash))) { 6210010 1229 6211239
276 6210010 ST(0) = HeVAL(he);
277 }
278 else {
279 1229 ST(0) = &PL_sv_undef;
280 }
281  
282 6211239 XSRETURN(1);
283 }
284  
« xs/ToInstance.xs ↑ Summary xs/MOP.xs »