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;
28 SV *key_package_cache_flag;
29 U32 hash_package_cache_flag;
35 SV *associated_metaclass;
39 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
40 #ifdef HvMROMETA /* 5.10.0 */
43 #define mro_meta_init(stash) Perl_mro_meta_init(aTHX_ stash) /* used in HvMROMETA macro */
44 #endif /* !mro_meta_init */
47 mop_check_package_cache_flag(pTHX_ HV* stash) {
48 assert(SvTYPE(stash) == SVt_PVHV);
50 return HvMROMETA(stash)->pkg_gen; /* mro::get_pkg_gen($pkg) */
53 #else /* pre 5.10.0 */
56 mop_check_package_cache_flag(pTHX_ HV *stash) {
57 PERL_UNUSED_ARG(stash);
58 assert(SvTYPE(stash) == SVt_PVHV);
60 return PL_sub_generation;
64 #define call0(s, m) mop_call0(aTHX_ s, m)
66 mop_call0(pTHX_ SV *const self, SV *const method) {
74 call_sv(method, G_SCALAR | G_METHOD);
84 get_code_info (SV *coderef, char **pkg, char **name)
86 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
90 coderef = SvRV(coderef);
91 /* I think this only gets triggered with a mangled coderef, but if
92 we hit it without the guard, we segfault. The slightly odd return
93 value strikes me as an improvement (mst)
96 if ( isGV_with_GP(CvGV(coderef)) ) {
98 *pkg = HvNAME( GvSTASH(CvGV(coderef)) );
99 *name = GvNAME( CvGV(coderef) );
102 *pkg = "__UNKNOWN__";
120 get_all_package_symbols(HV *stash, type_filter_t filter)
125 (void)hv_iterinit(stash);
127 if (filter == TYPE_FILTER_NONE) {
128 while ( (he = hv_iternext(stash)) ) {
130 char *key = HePV(he, keylen);
131 if (!hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0)) {
132 croak("failed to store glob ref");
139 while ( (he = hv_iternext(stash)) ) {
140 SV *const gv = HeVAL(he);
147 switch( SvTYPE(gv) ) {
153 /* expand the gv into a real typeglob if it
154 * contains stub functions and we were asked to
155 * return CODE symbols */
156 if (filter == TYPE_FILTER_CODE) {
158 /* we don't really care about the length,
159 but that's the API */
160 key = HePV(he, keylen);
161 package = HvNAME(stash);
162 fq = newSVpvf("%s::%s", package, key);
163 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
167 key = HePV(he, keylen);
168 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
173 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
174 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
175 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
176 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
177 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
179 croak("Unknown type");
187 char *key = HePV(he, keylen);
188 if (!hv_store(ret, key, keylen, newRV_inc(sv), 0)) {
189 croak("failed to store symbol ref");
199 mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
200 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
201 SV *method_metaclass_name;
208 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
210 (void)hv_iterinit(symbols);
211 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
212 CV *cv = (CV *)SvRV(coderef);
218 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
222 /* this checks to see that the subroutine is actually from our package */
223 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
224 if ( strNE(cvpkg_name, class_name_pv) ) {
229 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
230 if ( SvOK(method_slot) ) {
231 SV* const body = call0(method_slot, key_body); /* $method_object->body() */
232 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
237 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
240 $method_object = $method_metaclass->wrap(
242 associated_metaclass => $self,
243 package_name => $class_name,
252 PUSHs(method_metaclass_name); /* invocant */
253 mPUSHs(newRV_inc((SV *)cv));
254 PUSHs(associated_metaclass);
256 PUSHs(key_package_name);
259 mPUSHs(newSVpv(method_name, method_name_len));
262 call_sv(wrap, G_SCALAR | G_METHOD);
264 method_object = POPs;
266 /* $map->{$method_name} = $method_object */
267 sv_setsv(method_slot, method_object);
276 Pass in a coderef, returns:
277 [ $pkg_name, $coderef_name ] ie:
278 [ 'Foo::Bar', 'new' ]
281 MODULE = Class::MOP PACKAGE = Class::MOP
284 key_name = newSVpvs("name");
285 key_body = newSVpvs("body");
286 key_package = newSVpvs("package");
287 key_package_name = newSVpvs("package_name");
288 key_package_cache_flag = newSVpvs("_package_cache_flag");
289 key_methods = newSVpvs("methods");
291 PERL_HASH(hash_name, "name", 4);
292 PERL_HASH(hash_body, "body", 4);
293 PERL_HASH(hash_package, "package", 7);
294 PERL_HASH(hash_package_name, "package_name", 12);
295 PERL_HASH(hash_package_cache_flag, "_package_cache_flag", 19);
296 PERL_HASH(hash_methods, "methods", 7);
298 method_metaclass = newSVpvs("method_metaclass");
299 wrap = newSVpvs("wrap");
300 associated_metaclass = newSVpvs("associated_metaclass");
307 get_code_info(coderef)
313 if (get_code_info(coderef, &pkg, &name)) {
315 PUSHs(newSVpv(pkg, 0));
316 PUSHs(newSVpv(name, 0));
320 MODULE = Class::MOP PACKAGE = Class::MOP::Package
323 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
332 if ( ! SvROK(self) ) {
333 die("Cannot call get_all_package_symbols as a class method");
336 if (GIMME_V == G_VOID) {
343 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
344 stash = gv_stashsv(HeVAL(he), 0);
350 case G_SCALAR: XSRETURN_UNDEF; break;
351 case G_ARRAY: XSRETURN_EMPTY; break;
355 symbols = get_all_package_symbols(stash, filter);
359 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
362 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
364 EXTEND(SP, HvKEYS(symbols) * 2);
366 while ((he = hv_iternext(symbols))) {
367 PUSHs(hv_iterkeysv(he));
368 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
376 SvREFCNT_dec((SV *)symbols);
384 if ( ! SvROK(self) ) {
385 die("Cannot call name as a class method");
388 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
391 ST(0) = &PL_sv_undef;
393 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
401 if ( ! SvROK(self) ) {
402 die("Cannot call name as a class method");
405 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
408 ST(0) = &PL_sv_undef;
410 MODULE = Class::MOP PACKAGE = Class::MOP::Method
418 if ( ! SvROK(self) ) {
419 die("Cannot call name as a class method");
422 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
425 ST(0) = &PL_sv_undef;
433 if ( ! SvROK(self) ) {
434 die("Cannot call package_name as a class method");
437 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
440 ST(0) = &PL_sv_undef;
448 if ( ! SvROK(self) ) {
449 die("Cannot call body as a class method");
452 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
455 ST(0) = &PL_sv_undef;
458 MODULE = Class::MOP PACKAGE = Class::MOP::Class
464 HV *const obj = (HV *)SvRV(self);
465 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
466 HV *const stash = gv_stashsv(class_name, 0);
467 UV const current = check_package_cache_flag(stash);
468 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
469 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
471 /* in $self->{methods} does not yet exist (or got deleted) */
472 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
473 SV *new_map_ref = newRV_noinc((SV *)newHV());
474 sv_2mortal(new_map_ref);
475 sv_setsv(map_ref, new_map_ref);
478 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
479 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
480 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */