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;
36 SV *associated_metaclass;
40 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
41 #ifdef HvMROMETA /* 5.10.0 */
44 #define mro_meta_init(stash) Perl_mro_meta_init(aTHX_ stash) /* used in HvMROMETA macro */
45 #endif /* !mro_meta_init */
48 mop_check_package_cache_flag(pTHX_ HV* stash) {
49 assert(SvTYPE(stash) == SVt_PVHV);
51 return HvMROMETA(stash)->pkg_gen; /* mro::get_pkg_gen($pkg) */
54 #else /* pre 5.10.0 */
57 mop_check_package_cache_flag(pTHX_ HV *stash) {
58 PERL_UNUSED_ARG(stash);
59 assert(SvTYPE(stash) == SVt_PVHV);
61 return PL_sub_generation;
65 #define call0(s, m) mop_call0(aTHX_ s, m)
67 mop_call0(pTHX_ SV *const self, SV *const method) {
75 call_sv(method, G_SCALAR | G_METHOD);
85 get_code_info (SV *coderef, char **pkg, char **name)
87 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
91 coderef = SvRV(coderef);
92 /* I think this only gets triggered with a mangled coderef, but if
93 we hit it without the guard, we segfault. The slightly odd return
94 value strikes me as an improvement (mst)
97 if ( isGV_with_GP(CvGV(coderef)) ) {
99 *pkg = HvNAME( GvSTASH(CvGV(coderef)) );
100 *name = GvNAME( CvGV(coderef) );
103 *pkg = "__UNKNOWN__";
121 get_all_package_symbols(HV *stash, type_filter_t filter)
126 (void)hv_iterinit(stash);
128 if (filter == TYPE_FILTER_NONE) {
129 while ( (he = hv_iternext(stash)) ) {
131 char *key = HePV(he, keylen);
132 if (!hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0)) {
133 croak("failed to store glob ref");
140 while ( (he = hv_iternext(stash)) ) {
141 SV *const gv = HeVAL(he);
148 switch( SvTYPE(gv) ) {
154 /* expand the gv into a real typeglob if it
155 * contains stub functions and we were asked to
156 * return CODE symbols */
157 if (filter == TYPE_FILTER_CODE) {
159 /* we don't really care about the length,
160 but that's the API */
161 key = HePV(he, keylen);
162 package = HvNAME(stash);
163 fq = newSVpvf("%s::%s", package, key);
164 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
168 key = HePV(he, keylen);
169 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
174 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
175 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
176 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
177 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
178 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
180 croak("Unknown type");
188 char *key = HePV(he, keylen);
189 if (!hv_store(ret, key, keylen, newRV_inc(sv), 0)) {
190 croak("failed to store symbol ref");
200 mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
201 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
202 SV *method_metaclass_name;
209 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
211 (void)hv_iterinit(symbols);
212 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
213 CV *cv = (CV *)SvRV(coderef);
219 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
223 /* this checks to see that the subroutine is actually from our package */
224 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
225 if ( strNE(cvpkg_name, class_name_pv) ) {
230 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
231 if ( SvOK(method_slot) ) {
232 SV* const body = call0(method_slot, key_body); /* $method_object->body() */
233 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
238 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
241 $method_object = $method_metaclass->wrap(
243 associated_metaclass => $self,
244 package_name => $class_name,
253 PUSHs(method_metaclass_name); /* invocant */
254 mPUSHs(newRV_inc((SV *)cv));
255 PUSHs(associated_metaclass);
257 PUSHs(key_package_name);
260 mPUSHs(newSVpv(method_name, method_name_len));
263 call_sv(wrap, G_SCALAR | G_METHOD);
265 method_object = POPs;
267 /* $map->{$method_name} = $method_object */
268 sv_setsv(method_slot, method_object);
277 Pass in a coderef, returns:
278 [ $pkg_name, $coderef_name ] ie:
279 [ 'Foo::Bar', 'new' ]
282 MODULE = Class::MOP PACKAGE = Class::MOP
285 key_name = newSVpvs("name");
286 key_body = newSVpvs("body");
287 key_package = newSVpvs("package");
288 key_package_name = newSVpvs("package_name");
289 key_package_cache_flag = newSVpvs("_package_cache_flag");
290 key_methods = newSVpvs("methods");
292 PERL_HASH(hash_name, "name", 4);
293 PERL_HASH(hash_body, "body", 4);
294 PERL_HASH(hash_package, "package", 7);
295 PERL_HASH(hash_package_name, "package_name", 12);
296 PERL_HASH(hash_package_cache_flag, "_package_cache_flag", 19);
297 PERL_HASH(hash_methods, "methods", 7);
299 method_metaclass = newSVpvs("method_metaclass");
300 wrap = newSVpvs("wrap");
301 associated_metaclass = newSVpvs("associated_metaclass");
308 get_code_info(coderef)
314 if (get_code_info(coderef, &pkg, &name)) {
316 PUSHs(newSVpv(pkg, 0));
317 PUSHs(newSVpv(name, 0));
321 MODULE = Class::MOP PACKAGE = Class::MOP::Package
324 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
333 if ( ! SvROK(self) ) {
334 die("Cannot call get_all_package_symbols as a class method");
337 if (GIMME_V == G_VOID) {
344 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
345 stash = gv_stashsv(HeVAL(he), 0);
351 case G_SCALAR: XSRETURN_UNDEF; break;
352 case G_ARRAY: XSRETURN_EMPTY; break;
356 symbols = get_all_package_symbols(stash, filter);
360 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
363 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
365 EXTEND(SP, HvKEYS(symbols) * 2);
367 while ((he = hv_iternext(symbols))) {
368 PUSHs(hv_iterkeysv(he));
369 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
377 SvREFCNT_dec((SV *)symbols);
385 if ( ! SvROK(self) ) {
386 die("Cannot call name as a class method");
389 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
392 ST(0) = &PL_sv_undef;
394 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
402 if ( ! SvROK(self) ) {
403 die("Cannot call name as a class method");
406 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
409 ST(0) = &PL_sv_undef;
411 MODULE = Class::MOP PACKAGE = Class::MOP::Method
419 if ( ! SvROK(self) ) {
420 die("Cannot call name as a class method");
423 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
426 ST(0) = &PL_sv_undef;
434 if ( ! SvROK(self) ) {
435 die("Cannot call package_name as a class method");
438 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
441 ST(0) = &PL_sv_undef;
449 if ( ! SvROK(self) ) {
450 die("Cannot call body as a class method");
453 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
456 ST(0) = &PL_sv_undef;
459 MODULE = Class::MOP PACKAGE = Class::MOP::Class
465 HV *const obj = (HV *)SvRV(self);
466 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
467 HV *const stash = gv_stashsv(class_name, 0);
468 UV const current = check_package_cache_flag(stash);
469 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
470 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
472 /* in $self->{methods} does not yet exist (or got deleted) */
473 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
474 SV *new_map_ref = newRV_noinc((SV *)newHV());
475 sv_2mortal(new_map_ref);
476 sv_setsv(map_ref, new_map_ref);
479 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
480 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
481 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */