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
17 #define DECLARE_KEY(name) SV *key_##name; U32 hash_##name;
19 #define PREHASH_KEY_WITH_VALUE(name, value) do { \
20 key_##name = newSVpvs(value); \
21 PERL_HASH(hash_##name, value, sizeof(value) - 1); \
24 /* this is basically the same as the above macro, except that the value will be
25 * the stringified name. However, we can't just implement this in terms of
26 * PREHASH_KEY_WITH_VALUE as that'd cause macro expansion on the value of
27 * 'name' when it's being passed to the other macro. suggestions on how to make
28 * this more elegant would be much appreciated */
30 #define PREHASH_KEY(name) do { \
31 key_##name = newSVpvs(#name); \
32 PERL_HASH(hash_##name, #name, sizeof(#name) - 1); \
37 DECLARE_KEY(package_name);
39 DECLARE_KEY(package_cache_flag);
45 SV *associated_metaclass;
49 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
50 #if PERL_VERSION >= 10
53 mop_check_package_cache_flag(pTHX_ HV* stash) {
54 assert(SvTYPE(stash) == SVt_PVHV);
56 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
57 * however the perl core doesn't make it easy for us. It doesn't provide an
58 * api that just does what we want.
60 * However, we know that the information we want is, inside the core,
61 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
62 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
63 * which is not public and only available inside the core, as the mro
64 * interface as well as the structure returned by mro_meta_init isn't
65 * considered to be stable yet.
67 * Perl_mro_meta_init isn't declared static, so we could just define it
68 * ourselfs if perls headers don't do that for us, except that won't work
69 * on platforms where symbols need to be explicitly exported when linking
72 * So our, hopefully temporary, solution is to be even more evil and
73 * basically reimplement HvMROMETA in a very fragile way that'll blow up
74 * when the relevant parts of the mro implementation in core change.
80 return HvAUX(stash)->xhv_mro_meta
81 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
85 #else /* pre 5.10.0 */
88 mop_check_package_cache_flag(pTHX_ HV *stash) {
89 PERL_UNUSED_ARG(stash);
90 assert(SvTYPE(stash) == SVt_PVHV);
92 return PL_sub_generation;
96 #define call0(s, m) mop_call0(aTHX_ s, m)
98 mop_call0(pTHX_ SV *const self, SV *const method) {
106 call_sv(method, G_SCALAR | G_METHOD);
116 get_code_info (SV *coderef, char **pkg, char **name)
118 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
122 coderef = SvRV(coderef);
123 /* I think this only gets triggered with a mangled coderef, but if
124 we hit it without the guard, we segfault. The slightly odd return
125 value strikes me as an improvement (mst)
128 if ( isGV_with_GP(CvGV(coderef)) ) {
130 *pkg = HvNAME( GvSTASH(CvGV(coderef)) );
131 *name = GvNAME( CvGV(coderef) );
134 *pkg = "__UNKNOWN__";
151 typedef bool (*get_package_symbols_cb_t) (const char *, STRLEN, SV *, void *);
154 get_package_symbols(HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
158 (void)hv_iterinit(stash);
160 if (filter == TYPE_FILTER_NONE) {
161 while ( (he = hv_iternext(stash)) ) {
163 const char *key = HePV(he, keylen);
164 if (!cb(key, keylen, HeVAL(he), ud)) {
171 while ( (he = hv_iternext(stash)) ) {
172 SV *const gv = HeVAL(he);
179 switch( SvTYPE(gv) ) {
185 /* expand the gv into a real typeglob if it
186 * contains stub functions and we were asked to
187 * return CODE symbols */
188 if (filter == TYPE_FILTER_CODE) {
190 /* we don't really care about the length,
191 but that's the API */
192 key = HePV(he, keylen);
193 package = HvNAME(stash);
194 fq = newSVpvf("%s::%s", package, key);
195 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
199 key = HePV(he, keylen);
200 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
205 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
206 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
207 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
208 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
209 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
211 croak("Unknown type");
219 const char *key = HePV(he, keylen);
220 if (!cb(key, keylen, sv, ud)) {
228 find_method (const char *key, STRLEN keylen, SV *val, void *ud)
230 bool *found_method = (bool *)ud;
231 *found_method = TRUE;
236 collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
240 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
241 croak("failed to store symbol ref");
248 get_all_package_symbols (HV *stash, type_filter_t filter)
251 get_package_symbols (stash, filter, collect_all_symbols, ret);
256 mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
257 const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
258 SV *method_metaclass_name;
265 symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
267 (void)hv_iterinit(symbols);
268 while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
269 CV *cv = (CV *)SvRV(coderef);
275 if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
279 /* this checks to see that the subroutine is actually from our package */
280 if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
281 if ( strNE(cvpkg_name, class_name_pv) ) {
286 method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
287 if ( SvOK(method_slot) ) {
288 SV *const body = call0(method_slot, key_body); /* $method_object->body() */
289 if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
294 method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
297 $method_object = $method_metaclass->wrap(
299 associated_metaclass => $self,
300 package_name => $class_name,
309 PUSHs(method_metaclass_name); /* invocant */
310 mPUSHs(newRV_inc((SV *)cv));
311 PUSHs(associated_metaclass);
313 PUSHs(key_package_name);
316 mPUSHs(newSVpv(method_name, method_name_len));
319 call_sv(wrap, G_SCALAR | G_METHOD);
321 method_object = POPs;
323 /* $map->{$method_name} = $method_object */
324 sv_setsv(method_slot, method_object);
333 Pass in a coderef, returns:
334 [ $pkg_name, $coderef_name ] ie:
335 [ 'Foo::Bar', 'new' ]
338 MODULE = Class::MOP PACKAGE = Class::MOP
343 PREHASH_KEY(package);
344 PREHASH_KEY(package_name);
345 PREHASH_KEY(methods);
347 PREHASH_KEY(VERSION);
348 PREHASH_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag");
350 method_metaclass = newSVpvs("method_metaclass");
351 wrap = newSVpvs("wrap");
352 associated_metaclass = newSVpvs("associated_metaclass");
357 # use prototype here to be compatible with get_code_info from Sub::Identify
359 get_code_info(coderef)
366 if (get_code_info(coderef, &pkg, &name)) {
368 PUSHs(newSVpv(pkg, 0));
369 PUSHs(newSVpv(name, 0));
372 # This is some pretty grotty logic. It _should_ be parallel to the
373 # pure Perl version in lib/Class/MOP.pm, so if you want to understand
374 # it we suggest you start there.
376 is_class_loaded(klass=&PL_sv_undef)
380 bool found_method = FALSE;
382 if (!SvPOK(klass) || !SvCUR(klass)) {
386 stash = gv_stashsv(klass, 0);
391 if (hv_exists_ent (stash, key_VERSION, hash_VERSION)) {
392 HE *version = hv_fetch_ent(stash, key_VERSION, 0, hash_VERSION);
394 if (version && HeVAL(version) && (version_sv = GvSV(HeVAL(version)))) {
395 if (SvROK(version_sv)) {
396 SV *version_sv_ref = SvRV(version_sv);
398 if (SvOK(version_sv_ref)) {
402 else if (SvOK(version_sv)) {
408 if (hv_exists_ent (stash, key_ISA, hash_ISA)) {
409 HE *isa = hv_fetch_ent(stash, key_ISA, 0, hash_ISA);
410 if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
415 get_package_symbols(stash, TYPE_FILTER_CODE, find_method, &found_method);
422 MODULE = Class::MOP PACKAGE = Class::MOP::Package
425 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
433 if ( ! SvROK(self) ) {
434 die("Cannot call get_all_package_symbols as a class method");
437 if (GIMME_V == G_VOID) {
443 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
444 stash = gv_stashsv(HeVAL(he), 0);
452 symbols = get_all_package_symbols(stash, filter);
453 PUSHs(sv_2mortal(newRV_noinc((SV *)symbols)));
461 if ( ! SvROK(self) ) {
462 die("Cannot call name as a class method");
465 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
468 ST(0) = &PL_sv_undef;
470 MODULE = Class::MOP PACKAGE = Class::MOP::Attribute
478 if ( ! SvROK(self) ) {
479 die("Cannot call name as a class method");
482 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
485 ST(0) = &PL_sv_undef;
487 MODULE = Class::MOP PACKAGE = Class::MOP::Method
495 if ( ! SvROK(self) ) {
496 die("Cannot call name as a class method");
499 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
502 ST(0) = &PL_sv_undef;
510 if ( ! SvROK(self) ) {
511 die("Cannot call package_name as a class method");
514 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
517 ST(0) = &PL_sv_undef;
525 if ( ! SvROK(self) ) {
526 die("Cannot call body as a class method");
529 if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
532 ST(0) = &PL_sv_undef;
535 MODULE = Class::MOP PACKAGE = Class::MOP::Class
541 HV *const obj = (HV *)SvRV(self);
542 SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
543 HV *const stash = gv_stashsv(class_name, 0);
544 UV const current = check_package_cache_flag(stash);
545 SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
546 SV *const map_ref = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
548 /* in $self->{methods} does not yet exist (or got deleted) */
549 if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
550 SV *new_map_ref = newRV_noinc((SV *)newHV());
551 sv_2mortal(new_map_ref);
552 sv_setsv(map_ref, new_map_ref);
555 if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
556 mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
557 sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */