bump all the versions to 0.76
[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
151static HV *
152get_all_package_symbols(HV *stash, type_filter_t filter)
153{
154 HE *he;
155 HV *ret = newHV();
156
157 (void)hv_iterinit(stash);
158
159 if (filter == TYPE_FILTER_NONE) {
160 while ( (he = hv_iternext(stash)) ) {
161 STRLEN keylen;
162 char *key = HePV(he, keylen);
7291ecef 163 if (!hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0)) {
164 croak("failed to store glob ref");
165 }
b49ee39f 166 }
167
168 return ret;
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) {
219 char *key = HePV(he, keylen);
7291ecef 220 if (!hv_store(ret, key, keylen, newRV_inc(sv), 0)) {
221 croak("failed to store symbol ref");
222 }
b49ee39f 223 }
224 }
225
226 return ret;
227}
c94afdc4 228
0e0b5b8d 229
230static void
00c93f7a 231mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
232 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
233 SV *method_metaclass_name;
234 char *method_name;
0e0b5b8d 235 I32 method_name_len;
00c93f7a 236 SV *coderef;
237 HV *symbols;
0e0b5b8d 238 dSP;
239
240 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
241
242 (void)hv_iterinit(symbols);
243 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
00c93f7a 244 CV *cv = (CV *)SvRV(coderef);
245 char *cvpkg_name;
246 char *cv_name;
247 SV *method_slot;
248 SV *method_object;
0e0b5b8d 249
250 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
251 continue;
252 }
253
254 /* this checks to see that the subroutine is actually from our package */
255 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
256 if ( strNE(cvpkg_name, class_name_pv) ) {
257 continue;
258 }
259 }
260
261 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
262 if ( SvOK(method_slot) ) {
6d915932 263 SV *const body = call0(method_slot, key_body); /* $method_object->body() */
00c93f7a 264 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
0e0b5b8d 265 continue;
266 }
267 }
268
269 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
270
271 /*
272 $method_object = $method_metaclass->wrap(
273 $cv,
274 associated_metaclass => $self,
275 package_name => $class_name,
276 name => $method_name
277 );
278 */
279 ENTER;
280 SAVETMPS;
281
282 PUSHMARK(SP);
283 EXTEND(SP, 8);
284 PUSHs(method_metaclass_name); /* invocant */
00c93f7a 285 mPUSHs(newRV_inc((SV *)cv));
0e0b5b8d 286 PUSHs(associated_metaclass);
287 PUSHs(self);
288 PUSHs(key_package_name);
289 PUSHs(class_name);
290 PUSHs(key_name);
291 mPUSHs(newSVpv(method_name, method_name_len));
292 PUTBACK;
293
294 call_sv(wrap, G_SCALAR | G_METHOD);
295 SPAGAIN;
296 method_object = POPs;
297 PUTBACK;
298 /* $map->{$method_name} = $method_object */
299 sv_setsv(method_slot, method_object);
300
301 FREETMPS;
302 LEAVE;
303 }
304}
305
e0e4674a 306/*
e0e4674a 307get_code_info:
308 Pass in a coderef, returns:
309 [ $pkg_name, $coderef_name ] ie:
310 [ 'Foo::Bar', 'new' ]
311*/
312
313MODULE = Class::MOP PACKAGE = Class::MOP
314
cc856b56 315BOOT:
1654881c 316 PREHASH_KEY(name);
317 PREHASH_KEY(body);
318 PREHASH_KEY(package);
319 PREHASH_KEY(package_name);
320 PREHASH_KEY(methods);
321 PREHASH_KEY(ISA);
45a46f7b 322 PREHASH_KEY(VERSION);
1654881c 323 PREHASH_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag");
cc856b56 324
c94afdc4 325 method_metaclass = newSVpvs("method_metaclass");
326 wrap = newSVpvs("wrap");
327 associated_metaclass = newSVpvs("associated_metaclass");
328
cc856b56 329
36756faf 330PROTOTYPES: DISABLE
cc856b56 331
36756faf 332# use prototype here to be compatible with get_code_info from Sub::Identify
e0e4674a 333void
334get_code_info(coderef)
00c93f7a 335 SV *coderef
36756faf 336 PROTOTYPE: $
0e0b5b8d 337 PREINIT:
6d915932 338 char *pkg = NULL;
339 char *name = NULL;
0e0b5b8d 340 PPCODE:
341 if (get_code_info(coderef, &pkg, &name)) {
342 EXTEND(SP, 2);
343 PUSHs(newSVpv(pkg, 0));
344 PUSHs(newSVpv(name, 0));
345 }
e0e4674a 346
863178c8 347void
348is_class_loaded(klass=&PL_sv_undef)
349 SV *klass
350 PREINIT:
351 HV *stash;
352 char *key;
353 I32 keylen;
354 GV *gv;
355 PPCODE:
356 if (!SvPOK(klass) || !SvCUR(klass)) {
357 XSRETURN_NO;
358 }
359
360 stash = gv_stashsv(klass, 0);
361 if (!stash) {
362 XSRETURN_NO;
363 }
364
d508fabd 365 if (hv_exists_ent (stash, key_VERSION, hash_VERSION)) {
366 HE *version = hv_fetch_ent(stash, key_VERSION, 0, hash_VERSION);
367 if (version && HeVAL(version) && GvSV(HeVAL(version))) {
863178c8 368 XSRETURN_YES;
369 }
370 }
371
d508fabd 372 if (hv_exists_ent (stash, key_ISA, hash_ISA)) {
373 HE *isa = hv_fetch_ent(stash, key_ISA, 0, hash_ISA);
374 if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
863178c8 375 XSRETURN_YES;
376 }
377 }
378
379 (void)hv_iterinit(stash);
002254af 380 while ((gv = (GV *)hv_iternextsv(stash, &key, &keylen))) {
863178c8 381 if (keylen <= 0) {
382 continue;
383 }
384
385 if (key[keylen - 1] == ':' && key[keylen - 2] == ':') {
386 continue;
387 }
388
da2a38e2 389 if (!isGV(gv) || GvCV(gv) || GvSV(gv) || GvAV(gv)
390 || GvHV(gv) || GvIO(gv) || GvFORM(gv)) {
863178c8 391 XSRETURN_YES;
392 }
393 }
394
395 XSRETURN_NO;
15273f3c 396
397MODULE = Class::MOP PACKAGE = Class::MOP::Package
398
399void
b49ee39f 400get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
cc856b56 401 SV *self
b49ee39f 402 type_filter_t filter
15273f3c 403 PREINIT:
cc856b56 404 HV *stash = NULL;
b49ee39f 405 HV *symbols = NULL;
75705e60 406 register HE *he;
15273f3c 407 PPCODE:
a7f711e5 408 if ( ! SvROK(self) ) {
988fb42e 409 die("Cannot call get_all_package_symbols as a class method");
410 }
15273f3c 411
b49ee39f 412 if (GIMME_V == G_VOID) {
413 XSRETURN_EMPTY;
414 }
415
15273f3c 416
15273f3c 417 PUTBACK;
418
b49ee39f 419 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
420 stash = gv_stashsv(HeVAL(he), 0);
421 }
15273f3c 422
15273f3c 423
b49ee39f 424 if (!stash) {
6ccb3af5 425 switch (GIMME_V) {
426 case G_SCALAR: XSRETURN_UNDEF; break;
427 case G_ARRAY: XSRETURN_EMPTY; break;
428 }
b49ee39f 429 }
15273f3c 430
b49ee39f 431 symbols = get_all_package_symbols(stash, filter);
15273f3c 432
6ccb3af5 433 switch (GIMME_V) {
434 case G_SCALAR:
435 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
436 break;
437 case G_ARRAY:
438 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
439
440 EXTEND(SP, HvKEYS(symbols) * 2);
441
442 while ((he = hv_iternext(symbols))) {
443 PUSHs(hv_iterkeysv(he));
444 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
445 }
446
447 break;
448 default:
449 break;
15273f3c 450 }
451
b49ee39f 452 SvREFCNT_dec((SV *)symbols);
453
e2c189ae 454void
cc856b56 455name(self)
456 SV *self
457 PREINIT:
458 register HE *he;
459 PPCODE:
a7f711e5 460 if ( ! SvROK(self) ) {
988fb42e 461 die("Cannot call name as a class method");
462 }
463
e7b69038 464 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
cc856b56 465 XPUSHs(HeVAL(he));
466 else
467 ST(0) = &PL_sv_undef;
468
dcbfe027 469MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
cc856b56 470
e2c189ae 471void
cc856b56 472name(self)
473 SV *self
474 PREINIT:
475 register HE *he;
476 PPCODE:
a7f711e5 477 if ( ! SvROK(self) ) {
988fb42e 478 die("Cannot call name as a class method");
479 }
480
e7b69038 481 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
cc856b56 482 XPUSHs(HeVAL(he));
483 else
484 ST(0) = &PL_sv_undef;
485
dcbfe027 486MODULE = Class::MOP PACKAGE = Class::MOP::Method
cc856b56 487
e2c189ae 488void
da88f307 489name(self)
490 SV *self
491 PREINIT:
492 register HE *he;
493 PPCODE:
a7f711e5 494 if ( ! SvROK(self) ) {
da88f307 495 die("Cannot call name as a class method");
496 }
497
e7b69038 498 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
da88f307 499 XPUSHs(HeVAL(he));
500 else
501 ST(0) = &PL_sv_undef;
502
e2c189ae 503void
da88f307 504package_name(self)
505 SV *self
506 PREINIT:
507 register HE *he;
508 PPCODE:
a7f711e5 509 if ( ! SvROK(self) ) {
da88f307 510 die("Cannot call package_name as a class method");
511 }
512
e7b69038 513 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
da88f307 514 XPUSHs(HeVAL(he));
515 else
516 ST(0) = &PL_sv_undef;
517
e2c189ae 518void
cc856b56 519body(self)
520 SV *self
521 PREINIT:
522 register HE *he;
523 PPCODE:
a7f711e5 524 if ( ! SvROK(self) ) {
da88f307 525 die("Cannot call body as a class method");
526 }
527
e7b69038 528 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
cc856b56 529 XPUSHs(HeVAL(he));
530 else
531 ST(0) = &PL_sv_undef;
c94afdc4 532
533
534MODULE = Class::MOP PACKAGE = Class::MOP::Class
535
536void
537get_method_map(self)
00c93f7a 538 SV *self
c200a770 539 PREINIT:
c686d0ed 540 HV *const obj = (HV *)SvRV(self);
541 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
542 HV *const stash = gv_stashsv(class_name, 0);
c200a770 543 UV const current = check_package_cache_flag(stash);
c686d0ed 544 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
545 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
c200a770 546 PPCODE:
c200a770 547 /* in $self->{methods} does not yet exist (or got deleted) */
c686d0ed 548 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
00c93f7a 549 SV *new_map_ref = newRV_noinc((SV *)newHV());
c200a770 550 sv_2mortal(new_map_ref);
551 sv_setsv(map_ref, new_map_ref);
552 }
c94afdc4 553
c686d0ed 554 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
00c93f7a 555 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
c200a770 556 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */
c200a770 557 }
558
559 XPUSHs(map_ref);