1 /* There's a lot of cases of doubled parens in here like this:
5 This shuts up warnings from gcc -Wall
12 #define NEED_newRV_noinc
13 #define NEED_sv_2pv_flags
14 #define NEED_sv_2pv_nolen
17 #define DECLARE_KEY(name) SV *key_##name; U32 hash_##name;
19 #define PREHASH_KEY_WITH_VALUE(name, value) do { \
20 key_##name = newSVpvs(value); \
21 PERL_HASH(hash_##name, value, sizeof(value) - 1); \
24 #define PREHASH_KEY(name) PREHASH_KEY_WITH_VALUE(name, #name)
28 DECLARE_KEY(package_name);
30 DECLARE_KEY(package_cache_flag);
36 SV *associated_metaclass;
40 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
41 #if PERL_VERSION >= 10
44 mop_check_package_cache_flag(pTHX_ HV* stash) {
45 assert(SvTYPE(stash) == SVt_PVHV);
47 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
48 * however the perl core doesn't make it easy for us. It doesn't provide an
49 * api that just does what we want.
51 * However, we know that the information we want is, inside the core,
52 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
53 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
54 * which is not public and only available inside the core, as the mro
55 * interface as well as the structure returned by mro_meta_init isn't
56 * considered to be stable yet.
58 * Perl_mro_meta_init isn't declared static, so we could just define it
59 * ourselfs if perls headers don't do that for us, except that won't work
60 * on platforms where symbols need to be explicitly exported when linking
63 * So our, hopefully temporary, solution is to be even more evil and
64 * basically reimplement HvMROMETA in a very fragile way that'll blow up
65 * when the relevant parts of the mro implementation in core change.
71 return HvAUX(stash)->xhv_mro_meta
72 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
76 #else /* pre 5.10.0 */
79 mop_check_package_cache_flag(pTHX_ HV *stash) {
80 PERL_UNUSED_ARG(stash);
81 assert(SvTYPE(stash) == SVt_PVHV);
83 return PL_sub_generation;
87 #define call0(s, m) mop_call0(aTHX_ s, m)
89 mop_call0(pTHX_ SV *const self, SV *const method) {
97 call_sv(method, G_SCALAR | G_METHOD);
107 get_code_info (SV *coderef, char **pkg, char **name)
109 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
113 coderef = SvRV(coderef);
114 /* I think this only gets triggered with a mangled coderef, but if
115 we hit it without the guard, we segfault. The slightly odd return
116 value strikes me as an improvement (mst)
119 if ( isGV_with_GP(CvGV(coderef)) ) {
121 *pkg = HvNAME( GvSTASH(CvGV(coderef)) );
122 *name = GvNAME( CvGV(coderef) );
125 *pkg = "__UNKNOWN__";
143 get_all_package_symbols(HV *stash, type_filter_t filter)
148 (void)hv_iterinit(stash);
150 if (filter == TYPE_FILTER_NONE) {
151 while ( (he = hv_iternext(stash)) ) {
153 char *key = HePV(he, keylen);
154 if (!hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0)) {
155 croak("failed to store glob ref");
162 while ( (he = hv_iternext(stash)) ) {
163 SV *const gv = HeVAL(he);
170 switch( SvTYPE(gv) ) {
176 /* expand the gv into a real typeglob if it
177 * contains stub functions and we were asked to
178 * return CODE symbols */
179 if (filter == TYPE_FILTER_CODE) {
181 /* we don't really care about the length,
182 but that's the API */
183 key = HePV(he, keylen);
184 package = HvNAME(stash);
185 fq = newSVpvf("%s::%s", package, key);
186 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
190 key = HePV(he, keylen);
191 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
196 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
197 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
198 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
199 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
200 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
202 croak("Unknown type");
210 char *key = HePV(he, keylen);
211 if (!hv_store(ret, key, keylen, newRV_inc(sv), 0)) {
212 croak("failed to store symbol ref");
222 mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
223 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
224 SV *method_metaclass_name;
231 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
233 (void)hv_iterinit(symbols);
234 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
235 CV *cv = (CV *)SvRV(coderef);
241 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
245 /* this checks to see that the subroutine is actually from our package */
246 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
247 if ( strNE(cvpkg_name, class_name_pv) ) {
252 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
253 if ( SvOK(method_slot) ) {
254 SV *const body = call0(method_slot, key_body); /* $method_object->body() */
255 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
260 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
263 $method_object = $method_metaclass->wrap(
265 associated_metaclass => $self,
266 package_name => $class_name,
275 PUSHs(method_metaclass_name); /* invocant */
276 mPUSHs(newRV_inc((SV *)cv));
277 PUSHs(associated_metaclass);
279 PUSHs(key_package_name);
282 mPUSHs(newSVpv(method_name, method_name_len));
285 call_sv(wrap, G_SCALAR | G_METHOD);
287 method_object = POPs;
289 /* $map->{$method_name} = $method_object */
290 sv_setsv(method_slot, method_object);
299 Pass in a coderef, returns:
300 [ $pkg_name, $coderef_name ] ie:
301 [ 'Foo::Bar', 'new' ]
304 MODULE = Class::MOP PACKAGE = Class::MOP
309 PREHASH_KEY(package);
310 PREHASH_KEY(package_name);
311 PREHASH_KEY(methods);
313 PREHASH_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag");
314 /* we can't stringify VERSION as it's a define already */
315 PREHASH_KEY_WITH_VALUE(VERSION, "VERSION");
317 method_metaclass = newSVpvs("method_metaclass");
318 wrap = newSVpvs("wrap");
319 associated_metaclass = newSVpvs("associated_metaclass");
326 get_code_info(coderef)
332 if (get_code_info(coderef, &pkg, &name)) {
334 PUSHs(newSVpv(pkg, 0));
335 PUSHs(newSVpv(name, 0));
341 is_class_loaded(klass=&PL_sv_undef)
349 if (!SvPOK(klass) || !SvCUR(klass)) {
353 stash = gv_stashsv(klass, 0);
358 if (hv_exists_ent (stash, key_VERSION, hash_VERSION)) {
359 HE *version = hv_fetch_ent(stash, key_VERSION, 0, hash_VERSION);
360 if (version && HeVAL(version) && GvSV(HeVAL(version))) {
365 if (hv_exists_ent (stash, key_ISA, hash_ISA)) {
366 HE *isa = hv_fetch_ent(stash, key_ISA, 0, hash_ISA);
367 if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
372 (void)hv_iterinit(stash);
373 while ((gv = (GV *)hv_iternextsv(stash, &key, &keylen))) {
378 if (key[keylen - 1] == ':' && key[keylen - 2] == ':') {
382 if (!isGV(gv) || GvCV(gv) || GvSV(gv) || GvAV(gv) || GvHV(gv) || GvIO(gv) || GvFORM(gv)) {
389 MODULE = Class::MOP PACKAGE = Class::MOP::Package
394 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
403 if ( ! SvROK(self) ) {
404 die("Cannot call get_all_package_symbols as a class method");
407 if (GIMME_V == G_VOID) {
414 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
415 stash = gv_stashsv(HeVAL(he), 0);
421 case G_SCALAR: XSRETURN_UNDEF; break;
422 case G_ARRAY: XSRETURN_EMPTY; break;
426 symbols = get_all_package_symbols(stash, filter);
430 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
433 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
435 EXTEND(SP, HvKEYS(symbols) * 2);
437 while ((he = hv_iternext(symbols))) {
438 PUSHs(hv_iterkeysv(he));
439 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
447 SvREFCNT_dec((SV *)symbols);
455 if ( ! SvROK(self) ) {
456 die("Cannot call name as a class method");
459 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
462 ST(0) = &PL_sv_undef;
464 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
472 if ( ! SvROK(self) ) {
473 die("Cannot call name as a class method");
476 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
479 ST(0) = &PL_sv_undef;
481 MODULE = Class::MOP PACKAGE = Class::MOP::Method
489 if ( ! SvROK(self) ) {
490 die("Cannot call name as a class method");
493 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
496 ST(0) = &PL_sv_undef;
504 if ( ! SvROK(self) ) {
505 die("Cannot call package_name as a class method");
508 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
511 ST(0) = &PL_sv_undef;
519 if ( ! SvROK(self) ) {
520 die("Cannot call body as a class method");
523 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
526 ST(0) = &PL_sv_undef;
529 MODULE = Class::MOP PACKAGE = Class::MOP::Class
535 HV *const obj = (HV *)SvRV(self);
536 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
537 HV *const stash = gv_stashsv(class_name, 0);
538 UV const current = check_package_cache_flag(stash);
539 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
540 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
542 /* in $self->{methods} does not yet exist (or got deleted) */
543 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
544 SV *new_map_ref = newRV_noinc((SV *)newHV());
545 sv_2mortal(new_map_ref);
546 sv_setsv(map_ref, new_map_ref);
549 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
550 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
551 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */