Commit | Line | Data |
e7b69038 |
1 | /* There's a lot of cases of doubled parens in here like this: |
2 | |
3 | while ( (he = ...) ) { |
4 | |
5 | This shuts up warnings from gcc -Wall |
6 | */ |
7 | |
e0e4674a |
8 | #include "EXTERN.h" |
9 | #include "perl.h" |
10 | #include "XSUB.h" |
15273f3c |
11 | |
599791aa |
12 | #define NEED_sv_2pv_flags |
15273f3c |
13 | #define NEED_sv_2pv_nolen |
b0e94057 |
14 | #include "ppport.h" |
e0e4674a |
15 | |
cc856b56 |
16 | SV *key_name; |
17 | U32 hash_name; |
18 | |
19 | SV *key_package; |
20 | U32 hash_package; |
21 | |
da88f307 |
22 | SV *key_package_name; |
23 | U32 hash_package_name; |
24 | |
cc856b56 |
25 | SV *key_body; |
26 | U32 hash_body; |
27 | |
c686d0ed |
28 | SV *key_package_cache_flag; |
29 | U32 hash_package_cache_flag; |
30 | |
31 | SV *key_methods; |
32 | U32 hash_methods; |
33 | |
00c93f7a |
34 | SV *method_metaclass; |
35 | SV *associated_metaclass; |
36 | SV *wrap; |
c94afdc4 |
37 | |
38 | |
39 | #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash) |
40 | #ifdef HvMROMETA /* 5.10.0 */ |
41 | |
42 | #ifndef mro_meta_init |
43 | #define mro_meta_init(stash) Perl_mro_meta_init(aTHX_ stash) /* used in HvMROMETA macro */ |
44 | #endif /* !mro_meta_init */ |
45 | |
46 | static UV |
47 | mop_check_package_cache_flag(pTHX_ HV* stash) { |
48 | assert(SvTYPE(stash) == SVt_PVHV); |
49 | |
50 | return HvMROMETA(stash)->pkg_gen; /* mro::get_pkg_gen($pkg) */ |
51 | } |
52 | |
53 | #else /* pre 5.10.0 */ |
54 | |
55 | static UV |
00c93f7a |
56 | mop_check_package_cache_flag(pTHX_ HV *stash) { |
c94afdc4 |
57 | PERL_UNUSED_ARG(stash); |
58 | assert(SvTYPE(stash) == SVt_PVHV); |
59 | |
60 | return PL_sub_generation; |
61 | } |
62 | #endif |
63 | |
64 | #define call0(s, m) mop_call0(aTHX_ s, m) |
00c93f7a |
65 | static SV * |
66 | mop_call0(pTHX_ SV *const self, SV *const method) { |
c94afdc4 |
67 | dSP; |
00c93f7a |
68 | SV *ret; |
c94afdc4 |
69 | |
70 | PUSHMARK(SP); |
71 | XPUSHs(self); |
72 | PUTBACK; |
73 | |
74 | call_sv(method, G_SCALAR | G_METHOD); |
75 | |
76 | SPAGAIN; |
77 | ret = POPs; |
78 | PUTBACK; |
79 | |
80 | return ret; |
81 | } |
82 | |
ee82a43e |
83 | static int |
0e0b5b8d |
84 | get_code_info (SV *coderef, char **pkg, char **name) |
85 | { |
86 | if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { |
87 | return 0; |
88 | } |
6bd19edb |
89 | |
0e0b5b8d |
90 | coderef = SvRV(coderef); |
91 | /* I think this only gets triggered with a mangled coderef, but if |
92 | we hit it without the guard, we segfault. The slightly odd return |
93 | value strikes me as an improvement (mst) |
94 | */ |
95 | #ifdef isGV_with_GP |
96 | if ( isGV_with_GP(CvGV(coderef)) ) { |
cb0ec494 |
97 | #endif |
0e0b5b8d |
98 | *pkg = HvNAME( GvSTASH(CvGV(coderef)) ); |
99 | *name = GvNAME( CvGV(coderef) ); |
100 | #ifdef isGV_with_GP |
101 | } else { |
102 | *pkg = "__UNKNOWN__"; |
103 | *name = "__ANON__"; |
c94afdc4 |
104 | } |
0e0b5b8d |
105 | #endif |
106 | |
107 | return 1; |
c94afdc4 |
108 | } |
109 | |
b49ee39f |
110 | typedef enum { |
111 | TYPE_FILTER_NONE, |
112 | TYPE_FILTER_CODE, |
113 | TYPE_FILTER_ARRAY, |
114 | TYPE_FILTER_IO, |
115 | TYPE_FILTER_HASH, |
116 | TYPE_FILTER_SCALAR, |
117 | } type_filter_t; |
118 | |
119 | static HV * |
120 | get_all_package_symbols(HV *stash, type_filter_t filter) |
121 | { |
122 | HE *he; |
123 | HV *ret = newHV(); |
124 | |
125 | (void)hv_iterinit(stash); |
126 | |
127 | if (filter == TYPE_FILTER_NONE) { |
128 | while ( (he = hv_iternext(stash)) ) { |
129 | STRLEN keylen; |
130 | char *key = HePV(he, keylen); |
131 | hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0); |
132 | } |
133 | |
134 | return ret; |
135 | } |
136 | |
137 | while ( (he = hv_iternext(stash)) ) { |
138 | SV *const gv = HeVAL(he); |
139 | SV *sv = NULL; |
140 | char *key; |
141 | STRLEN keylen; |
142 | char *package; |
143 | SV *fq; |
144 | |
145 | switch( SvTYPE(gv) ) { |
146 | #ifndef SVt_RV |
147 | case SVt_RV: |
148 | #endif |
149 | case SVt_PV: |
150 | case SVt_IV: |
151 | /* expand the gv into a real typeglob if it |
152 | * contains stub functions and we were asked to |
153 | * return CODE symbols */ |
154 | if (filter == TYPE_FILTER_CODE) { |
155 | if (SvROK(gv)) { |
156 | /* we don't really care about the length, |
157 | but that's the API */ |
158 | key = HePV(he, keylen); |
159 | package = HvNAME(stash); |
160 | fq = newSVpvf("%s::%s", package, key); |
161 | sv = (SV *)get_cv(SvPV_nolen(fq), 0); |
162 | break; |
163 | } |
164 | |
165 | key = HePV(he, keylen); |
166 | gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI); |
167 | } |
168 | /* fall through */ |
169 | case SVt_PVGV: |
170 | switch (filter) { |
171 | case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break; |
172 | case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break; |
173 | case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break; |
174 | case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break; |
175 | case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break; |
176 | default: |
177 | croak("Unknown type"); |
178 | } |
179 | break; |
180 | default: |
181 | continue; |
182 | } |
183 | |
184 | if (sv) { |
185 | char *key = HePV(he, keylen); |
186 | hv_store(ret, key, keylen, newRV_inc(sv), 0); |
187 | } |
188 | } |
189 | |
190 | return ret; |
191 | } |
c94afdc4 |
192 | |
0e0b5b8d |
193 | |
194 | static void |
00c93f7a |
195 | mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) { |
196 | const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */ |
197 | SV *method_metaclass_name; |
198 | char *method_name; |
0e0b5b8d |
199 | I32 method_name_len; |
00c93f7a |
200 | SV *coderef; |
201 | HV *symbols; |
0e0b5b8d |
202 | dSP; |
203 | |
204 | symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE); |
205 | |
206 | (void)hv_iterinit(symbols); |
207 | while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) { |
00c93f7a |
208 | CV *cv = (CV *)SvRV(coderef); |
209 | char *cvpkg_name; |
210 | char *cv_name; |
211 | SV *method_slot; |
212 | SV *method_object; |
0e0b5b8d |
213 | |
214 | if (!get_code_info(coderef, &cvpkg_name, &cv_name)) { |
215 | continue; |
216 | } |
217 | |
218 | /* this checks to see that the subroutine is actually from our package */ |
219 | if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) { |
220 | if ( strNE(cvpkg_name, class_name_pv) ) { |
221 | continue; |
222 | } |
223 | } |
224 | |
225 | method_slot = *hv_fetch(map, method_name, method_name_len, TRUE); |
226 | if ( SvOK(method_slot) ) { |
227 | SV* const body = call0(method_slot, key_body); /* $method_object->body() */ |
00c93f7a |
228 | if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) { |
0e0b5b8d |
229 | continue; |
230 | } |
231 | } |
232 | |
233 | method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */ |
234 | |
235 | /* |
236 | $method_object = $method_metaclass->wrap( |
237 | $cv, |
238 | associated_metaclass => $self, |
239 | package_name => $class_name, |
240 | name => $method_name |
241 | ); |
242 | */ |
243 | ENTER; |
244 | SAVETMPS; |
245 | |
246 | PUSHMARK(SP); |
247 | EXTEND(SP, 8); |
248 | PUSHs(method_metaclass_name); /* invocant */ |
00c93f7a |
249 | mPUSHs(newRV_inc((SV *)cv)); |
0e0b5b8d |
250 | PUSHs(associated_metaclass); |
251 | PUSHs(self); |
252 | PUSHs(key_package_name); |
253 | PUSHs(class_name); |
254 | PUSHs(key_name); |
255 | mPUSHs(newSVpv(method_name, method_name_len)); |
256 | PUTBACK; |
257 | |
258 | call_sv(wrap, G_SCALAR | G_METHOD); |
259 | SPAGAIN; |
260 | method_object = POPs; |
261 | PUTBACK; |
262 | /* $map->{$method_name} = $method_object */ |
263 | sv_setsv(method_slot, method_object); |
264 | |
265 | FREETMPS; |
266 | LEAVE; |
267 | } |
268 | } |
269 | |
e0e4674a |
270 | /* |
e0e4674a |
271 | get_code_info: |
272 | Pass in a coderef, returns: |
273 | [ $pkg_name, $coderef_name ] ie: |
274 | [ 'Foo::Bar', 'new' ] |
275 | */ |
276 | |
277 | MODULE = Class::MOP PACKAGE = Class::MOP |
278 | |
cc856b56 |
279 | BOOT: |
280 | key_name = newSVpvs("name"); |
281 | key_body = newSVpvs("body"); |
282 | key_package = newSVpvs("package"); |
da88f307 |
283 | key_package_name = newSVpvs("package_name"); |
c686d0ed |
284 | key_package_cache_flag = newSVpvs("_package_cache_flag"); |
285 | key_methods = newSVpvs("methods"); |
cc856b56 |
286 | |
287 | PERL_HASH(hash_name, "name", 4); |
288 | PERL_HASH(hash_body, "body", 4); |
289 | PERL_HASH(hash_package, "package", 7); |
da88f307 |
290 | PERL_HASH(hash_package_name, "package_name", 12); |
c686d0ed |
291 | PERL_HASH(hash_package_cache_flag, "_package_cache_flag", 19); |
292 | PERL_HASH(hash_methods, "methods", 7); |
cc856b56 |
293 | |
c94afdc4 |
294 | method_metaclass = newSVpvs("method_metaclass"); |
295 | wrap = newSVpvs("wrap"); |
296 | associated_metaclass = newSVpvs("associated_metaclass"); |
297 | |
cc856b56 |
298 | |
d7bf3478 |
299 | PROTOTYPES: ENABLE |
300 | |
cc856b56 |
301 | |
e0e4674a |
302 | void |
303 | get_code_info(coderef) |
00c93f7a |
304 | SV *coderef |
0e0b5b8d |
305 | PREINIT: |
306 | char* pkg = NULL; |
307 | char* name = NULL; |
308 | PPCODE: |
309 | if (get_code_info(coderef, &pkg, &name)) { |
310 | EXTEND(SP, 2); |
311 | PUSHs(newSVpv(pkg, 0)); |
312 | PUSHs(newSVpv(name, 0)); |
313 | } |
e0e4674a |
314 | |
15273f3c |
315 | |
316 | MODULE = Class::MOP PACKAGE = Class::MOP::Package |
317 | |
318 | void |
b49ee39f |
319 | get_all_package_symbols(self, filter=TYPE_FILTER_NONE) |
cc856b56 |
320 | SV *self |
b49ee39f |
321 | type_filter_t filter |
15273f3c |
322 | PROTOTYPE: $;$ |
323 | PREINIT: |
cc856b56 |
324 | HV *stash = NULL; |
b49ee39f |
325 | HV *symbols = NULL; |
75705e60 |
326 | register HE *he; |
15273f3c |
327 | PPCODE: |
a7f711e5 |
328 | if ( ! SvROK(self) ) { |
988fb42e |
329 | die("Cannot call get_all_package_symbols as a class method"); |
330 | } |
15273f3c |
331 | |
b49ee39f |
332 | if (GIMME_V == G_VOID) { |
333 | XSRETURN_EMPTY; |
334 | } |
335 | |
15273f3c |
336 | |
15273f3c |
337 | PUTBACK; |
338 | |
b49ee39f |
339 | if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) { |
340 | stash = gv_stashsv(HeVAL(he), 0); |
341 | } |
15273f3c |
342 | |
15273f3c |
343 | |
b49ee39f |
344 | if (!stash) { |
6ccb3af5 |
345 | switch (GIMME_V) { |
346 | case G_SCALAR: XSRETURN_UNDEF; break; |
347 | case G_ARRAY: XSRETURN_EMPTY; break; |
348 | } |
b49ee39f |
349 | } |
15273f3c |
350 | |
b49ee39f |
351 | symbols = get_all_package_symbols(stash, filter); |
15273f3c |
352 | |
6ccb3af5 |
353 | switch (GIMME_V) { |
354 | case G_SCALAR: |
355 | PUSHs(sv_2mortal(newRV_inc((SV *)symbols))); |
356 | break; |
357 | case G_ARRAY: |
358 | warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead."); |
359 | |
360 | EXTEND(SP, HvKEYS(symbols) * 2); |
361 | |
362 | while ((he = hv_iternext(symbols))) { |
363 | PUSHs(hv_iterkeysv(he)); |
364 | PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he)))); |
365 | } |
366 | |
367 | break; |
368 | default: |
369 | break; |
15273f3c |
370 | } |
371 | |
b49ee39f |
372 | SvREFCNT_dec((SV *)symbols); |
373 | |
e2c189ae |
374 | void |
cc856b56 |
375 | name(self) |
376 | SV *self |
377 | PREINIT: |
378 | register HE *he; |
379 | PPCODE: |
a7f711e5 |
380 | if ( ! SvROK(self) ) { |
988fb42e |
381 | die("Cannot call name as a class method"); |
382 | } |
383 | |
e7b69038 |
384 | if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) |
cc856b56 |
385 | XPUSHs(HeVAL(he)); |
386 | else |
387 | ST(0) = &PL_sv_undef; |
388 | |
dcbfe027 |
389 | MODULE = Class::MOP PACKAGE = Class::MOP::Attribute |
cc856b56 |
390 | |
e2c189ae |
391 | void |
cc856b56 |
392 | name(self) |
393 | SV *self |
394 | PREINIT: |
395 | register HE *he; |
396 | PPCODE: |
a7f711e5 |
397 | if ( ! SvROK(self) ) { |
988fb42e |
398 | die("Cannot call name as a class method"); |
399 | } |
400 | |
e7b69038 |
401 | if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) ) |
cc856b56 |
402 | XPUSHs(HeVAL(he)); |
403 | else |
404 | ST(0) = &PL_sv_undef; |
405 | |
dcbfe027 |
406 | MODULE = Class::MOP PACKAGE = Class::MOP::Method |
cc856b56 |
407 | |
e2c189ae |
408 | void |
da88f307 |
409 | name(self) |
410 | SV *self |
411 | PREINIT: |
412 | register HE *he; |
413 | PPCODE: |
a7f711e5 |
414 | if ( ! SvROK(self) ) { |
da88f307 |
415 | die("Cannot call name as a class method"); |
416 | } |
417 | |
e7b69038 |
418 | if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) ) |
da88f307 |
419 | XPUSHs(HeVAL(he)); |
420 | else |
421 | ST(0) = &PL_sv_undef; |
422 | |
e2c189ae |
423 | void |
da88f307 |
424 | package_name(self) |
425 | SV *self |
426 | PREINIT: |
427 | register HE *he; |
428 | PPCODE: |
a7f711e5 |
429 | if ( ! SvROK(self) ) { |
da88f307 |
430 | die("Cannot call package_name as a class method"); |
431 | } |
432 | |
e7b69038 |
433 | if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) ) |
da88f307 |
434 | XPUSHs(HeVAL(he)); |
435 | else |
436 | ST(0) = &PL_sv_undef; |
437 | |
e2c189ae |
438 | void |
cc856b56 |
439 | body(self) |
440 | SV *self |
441 | PREINIT: |
442 | register HE *he; |
443 | PPCODE: |
a7f711e5 |
444 | if ( ! SvROK(self) ) { |
da88f307 |
445 | die("Cannot call body as a class method"); |
446 | } |
447 | |
e7b69038 |
448 | if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) ) |
cc856b56 |
449 | XPUSHs(HeVAL(he)); |
450 | else |
451 | ST(0) = &PL_sv_undef; |
c94afdc4 |
452 | |
453 | |
454 | MODULE = Class::MOP PACKAGE = Class::MOP::Class |
455 | |
456 | void |
457 | get_method_map(self) |
00c93f7a |
458 | SV *self |
c200a770 |
459 | PREINIT: |
c686d0ed |
460 | HV *const obj = (HV *)SvRV(self); |
461 | SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) ); |
462 | HV *const stash = gv_stashsv(class_name, 0); |
c200a770 |
463 | UV const current = check_package_cache_flag(stash); |
c686d0ed |
464 | SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag)); |
465 | SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods)); |
c200a770 |
466 | PPCODE: |
c200a770 |
467 | /* in $self->{methods} does not yet exist (or got deleted) */ |
c686d0ed |
468 | if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) { |
00c93f7a |
469 | SV *new_map_ref = newRV_noinc((SV *)newHV()); |
c200a770 |
470 | sv_2mortal(new_map_ref); |
471 | sv_setsv(map_ref, new_map_ref); |
472 | } |
c94afdc4 |
473 | |
c686d0ed |
474 | if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) { |
00c93f7a |
475 | mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref)); |
c200a770 |
476 | sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */ |
c200a770 |
477 | } |
478 | |
479 | XPUSHs(map_ref); |