Changelogging.
[gitmo/Class-MOP.git] / MOP.xs
CommitLineData
e7b69038 1/* There's a lot of cases of doubled parens in here like this:
2
3 while ( (he = ...) ) {
4
5This shuts up warnings from gcc -Wall
6*/
7
e0e4674a 8#include "EXTERN.h"
9#include "perl.h"
10#include "XSUB.h"
15273f3c 11
08f4716f 12#define NEED_newRV_noinc
599791aa 13#define NEED_sv_2pv_flags
15273f3c 14#define NEED_sv_2pv_nolen
b0e94057 15#include "ppport.h"
e0e4674a 16
193bfdc0 17#define DECLARE_KEY(name) SV *key_##name; U32 hash_##name;
18
1654881c 19#define PREHASH_KEY_WITH_VALUE(name, value) do { \
193bfdc0 20 key_##name = newSVpvs(value); \
21 PERL_HASH(hash_##name, value, sizeof(value) - 1); \
22} while (0)
23
45a46f7b 24/* this is basically the same as the above macro, except that the value will be
25 * the stringified name. However, we can't just implement this in terms of
26 * PREHASH_KEY_WITH_VALUE as that'd cause macro expansion on the value of
27 * 'name' when it's being passed to the other macro. suggestions on how to make
28 * this more elegant would be much appreciated */
29
30#define PREHASH_KEY(name) do { \
31 key_##name = newSVpvs(#name); \
32 PERL_HASH(hash_##name, #name, sizeof(#name) - 1); \
33} while (0)
1654881c 34
193bfdc0 35DECLARE_KEY(name);
36DECLARE_KEY(package);
37DECLARE_KEY(package_name);
38DECLARE_KEY(body);
39DECLARE_KEY(package_cache_flag);
40DECLARE_KEY(methods);
41DECLARE_KEY(VERSION);
42DECLARE_KEY(ISA);
d508fabd 43
00c93f7a 44SV *method_metaclass;
45SV *associated_metaclass;
46SV *wrap;
c94afdc4 47
48
49#define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
1d4cf2a9 50#if PERL_VERSION >= 10
c94afdc4 51
52static UV
53mop_check_package_cache_flag(pTHX_ HV* stash) {
54 assert(SvTYPE(stash) == SVt_PVHV);
55
1d4cf2a9 56 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
57 * however the perl core doesn't make it easy for us. It doesn't provide an
58 * api that just does what we want.
59 *
60 * However, we know that the information we want is, inside the core,
61 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
62 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
63 * which is not public and only available inside the core, as the mro
64 * interface as well as the structure returned by mro_meta_init isn't
65 * considered to be stable yet.
66 *
67 * Perl_mro_meta_init isn't declared static, so we could just define it
68 * ourselfs if perls headers don't do that for us, except that won't work
69 * on platforms where symbols need to be explicitly exported when linking
70 * shared libraries.
71 *
72 * So our, hopefully temporary, solution is to be even more evil and
73 * basically reimplement HvMROMETA in a very fragile way that'll blow up
74 * when the relevant parts of the mro implementation in core change.
75 *
76 * :-(
77 *
78 */
79
80 return HvAUX(stash)->xhv_mro_meta
81 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
82 : 0;
c94afdc4 83}
84
85#else /* pre 5.10.0 */
86
87static UV
00c93f7a 88mop_check_package_cache_flag(pTHX_ HV *stash) {
c94afdc4 89 PERL_UNUSED_ARG(stash);
90 assert(SvTYPE(stash) == SVt_PVHV);
91
92 return PL_sub_generation;
93}
94#endif
95
96#define call0(s, m) mop_call0(aTHX_ s, m)
00c93f7a 97static SV *
98mop_call0(pTHX_ SV *const self, SV *const method) {
c94afdc4 99 dSP;
00c93f7a 100 SV *ret;
c94afdc4 101
102 PUSHMARK(SP);
103 XPUSHs(self);
104 PUTBACK;
105
106 call_sv(method, G_SCALAR | G_METHOD);
107
108 SPAGAIN;
109 ret = POPs;
110 PUTBACK;
111
112 return ret;
113}
114
ee82a43e 115static int
0e0b5b8d 116get_code_info (SV *coderef, char **pkg, char **name)
117{
118 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
119 return 0;
120 }
6bd19edb 121
0e0b5b8d 122 coderef = SvRV(coderef);
123 /* I think this only gets triggered with a mangled coderef, but if
124 we hit it without the guard, we segfault. The slightly odd return
125 value strikes me as an improvement (mst)
126 */
127#ifdef isGV_with_GP
128 if ( isGV_with_GP(CvGV(coderef)) ) {
cb0ec494 129#endif
0e0b5b8d 130 *pkg = HvNAME( GvSTASH(CvGV(coderef)) );
131 *name = GvNAME( CvGV(coderef) );
132#ifdef isGV_with_GP
133 } else {
134 *pkg = "__UNKNOWN__";
135 *name = "__ANON__";
c94afdc4 136 }
0e0b5b8d 137#endif
138
139 return 1;
c94afdc4 140}
141
b49ee39f 142typedef enum {
143 TYPE_FILTER_NONE,
144 TYPE_FILTER_CODE,
145 TYPE_FILTER_ARRAY,
146 TYPE_FILTER_IO,
147 TYPE_FILTER_HASH,
148 TYPE_FILTER_SCALAR,
149} type_filter_t;
150
f6707263 151typedef bool (*get_package_symbols_cb_t) (const char *, STRLEN, SV *, void *);
152
153static void
154get_package_symbols(HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
b49ee39f 155{
156 HE *he;
b49ee39f 157
158 (void)hv_iterinit(stash);
159
160 if (filter == TYPE_FILTER_NONE) {
161 while ( (he = hv_iternext(stash)) ) {
162 STRLEN keylen;
f6707263 163 const char *key = HePV(he, keylen);
164 if (!cb(key, keylen, HeVAL(he), ud)) {
165 return;
7291ecef 166 }
b49ee39f 167 }
f6707263 168 return;
b49ee39f 169 }
170
171 while ( (he = hv_iternext(stash)) ) {
172 SV *const gv = HeVAL(he);
173 SV *sv = NULL;
174 char *key;
175 STRLEN keylen;
176 char *package;
177 SV *fq;
178
179 switch( SvTYPE(gv) ) {
180#ifndef SVt_RV
181 case SVt_RV:
182#endif
183 case SVt_PV:
184 case SVt_IV:
185 /* expand the gv into a real typeglob if it
186 * contains stub functions and we were asked to
187 * return CODE symbols */
188 if (filter == TYPE_FILTER_CODE) {
189 if (SvROK(gv)) {
190 /* we don't really care about the length,
191 but that's the API */
192 key = HePV(he, keylen);
193 package = HvNAME(stash);
194 fq = newSVpvf("%s::%s", package, key);
195 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
196 break;
197 }
198
199 key = HePV(he, keylen);
200 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
201 }
202 /* fall through */
203 case SVt_PVGV:
204 switch (filter) {
205 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
206 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
207 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
208 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
209 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
210 default:
211 croak("Unknown type");
212 }
213 break;
214 default:
215 continue;
216 }
217
218 if (sv) {
f6707263 219 const char *key = HePV(he, keylen);
220 if (!cb(key, keylen, sv, ud)) {
221 return;
7291ecef 222 }
b49ee39f 223 }
224 }
f6707263 225}
b49ee39f 226
f6707263 227static bool
82a64f20 228find_method (const char *key, STRLEN keylen, SV *val, void *ud)
229{
230 bool *found_method = (bool *)ud;
231 *found_method = TRUE;
232 return FALSE;
233}
234
235static bool
f6707263 236collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
237{
238 HV *hash = (HV *)ud;
239
240 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
241 croak("failed to store symbol ref");
242 }
243
244 return TRUE;
b49ee39f 245}
c94afdc4 246
f6707263 247static HV *
248get_all_package_symbols (HV *stash, type_filter_t filter)
249{
250 HV *ret = newHV ();
251 get_package_symbols (stash, filter, collect_all_symbols, ret);
252 return ret;
253}
0e0b5b8d 254
255static void
00c93f7a 256mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
257 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
258 SV *method_metaclass_name;
259 char *method_name;
0e0b5b8d 260 I32 method_name_len;
00c93f7a 261 SV *coderef;
262 HV *symbols;
0e0b5b8d 263 dSP;
264
265 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
266
267 (void)hv_iterinit(symbols);
268 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
00c93f7a 269 CV *cv = (CV *)SvRV(coderef);
270 char *cvpkg_name;
271 char *cv_name;
272 SV *method_slot;
273 SV *method_object;
0e0b5b8d 274
275 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
276 continue;
277 }
278
279 /* this checks to see that the subroutine is actually from our package */
280 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
281 if ( strNE(cvpkg_name, class_name_pv) ) {
282 continue;
283 }
284 }
285
286 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
287 if ( SvOK(method_slot) ) {
6d915932 288 SV *const body = call0(method_slot, key_body); /* $method_object->body() */
00c93f7a 289 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
0e0b5b8d 290 continue;
291 }
292 }
293
294 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
295
296 /*
297 $method_object = $method_metaclass->wrap(
298 $cv,
299 associated_metaclass => $self,
300 package_name => $class_name,
301 name => $method_name
302 );
303 */
304 ENTER;
305 SAVETMPS;
306
307 PUSHMARK(SP);
308 EXTEND(SP, 8);
309 PUSHs(method_metaclass_name); /* invocant */
00c93f7a 310 mPUSHs(newRV_inc((SV *)cv));
0e0b5b8d 311 PUSHs(associated_metaclass);
312 PUSHs(self);
313 PUSHs(key_package_name);
314 PUSHs(class_name);
315 PUSHs(key_name);
316 mPUSHs(newSVpv(method_name, method_name_len));
317 PUTBACK;
318
319 call_sv(wrap, G_SCALAR | G_METHOD);
320 SPAGAIN;
321 method_object = POPs;
322 PUTBACK;
323 /* $map->{$method_name} = $method_object */
324 sv_setsv(method_slot, method_object);
325
326 FREETMPS;
327 LEAVE;
328 }
329}
330
e0e4674a 331/*
e0e4674a 332get_code_info:
333 Pass in a coderef, returns:
334 [ $pkg_name, $coderef_name ] ie:
335 [ 'Foo::Bar', 'new' ]
336*/
337
338MODULE = Class::MOP PACKAGE = Class::MOP
339
cc856b56 340BOOT:
1654881c 341 PREHASH_KEY(name);
342 PREHASH_KEY(body);
343 PREHASH_KEY(package);
344 PREHASH_KEY(package_name);
345 PREHASH_KEY(methods);
346 PREHASH_KEY(ISA);
45a46f7b 347 PREHASH_KEY(VERSION);
1654881c 348 PREHASH_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag");
cc856b56 349
c94afdc4 350 method_metaclass = newSVpvs("method_metaclass");
351 wrap = newSVpvs("wrap");
352 associated_metaclass = newSVpvs("associated_metaclass");
353
cc856b56 354
36756faf 355PROTOTYPES: DISABLE
cc856b56 356
36756faf 357# use prototype here to be compatible with get_code_info from Sub::Identify
e0e4674a 358void
359get_code_info(coderef)
00c93f7a 360 SV *coderef
36756faf 361 PROTOTYPE: $
0e0b5b8d 362 PREINIT:
6d915932 363 char *pkg = NULL;
364 char *name = NULL;
0e0b5b8d 365 PPCODE:
366 if (get_code_info(coderef, &pkg, &name)) {
367 EXTEND(SP, 2);
368 PUSHs(newSVpv(pkg, 0));
369 PUSHs(newSVpv(name, 0));
370 }
e0e4674a 371
9cb15af7 372# This is some pretty grotty logic. It _should_ be parallel to the
373# pure Perl version in lib/Class/MOP.pm, so if you want to understand
374# it we suggest you start there.
863178c8 375void
376is_class_loaded(klass=&PL_sv_undef)
377 SV *klass
378 PREINIT:
379 HV *stash;
82a64f20 380 bool found_method = FALSE;
863178c8 381 PPCODE:
382 if (!SvPOK(klass) || !SvCUR(klass)) {
383 XSRETURN_NO;
384 }
385
386 stash = gv_stashsv(klass, 0);
387 if (!stash) {
388 XSRETURN_NO;
389 }
390
d508fabd 391 if (hv_exists_ent (stash, key_VERSION, hash_VERSION)) {
392 HE *version = hv_fetch_ent(stash, key_VERSION, 0, hash_VERSION);
b7e6420b 393 SV *version_sv;
394 if (version && HeVAL(version) && (version_sv = GvSV(HeVAL(version)))) {
395 if (SvROK(version_sv)) {
396 SV *version_sv_ref = SvRV(version_sv);
397
398 if (SvOK(version_sv_ref)) {
399 XSRETURN_YES;
400 }
401 }
402 else if (SvOK(version_sv)) {
403 XSRETURN_YES;
404 }
863178c8 405 }
406 }
407
d508fabd 408 if (hv_exists_ent (stash, key_ISA, hash_ISA)) {
409 HE *isa = hv_fetch_ent(stash, key_ISA, 0, hash_ISA);
410 if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
863178c8 411 XSRETURN_YES;
412 }
413 }
414
82a64f20 415 get_package_symbols(stash, TYPE_FILTER_CODE, find_method, &found_method);
416 if (found_method) {
2a2586a9 417 XSRETURN_YES;
863178c8 418 }
419
420 XSRETURN_NO;
15273f3c 421
422MODULE = Class::MOP PACKAGE = Class::MOP::Package
423
424void
b49ee39f 425get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
cc856b56 426 SV *self
b49ee39f 427 type_filter_t filter
15273f3c 428 PREINIT:
cc856b56 429 HV *stash = NULL;
b49ee39f 430 HV *symbols = NULL;
75705e60 431 register HE *he;
15273f3c 432 PPCODE:
a7f711e5 433 if ( ! SvROK(self) ) {
988fb42e 434 die("Cannot call get_all_package_symbols as a class method");
435 }
15273f3c 436
b49ee39f 437 if (GIMME_V == G_VOID) {
438 XSRETURN_EMPTY;
439 }
440
15273f3c 441 PUTBACK;
442
b49ee39f 443 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
444 stash = gv_stashsv(HeVAL(he), 0);
445 }
15273f3c 446
15273f3c 447
b49ee39f 448 if (!stash) {
a4bb3172 449 XSRETURN_UNDEF;
b49ee39f 450 }
15273f3c 451
b49ee39f 452 symbols = get_all_package_symbols(stash, filter);
a4bb3172 453 PUSHs(sv_2mortal(newRV_noinc((SV *)symbols)));
b49ee39f 454
e2c189ae 455void
cc856b56 456name(self)
457 SV *self
458 PREINIT:
459 register HE *he;
460 PPCODE:
a7f711e5 461 if ( ! SvROK(self) ) {
988fb42e 462 die("Cannot call name as a class method");
463 }
464
e7b69038 465 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
cc856b56 466 XPUSHs(HeVAL(he));
467 else
468 ST(0) = &PL_sv_undef;
469
dcbfe027 470MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
cc856b56 471
e2c189ae 472void
cc856b56 473name(self)
474 SV *self
475 PREINIT:
476 register HE *he;
477 PPCODE:
a7f711e5 478 if ( ! SvROK(self) ) {
988fb42e 479 die("Cannot call name as a class method");
480 }
481
e7b69038 482 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
cc856b56 483 XPUSHs(HeVAL(he));
484 else
485 ST(0) = &PL_sv_undef;
486
dcbfe027 487MODULE = Class::MOP PACKAGE = Class::MOP::Method
cc856b56 488
e2c189ae 489void
da88f307 490name(self)
491 SV *self
492 PREINIT:
493 register HE *he;
494 PPCODE:
a7f711e5 495 if ( ! SvROK(self) ) {
da88f307 496 die("Cannot call name as a class method");
497 }
498
e7b69038 499 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
da88f307 500 XPUSHs(HeVAL(he));
501 else
502 ST(0) = &PL_sv_undef;
503
e2c189ae 504void
da88f307 505package_name(self)
506 SV *self
507 PREINIT:
508 register HE *he;
509 PPCODE:
a7f711e5 510 if ( ! SvROK(self) ) {
da88f307 511 die("Cannot call package_name as a class method");
512 }
513
e7b69038 514 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
da88f307 515 XPUSHs(HeVAL(he));
516 else
517 ST(0) = &PL_sv_undef;
518
e2c189ae 519void
cc856b56 520body(self)
521 SV *self
522 PREINIT:
523 register HE *he;
524 PPCODE:
a7f711e5 525 if ( ! SvROK(self) ) {
da88f307 526 die("Cannot call body as a class method");
527 }
528
e7b69038 529 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
cc856b56 530 XPUSHs(HeVAL(he));
531 else
532 ST(0) = &PL_sv_undef;
c94afdc4 533
534
535MODULE = Class::MOP PACKAGE = Class::MOP::Class
536
537void
538get_method_map(self)
00c93f7a 539 SV *self
c200a770 540 PREINIT:
c686d0ed 541 HV *const obj = (HV *)SvRV(self);
542 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
543 HV *const stash = gv_stashsv(class_name, 0);
c200a770 544 UV const current = check_package_cache_flag(stash);
c686d0ed 545 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
546 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
c200a770 547 PPCODE:
c200a770 548 /* in $self->{methods} does not yet exist (or got deleted) */
c686d0ed 549 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
00c93f7a 550 SV *new_map_ref = newRV_noinc((SV *)newHV());
c200a770 551 sv_2mortal(new_map_ref);
552 sv_setsv(map_ref, new_map_ref);
553 }
c94afdc4 554
c686d0ed 555 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
00c93f7a 556 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
c200a770 557 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */
c200a770 558 }
559
560 XPUSHs(map_ref);