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_sv_2pv_flags
13 #define NEED_sv_2pv_nolen
23 U32 hash_package_name;
29 SV* associated_metaclass;
33 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
34 #ifdef HvMROMETA /* 5.10.0 */
37 #define mro_meta_init(stash) Perl_mro_meta_init(aTHX_ stash) /* used in HvMROMETA macro */
38 #endif /* !mro_meta_init */
41 mop_check_package_cache_flag(pTHX_ HV* stash) {
42 assert(SvTYPE(stash) == SVt_PVHV);
44 return HvMROMETA(stash)->pkg_gen; /* mro::get_pkg_gen($pkg) */
47 #else /* pre 5.10.0 */
50 mop_check_package_cache_flag(pTHX_ HV* stash) {
51 PERL_UNUSED_ARG(stash);
52 assert(SvTYPE(stash) == SVt_PVHV);
54 return PL_sub_generation;
58 #define call0(s, m) mop_call0(aTHX_ s, m)
60 mop_call0(pTHX_ SV* const self, SV* const method) {
68 call_sv(method, G_SCALAR | G_METHOD);
78 mop_update_method_map(pTHX_ SV* const self, SV* const class_name, HV* const stash, HV* const map) {
79 const char* const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
80 SV* method_metaclass_name;
86 /* this function massivly overlaps with the xs version of
87 * get_all_package_symbols. a common c function to walk the symbol table
88 * should be factored out and used by both. --rafl */
91 while ( (gv = (GV*)hv_iternextsv(stash, &method_name, &method_name_len)) ) {
93 switch (SvTYPE (gv)) {
99 /* rafl says that this wastes memory savings that GvSVs have
100 in 5.8.9 and 5.10.x. But without it some tests fail. rafl
101 says the right thing to do is to handle GvSVs differently
103 gv_init((GV*)gv, stash, method_name, method_name_len, GV_ADDMULTI);
109 if ( SvTYPE(gv) == SVt_PVGV && (cv = GvCVu(gv)) ) {
110 GV* const cvgv = CvGV(cv);
111 /* ($cvpkg_name, $cv_name) = get_code_info($cv) */
112 const char* const cvpkg_name = HvNAME(GvSTASH(cvgv));
113 const char* const cv_name = GvNAME(cvgv);
117 /* this checks to see that the subroutine is actually from our package */
118 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
119 if ( strNE(cvpkg_name, class_name_pv) ) {
124 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
125 if ( SvOK(method_slot) ) {
126 SV* const body = call0(method_slot, key_body); /* $method_object->body() */
127 if ( SvROK(body) && ((CV*) SvRV(body)) == cv ) {
132 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
135 $method_object = $method_metaclass->wrap(
137 associated_metaclass => $self,
138 package_name => $class_name,
147 PUSHs(method_metaclass_name); /* invocant */
148 mPUSHs(newRV_inc((SV*)cv));
149 PUSHs(associated_metaclass);
151 PUSHs(key_package_name);
154 mPUSHs(newSVpv(method_name, method_name_len));
157 call_sv(wrap, G_SCALAR | G_METHOD);
159 method_object = POPs;
161 /* $map->{$method_name} = $method_object */
162 sv_setsv(method_slot, method_object);
180 get_all_package_symbols(HV *stash, type_filter_t filter)
185 (void)hv_iterinit(stash);
187 if (filter == TYPE_FILTER_NONE) {
188 while ( (he = hv_iternext(stash)) ) {
190 char *key = HePV(he, keylen);
191 hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0);
197 while ( (he = hv_iternext(stash)) ) {
198 SV *const gv = HeVAL(he);
205 switch( SvTYPE(gv) ) {
211 /* expand the gv into a real typeglob if it
212 * contains stub functions and we were asked to
213 * return CODE symbols */
214 if (filter == TYPE_FILTER_CODE) {
216 /* we don't really care about the length,
217 but that's the API */
218 key = HePV(he, keylen);
219 package = HvNAME(stash);
220 fq = newSVpvf("%s::%s", package, key);
221 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
225 key = HePV(he, keylen);
226 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
231 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
232 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
233 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
234 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
235 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
237 croak("Unknown type");
245 char *key = HePV(he, keylen);
246 hv_store(ret, key, keylen, newRV_inc(sv), 0);
255 Pass in a coderef, returns:
256 [ $pkg_name, $coderef_name ] ie:
257 [ 'Foo::Bar', 'new' ]
260 MODULE = Class::MOP PACKAGE = Class::MOP
263 key_name = newSVpvs("name");
264 key_body = newSVpvs("body");
265 key_package = newSVpvs("package");
266 key_package_name = newSVpvs("package_name");
268 PERL_HASH(hash_name, "name", 4);
269 PERL_HASH(hash_body, "body", 4);
270 PERL_HASH(hash_package, "package", 7);
271 PERL_HASH(hash_package_name, "package_name", 12);
273 method_metaclass = newSVpvs("method_metaclass");
274 wrap = newSVpvs("wrap");
275 associated_metaclass = newSVpvs("associated_metaclass");
282 get_code_info(coderef)
288 if ( SvOK(coderef) && SvROK(coderef) && SvTYPE(SvRV(coderef)) == SVt_PVCV ) {
289 coderef = SvRV(coderef);
290 /* I think this only gets triggered with a mangled coderef, but if
291 we hit it without the guard, we segfault. The slightly odd return
292 value strikes me as an improvement (mst)
295 if ( isGV_with_GP(CvGV(coderef)) ) {
297 pkg = HvNAME( GvSTASH(CvGV(coderef)) );
298 name = GvNAME( CvGV(coderef) );
307 PUSHs(newSVpvn(pkg, strlen(pkg)));
308 PUSHs(newSVpvn(name, strlen(name)));
312 MODULE = Class::MOP PACKAGE = Class::MOP::Package
315 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
324 if ( ! SvROK(self) ) {
325 die("Cannot call get_all_package_symbols as a class method");
328 if (GIMME_V == G_VOID) {
335 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
336 stash = gv_stashsv(HeVAL(he), 0);
342 case G_SCALAR: XSRETURN_UNDEF; break;
343 case G_ARRAY: XSRETURN_EMPTY; break;
347 symbols = get_all_package_symbols(stash, filter);
351 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
354 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
356 EXTEND(SP, HvKEYS(symbols) * 2);
358 while ((he = hv_iternext(symbols))) {
359 PUSHs(hv_iterkeysv(he));
360 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
368 SvREFCNT_dec((SV *)symbols);
376 if ( ! SvROK(self) ) {
377 die("Cannot call name as a class method");
380 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
383 ST(0) = &PL_sv_undef;
385 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
393 if ( ! SvROK(self) ) {
394 die("Cannot call name as a class method");
397 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
400 ST(0) = &PL_sv_undef;
402 MODULE = Class::MOP PACKAGE = Class::MOP::Method
410 if ( ! SvROK(self) ) {
411 die("Cannot call name as a class method");
414 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
417 ST(0) = &PL_sv_undef;
425 if ( ! SvROK(self) ) {
426 die("Cannot call package_name as a class method");
429 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
432 ST(0) = &PL_sv_undef;
440 if ( ! SvROK(self) ) {
441 die("Cannot call body as a class method");
444 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
447 ST(0) = &PL_sv_undef;
450 MODULE = Class::MOP PACKAGE = Class::MOP::Class
456 SV* const class_name = HeVAL( hv_fetch_ent((HV*)SvRV(self), key_package, TRUE, hash_package) );
457 HV* const stash = gv_stashsv(class_name, TRUE);
458 UV const current = check_package_cache_flag(stash);
459 SV* const cache_flag = *hv_fetchs((HV*)SvRV(self), "_package_cache_flag", TRUE);
460 SV* const map_ref = *hv_fetchs((HV*)SvRV(self), "methods", TRUE);
462 if ( ! SvRV(self) ) {
463 die("Cannot call get_method_map as a class method");
466 /* in $self->{methods} does not yet exist (or got deleted) */
467 if ( ! (SvROK(map_ref) && SvTYPE(SvRV(map_ref)) == SVt_PVHV) ) {
468 SV* new_map_ref = newRV_noinc((SV*)newHV());
469 sv_2mortal(new_map_ref);
470 sv_setsv(map_ref, new_map_ref);
473 if ( ! (SvOK(cache_flag) && SvUV(cache_flag) == current) ) {
477 mop_update_method_map(aTHX_ self, class_name, stash, (HV*)SvRV(map_ref));
478 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */