1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
|
/* mro.c
*
* Copyright (c) 2007 Brandon L Black
* Copyright (c) 2007, 2008 Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'Which order shall we go in?' said Frodo. 'Eldest first, or quickest first?
* You'll be last either way, Master Peregrin.'
*
* [p.101 of _The Lord of the Rings_, I/iii: "A Conspiracy Unmasked"]
*/
/*
=head1 MRO Functions
These functions are related to the method resolution order of perl classes
=cut
*/
#include "EXTERN.h"
#define PERL_IN_MRO_C
#include "perl.h"
static const struct mro_alg dfs_alg =
{S_mro_get_linear_isa_dfs, "dfs", 3, 0, 0};
SV *
Perl_mro_get_private_data(pTHX_ struct mro_meta *const smeta,
const struct mro_alg *const which)
{
SV **data;
PERL_ARGS_ASSERT_MRO_GET_PRIVATE_DATA;
data = (SV **)Perl_hv_common(aTHX_ smeta->mro_linear_all, NULL,
which->name, which->length, which->kflags,
HV_FETCH_JUST_SV, NULL, which->hash);
if (!data)
return NULL;
/* If we've been asked to look up the private data for the current MRO, then
cache it. */
if (smeta->mro_which == which)
smeta->mro_linear_current = *data;
return *data;
}
SV *
Perl_mro_set_private_data(pTHX_ struct mro_meta *const smeta,
const struct mro_alg *const which, SV *const data)
{
PERL_ARGS_ASSERT_MRO_SET_PRIVATE_DATA;
if (!smeta->mro_linear_all) {
if (smeta->mro_which == which) {
/* If all we need to store is the current MRO's data, then don't use
memory on a hash with 1 element - store it direct, and signal
this by leaving the would-be-hash NULL. */
smeta->mro_linear_current = data;
return data;
} else {
HV *const hv = newHV();
/* Start with 2 buckets. It's unlikely we'll need more. */
HvMAX(hv) = 1;
smeta->mro_linear_all = hv;
if (smeta->mro_linear_current) {
/* If we were storing something directly, put it in the hash
before we lose it. */
Perl_mro_set_private_data(aTHX_ smeta, smeta->mro_which,
smeta->mro_linear_current);
}
}
}
/* We get here if we're storing more than one linearisation for this stash,
or the linearisation we are storing is not that if its current MRO. */
if (smeta->mro_which == which) {
/* If we've been asked to store the private data for the current MRO,
then cache it. */
smeta->mro_linear_current = data;
}
if (!Perl_hv_common(aTHX_ smeta->mro_linear_all, NULL,
which->name, which->length, which->kflags,
HV_FETCH_ISSTORE, data, which->hash)) {
Perl_croak(aTHX_ "panic: hv_store() failed in set_mro_private_data() "
"for '%.*s' %d", (int) which->length, which->name,
which->kflags);
}
return data;
}
const struct mro_alg *
Perl_mro_get_from_name(pTHX_ SV *name) {
SV **data;
PERL_ARGS_ASSERT_MRO_GET_FROM_NAME;
data = (SV **)Perl_hv_common(aTHX_ PL_registered_mros, name, NULL, 0, 0,
HV_FETCH_JUST_SV, NULL, 0);
if (!data)
return NULL;
assert(SvTYPE(*data) == SVt_IV);
assert(SvIOK(*data));
return INT2PTR(const struct mro_alg *, SvUVX(*data));
}
void
Perl_mro_register(pTHX_ const struct mro_alg *mro) {
SV *wrapper = newSVuv(PTR2UV(mro));
PERL_ARGS_ASSERT_MRO_REGISTER;
if (!Perl_hv_common(aTHX_ PL_registered_mros, NULL,
mro->name, mro->length, mro->kflags,
HV_FETCH_ISSTORE, wrapper, mro->hash)) {
SvREFCNT_dec(wrapper);
Perl_croak(aTHX_ "panic: hv_store() failed in mro_register() "
"for '%.*s' %d", (int) mro->length, mro->name, mro->kflags);
}
}
struct mro_meta*
Perl_mro_meta_init(pTHX_ HV* stash)
{
struct mro_meta* newmeta;
PERL_ARGS_ASSERT_MRO_META_INIT;
assert(HvAUX(stash));
assert(!(HvAUX(stash)->xhv_mro_meta));
Newxz(newmeta, 1, struct mro_meta);
HvAUX(stash)->xhv_mro_meta = newmeta;
newmeta->cache_gen = 1;
newmeta->pkg_gen = 1;
newmeta->mro_which = &dfs_alg;
return newmeta;
}
#if defined(USE_ITHREADS)
/* for sv_dup on new threads */
struct mro_meta*
Perl_mro_meta_dup(pTHX_ struct mro_meta* smeta, CLONE_PARAMS* param)
{
struct mro_meta* newmeta;
PERL_ARGS_ASSERT_MRO_META_DUP;
Newx(newmeta, 1, struct mro_meta);
Copy(smeta, newmeta, 1, struct mro_meta);
if (newmeta->mro_linear_all) {
newmeta->mro_linear_all
= MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_linear_all, param));
/* This is just acting as a shortcut pointer, and will be automatically
updated on the first get. */
newmeta->mro_linear_current = NULL;
} else if (newmeta->mro_linear_current) {
/* Only the current MRO is stored, so this owns the data. */
newmeta->mro_linear_current
= sv_dup_inc((const SV *)newmeta->mro_linear_current, param);
}
if (newmeta->mro_nextmethod)
newmeta->mro_nextmethod
= MUTABLE_HV(sv_dup_inc((const SV *)newmeta->mro_nextmethod, param));
if (newmeta->isa)
newmeta->isa
= MUTABLE_HV(sv_dup_inc((const SV *)newmeta->isa, param));
return newmeta;
}
#endif /* USE_ITHREADS */
/*
=for apidoc mro_get_linear_isa_dfs
Returns the Depth-First Search linearization of @ISA
the given stash. The return value is a read-only AV*.
C<level> should be 0 (it is used internally in this
function's recursion).
You are responsible for C<SvREFCNT_inc()> on the
return value if you plan to store it anywhere
semi-permanently (otherwise it might be deleted
out from under you the next time the cache is
invalidated).
=cut
*/
static AV*
S_mro_get_linear_isa_dfs(pTHX_ HV *stash, U32 level)
{
AV* retval;
GV** gvp;
GV* gv;
AV* av;
const HEK* stashhek;
struct mro_meta* meta;
SV *our_name;
HV *stored = NULL;
PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_DFS;
assert(HvAUX(stash));
stashhek = HvNAME_HEK(stash);
if (!stashhek)
Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
if (level > 100)
Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
HEK_KEY(stashhek));
meta = HvMROMETA(stash);
/* return cache if valid */
if((retval = MUTABLE_AV(MRO_GET_PRIVATE_DATA(meta, &dfs_alg)))) {
return retval;
}
/* not in cache, make a new one */
retval = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV())));
/* We use this later in this function, but don't need a reference to it
beyond the end of this function, so reference count is fine. */
our_name = newSVhek(stashhek);
av_push(retval, our_name); /* add ourselves at the top */
/* fetch our @ISA */
gvp = (GV**)hv_fetchs(stash, "ISA", FALSE);
av = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL;
/* "stored" is used to keep track of all of the classnames we have added to
the MRO so far, so we can do a quick exists check and avoid adding
duplicate classnames to the MRO as we go.
It's then retained to be re-used as a fast lookup for ->isa(), by adding
our own name and "UNIVERSAL" to it. */
if(av && AvFILLp(av) >= 0) {
SV **svp = AvARRAY(av);
I32 items = AvFILLp(av) + 1;
/* foreach(@ISA) */
while (items--) {
SV* const sv = *svp++;
HV* const basestash = gv_stashsv(sv, 0);
SV *const *subrv_p;
I32 subrv_items;
if (!basestash) {
/* if no stash exists for this @ISA member,
simply add it to the MRO and move on */
subrv_p = &sv;
subrv_items = 1;
}
else {
/* otherwise, recurse into ourselves for the MRO
of this @ISA member, and append their MRO to ours.
The recursive call could throw an exception, which
has memory management implications here, hence the use of
the mortal. */
const AV *const subrv
= mro_get_linear_isa_dfs(basestash, level + 1);
subrv_p = AvARRAY(subrv);
subrv_items = AvFILLp(subrv) + 1;
}
if (stored) {
while(subrv_items--) {
SV *const subsv = *subrv_p++;
/* LVALUE fetch will create a new undefined SV if necessary
*/
HE *const he = hv_fetch_ent(stored, subsv, 1, 0);
assert(he);
if(HeVAL(he) != &PL_sv_undef) {
/* It was newly created. Steal it for our new SV, and
replace it in the hash with the "real" thing. */
SV *const val = HeVAL(he);
HEK *const key = HeKEY_hek(he);
HeVAL(he) = &PL_sv_undef;
/* Save copying by making a shared hash key scalar. We
inline this here rather than calling
Perl_newSVpvn_share because we already have the
scalar, and we already have the hash key. */
assert(SvTYPE(val) == SVt_NULL);
sv_upgrade(val, SVt_PV);
SvPV_set(val, HEK_KEY(share_hek_hek(key)));
SvCUR_set(val, HEK_LEN(key));
SvREADONLY_on(val);
SvFAKE_on(val);
SvPOK_on(val);
if (HEK_UTF8(key))
SvUTF8_on(val);
av_push(retval, val);
}
}
} else {
/* We are the first (or only) parent. We can short cut the
complexity above, because our @ISA is simply us prepended
to our parent's @ISA, and our ->isa cache is simply our
parent's, with our name added. */
/* newSVsv() is slow. This code is only faster if we can avoid
it by ensuring that SVs in the arrays are shared hash key
scalar SVs, because we can "copy" them very efficiently.
Although to be fair, we can't *ensure* this, as a reference
to the internal array is returned by mro::get_linear_isa(),
so we'll have to be defensive just in case someone faffed
with it. */
if (basestash) {
SV **svp;
stored = MUTABLE_HV(sv_2mortal((SV*)newHVhv(HvMROMETA(basestash)->isa)));
av_extend(retval, subrv_items);
AvFILLp(retval) = subrv_items;
svp = AvARRAY(retval);
while(subrv_items--) {
SV *const val = *subrv_p++;
*++svp = SvIsCOW_shared_hash(val)
? newSVhek(SvSHARED_HEK_FROM_PV(SvPVX(val)))
: newSVsv(val);
}
} else {
/* They have no stash. So create ourselves an ->isa cache
as if we'd copied it from what theirs should be. */
stored = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
(void) hv_store(stored, "UNIVERSAL", 9, &PL_sv_undef, 0);
av_push(retval,
newSVhek(HeKEY_hek(hv_store_ent(stored, sv,
&PL_sv_undef, 0))));
}
}
}
} else {
/* We have no parents. */
stored = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV())));
(void) hv_store(stored, "UNIVERSAL", 9, &PL_sv_undef, 0);
}
(void) hv_store_ent(stored, our_name, &PL_sv_undef, 0);
SvREFCNT_inc_simple_void_NN(stored);
SvTEMP_off(stored);
SvREADONLY_on(stored);
meta->isa = stored;
/* now that we're past the exception dangers, grab our own reference to
the AV we're about to use for the result. The reference owned by the
mortals' stack will be released soon, so everything will balance. */
SvREFCNT_inc_simple_void_NN(retval);
SvTEMP_off(retval);
/* we don't want anyone modifying the cache entry but us,
and we do so by replacing it completely */
SvREADONLY_on(retval);
return MUTABLE_AV(Perl_mro_set_private_data(aTHX_ meta, &dfs_alg,
MUTABLE_SV(retval)));
}
/*
=for apidoc mro_get_linear_isa
Returns either C<mro_get_linear_isa_c3> or
C<mro_get_linear_isa_dfs> for the given stash,
dependant upon which MRO is in effect
for that stash. The return value is a
read-only AV*.
You are responsible for C<SvREFCNT_inc()> on the
return value if you plan to store it anywhere
semi-permanently (otherwise it might be deleted
out from under you the next time the cache is
invalidated).
=cut
*/
AV*
Perl_mro_get_linear_isa(pTHX_ HV *stash)
{
struct mro_meta* meta;
PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA;
if(!SvOOK(stash))
Perl_croak(aTHX_ "Can't linearize anonymous symbol table");
meta = HvMROMETA(stash);
if (!meta->mro_which)
Perl_croak(aTHX_ "panic: invalid MRO!");
return meta->mro_which->resolve(aTHX_ stash, 0);
}
/*
=for apidoc mro_isa_changed_in
Takes the necessary steps (cache invalidations, mostly)
when the @ISA of the given package has changed. Invoked
by the C<setisa> magic, should not need to invoke directly.
=for apidoc mro_isa_changed_in3
Takes the necessary steps (cache invalidations, mostly)
when the @ISA of the given package has changed. Invoked
by the C<setisa> magic, should not need to invoke directly.
The stash can be passed as the first argument, or its name and length as
the second and third (or both). If just the name is passed and the stash
does not exist, then only the subclasses' method and isa caches will be
invalidated.
=cut
*/
void
Perl_mro_isa_changed_in3(pTHX_ HV* stash, const char *stashname,
STRLEN stashname_len)
{
dVAR;
HV* isarev;
AV* linear_mro;
HE* iter;
SV** svp;
I32 items;
bool is_universal;
struct mro_meta * meta = NULL;
if(!stashname && stash) {
stashname = HvNAME_get(stash);
stashname_len = HvNAMELEN_get(stash);
}
else if(!stash)
stash = gv_stashpvn(stashname, stashname_len, 0 /* don't add */);
if(!stashname)
Perl_croak(aTHX_ "Can't call mro_isa_changed_in() on anonymous symbol table");
if(stash) {
/* wipe out the cached linearizations for this stash */
meta = HvMROMETA(stash);
if (meta->mro_linear_all) {
SvREFCNT_dec(MUTABLE_SV(meta->mro_linear_all));
meta->mro_linear_all = NULL;
/* This is just acting as a shortcut pointer. */
meta->mro_linear_current = NULL;
} else if (meta->mro_linear_current) {
/* Only the current MRO is stored, so this owns the data. */
SvREFCNT_dec(meta->mro_linear_current);
meta->mro_linear_current = NULL;
}
if (meta->isa) {
SvREFCNT_dec(meta->isa);
meta->isa = NULL;
}
/* Inc the package generation, since our @ISA changed */
meta->pkg_gen++;
}
/* Wipe the global method cache if this package
is UNIVERSAL or one of its parents */
svp = hv_fetch(PL_isarev, stashname, stashname_len, 0);
isarev = svp ? MUTABLE_HV(*svp) : NULL;
if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
|| (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
PL_sub_generation++;
is_universal = TRUE;
}
else { /* Wipe the local method cache otherwise */
if(meta) meta->cache_gen++;
is_universal = FALSE;
}
/* wipe next::method cache too */
if(meta && meta->mro_nextmethod) hv_clear(meta->mro_nextmethod);
/* Iterate the isarev (classes that are our children),
wiping out their linearization, method and isa caches */
if(isarev) {
hv_iterinit(isarev);
while((iter = hv_iternext(isarev))) {
I32 len;
const char* const revkey = hv_iterkey(iter, &len);
HV* revstash = gv_stashpvn(revkey, len, 0);
struct mro_meta* revmeta;
if(!revstash) continue;
revmeta = HvMROMETA(revstash);
if (revmeta->mro_linear_all) {
SvREFCNT_dec(MUTABLE_SV(revmeta->mro_linear_all));
revmeta->mro_linear_all = NULL;
/* This is just acting as a shortcut pointer. */
revmeta->mro_linear_current = NULL;
} else if (revmeta->mro_linear_current) {
/* Only the current MRO is stored, so this owns the data. */
SvREFCNT_dec(revmeta->mro_linear_current);
revmeta->mro_linear_current = NULL;
}
if(!is_universal)
revmeta->cache_gen++;
if(revmeta->mro_nextmethod)
hv_clear(revmeta->mro_nextmethod);
if (revmeta->isa) {
SvREFCNT_dec(revmeta->isa);
revmeta->isa = NULL;
}
}
}
/* Now iterate our MRO (parents), and do a few things:
1) instantiate with the "fake" flag if they don't exist
2) flag them as universal if we are universal
3) Add everything from our isarev to their isarev
*/
/* This only applies if the stash exists. */
if(!stash) return;
/* We're starting at the 2nd element, skipping ourselves here */
linear_mro = mro_get_linear_isa(stash);
svp = AvARRAY(linear_mro) + 1;
items = AvFILLp(linear_mro);
while (items--) {
SV* const sv = *svp++;
HV* mroisarev;
HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0);
/* That fetch should not fail. But if it had to create a new SV for
us, then will need to upgrade it to an HV (which sv_upgrade() can
now do for us. */
mroisarev = MUTABLE_HV(HeVAL(he));
SvUPGRADE(MUTABLE_SV(mroisarev), SVt_PVHV);
/* This hash only ever contains PL_sv_yes. Storing it over itself is
almost as cheap as calling hv_exists, so on aggregate we expect to
save time by not making two calls to the common HV code for the
case where it doesn't exist. */
(void)hv_store(mroisarev, stashname, stashname_len, &PL_sv_yes, 0);
if(isarev) {
hv_iterinit(isarev);
while((iter = hv_iternext(isarev))) {
I32 revkeylen;
char* const revkey = hv_iterkey(iter, &revkeylen);
(void)hv_store(mroisarev, revkey, revkeylen, &PL_sv_yes, 0);
}
}
}
}
/*
=for apidoc mro_package_moved
Call this function to signal to a stash that it has been assigned to
another spot in the stash hierarchy. C<stash> is the stash that has been
assigned. C<oldstash> is the stash it replaces, if any. C<gv> is the glob
that is actually being assigned to. C<newname> and C<newname_len> are the
full name of the GV. If these last two arguments are omitted, they can be
inferred from C<oldstash> or C<gv>.
This can also be called with a null first argument and a null C<gv>, to
indicate that C<oldstash> has been deleted.
This function invalidates isa caches on the old stash, on all subpackages
nested inside it, and on the subclasses of all those, including
non-existent packages that have corresponding entries in C<stash>.
=cut
*/
void
Perl_mro_package_moved(pTHX_ HV * const stash, const HV * const oldstash,
const GV * const gv, const char *newname,
I32 newname_len)
{
register XPVHV* xhv;
register HE *entry;
I32 riter = -1;
HV *seen = NULL;
/* If newname_len is negative, it is actually the call depth (negated).
*/
const I32 level = newname_len < 0 ? newname_len : 0;
assert(stash || oldstash);
assert(oldstash || gv || newname);
if(level < -100) return;
if(!newname && oldstash) {
newname = HvNAME_get(oldstash);
newname_len = HvNAMELEN_get(oldstash);
}
if(!newname && gv) {
SV * const namesv = sv_newmortal();
STRLEN len;
gv_fullname4(namesv, gv, NULL, 0);
newname = SvPV_const(namesv, len);
newname_len = len - 2; /* skip trailing :: */
}
/* XXX This relies on the fact that package names cannot contain nulls.
*/
if(newname_len < 0) newname_len = strlen(newname);
mro_isa_changed_in3((HV *)oldstash, newname, newname_len);
if(
(!stash || !HvARRAY(stash)) && (!oldstash || !HvARRAY(oldstash))
) return;
/* This is partly based on code in hv_iternext_flags. We are not call-
ing that here, as we want to avoid resetting the hash iterator. */
/* Skip the entire loop if the hash is empty. */
if(oldstash && HvUSEDKEYS(oldstash)) {
xhv = (XPVHV*)SvANY(oldstash);
seen = newHV();
/* Iterate through entries in the oldstash, calling
mro_package_moved(
corresponding_entry_in_new_stash, current_entry, ...
)
meanwhile doing the equivalent of $seen{$key} = 1.
*/
while (++riter <= (I32)xhv->xhv_max) {
entry = (HvARRAY(oldstash))[riter];
/* Iterate through the entries in this list */
for(; entry; entry = HeNEXT(entry)) {
const char* key;
I32 len;
/* If this entry is not a glob, ignore it.
Try the next. */
if (!isGV(HeVAL(entry))) continue;
key = hv_iterkey(entry, &len);
if(len > 1 && key[len-2] == ':' && key[len-1] == ':') {
HV * const oldsubstash = GvHV(HeVAL(entry));
SV ** const stashentry
= stash ? hv_fetch(stash, key, len, 0) : NULL;
HV *substash;
/* Avoid main::main::main::... */
if(oldsubstash == oldstash) continue;
if(
stashentry && *stashentry
&& (substash = GvHV(*stashentry))
&& HvNAME(substash)
)
mro_package_moved(
substash, oldsubstash, NULL, NULL, level-1
);
else if(oldsubstash && HvNAME(oldsubstash))
mro_isa_changed_in(oldsubstash);
(void)hv_store(seen, key, len, &PL_sv_yes, 0);
}
}
}
}
/* Skip the entire loop if the hash is empty. */
if (stash && HvUSEDKEYS(stash)) {
xhv = (XPVHV*)SvANY(stash);
/* Iterate through the new stash, skipping $seen{$key} items,
calling mro_package_moved(entry, NULL, ...). */
while (++riter <= (I32)xhv->xhv_max) {
entry = (HvARRAY(stash))[riter];
/* Iterate through the entries in this list */
for(; entry; entry = HeNEXT(entry)) {
const char* key;
I32 len;
/* If this entry is not a glob, ignore it.
Try the next. */
if (!isGV(HeVAL(entry))) continue;
key = hv_iterkey(entry, &len);
if(len > 1 && key[len-2] == ':' && key[len-1] == ':') {
HV *substash;
/* If this entry was seen when we iterated through the
oldstash, skip it. */
if(seen && hv_exists(seen, key, len)) continue;
/* We get here only if this stash has no corresponding
entry in the stash being replaced. */
substash = GvHV(HeVAL(entry));
if(substash && HvNAME(substash)) {
SV *namesv;
/* Avoid checking main::main::main::... */
if(substash == stash) continue;
/* Add :: and the key (minus the trailing ::)
to newname. */
namesv
= newSVpvn_flags(newname, newname_len, SVs_TEMP);
sv_catpvs(namesv, "::");
sv_catpvn(namesv, key, len-2);
mro_package_moved(
substash, NULL, NULL,
SvPV_nolen_const(namesv),
level-1
);
}
}
}
}
}
if(seen) SvREFCNT_dec((SV *)seen);
}
/*
=for apidoc mro_method_changed_in
Invalidates method caching on any child classes
of the given stash, so that they might notice
the changes in this one.
Ideally, all instances of C<PL_sub_generation++> in
perl source outside of C<mro.c> should be
replaced by calls to this.
Perl automatically handles most of the common
ways a method might be redefined. However, there
are a few ways you could change a method in a stash
without the cache code noticing, in which case you
need to call this method afterwards:
1) Directly manipulating the stash HV entries from
XS code.
2) Assigning a reference to a readonly scalar
constant into a stash entry in order to create
a constant subroutine (like constant.pm
does).
This same method is available from pure perl
via, C<mro::method_changed_in(classname)>.
=cut
*/
void
Perl_mro_method_changed_in(pTHX_ HV *stash)
{
const char * const stashname = HvNAME_get(stash);
const STRLEN stashname_len = HvNAMELEN_get(stash);
SV ** const svp = hv_fetch(PL_isarev, stashname, stashname_len, 0);
HV * const isarev = svp ? MUTABLE_HV(*svp) : NULL;
PERL_ARGS_ASSERT_MRO_METHOD_CHANGED_IN;
if(!stashname)
Perl_croak(aTHX_ "Can't call mro_method_changed_in() on anonymous symbol table");
/* Inc the package generation, since a local method changed */
HvMROMETA(stash)->pkg_gen++;
/* If stash is UNIVERSAL, or one of UNIVERSAL's parents,
invalidate all method caches globally */
if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL"))
|| (isarev && hv_exists(isarev, "UNIVERSAL", 9))) {
PL_sub_generation++;
return;
}
/* else, invalidate the method caches of all child classes,
but not itself */
if(isarev) {
HE* iter;
hv_iterinit(isarev);
while((iter = hv_iternext(isarev))) {
I32 len;
const char* const revkey = hv_iterkey(iter, &len);
HV* const revstash = gv_stashpvn(revkey, len, 0);
struct mro_meta* mrometa;
if(!revstash) continue;
mrometa = HvMROMETA(revstash);
mrometa->cache_gen++;
if(mrometa->mro_nextmethod)
hv_clear(mrometa->mro_nextmethod);
}
}
}
void
Perl_mro_set_mro(pTHX_ struct mro_meta *const meta, SV *const name)
{
const struct mro_alg *const which = Perl_mro_get_from_name(aTHX_ name);
PERL_ARGS_ASSERT_MRO_SET_MRO;
if (!which)
Perl_croak(aTHX_ "Invalid mro name: '%"SVf"'", name);
if(meta->mro_which != which) {
if (meta->mro_linear_current && !meta->mro_linear_all) {
/* If we were storing something directly, put it in the hash before
we lose it. */
Perl_mro_set_private_data(aTHX_ meta, meta->mro_which,
MUTABLE_SV(meta->mro_linear_current));
}
meta->mro_which = which;
/* Scrub our cached pointer to the private data. */
meta->mro_linear_current = NULL;
/* Only affects local method cache, not
even child classes */
meta->cache_gen++;
if(meta->mro_nextmethod)
hv_clear(meta->mro_nextmethod);
}
}
#include "XSUB.h"
XS(XS_mro_method_changed_in);
void
Perl_boot_core_mro(pTHX)
{
dVAR;
static const char file[] = __FILE__;
Perl_mro_register(aTHX_ &dfs_alg);
newXSproto("mro::method_changed_in", XS_mro_method_changed_in, file, "$");
}
XS(XS_mro_method_changed_in)
{
dVAR;
dXSARGS;
SV* classname;
HV* class_stash;
if(items != 1)
croak_xs_usage(cv, "classname");
classname = ST(0);
class_stash = gv_stashsv(classname, 0);
if(!class_stash) Perl_croak(aTHX_ "No such class: '%"SVf"'!", SVfARG(classname));
mro_method_changed_in(class_stash);
XSRETURN_EMPTY;
}
/*
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*
* ex: set ts=8 sts=4 sw=4 noet:
*/
|