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 hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0);
137 while ( (he = hv_iternext(stash)) ) {
138 SV *const gv = HeVAL(he);
145 switch( SvTYPE(gv) ) {
151 /* expand the gv into a real typeglob if it
152 * contains stub functions and we were asked to
153 * return CODE symbols */
154 if (filter == TYPE_FILTER_CODE) {
156 /* we don't really care about the length,
157 but that's the API */
158 key = HePV(he, keylen);
159 package = HvNAME(stash);
160 fq = newSVpvf("%s::%s", package, key);
161 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
165 key = HePV(he, keylen);
166 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
171 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
172 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
173 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
174 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
175 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
177 croak("Unknown type");
185 char *key = HePV(he, keylen);
186 hv_store(ret, key, keylen, newRV_inc(sv), 0);
195 mop_update_method_map(pTHX_ SV* const self, SV* const class_name, HV* const stash, HV* const map) {
196 const char* const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
197 SV* method_metaclass_name;
204 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
206 (void)hv_iterinit(symbols);
207 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
208 CV* cv = (CV *)SvRV(coderef);
214 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
218 /* this checks to see that the subroutine is actually from our package */
219 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
220 if ( strNE(cvpkg_name, class_name_pv) ) {
225 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
226 if ( SvOK(method_slot) ) {
227 SV* const body = call0(method_slot, key_body); /* $method_object->body() */
228 if ( SvROK(body) && ((CV*) SvRV(body)) == cv ) {
233 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
236 $method_object = $method_metaclass->wrap(
238 associated_metaclass => $self,
239 package_name => $class_name,
248 PUSHs(method_metaclass_name); /* invocant */
249 mPUSHs(newRV_inc((SV*)cv));
250 PUSHs(associated_metaclass);
252 PUSHs(key_package_name);
255 mPUSHs(newSVpv(method_name, method_name_len));
258 call_sv(wrap, G_SCALAR | G_METHOD);
260 method_object = POPs;
262 /* $map->{$method_name} = $method_object */
263 sv_setsv(method_slot, method_object);
272 Pass in a coderef, returns:
273 [ $pkg_name, $coderef_name ] ie:
274 [ 'Foo::Bar', 'new' ]
277 MODULE = Class::MOP PACKAGE = Class::MOP
280 key_name = newSVpvs("name");
281 key_body = newSVpvs("body");
282 key_package = newSVpvs("package");
283 key_package_name = newSVpvs("package_name");
284 key_package_cache_flag = newSVpvs("_package_cache_flag");
285 key_methods = newSVpvs("methods");
287 PERL_HASH(hash_name, "name", 4);
288 PERL_HASH(hash_body, "body", 4);
289 PERL_HASH(hash_package, "package", 7);
290 PERL_HASH(hash_package_name, "package_name", 12);
291 PERL_HASH(hash_package_cache_flag, "_package_cache_flag", 19);
292 PERL_HASH(hash_methods, "methods", 7);
294 method_metaclass = newSVpvs("method_metaclass");
295 wrap = newSVpvs("wrap");
296 associated_metaclass = newSVpvs("associated_metaclass");
303 get_code_info(coderef)
309 if (get_code_info(coderef, &pkg, &name)) {
311 PUSHs(newSVpv(pkg, 0));
312 PUSHs(newSVpv(name, 0));
316 MODULE = Class::MOP PACKAGE = Class::MOP::Package
319 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
328 if ( ! SvROK(self) ) {
329 die("Cannot call get_all_package_symbols as a class method");
332 if (GIMME_V == G_VOID) {
339 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
340 stash = gv_stashsv(HeVAL(he), 0);
346 case G_SCALAR: XSRETURN_UNDEF; break;
347 case G_ARRAY: XSRETURN_EMPTY; break;
351 symbols = get_all_package_symbols(stash, filter);
355 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
358 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
360 EXTEND(SP, HvKEYS(symbols) * 2);
362 while ((he = hv_iternext(symbols))) {
363 PUSHs(hv_iterkeysv(he));
364 PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
372 SvREFCNT_dec((SV *)symbols);
380 if ( ! SvROK(self) ) {
381 die("Cannot call name as a class method");
384 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
387 ST(0) = &PL_sv_undef;
389 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
397 if ( ! SvROK(self) ) {
398 die("Cannot call name as a class method");
401 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
404 ST(0) = &PL_sv_undef;
406 MODULE = Class::MOP PACKAGE = Class::MOP::Method
414 if ( ! SvROK(self) ) {
415 die("Cannot call name as a class method");
418 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
421 ST(0) = &PL_sv_undef;
429 if ( ! SvROK(self) ) {
430 die("Cannot call package_name as a class method");
433 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
436 ST(0) = &PL_sv_undef;
444 if ( ! SvROK(self) ) {
445 die("Cannot call body as a class method");
448 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
451 ST(0) = &PL_sv_undef;
454 MODULE = Class::MOP PACKAGE = Class::MOP::Class
460 HV *const obj = (HV *)SvRV(self);
461 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
462 HV *const stash = gv_stashsv(class_name, 0);
463 UV const current = check_package_cache_flag(stash);
464 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
465 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
467 /* in $self->{methods} does not yet exist (or got deleted) */
468 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
469 SV* new_map_ref = newRV_noinc((SV *)newHV());
470 sv_2mortal(new_map_ref);
471 sv_setsv(map_ref, new_map_ref);
474 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
475 mop_update_method_map(aTHX_ self, class_name, stash, (HV*)SvRV(map_ref));
476 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */