3 * Copyright (c) 2007 Brandon L Black
4 * Copyright (c) 2007, 2008 Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * 'Which order shall we go in?' said Frodo. 'Eldest first, or quickest first?
13 * You'll be last either way, Master Peregrin.'
15 * [p.101 of _The Lord of the Rings_, I/iii: "A Conspiracy Unmasked"]
21 These functions are related to the method resolution order of perl classes
32 AV *(*resolve)(pTHX_ HV* stash, I32 level);
35 /* First one is the default */
36 static struct mro_alg mros[] = {
37 {"dfs", S_mro_get_linear_isa_dfs},
38 {"c3", S_mro_get_linear_isa_c3}
41 #define NUMBER_OF_MROS (sizeof(mros)/sizeof(struct mro_alg))
43 static const struct mro_alg *
44 S_get_mro_from_name(pTHX_ const char *const name) {
45 const struct mro_alg *algo = mros;
46 const struct mro_alg *const end = mros + NUMBER_OF_MROS;
48 if(strEQ(name, algo->name))
56 Perl_mro_meta_init(pTHX_ HV* stash)
58 struct mro_meta* newmeta;
60 PERL_ARGS_ASSERT_MRO_META_INIT;
62 assert(!(HvAUX(stash)->xhv_mro_meta));
63 Newxz(newmeta, 1, struct mro_meta);
64 HvAUX(stash)->xhv_mro_meta = newmeta;
65 newmeta->cache_gen = 1;
67 newmeta->mro_which = mros;
72 #if defined(USE_ITHREADS)
74 /* for sv_dup on new threads */
76 Perl_mro_meta_dup(pTHX_ struct mro_meta* smeta, CLONE_PARAMS* param)
78 struct mro_meta* newmeta;
80 PERL_ARGS_ASSERT_MRO_META_DUP;
82 Newx(newmeta, 1, struct mro_meta);
83 Copy(smeta, newmeta, 1, struct mro_meta);
85 if (newmeta->mro_linear_dfs)
86 newmeta->mro_linear_dfs
87 = MUTABLE_AV(SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_linear_dfs, param)));
88 if (newmeta->mro_linear_c3)
89 newmeta->mro_linear_c3
90 = MUTABLE_AV(SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_linear_c3, param)));
91 if (newmeta->mro_nextmethod)
92 newmeta->mro_nextmethod
93 = MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_nextmethod, param)));
96 = MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)newmeta->isa, param)));
101 #endif /* USE_ITHREADS */
104 Perl_get_isa_hash(pTHX_ HV *const stash)
107 struct mro_meta *const meta = HvMROMETA(stash);
109 PERL_ARGS_ASSERT_GET_ISA_HASH;
112 AV *const isa = mro_get_linear_isa(stash);
114 HV *const isa_hash = newHV();
115 /* Linearisation didn't build it for us, so do it here. */
116 SV *const *svp = AvARRAY(isa);
117 SV *const *const svp_end = svp + AvFILLp(isa) + 1;
118 const HEK *const canon_name = HvNAME_HEK(stash);
120 while (svp < svp_end) {
121 (void) hv_store_ent(isa_hash, *svp++, &PL_sv_undef, 0);
124 (void) hv_common(isa_hash, NULL, HEK_KEY(canon_name),
125 HEK_LEN(canon_name), HEK_FLAGS(canon_name),
126 HV_FETCH_ISSTORE, &PL_sv_undef,
127 HEK_HASH(canon_name));
128 (void) hv_store(isa_hash, "UNIVERSAL", 9, &PL_sv_undef, 0);
130 SvREADONLY_on(isa_hash);
132 meta->isa = isa_hash;
139 =for apidoc mro_get_linear_isa_dfs
141 Returns the Depth-First Search linearization of @ISA
142 the given stash. The return value is a read-only AV*.
143 C<level> should be 0 (it is used internally in this
144 function's recursion).
146 You are responsible for C<SvREFCNT_inc()> on the
147 return value if you plan to store it anywhere
148 semi-permanently (otherwise it might be deleted
149 out from under you the next time the cache is
155 S_mro_get_linear_isa_dfs(pTHX_ HV *stash, I32 level)
162 struct mro_meta* meta;
166 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_DFS;
167 assert(HvAUX(stash));
169 stashhek = HvNAME_HEK(stash);
171 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
174 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
177 meta = HvMROMETA(stash);
179 /* return cache if valid */
180 if((retval = meta->mro_linear_dfs)) {
184 /* not in cache, make a new one */
186 retval = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV())));
187 /* We use this later in this function, but don't need a reference to it
188 beyond the end of this function, so reference count is fine. */
189 our_name = newSVhek(stashhek);
190 av_push(retval, our_name); /* add ourselves at the top */
193 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
194 av = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
196 /* "stored" is used to keep track of all of the classnames we have added to
197 the MRO so far, so we can do a quick exists check and avoid adding
198 duplicate classnames to the MRO as we go.
199 It's then retained to be re-used as a fast lookup for ->isa(), by adding
200 our own name and "UNIVERSAL" to it. */
202 stored = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
204 if(av && AvFILLp(av) >= 0) {
206 SV **svp = AvARRAY(av);
207 I32 items = AvFILLp(av) + 1;
211 SV* const sv = *svp++;
212 HV* const basestash = gv_stashsv(sv, 0);
217 /* if no stash exists for this @ISA member,
218 simply add it to the MRO and move on */
223 /* otherwise, recurse into ourselves for the MRO
224 of this @ISA member, and append their MRO to ours.
225 The recursive call could throw an exception, which
226 has memory management implications here, hence the use of
228 const AV *const subrv
229 = mro_get_linear_isa_dfs(basestash, level + 1);
231 subrv_p = AvARRAY(subrv);
232 subrv_items = AvFILLp(subrv) + 1;
234 while(subrv_items--) {
235 SV *const subsv = *subrv_p++;
236 /* LVALUE fetch will create a new undefined SV if necessary
238 HE *const he = hv_fetch_ent(stored, subsv, 1, 0);
240 if(HeVAL(he) != &PL_sv_undef) {
241 /* It was newly created. Steal it for our new SV, and
242 replace it in the hash with the "real" thing. */
243 SV *const val = HeVAL(he);
244 HEK *const key = HeKEY_hek(he);
246 HeVAL(he) = &PL_sv_undef;
247 /* Save copying by making a shared hash key scalar. We
248 inline this here rather than calling Perl_newSVpvn_share
249 because we already have the scalar, and we already have
251 assert(SvTYPE(val) == SVt_NULL);
252 sv_upgrade(val, SVt_PV);
253 SvPV_set(val, HEK_KEY(share_hek_hek(key)));
254 SvCUR_set(val, HEK_LEN(key));
261 av_push(retval, val);
267 (void) hv_store_ent(stored, our_name, &PL_sv_undef, 0);
268 (void) hv_store(stored, "UNIVERSAL", 9, &PL_sv_undef, 0);
270 SvREFCNT_inc_simple_void_NN(stored);
272 SvREADONLY_on(stored);
276 /* now that we're past the exception dangers, grab our own reference to
277 the AV we're about to use for the result. The reference owned by the
278 mortals' stack will be released soon, so everything will balance. */
279 SvREFCNT_inc_simple_void_NN(retval);
282 /* we don't want anyone modifying the cache entry but us,
283 and we do so by replacing it completely */
284 SvREADONLY_on(retval);
286 meta->mro_linear_dfs = retval;
291 =for apidoc mro_get_linear_isa_c3
293 Returns the C3 linearization of @ISA
294 the given stash. The return value is a read-only AV*.
295 C<level> should be 0 (it is used internally in this
296 function's recursion).
298 You are responsible for C<SvREFCNT_inc()> on the
299 return value if you plan to store it anywhere
300 semi-permanently (otherwise it might be deleted
301 out from under you the next time the cache is
308 S_mro_get_linear_isa_c3(pTHX_ HV* stash, I32 level)
315 struct mro_meta* meta;
317 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_C3;
318 assert(HvAUX(stash));
320 stashhek = HvNAME_HEK(stash);
322 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
325 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
328 meta = HvMROMETA(stash);
330 /* return cache if valid */
331 if((retval = meta->mro_linear_c3)) {
335 /* not in cache, make a new one */
337 gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
338 isa = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
340 /* For a better idea how the rest of this works, see the much clearer
341 pure perl version in Algorithm::C3 0.01:
342 http://search.cpan.org/src/STEVAN/Algorithm-C3-0.01/lib/Algorithm/C3.pm
343 (later versions go about it differently than this code for speed reasons)
346 if(isa && AvFILLp(isa) >= 0) {
349 HV* const tails = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
350 AV *const seqs = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV())));
353 /* This builds @seqs, which is an array of arrays.
354 The members of @seqs are the MROs of
355 the members of @ISA, followed by @ISA itself.
357 I32 items = AvFILLp(isa) + 1;
358 SV** isa_ptr = AvARRAY(isa);
360 SV* const isa_item = *isa_ptr++;
361 HV* const isa_item_stash = gv_stashsv(isa_item, 0);
362 if(!isa_item_stash) {
363 /* if no stash, make a temporary fake MRO
364 containing just itself */
365 AV* const isa_lin = newAV();
366 av_push(isa_lin, newSVsv(isa_item));
367 av_push(seqs, MUTABLE_SV(isa_lin));
371 AV* const isa_lin = mro_get_linear_isa_c3(isa_item_stash, level + 1);
372 av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa_lin)));
375 av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa)));
377 /* This builds "heads", which as an array of integer array
378 indices, one per seq, which point at the virtual "head"
379 of the seq (initially zero) */
380 Newxz(heads, AvFILLp(seqs)+1, I32);
382 /* This builds %tails, which has one key for every class
383 mentioned in the tail of any sequence in @seqs (tail meaning
384 everything after the first class, the "head"). The value
385 is how many times this key appears in the tails of @seqs.
387 seqs_ptr = AvARRAY(seqs);
388 seqs_items = AvFILLp(seqs) + 1;
389 while(seqs_items--) {
390 AV *const seq = MUTABLE_AV(*seqs_ptr++);
391 I32 seq_items = AvFILLp(seq);
393 SV** seq_ptr = AvARRAY(seq) + 1;
395 SV* const seqitem = *seq_ptr++;
396 /* LVALUE fetch will create a new undefined SV if necessary
398 HE* const he = hv_fetch_ent(tails, seqitem, 1, 0);
400 SV* const val = HeVAL(he);
401 /* This will increment undef to 1, which is what we
402 want for a newly created entry. */
409 /* Initialize retval to build the return value in */
411 av_push(retval, newSVhek(stashhek)); /* us first */
413 /* This loop won't terminate until we either finish building
414 the MRO, or get an exception. */
420 /* "foreach $seq (@seqs)" */
421 SV** const avptr = AvARRAY(seqs);
422 for(s = 0; s <= AvFILLp(seqs); s++) {
424 AV * const seq = MUTABLE_AV(avptr[s]);
426 if(!seq) continue; /* skip empty seqs */
427 svp = av_fetch(seq, heads[s], 0);
428 seqhead = *svp; /* seqhead = head of this seq */
432 /* if we haven't found a winner for this round yet,
433 and this seqhead is not in tails (or the count
434 for it in tails has dropped to zero), then this
435 seqhead is our new winner, and is added to the
436 final MRO immediately */
438 if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
439 && (val = HeVAL(tail_entry))
442 winner = newSVsv(cand);
443 av_push(retval, winner);
444 /* note however that even when we find a winner,
445 we continue looping over @seqs to do housekeeping */
447 if(!sv_cmp(seqhead, winner)) {
448 /* Once we have a winner (including the iteration
449 where we first found him), inc the head ptr
450 for any seq which had the winner as a head,
451 NULL out any seq which is now empty,
452 and adjust tails for consistency */
454 const int new_head = ++heads[s];
455 if(new_head > AvFILLp(seq)) {
456 SvREFCNT_dec(avptr[s]);
462 /* Because we know this new seqhead used to be
463 a tail, we can assume it is in tails and has
464 a positive value, which we need to dec */
465 svp = av_fetch(seq, new_head, 0);
467 tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
468 val = HeVAL(tail_entry);
474 /* if we found no candidates, we are done building the MRO.
475 !cand means no seqs have any entries left to check */
481 /* If we had candidates, but nobody won, then the @ISA
482 hierarchy is not C3-incompatible */
484 /* we have to do some cleanup before we croak */
486 SvREFCNT_dec(retval);
489 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
490 "merging failed on parent '%"SVf"'", HEK_KEY(stashhek), SVfARG(cand));
494 else { /* @ISA was undefined or empty */
495 /* build a retval containing only ourselves */
497 av_push(retval, newSVhek(stashhek));
500 /* we don't want anyone modifying the cache entry but us,
501 and we do so by replacing it completely */
502 SvREADONLY_on(retval);
504 meta->mro_linear_c3 = retval;
509 =for apidoc mro_get_linear_isa
511 Returns either C<mro_get_linear_isa_c3> or
512 C<mro_get_linear_isa_dfs> for the given stash,
513 dependant upon which MRO is in effect
514 for that stash. The return value is a
517 You are responsible for C<SvREFCNT_inc()> on the
518 return value if you plan to store it anywhere
519 semi-permanently (otherwise it might be deleted
520 out from under you the next time the cache is
526 Perl_mro_get_linear_isa(pTHX_ HV *stash)
528 struct mro_meta* meta;
530 PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA;
532 Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
534 meta = HvMROMETA(stash);
535 if (!meta->mro_which)
536 Perl_croak(aTHX_ "panic: invalid MRO!");
537 return meta->mro_which->resolve(aTHX_ stash, 0);
541 =for apidoc mro_isa_changed_in
543 Takes the necessary steps (cache invalidations, mostly)
544 when the @ISA of the given package has changed. Invoked
545 by the C<setisa> magic, should not need to invoke directly.
550 Perl_mro_isa_changed_in(pTHX_ HV* stash)
559 struct mro_meta * meta;
561 const char * const stashname = HvNAME_get(stash);
562 const STRLEN stashname_len = HvNAMELEN_get(stash);
564 PERL_ARGS_ASSERT_MRO_ISA_CHANGED_IN;
567 Perl_croak(aTHX_ "Can't call mro_isa_changed_in() on anonymous symbol table");
569 /* wipe out the cached linearizations for this stash */
570 meta = HvMROMETA(stash);
571 SvREFCNT_dec(MUTABLE_SV(meta->mro_linear_dfs));
572 SvREFCNT_dec(MUTABLE_SV(meta->mro_linear_c3));
573 meta->mro_linear_dfs = NULL;
574 meta->mro_linear_c3 = NULL;
576 SvREFCNT_dec(meta->isa);
580 /* Inc the package generation, since our @ISA changed */
583 /* Wipe the global method cache if this package
584 is UNIVERSAL or one of its parents */
586 svp = hv_fetch(PL_isarev, stashname, stashname_len, 0);
587 isarev = svp ? MUTABLE_HV(*svp) : NULL;
589 if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
590 || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
594 else { /* Wipe the local method cache otherwise */
596 is_universal = FALSE;
599 /* wipe next::method cache too */
600 if(meta->mro_nextmethod) hv_clear(meta->mro_nextmethod);
602 /* Iterate the isarev (classes that are our children),
603 wiping out their linearization and method caches */
606 while((iter = hv_iternext(isarev))) {
608 const char* const revkey = hv_iterkey(iter, &len);
609 HV* revstash = gv_stashpvn(revkey, len, 0);
610 struct mro_meta* revmeta;
612 if(!revstash) continue;
613 revmeta = HvMROMETA(revstash);
614 SvREFCNT_dec(MUTABLE_SV(revmeta->mro_linear_dfs));
615 SvREFCNT_dec(MUTABLE_SV(revmeta->mro_linear_c3));
616 revmeta->mro_linear_dfs = NULL;
617 revmeta->mro_linear_c3 = NULL;
619 revmeta->cache_gen++;
620 if(revmeta->mro_nextmethod)
621 hv_clear(revmeta->mro_nextmethod);
625 /* Now iterate our MRO (parents), and do a few things:
626 1) instantiate with the "fake" flag if they don't exist
627 2) flag them as universal if we are universal
628 3) Add everything from our isarev to their isarev
631 /* We're starting at the 2nd element, skipping ourselves here */
632 linear_mro = mro_get_linear_isa(stash);
633 svp = AvARRAY(linear_mro) + 1;
634 items = AvFILLp(linear_mro);
637 SV* const sv = *svp++;
640 HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0);
642 /* That fetch should not fail. But if it had to create a new SV for
643 us, then will need to upgrade it to an HV (which sv_upgrade() can
646 mroisarev = MUTABLE_HV(HeVAL(he));
648 SvUPGRADE(MUTABLE_SV(mroisarev), SVt_PVHV);
650 /* This hash only ever contains PL_sv_yes. Storing it over itself is
651 almost as cheap as calling hv_exists, so on aggregate we expect to
652 save time by not making two calls to the common HV code for the
653 case where it doesn't exist. */
655 (void)hv_store(mroisarev, stashname, stashname_len, &PL_sv_yes, 0);
659 while((iter = hv_iternext(isarev))) {
661 char* const revkey = hv_iterkey(iter, &revkeylen);
662 (void)hv_store(mroisarev, revkey, revkeylen, &PL_sv_yes, 0);
669 =for apidoc mro_method_changed_in
671 Invalidates method caching on any child classes
672 of the given stash, so that they might notice
673 the changes in this one.
675 Ideally, all instances of C<PL_sub_generation++> in
676 perl source outside of C<mro.c> should be
677 replaced by calls to this.
679 Perl automatically handles most of the common
680 ways a method might be redefined. However, there
681 are a few ways you could change a method in a stash
682 without the cache code noticing, in which case you
683 need to call this method afterwards:
685 1) Directly manipulating the stash HV entries from
688 2) Assigning a reference to a readonly scalar
689 constant into a stash entry in order to create
690 a constant subroutine (like constant.pm
693 This same method is available from pure perl
694 via, C<mro::method_changed_in(classname)>.
699 Perl_mro_method_changed_in(pTHX_ HV *stash)
701 const char * const stashname = HvNAME_get(stash);
702 const STRLEN stashname_len = HvNAMELEN_get(stash);
704 SV ** const svp = hv_fetch(PL_isarev, stashname, stashname_len, 0);
705 HV * const isarev = svp ? MUTABLE_HV(*svp) : NULL;
707 PERL_ARGS_ASSERT_MRO_METHOD_CHANGED_IN;
710 Perl_croak(aTHX_ "Can't call mro_method_changed_in() on anonymous symbol table");
712 /* Inc the package generation, since a local method changed */
713 HvMROMETA(stash)->pkg_gen++;
715 /* If stash is UNIVERSAL, or one of UNIVERSAL's parents,
716 invalidate all method caches globally */
717 if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
718 || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
723 /* else, invalidate the method caches of all child classes,
729 while((iter = hv_iternext(isarev))) {
731 const char* const revkey = hv_iterkey(iter, &len);
732 HV* const revstash = gv_stashpvn(revkey, len, 0);
733 struct mro_meta* mrometa;
735 if(!revstash) continue;
736 mrometa = HvMROMETA(revstash);
737 mrometa->cache_gen++;
738 if(mrometa->mro_nextmethod)
739 hv_clear(mrometa->mro_nextmethod);
744 /* These two are static helpers for next::method and friends,
745 and re-implement a bunch of the code from pp_caller() in
746 a more efficient manner for this particular usage.
750 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
752 for (i = startingblock; i >= 0; i--) {
753 if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
760 XS(XS_mro_get_linear_isa);
763 XS(XS_mro_get_isarev);
764 XS(XS_mro_is_universal);
765 XS(XS_mro_invalidate_method_caches);
766 XS(XS_mro_method_changed_in);
767 XS(XS_mro_get_pkg_gen);
771 Perl_boot_core_mro(pTHX)
774 static const char file[] = __FILE__;
776 newXSproto("mro::get_linear_isa", XS_mro_get_linear_isa, file, "$;$");
777 newXSproto("mro::set_mro", XS_mro_set_mro, file, "$$");
778 newXSproto("mro::get_mro", XS_mro_get_mro, file, "$");
779 newXSproto("mro::get_isarev", XS_mro_get_isarev, file, "$");
780 newXSproto("mro::is_universal", XS_mro_is_universal, file, "$");
781 newXSproto("mro::invalidate_all_method_caches", XS_mro_invalidate_method_caches, file, "");
782 newXSproto("mro::method_changed_in", XS_mro_method_changed_in, file, "$");
783 newXSproto("mro::get_pkg_gen", XS_mro_get_pkg_gen, file, "$");
784 newXS("mro::_nextcan", XS_mro_nextcan, file);
787 XS(XS_mro_get_linear_isa) {
794 if(items < 1 || items > 2)
795 croak_xs_usage(cv, "classname [, type ]");
798 class_stash = gv_stashsv(classname, 0);
801 /* No stash exists yet, give them just the classname */
802 AV* isalin = newAV();
803 av_push(isalin, newSVsv(classname));
804 ST(0) = sv_2mortal(newRV_noinc(MUTABLE_SV(isalin)));
808 const char* const which = SvPV_nolen(ST(1));
809 const struct mro_alg *const algo = S_get_mro_from_name(aTHX_ which);
811 Perl_croak(aTHX_ "Invalid mro name: '%s'", which);
812 RETVAL = algo->resolve(aTHX_ class_stash, 0);
815 RETVAL = mro_get_linear_isa(class_stash);
818 ST(0) = newRV_inc(MUTABLE_SV(RETVAL));
828 const char* whichstr;
829 const struct mro_alg *which;
831 struct mro_meta* meta;
834 croak_xs_usage(cv, "classname, type");
837 whichstr = SvPV_nolen(ST(1));
838 class_stash = gv_stashsv(classname, GV_ADD);
839 if(!class_stash) Perl_croak(aTHX_ "Cannot create class: '%"SVf"'!", SVfARG(classname));
840 meta = HvMROMETA(class_stash);
842 which = S_get_mro_from_name(aTHX_ whichstr);
844 Perl_croak(aTHX_ "Invalid mro name: '%s'", whichstr);
846 if(meta->mro_which != which) {
847 meta->mro_which = which;
848 /* Only affects local method cache, not
849 even child classes */
851 if(meta->mro_nextmethod)
852 hv_clear(meta->mro_nextmethod);
867 croak_xs_usage(cv, "classname");
870 class_stash = gv_stashsv(classname, 0);
872 ST(0) = sv_2mortal(newSVpv(class_stash
873 ? HvMROMETA(class_stash)->mro_which->name
878 XS(XS_mro_get_isarev)
888 croak_xs_usage(cv, "classname");
895 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
896 isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL;
902 while((iter = hv_iternext(isarev)))
903 av_push(ret_array, newSVsv(hv_iterkeysv(iter)));
905 mXPUSHs(newRV_noinc(MUTABLE_SV(ret_array)));
911 XS(XS_mro_is_universal)
918 STRLEN classname_len;
922 croak_xs_usage(cv, "classname");
926 classname_pv = SvPV(classname,classname_len);
928 he = hv_fetch_ent(PL_isarev, classname, 0, 0);
929 isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL;
931 if((classname_len == 9 && strEQ(classname_pv, "UNIVERSAL"))
932 || (isarev && hv_exists(isarev, "UNIVERSAL", 9)))
938 XS(XS_mro_invalidate_method_caches)
944 croak_xs_usage(cv, "");
951 XS(XS_mro_method_changed_in)
959 croak_xs_usage(cv, "classname");
963 class_stash = gv_stashsv(classname, 0);
964 if(!class_stash) Perl_croak(aTHX_ "No such class: '%"SVf"'!", SVfARG(classname));
966 mro_method_changed_in(class_stash);
971 XS(XS_mro_get_pkg_gen)
979 croak_xs_usage(cv, "classname");
983 class_stash = gv_stashsv(classname, 0);
987 mXPUSHi(class_stash ? HvMROMETA(class_stash)->pkg_gen : 0);
998 const I32 throw_nomethod = SvIVX(ST(1));
999 register I32 cxix = cxstack_ix;
1000 register const PERL_CONTEXT *ccstack = cxstack;
1001 const PERL_SI *top_si = PL_curstackinfo;
1004 const char *fq_subname;
1005 const char *subname;
1006 STRLEN stashname_len;
1014 struct mro_meta* selfmeta;
1018 PERL_UNUSED_ARG(cv);
1022 if(sv_isobject(self))
1023 selfstash = SvSTASH(SvRV(self));
1025 selfstash = gv_stashsv(self, GV_ADD);
1029 hvname = HvNAME_get(selfstash);
1031 Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup");
1033 /* This block finds the contextually-enclosing fully-qualified subname,
1034 much like looking at (caller($i))[3] until you find a real sub that
1035 isn't ANON, etc (also skips over pureperl next::method, etc) */
1036 for(i = 0; i < 2; i++) {
1037 cxix = __dopoptosub_at(ccstack, cxix);
1040 STRLEN fq_subname_len;
1042 /* we may be in a higher stacklevel, so dig down deeper */
1044 if(top_si->si_type == PERLSI_MAIN)
1045 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method must be used in method context");
1046 top_si = top_si->si_prev;
1047 ccstack = top_si->si_cxstack;
1048 cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
1051 if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
1052 || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
1053 cxix = __dopoptosub_at(ccstack, cxix - 1);
1058 const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
1059 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
1060 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
1067 cvgv = CvGV(ccstack[cxix].blk_sub.cv);
1070 cxix = __dopoptosub_at(ccstack, cxix - 1);
1074 /* we found a real sub here */
1075 sv = sv_2mortal(newSV(0));
1077 gv_efullname3(sv, cvgv, NULL);
1079 fq_subname = SvPVX(sv);
1080 fq_subname_len = SvCUR(sv);
1082 subname = strrchr(fq_subname, ':');
1084 Perl_croak(aTHX_ "next::method/next::can/maybe::next::method cannot find enclosing method");
1087 subname_len = fq_subname_len - (subname - fq_subname);
1088 if(subname_len == 8 && strEQ(subname, "__ANON__")) {
1089 cxix = __dopoptosub_at(ccstack, cxix - 1);
1097 /* If we made it to here, we found our context */
1099 /* Initialize the next::method cache for this stash
1101 selfmeta = HvMROMETA(selfstash);
1102 if(!(nmcache = selfmeta->mro_nextmethod)) {
1103 nmcache = selfmeta->mro_nextmethod = newHV();
1105 else { /* Use the cached coderef if it exists */
1106 HE* cache_entry = hv_fetch_ent(nmcache, sv, 0, 0);
1108 SV* const val = HeVAL(cache_entry);
1109 if(val == &PL_sv_undef) {
1111 Perl_croak(aTHX_ "No next::method '%s' found for %s", subname, hvname);
1114 mXPUSHs(newRV_inc(val));
1119 /* beyond here is just for cache misses, so perf isn't as critical */
1121 stashname_len = subname - fq_subname - 2;
1122 stashname = newSVpvn_flags(fq_subname, stashname_len, SVs_TEMP);
1124 linear_av = mro_get_linear_isa_c3(selfstash, 0); /* has ourselves at the top of the list */
1126 linear_svp = AvARRAY(linear_av);
1127 entries = AvFILLp(linear_av) + 1;
1129 /* Walk down our MRO, skipping everything up
1130 to the contextually enclosing class */
1132 SV * const linear_sv = *linear_svp++;
1134 if(sv_eq(linear_sv, stashname))
1138 /* Now search the remainder of the MRO for the
1139 same method name as the contextually enclosing
1143 SV * const linear_sv = *linear_svp++;
1149 curstash = gv_stashsv(linear_sv, FALSE);
1152 if (ckWARN(WARN_SYNTAX))
1153 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Can't locate package %"SVf" for @%s::ISA",
1154 (void*)linear_sv, hvname);
1160 gvp = (GV**)hv_fetch(curstash, subname, subname_len, 0);
1166 if (SvTYPE(candidate) != SVt_PVGV)
1167 gv_init(candidate, curstash, subname, subname_len, TRUE);
1169 /* Notably, we only look for real entries, not method cache
1170 entries, because in C3 the method cache of a parent is not
1171 valid for the child */
1172 if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
1173 SvREFCNT_inc_simple_void_NN(MUTABLE_SV(cand_cv));
1174 (void)hv_store_ent(nmcache, newSVsv(sv), MUTABLE_SV(cand_cv), 0);
1175 mXPUSHs(newRV_inc(MUTABLE_SV(cand_cv)));
1181 (void)hv_store_ent(nmcache, newSVsv(sv), &PL_sv_undef, 0);
1183 Perl_croak(aTHX_ "No next::method '%s' found for %s", subname, hvname);
1189 * c-indentation-style: bsd
1191 * indent-tabs-mode: t
1194 * ex: set ts=8 sts=4 sw=4 noet: