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(name, value) do { \
20 key_##name = newSVpvs(value); \
21 PERL_HASH(hash_##name, value, sizeof(value) - 1); \
26 DECLARE_KEY(package_name);
28 DECLARE_KEY(package_cache_flag);
34 SV *associated_metaclass;
38 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
39 #if PERL_VERSION >= 10
42 mop_check_package_cache_flag(pTHX_ HV* stash) {
43 assert(SvTYPE(stash) == SVt_PVHV);
45 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
46 * however the perl core doesn't make it easy for us. It doesn't provide an
47 * api that just does what we want.
49 * However, we know that the information we want is, inside the core,
50 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
51 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
52 * which is not public and only available inside the core, as the mro
53 * interface as well as the structure returned by mro_meta_init isn't
54 * considered to be stable yet.
56 * Perl_mro_meta_init isn't declared static, so we could just define it
57 * ourselfs if perls headers don't do that for us, except that won't work
58 * on platforms where symbols need to be explicitly exported when linking
61 * So our, hopefully temporary, solution is to be even more evil and
62 * basically reimplement HvMROMETA in a very fragile way that'll blow up
63 * when the relevant parts of the mro implementation in core change.
69 return HvAUX(stash)->xhv_mro_meta
70 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
74 #else /* pre 5.10.0 */
77 mop_check_package_cache_flag(pTHX_ HV *stash) {
78 PERL_UNUSED_ARG(stash);
79 assert(SvTYPE(stash) == SVt_PVHV);
81 return PL_sub_generation;
85 #define call0(s, m) mop_call0(aTHX_ s, m)
87 mop_call0(pTHX_ SV *const self, SV *const method) {
95 call_sv(method, G_SCALAR | G_METHOD);
105 get_code_info (SV *coderef, char **pkg, char **name)
107 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
111 coderef = SvRV(coderef);
112 /* I think this only gets triggered with a mangled coderef, but if
113 we hit it without the guard, we segfault. The slightly odd return
114 value strikes me as an improvement (mst)
117 if ( isGV_with_GP(CvGV(coderef)) ) {
119 *pkg = HvNAME( GvSTASH(CvGV(coderef)) );
120 *name = GvNAME( CvGV(coderef) );
123 *pkg = "__UNKNOWN__";
141 get_all_package_symbols(HV *stash, type_filter_t filter)
146 (void)hv_iterinit(stash);
148 if (filter == TYPE_FILTER_NONE) {
149 while ( (he = hv_iternext(stash)) ) {
151 char *key = HePV(he, keylen);
152 if (!hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0)) {
153 croak("failed to store glob ref");
160 while ( (he = hv_iternext(stash)) ) {
161 SV *const gv = HeVAL(he);
168 switch( SvTYPE(gv) ) {
174 /* expand the gv into a real typeglob if it
175 * contains stub functions and we were asked to
176 * return CODE symbols */
177 if (filter == TYPE_FILTER_CODE) {
179 /* we don't really care about the length,
180 but that's the API */
181 key = HePV(he, keylen);
182 package = HvNAME(stash);
183 fq = newSVpvf("%s::%s", package, key);
184 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
188 key = HePV(he, keylen);
189 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
194 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
195 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
196 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
197 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
198 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
200 croak("Unknown type");
208 char *key = HePV(he, keylen);
209 if (!hv_store(ret, key, keylen, newRV_inc(sv), 0)) {
210 croak("failed to store symbol ref");
220 mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
221 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
222 SV *method_metaclass_name;
229 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
231 (void)hv_iterinit(symbols);
232 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
233 CV *cv = (CV *)SvRV(coderef);
239 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
243 /* this checks to see that the subroutine is actually from our package */
244 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
245 if ( strNE(cvpkg_name, class_name_pv) ) {
250 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
251 if ( SvOK(method_slot) ) {
252 SV *const body = call0(method_slot, key_body); /* $method_object->body() */
253 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
258 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
261 $method_object = $method_metaclass->wrap(
263 associated_metaclass => $self,
264 package_name => $class_name,
273 PUSHs(method_metaclass_name); /* invocant */
274 mPUSHs(newRV_inc((SV *)cv));
275 PUSHs(associated_metaclass);
277 PUSHs(key_package_name);
280 mPUSHs(newSVpv(method_name, method_name_len));
283 call_sv(wrap, G_SCALAR | G_METHOD);
285 method_object = POPs;
287 /* $map->{$method_name} = $method_object */
288 sv_setsv(method_slot, method_object);
297 Pass in a coderef, returns:
298 [ $pkg_name, $coderef_name ] ie:
299 [ 'Foo::Bar', 'new' ]
302 MODULE = Class::MOP PACKAGE = Class::MOP
305 PREHASH_KEY(name, "name");
306 PREHASH_KEY(body, "body");
307 PREHASH_KEY(package, "package");
308 PREHASH_KEY(package_name, "package_name");
309 PREHASH_KEY(package_cache_flag, "_package_cache_flag");
310 PREHASH_KEY(methods, "methods");
311 PREHASH_KEY(VERSION, "VERSION");
312 PREHASH_KEY(ISA, "ISA");
314 method_metaclass = newSVpvs("method_metaclass");
315 wrap = newSVpvs("wrap");
316 associated_metaclass = newSVpvs("associated_metaclass");
323 get_code_info(coderef)
329 if (get_code_info(coderef, &pkg, &name)) {
331 PUSHs(newSVpv(pkg, 0));
332 PUSHs(newSVpv(name, 0));
338 is_class_loaded(klass=&PL_sv_undef)
346 if (!SvPOK(klass) || !SvCUR(klass)) {
350 stash = gv_stashsv(klass, 0);
355 if (hv_exists_ent (stash, key_VERSION, hash_VERSION)) {
356 HE *version = hv_fetch_ent(stash, key_VERSION, 0, hash_VERSION);
357 if (version && HeVAL(version) && GvSV(HeVAL(version))) {
362 if (hv_exists_ent (stash, key_ISA, hash_ISA)) {
363 HE *isa = hv_fetch_ent(stash, key_ISA, 0, hash_ISA);
364 if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
369 (void)hv_iterinit(stash);
370 while ((gv = (GV *)hv_iternextsv(stash, &key, &keylen))) {
375 if (key[keylen - 1] == ':' && key[keylen - 2] == ':') {
379 if (!isGV(gv) || GvCV(gv) || GvSV(gv) || GvAV(gv) || GvHV(gv) || GvIO(gv) || GvFORM(gv)) {
386 MODULE = Class::MOP PACKAGE = Class::MOP::Package
391 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
400 if ( ! SvROK(self) ) {
401 die("Cannot call get_all_package_symbols as a class method");
404 if (GIMME_V == G_VOID) {
411 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
412 stash = gv_stashsv(HeVAL(he), 0);
418 case G_SCALAR: XSRETURN_UNDEF; break;
419 case G_ARRAY: XSRETURN_EMPTY; break;
423 symbols = get_all_package_symbols(stash, filter);
427 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
430 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
432 EXTEND(SP, HvKEYS(symbols) * 2);
434 while ((he = hv_iternext(symbols))) {
435 PUSHs(hv_iterkeysv(he));
436 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
444 SvREFCNT_dec((SV *)symbols);
452 if ( ! SvROK(self) ) {
453 die("Cannot call name as a class method");
456 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
459 ST(0) = &PL_sv_undef;
461 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
469 if ( ! SvROK(self) ) {
470 die("Cannot call name as a class method");
473 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
476 ST(0) = &PL_sv_undef;
478 MODULE = Class::MOP PACKAGE = Class::MOP::Method
486 if ( ! SvROK(self) ) {
487 die("Cannot call name as a class method");
490 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
493 ST(0) = &PL_sv_undef;
501 if ( ! SvROK(self) ) {
502 die("Cannot call package_name as a class method");
505 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
508 ST(0) = &PL_sv_undef;
516 if ( ! SvROK(self) ) {
517 die("Cannot call body as a class method");
520 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
523 ST(0) = &PL_sv_undef;
526 MODULE = Class::MOP PACKAGE = Class::MOP::Class
532 HV *const obj = (HV *)SvRV(self);
533 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
534 HV *const stash = gv_stashsv(class_name, 0);
535 UV const current = check_package_cache_flag(stash);
536 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
537 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
539 /* in $self->{methods} does not yet exist (or got deleted) */
540 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
541 SV *new_map_ref = newRV_noinc((SV *)newHV());
542 sv_2mortal(new_map_ref);
543 sv_setsv(map_ref, new_map_ref);
546 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
547 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
548 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */