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
24 U32 hash_package_name;
29 SV *key_package_cache_flag;
30 U32 hash_package_cache_flag;
42 SV *associated_metaclass;
46 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
47 #if PERL_VERSION >= 10
50 mop_check_package_cache_flag(pTHX_ HV* stash) {
51 assert(SvTYPE(stash) == SVt_PVHV);
53 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
54 * however the perl core doesn't make it easy for us. It doesn't provide an
55 * api that just does what we want.
57 * However, we know that the information we want is, inside the core,
58 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
59 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
60 * which is not public and only available inside the core, as the mro
61 * interface as well as the structure returned by mro_meta_init isn't
62 * considered to be stable yet.
64 * Perl_mro_meta_init isn't declared static, so we could just define it
65 * ourselfs if perls headers don't do that for us, except that won't work
66 * on platforms where symbols need to be explicitly exported when linking
69 * So our, hopefully temporary, solution is to be even more evil and
70 * basically reimplement HvMROMETA in a very fragile way that'll blow up
71 * when the relevant parts of the mro implementation in core change.
77 return HvAUX(stash)->xhv_mro_meta
78 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
82 #else /* pre 5.10.0 */
85 mop_check_package_cache_flag(pTHX_ HV *stash) {
86 PERL_UNUSED_ARG(stash);
87 assert(SvTYPE(stash) == SVt_PVHV);
89 return PL_sub_generation;
93 #define call0(s, m) mop_call0(aTHX_ s, m)
95 mop_call0(pTHX_ SV *const self, SV *const method) {
103 call_sv(method, G_SCALAR | G_METHOD);
113 get_code_info (SV *coderef, char **pkg, char **name)
115 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
119 coderef = SvRV(coderef);
120 /* I think this only gets triggered with a mangled coderef, but if
121 we hit it without the guard, we segfault. The slightly odd return
122 value strikes me as an improvement (mst)
125 if ( isGV_with_GP(CvGV(coderef)) ) {
127 *pkg = HvNAME( GvSTASH(CvGV(coderef)) );
128 *name = GvNAME( CvGV(coderef) );
131 *pkg = "__UNKNOWN__";
149 get_all_package_symbols(HV *stash, type_filter_t filter)
154 (void)hv_iterinit(stash);
156 if (filter == TYPE_FILTER_NONE) {
157 while ( (he = hv_iternext(stash)) ) {
159 char *key = HePV(he, keylen);
160 if (!hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0)) {
161 croak("failed to store glob ref");
168 while ( (he = hv_iternext(stash)) ) {
169 SV *const gv = HeVAL(he);
176 switch( SvTYPE(gv) ) {
182 /* expand the gv into a real typeglob if it
183 * contains stub functions and we were asked to
184 * return CODE symbols */
185 if (filter == TYPE_FILTER_CODE) {
187 /* we don't really care about the length,
188 but that's the API */
189 key = HePV(he, keylen);
190 package = HvNAME(stash);
191 fq = newSVpvf("%s::%s", package, key);
192 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
196 key = HePV(he, keylen);
197 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
202 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
203 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
204 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
205 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
206 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
208 croak("Unknown type");
216 char *key = HePV(he, keylen);
217 if (!hv_store(ret, key, keylen, newRV_inc(sv), 0)) {
218 croak("failed to store symbol ref");
228 mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
229 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
230 SV *method_metaclass_name;
237 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
239 (void)hv_iterinit(symbols);
240 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
241 CV *cv = (CV *)SvRV(coderef);
247 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
251 /* this checks to see that the subroutine is actually from our package */
252 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
253 if ( strNE(cvpkg_name, class_name_pv) ) {
258 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
259 if ( SvOK(method_slot) ) {
260 SV *const body = call0(method_slot, key_body); /* $method_object->body() */
261 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
266 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
269 $method_object = $method_metaclass->wrap(
271 associated_metaclass => $self,
272 package_name => $class_name,
281 PUSHs(method_metaclass_name); /* invocant */
282 mPUSHs(newRV_inc((SV *)cv));
283 PUSHs(associated_metaclass);
285 PUSHs(key_package_name);
288 mPUSHs(newSVpv(method_name, method_name_len));
291 call_sv(wrap, G_SCALAR | G_METHOD);
293 method_object = POPs;
295 /* $map->{$method_name} = $method_object */
296 sv_setsv(method_slot, method_object);
305 Pass in a coderef, returns:
306 [ $pkg_name, $coderef_name ] ie:
307 [ 'Foo::Bar', 'new' ]
310 MODULE = Class::MOP PACKAGE = Class::MOP
313 key_name = newSVpvs("name");
314 key_body = newSVpvs("body");
315 key_package = newSVpvs("package");
316 key_package_name = newSVpvs("package_name");
317 key_package_cache_flag = newSVpvs("_package_cache_flag");
318 key_methods = newSVpvs("methods");
319 key_VERSION = newSVpvs("VERSION");
320 key_ISA = newSVpvs("ISA");
322 PERL_HASH(hash_name, "name", 4);
323 PERL_HASH(hash_body, "body", 4);
324 PERL_HASH(hash_package, "package", 7);
325 PERL_HASH(hash_package_name, "package_name", 12);
326 PERL_HASH(hash_package_cache_flag, "_package_cache_flag", 19);
327 PERL_HASH(hash_methods, "methods", 7);
328 PERL_HASH(hash_VERSION, "VERSION", 7);
329 PERL_HASH(hash_ISA, "ISA", 3);
331 method_metaclass = newSVpvs("method_metaclass");
332 wrap = newSVpvs("wrap");
333 associated_metaclass = newSVpvs("associated_metaclass");
340 get_code_info(coderef)
346 if (get_code_info(coderef, &pkg, &name)) {
348 PUSHs(newSVpv(pkg, 0));
349 PUSHs(newSVpv(name, 0));
355 is_class_loaded(klass=&PL_sv_undef)
363 if (!SvPOK(klass) || !SvCUR(klass)) {
367 stash = gv_stashsv(klass, 0);
372 if (hv_exists_ent (stash, key_VERSION, hash_VERSION)) {
373 HE *version = hv_fetch_ent(stash, key_VERSION, 0, hash_VERSION);
374 if (version && HeVAL(version) && GvSV(HeVAL(version))) {
379 if (hv_exists_ent (stash, key_ISA, hash_ISA)) {
380 HE *isa = hv_fetch_ent(stash, key_ISA, 0, hash_ISA);
381 if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
386 (void)hv_iterinit(stash);
387 while ((gv = (GV *)hv_iternextsv(stash, &key, &keylen))) {
392 if (key[keylen - 1] == ':' && key[keylen - 2] == ':') {
396 if (!isGV(gv) || GvCV(gv) || GvSV(gv) || GvAV(gv) || GvHV(gv) || GvIO(gv) || GvFORM(gv)) {
403 MODULE = Class::MOP PACKAGE = Class::MOP::Package
408 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
417 if ( ! SvROK(self) ) {
418 die("Cannot call get_all_package_symbols as a class method");
421 if (GIMME_V == G_VOID) {
428 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
429 stash = gv_stashsv(HeVAL(he), 0);
435 case G_SCALAR: XSRETURN_UNDEF; break;
436 case G_ARRAY: XSRETURN_EMPTY; break;
440 symbols = get_all_package_symbols(stash, filter);
444 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
447 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
449 EXTEND(SP, HvKEYS(symbols) * 2);
451 while ((he = hv_iternext(symbols))) {
452 PUSHs(hv_iterkeysv(he));
453 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
461 SvREFCNT_dec((SV *)symbols);
469 if ( ! SvROK(self) ) {
470 die("Cannot call name as a class method");
473 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
476 ST(0) = &PL_sv_undef;
478 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
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;
495 MODULE = Class::MOP PACKAGE = Class::MOP::Method
503 if ( ! SvROK(self) ) {
504 die("Cannot call name as a class method");
507 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
510 ST(0) = &PL_sv_undef;
518 if ( ! SvROK(self) ) {
519 die("Cannot call package_name as a class method");
522 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
525 ST(0) = &PL_sv_undef;
533 if ( ! SvROK(self) ) {
534 die("Cannot call body as a class method");
537 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
540 ST(0) = &PL_sv_undef;
543 MODULE = Class::MOP PACKAGE = Class::MOP::Class
549 HV *const obj = (HV *)SvRV(self);
550 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
551 HV *const stash = gv_stashsv(class_name, 0);
552 UV const current = check_package_cache_flag(stash);
553 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
554 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
556 /* in $self->{methods} does not yet exist (or got deleted) */
557 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
558 SV *new_map_ref = newRV_noinc((SV *)newHV());
559 sv_2mortal(new_map_ref);
560 sv_setsv(map_ref, new_map_ref);
563 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
564 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
565 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */