Commit | Line | Data |
e1a479c5 |
1 | /* mro.c |
2 | * |
3 | * Copyright (c) 2007 Brandon L Black |
1129b882 |
4 | * Copyright (c) 2007, 2008 Larry Wall and others |
e1a479c5 |
5 | * |
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. |
8 | * |
9 | */ |
10 | |
11 | /* |
4ac71550 |
12 | * 'Which order shall we go in?' said Frodo. 'Eldest first, or quickest first? |
13 | * You'll be last either way, Master Peregrin.' |
14 | * |
15 | * [p.101 of _The Lord of the Rings_, I/iii: "A Conspiracy Unmasked"] |
cac98860 |
16 | */ |
17 | |
18 | /* |
e1a479c5 |
19 | =head1 MRO Functions |
20 | |
21 | These functions are related to the method resolution order of perl classes |
22 | |
23 | =cut |
24 | */ |
25 | |
26 | #include "EXTERN.h" |
4befac30 |
27 | #define PERL_IN_MRO_C |
e1a479c5 |
28 | #include "perl.h" |
29 | |
3d76853f |
30 | struct mro_alg { |
94d1e706 |
31 | AV *(*resolve)(pTHX_ HV* stash, U32 level); |
fa60396f |
32 | const char *name; |
33 | U16 length; |
34 | U16 kflags; /* For the hash API - set HVhek_UTF8 if name is UTF-8 */ |
35 | U32 hash; /* or 0 */ |
3d76853f |
36 | }; |
37 | |
38 | /* First one is the default */ |
39 | static struct mro_alg mros[] = { |
fa60396f |
40 | {S_mro_get_linear_isa_dfs, "dfs", 3, 0, 0}, |
41 | {S_mro_get_linear_isa_c3, "c3", 2, 0, 0} |
3d76853f |
42 | }; |
43 | |
44 | #define NUMBER_OF_MROS (sizeof(mros)/sizeof(struct mro_alg)) |
45 | |
fa60396f |
46 | #define dfs_alg (&mros[0]) |
47 | #define c3_alg (&mros[1]) |
48 | |
49 | SV * |
50 | Perl_mro_get_private_data(pTHX_ struct mro_meta *const smeta, |
51 | const struct mro_alg *const which) |
52 | { |
53 | SV **data; |
54 | PERL_ARGS_ASSERT_MRO_GET_PRIVATE_DATA; |
55 | |
56 | data = Perl_hv_common(aTHX_ MUTABLE_HV(smeta->mro_linear_dfs), NULL, |
57 | which->name, which->length, which->kflags, |
58 | HV_FETCH_JUST_SV, NULL, which->hash); |
59 | if (!data) |
60 | return NULL; |
61 | |
62 | /* If we've been asked to look up the private data for the current MRO, then |
63 | cache it. */ |
64 | if (smeta->mro_which == which) |
65 | smeta->mro_linear_c3 = MUTABLE_AV(*data); |
66 | |
67 | return *data; |
68 | } |
69 | |
70 | SV * |
71 | Perl_mro_set_private_data(pTHX_ struct mro_meta *const smeta, |
72 | const struct mro_alg *const which, SV *const data) |
73 | { |
74 | PERL_ARGS_ASSERT_MRO_SET_PRIVATE_DATA; |
75 | |
76 | /* If we've been asked to look up the private data for the current MRO, then |
77 | cache it. */ |
78 | if (smeta->mro_which == which) |
79 | smeta->mro_linear_c3 = MUTABLE_AV(data); |
80 | |
81 | if (!smeta->mro_linear_dfs) { |
82 | HV *const hv = newHV(); |
83 | HvMAX(hv) = 0; /* Start with 1 bucket. It's unlikely we'll need more. |
84 | */ |
85 | smeta->mro_linear_dfs = MUTABLE_AV(hv); |
86 | } |
87 | |
88 | if (!Perl_hv_common(aTHX_ MUTABLE_HV(smeta->mro_linear_dfs), NULL, |
89 | which->name, which->length, which->kflags, |
90 | HV_FETCH_ISSTORE, data, which->hash)) { |
91 | Perl_croak(aTHX_ "panic: hv_store() failed in set_mro_private_data() " |
92 | "for '%.*s' %d", (int) which->length, which->name, |
93 | which->kflags); |
94 | } |
95 | |
96 | return data; |
97 | } |
98 | |
3d76853f |
99 | static const struct mro_alg * |
100 | S_get_mro_from_name(pTHX_ const char *const name) { |
101 | const struct mro_alg *algo = mros; |
102 | const struct mro_alg *const end = mros + NUMBER_OF_MROS; |
103 | while (algo < end) { |
104 | if(strEQ(name, algo->name)) |
105 | return algo; |
106 | ++algo; |
107 | } |
108 | return NULL; |
109 | } |
110 | |
e1a479c5 |
111 | struct mro_meta* |
112 | Perl_mro_meta_init(pTHX_ HV* stash) |
113 | { |
9fe4aecf |
114 | struct mro_meta* newmeta; |
e1a479c5 |
115 | |
7918f24d |
116 | PERL_ARGS_ASSERT_MRO_META_INIT; |
e1a479c5 |
117 | assert(HvAUX(stash)); |
118 | assert(!(HvAUX(stash)->xhv_mro_meta)); |
183086be |
119 | Newxz(newmeta, 1, struct mro_meta); |
9fe4aecf |
120 | HvAUX(stash)->xhv_mro_meta = newmeta; |
dd69841b |
121 | newmeta->cache_gen = 1; |
70cd14a1 |
122 | newmeta->pkg_gen = 1; |
edf2cad7 |
123 | newmeta->mro_which = mros; |
e1a479c5 |
124 | |
125 | return newmeta; |
126 | } |
127 | |
128 | #if defined(USE_ITHREADS) |
129 | |
130 | /* for sv_dup on new threads */ |
131 | struct mro_meta* |
132 | Perl_mro_meta_dup(pTHX_ struct mro_meta* smeta, CLONE_PARAMS* param) |
133 | { |
e1a479c5 |
134 | struct mro_meta* newmeta; |
135 | |
7918f24d |
136 | PERL_ARGS_ASSERT_MRO_META_DUP; |
e1a479c5 |
137 | |
33e12d9d |
138 | Newx(newmeta, 1, struct mro_meta); |
139 | Copy(smeta, newmeta, 1, struct mro_meta); |
140 | |
141 | if (newmeta->mro_linear_dfs) |
142 | newmeta->mro_linear_dfs |
ad64d0ec |
143 | = MUTABLE_AV(SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_linear_dfs, param))); |
fa60396f |
144 | newmeta->mro_linear_c3 = NULL; |
33e12d9d |
145 | if (newmeta->mro_nextmethod) |
146 | newmeta->mro_nextmethod |
ad64d0ec |
147 | = MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)newmeta->mro_nextmethod, param))); |
a49ba3fc |
148 | if (newmeta->isa) |
149 | newmeta->isa |
ad64d0ec |
150 | = MUTABLE_HV(SvREFCNT_inc(sv_dup((const SV *)newmeta->isa, param))); |
e1a479c5 |
151 | |
152 | return newmeta; |
153 | } |
154 | |
155 | #endif /* USE_ITHREADS */ |
156 | |
a49ba3fc |
157 | HV * |
158 | Perl_get_isa_hash(pTHX_ HV *const stash) |
159 | { |
160 | dVAR; |
161 | struct mro_meta *const meta = HvMROMETA(stash); |
162 | |
163 | PERL_ARGS_ASSERT_GET_ISA_HASH; |
164 | |
6e4aef59 |
165 | if (!meta->isa) { |
166 | AV *const isa = mro_get_linear_isa(stash); |
167 | if (!meta->isa) { |
168 | HV *const isa_hash = newHV(); |
169 | /* Linearisation didn't build it for us, so do it here. */ |
170 | SV *const *svp = AvARRAY(isa); |
171 | SV *const *const svp_end = svp + AvFILLp(isa) + 1; |
172 | const HEK *const canon_name = HvNAME_HEK(stash); |
173 | |
174 | while (svp < svp_end) { |
175 | (void) hv_store_ent(isa_hash, *svp++, &PL_sv_undef, 0); |
176 | } |
177 | |
178 | (void) hv_common(isa_hash, NULL, HEK_KEY(canon_name), |
179 | HEK_LEN(canon_name), HEK_FLAGS(canon_name), |
180 | HV_FETCH_ISSTORE, &PL_sv_undef, |
181 | HEK_HASH(canon_name)); |
182 | (void) hv_store(isa_hash, "UNIVERSAL", 9, &PL_sv_undef, 0); |
183 | |
ed09b296 |
184 | SvREADONLY_on(isa_hash); |
185 | |
6e4aef59 |
186 | meta->isa = isa_hash; |
187 | } |
188 | } |
a49ba3fc |
189 | return meta->isa; |
190 | } |
191 | |
e1a479c5 |
192 | /* |
193 | =for apidoc mro_get_linear_isa_dfs |
194 | |
195 | Returns the Depth-First Search linearization of @ISA |
196 | the given stash. The return value is a read-only AV*. |
197 | C<level> should be 0 (it is used internally in this |
198 | function's recursion). |
199 | |
1c908217 |
200 | You are responsible for C<SvREFCNT_inc()> on the |
201 | return value if you plan to store it anywhere |
202 | semi-permanently (otherwise it might be deleted |
203 | out from under you the next time the cache is |
204 | invalidated). |
205 | |
e1a479c5 |
206 | =cut |
207 | */ |
4befac30 |
208 | static AV* |
94d1e706 |
209 | S_mro_get_linear_isa_dfs(pTHX_ HV *stash, U32 level) |
e1a479c5 |
210 | { |
211 | AV* retval; |
212 | GV** gvp; |
213 | GV* gv; |
214 | AV* av; |
190d0b22 |
215 | const HEK* stashhek; |
e1a479c5 |
216 | struct mro_meta* meta; |
a49ba3fc |
217 | SV *our_name; |
218 | HV *stored; |
e1a479c5 |
219 | |
7918f24d |
220 | PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_DFS; |
e1a479c5 |
221 | assert(HvAUX(stash)); |
222 | |
190d0b22 |
223 | stashhek = HvNAME_HEK(stash); |
224 | if (!stashhek) |
1e05feb3 |
225 | Perl_croak(aTHX_ "Can't linearize anonymous symbol table"); |
e1a479c5 |
226 | |
227 | if (level > 100) |
228 | Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'", |
190d0b22 |
229 | HEK_KEY(stashhek)); |
e1a479c5 |
230 | |
231 | meta = HvMROMETA(stash); |
1c908217 |
232 | |
233 | /* return cache if valid */ |
123892d9 |
234 | if((retval = MUTABLE_AV(MRO_GET_PRIVATE_DATA(meta, dfs_alg)))) { |
e1a479c5 |
235 | return retval; |
236 | } |
237 | |
238 | /* not in cache, make a new one */ |
1c908217 |
239 | |
ad64d0ec |
240 | retval = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV()))); |
a49ba3fc |
241 | /* We use this later in this function, but don't need a reference to it |
242 | beyond the end of this function, so reference count is fine. */ |
243 | our_name = newSVhek(stashhek); |
244 | av_push(retval, our_name); /* add ourselves at the top */ |
e1a479c5 |
245 | |
1c908217 |
246 | /* fetch our @ISA */ |
e1a479c5 |
247 | gvp = (GV**)hv_fetchs(stash, "ISA", FALSE); |
248 | av = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL; |
249 | |
a49ba3fc |
250 | /* "stored" is used to keep track of all of the classnames we have added to |
251 | the MRO so far, so we can do a quick exists check and avoid adding |
252 | duplicate classnames to the MRO as we go. |
253 | It's then retained to be re-used as a fast lookup for ->isa(), by adding |
254 | our own name and "UNIVERSAL" to it. */ |
255 | |
ad64d0ec |
256 | stored = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV()))); |
1c908217 |
257 | |
a49ba3fc |
258 | if(av && AvFILLp(av) >= 0) { |
1c908217 |
259 | |
ffd8da72 |
260 | SV **svp = AvARRAY(av); |
261 | I32 items = AvFILLp(av) + 1; |
1c908217 |
262 | |
263 | /* foreach(@ISA) */ |
e1a479c5 |
264 | while (items--) { |
265 | SV* const sv = *svp++; |
266 | HV* const basestash = gv_stashsv(sv, 0); |
ffd8da72 |
267 | SV *const *subrv_p; |
268 | I32 subrv_items; |
e1a479c5 |
269 | |
270 | if (!basestash) { |
1c908217 |
271 | /* if no stash exists for this @ISA member, |
272 | simply add it to the MRO and move on */ |
ffd8da72 |
273 | subrv_p = &sv; |
274 | subrv_items = 1; |
e1a479c5 |
275 | } |
276 | else { |
1c908217 |
277 | /* otherwise, recurse into ourselves for the MRO |
b1d0c68a |
278 | of this @ISA member, and append their MRO to ours. |
279 | The recursive call could throw an exception, which |
280 | has memory management implications here, hence the use of |
281 | the mortal. */ |
ffd8da72 |
282 | const AV *const subrv |
283 | = mro_get_linear_isa_dfs(basestash, level + 1); |
284 | |
285 | subrv_p = AvARRAY(subrv); |
286 | subrv_items = AvFILLp(subrv) + 1; |
287 | } |
288 | while(subrv_items--) { |
289 | SV *const subsv = *subrv_p++; |
8e45cc2b |
290 | /* LVALUE fetch will create a new undefined SV if necessary |
291 | */ |
292 | HE *const he = hv_fetch_ent(stored, subsv, 1, 0); |
293 | assert(he); |
294 | if(HeVAL(he) != &PL_sv_undef) { |
295 | /* It was newly created. Steal it for our new SV, and |
296 | replace it in the hash with the "real" thing. */ |
297 | SV *const val = HeVAL(he); |
f46ee248 |
298 | HEK *const key = HeKEY_hek(he); |
8e45cc2b |
299 | |
300 | HeVAL(he) = &PL_sv_undef; |
f46ee248 |
301 | /* Save copying by making a shared hash key scalar. We |
302 | inline this here rather than calling Perl_newSVpvn_share |
303 | because we already have the scalar, and we already have |
304 | the hash key. */ |
305 | assert(SvTYPE(val) == SVt_NULL); |
306 | sv_upgrade(val, SVt_PV); |
307 | SvPV_set(val, HEK_KEY(share_hek_hek(key))); |
308 | SvCUR_set(val, HEK_LEN(key)); |
309 | SvREADONLY_on(val); |
310 | SvFAKE_on(val); |
311 | SvPOK_on(val); |
312 | if (HEK_UTF8(key)) |
313 | SvUTF8_on(val); |
314 | |
8e45cc2b |
315 | av_push(retval, val); |
ffd8da72 |
316 | } |
e1a479c5 |
317 | } |
318 | } |
319 | } |
320 | |
ed09b296 |
321 | (void) hv_store_ent(stored, our_name, &PL_sv_undef, 0); |
322 | (void) hv_store(stored, "UNIVERSAL", 9, &PL_sv_undef, 0); |
323 | |
324 | SvREFCNT_inc_simple_void_NN(stored); |
325 | SvTEMP_off(stored); |
326 | SvREADONLY_on(stored); |
327 | |
328 | meta->isa = stored; |
329 | |
0fd7ece8 |
330 | /* now that we're past the exception dangers, grab our own reference to |
331 | the AV we're about to use for the result. The reference owned by the |
332 | mortals' stack will be released soon, so everything will balance. */ |
333 | SvREFCNT_inc_simple_void_NN(retval); |
334 | SvTEMP_off(retval); |
fdef73f9 |
335 | |
1c908217 |
336 | /* we don't want anyone modifying the cache entry but us, |
337 | and we do so by replacing it completely */ |
e1a479c5 |
338 | SvREADONLY_on(retval); |
1c908217 |
339 | |
fa60396f |
340 | return MUTABLE_AV(Perl_mro_set_private_data(aTHX_ meta, dfs_alg, |
341 | MUTABLE_SV(retval))); |
e1a479c5 |
342 | } |
343 | |
344 | /* |
345 | =for apidoc mro_get_linear_isa_c3 |
346 | |
347 | Returns the C3 linearization of @ISA |
348 | the given stash. The return value is a read-only AV*. |
349 | C<level> should be 0 (it is used internally in this |
350 | function's recursion). |
351 | |
1c908217 |
352 | You are responsible for C<SvREFCNT_inc()> on the |
353 | return value if you plan to store it anywhere |
354 | semi-permanently (otherwise it might be deleted |
355 | out from under you the next time the cache is |
356 | invalidated). |
357 | |
e1a479c5 |
358 | =cut |
359 | */ |
360 | |
4befac30 |
361 | static AV* |
94d1e706 |
362 | S_mro_get_linear_isa_c3(pTHX_ HV* stash, U32 level) |
e1a479c5 |
363 | { |
364 | AV* retval; |
365 | GV** gvp; |
366 | GV* gv; |
367 | AV* isa; |
190d0b22 |
368 | const HEK* stashhek; |
e1a479c5 |
369 | struct mro_meta* meta; |
370 | |
7918f24d |
371 | PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA_C3; |
e1a479c5 |
372 | assert(HvAUX(stash)); |
373 | |
190d0b22 |
374 | stashhek = HvNAME_HEK(stash); |
375 | if (!stashhek) |
1e05feb3 |
376 | Perl_croak(aTHX_ "Can't linearize anonymous symbol table"); |
e1a479c5 |
377 | |
378 | if (level > 100) |
379 | Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'", |
190d0b22 |
380 | HEK_KEY(stashhek)); |
e1a479c5 |
381 | |
382 | meta = HvMROMETA(stash); |
1c908217 |
383 | |
384 | /* return cache if valid */ |
123892d9 |
385 | if((retval = MUTABLE_AV(MRO_GET_PRIVATE_DATA(meta, c3_alg)))) { |
e1a479c5 |
386 | return retval; |
387 | } |
388 | |
389 | /* not in cache, make a new one */ |
390 | |
e1a479c5 |
391 | gvp = (GV**)hv_fetchs(stash, "ISA", FALSE); |
392 | isa = (gvp && (gv = *gvp) && isGV_with_GP(gv)) ? GvAV(gv) : NULL; |
393 | |
1c908217 |
394 | /* For a better idea how the rest of this works, see the much clearer |
395 | pure perl version in Algorithm::C3 0.01: |
396 | http://search.cpan.org/src/STEVAN/Algorithm-C3-0.01/lib/Algorithm/C3.pm |
397 | (later versions go about it differently than this code for speed reasons) |
398 | */ |
8638e433 |
399 | |
e1a479c5 |
400 | if(isa && AvFILLp(isa) >= 0) { |
401 | SV** seqs_ptr; |
402 | I32 seqs_items; |
ad64d0ec |
403 | HV* const tails = MUTABLE_HV(sv_2mortal(MUTABLE_SV(newHV()))); |
404 | AV *const seqs = MUTABLE_AV(sv_2mortal(MUTABLE_SV(newAV()))); |
8638e433 |
405 | I32* heads; |
406 | |
407 | /* This builds @seqs, which is an array of arrays. |
408 | The members of @seqs are the MROs of |
409 | the members of @ISA, followed by @ISA itself. |
410 | */ |
e1a479c5 |
411 | I32 items = AvFILLp(isa) + 1; |
412 | SV** isa_ptr = AvARRAY(isa); |
413 | while(items--) { |
1e05feb3 |
414 | SV* const isa_item = *isa_ptr++; |
415 | HV* const isa_item_stash = gv_stashsv(isa_item, 0); |
e1a479c5 |
416 | if(!isa_item_stash) { |
8638e433 |
417 | /* if no stash, make a temporary fake MRO |
418 | containing just itself */ |
70cd14a1 |
419 | AV* const isa_lin = newAV(); |
e1a479c5 |
420 | av_push(isa_lin, newSVsv(isa_item)); |
ad64d0ec |
421 | av_push(seqs, MUTABLE_SV(isa_lin)); |
e1a479c5 |
422 | } |
423 | else { |
70cd14a1 |
424 | /* recursion */ |
425 | AV* const isa_lin = mro_get_linear_isa_c3(isa_item_stash, level + 1); |
ad64d0ec |
426 | av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa_lin))); |
e1a479c5 |
427 | } |
e1a479c5 |
428 | } |
ad64d0ec |
429 | av_push(seqs, SvREFCNT_inc_simple_NN(MUTABLE_SV(isa))); |
8638e433 |
430 | |
431 | /* This builds "heads", which as an array of integer array |
432 | indices, one per seq, which point at the virtual "head" |
433 | of the seq (initially zero) */ |
434 | Newxz(heads, AvFILLp(seqs)+1, I32); |
435 | |
436 | /* This builds %tails, which has one key for every class |
437 | mentioned in the tail of any sequence in @seqs (tail meaning |
438 | everything after the first class, the "head"). The value |
439 | is how many times this key appears in the tails of @seqs. |
440 | */ |
e1a479c5 |
441 | seqs_ptr = AvARRAY(seqs); |
442 | seqs_items = AvFILLp(seqs) + 1; |
443 | while(seqs_items--) { |
502c6561 |
444 | AV *const seq = MUTABLE_AV(*seqs_ptr++); |
e1a479c5 |
445 | I32 seq_items = AvFILLp(seq); |
446 | if(seq_items > 0) { |
447 | SV** seq_ptr = AvARRAY(seq) + 1; |
448 | while(seq_items--) { |
1e05feb3 |
449 | SV* const seqitem = *seq_ptr++; |
694168e2 |
450 | /* LVALUE fetch will create a new undefined SV if necessary |
451 | */ |
452 | HE* const he = hv_fetch_ent(tails, seqitem, 1, 0); |
453 | if(he) { |
1e05feb3 |
454 | SV* const val = HeVAL(he); |
694168e2 |
455 | /* This will increment undef to 1, which is what we |
456 | want for a newly created entry. */ |
e1a479c5 |
457 | sv_inc(val); |
458 | } |
459 | } |
460 | } |
461 | } |
462 | |
1dcae283 |
463 | /* Initialize retval to build the return value in */ |
464 | retval = newAV(); |
190d0b22 |
465 | av_push(retval, newSVhek(stashhek)); /* us first */ |
1dcae283 |
466 | |
8638e433 |
467 | /* This loop won't terminate until we either finish building |
468 | the MRO, or get an exception. */ |
e1a479c5 |
469 | while(1) { |
e1a479c5 |
470 | SV* cand = NULL; |
471 | SV* winner = NULL; |
8638e433 |
472 | int s; |
473 | |
474 | /* "foreach $seq (@seqs)" */ |
1e05feb3 |
475 | SV** const avptr = AvARRAY(seqs); |
8638e433 |
476 | for(s = 0; s <= AvFILLp(seqs); s++) { |
e1a479c5 |
477 | SV** svp; |
502c6561 |
478 | AV * const seq = MUTABLE_AV(avptr[s]); |
1e05feb3 |
479 | SV* seqhead; |
8638e433 |
480 | if(!seq) continue; /* skip empty seqs */ |
481 | svp = av_fetch(seq, heads[s], 0); |
482 | seqhead = *svp; /* seqhead = head of this seq */ |
e1a479c5 |
483 | if(!winner) { |
1e05feb3 |
484 | HE* tail_entry; |
485 | SV* val; |
8638e433 |
486 | /* if we haven't found a winner for this round yet, |
487 | and this seqhead is not in tails (or the count |
488 | for it in tails has dropped to zero), then this |
489 | seqhead is our new winner, and is added to the |
490 | final MRO immediately */ |
e1a479c5 |
491 | cand = seqhead; |
492 | if((tail_entry = hv_fetch_ent(tails, cand, 0, 0)) |
493 | && (val = HeVAL(tail_entry)) |
25270bc0 |
494 | && (SvIVX(val) > 0)) |
e1a479c5 |
495 | continue; |
496 | winner = newSVsv(cand); |
497 | av_push(retval, winner); |
8638e433 |
498 | /* note however that even when we find a winner, |
499 | we continue looping over @seqs to do housekeeping */ |
e1a479c5 |
500 | } |
501 | if(!sv_cmp(seqhead, winner)) { |
8638e433 |
502 | /* Once we have a winner (including the iteration |
503 | where we first found him), inc the head ptr |
504 | for any seq which had the winner as a head, |
505 | NULL out any seq which is now empty, |
506 | and adjust tails for consistency */ |
507 | |
1e05feb3 |
508 | const int new_head = ++heads[s]; |
8638e433 |
509 | if(new_head > AvFILLp(seq)) { |
1dcae283 |
510 | SvREFCNT_dec(avptr[s]); |
8638e433 |
511 | avptr[s] = NULL; |
512 | } |
513 | else { |
1e05feb3 |
514 | HE* tail_entry; |
515 | SV* val; |
8638e433 |
516 | /* Because we know this new seqhead used to be |
517 | a tail, we can assume it is in tails and has |
518 | a positive value, which we need to dec */ |
519 | svp = av_fetch(seq, new_head, 0); |
520 | seqhead = *svp; |
521 | tail_entry = hv_fetch_ent(tails, seqhead, 0, 0); |
522 | val = HeVAL(tail_entry); |
523 | sv_dec(val); |
524 | } |
e1a479c5 |
525 | } |
526 | } |
8638e433 |
527 | |
528 | /* if we found no candidates, we are done building the MRO. |
529 | !cand means no seqs have any entries left to check */ |
530 | if(!cand) { |
531 | Safefree(heads); |
532 | break; |
533 | } |
534 | |
535 | /* If we had candidates, but nobody won, then the @ISA |
536 | hierarchy is not C3-incompatible */ |
e1a479c5 |
537 | if(!winner) { |
8638e433 |
538 | /* we have to do some cleanup before we croak */ |
8638e433 |
539 | |
e1a479c5 |
540 | SvREFCNT_dec(retval); |
8638e433 |
541 | Safefree(heads); |
542 | |
e1a479c5 |
543 | Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': " |
190d0b22 |
544 | "merging failed on parent '%"SVf"'", HEK_KEY(stashhek), SVfARG(cand)); |
e1a479c5 |
545 | } |
546 | } |
547 | } |
1dcae283 |
548 | else { /* @ISA was undefined or empty */ |
549 | /* build a retval containing only ourselves */ |
550 | retval = newAV(); |
190d0b22 |
551 | av_push(retval, newSVhek(stashhek)); |
1dcae283 |
552 | } |
e1a479c5 |
553 | |
1c908217 |
554 | /* we don't want anyone modifying the cache entry but us, |
555 | and we do so by replacing it completely */ |
e1a479c5 |
556 | SvREADONLY_on(retval); |
1c908217 |
557 | |
fa60396f |
558 | return MUTABLE_AV(Perl_mro_set_private_data(aTHX_ meta, c3_alg, |
559 | MUTABLE_SV(retval))); |
e1a479c5 |
560 | return retval; |
561 | } |
562 | |
563 | /* |
564 | =for apidoc mro_get_linear_isa |
565 | |
566 | Returns either C<mro_get_linear_isa_c3> or |
567 | C<mro_get_linear_isa_dfs> for the given stash, |
568 | dependant upon which MRO is in effect |
569 | for that stash. The return value is a |
570 | read-only AV*. |
571 | |
1c908217 |
572 | You are responsible for C<SvREFCNT_inc()> on the |
573 | return value if you plan to store it anywhere |
574 | semi-permanently (otherwise it might be deleted |
575 | out from under you the next time the cache is |
576 | invalidated). |
577 | |
e1a479c5 |
578 | =cut |
579 | */ |
580 | AV* |
581 | Perl_mro_get_linear_isa(pTHX_ HV *stash) |
582 | { |
583 | struct mro_meta* meta; |
2c7f4b87 |
584 | |
7918f24d |
585 | PERL_ARGS_ASSERT_MRO_GET_LINEAR_ISA; |
2c7f4b87 |
586 | if(!SvOOK(stash)) |
587 | Perl_croak(aTHX_ "Can't linearize anonymous symbol table"); |
e1a479c5 |
588 | |
589 | meta = HvMROMETA(stash); |
3d76853f |
590 | if (!meta->mro_which) |
14f97ce6 |
591 | Perl_croak(aTHX_ "panic: invalid MRO!"); |
3d76853f |
592 | return meta->mro_which->resolve(aTHX_ stash, 0); |
e1a479c5 |
593 | } |
594 | |
595 | /* |
596 | =for apidoc mro_isa_changed_in |
597 | |
1c908217 |
598 | Takes the necessary steps (cache invalidations, mostly) |
e1a479c5 |
599 | when the @ISA of the given package has changed. Invoked |
600 | by the C<setisa> magic, should not need to invoke directly. |
601 | |
602 | =cut |
603 | */ |
604 | void |
605 | Perl_mro_isa_changed_in(pTHX_ HV* stash) |
606 | { |
607 | dVAR; |
608 | HV* isarev; |
609 | AV* linear_mro; |
610 | HE* iter; |
611 | SV** svp; |
612 | I32 items; |
1e05feb3 |
613 | bool is_universal; |
2c7f4b87 |
614 | struct mro_meta * meta; |
e1a479c5 |
615 | |
0fa56319 |
616 | const char * const stashname = HvNAME_get(stash); |
617 | const STRLEN stashname_len = HvNAMELEN_get(stash); |
e1a479c5 |
618 | |
7918f24d |
619 | PERL_ARGS_ASSERT_MRO_ISA_CHANGED_IN; |
620 | |
2c7f4b87 |
621 | if(!stashname) |
622 | Perl_croak(aTHX_ "Can't call mro_isa_changed_in() on anonymous symbol table"); |
623 | |
e1a479c5 |
624 | /* wipe out the cached linearizations for this stash */ |
2c7f4b87 |
625 | meta = HvMROMETA(stash); |
ad64d0ec |
626 | SvREFCNT_dec(MUTABLE_SV(meta->mro_linear_dfs)); |
e1a479c5 |
627 | meta->mro_linear_dfs = NULL; |
628 | meta->mro_linear_c3 = NULL; |
5782d502 |
629 | if (meta->isa) { |
630 | SvREFCNT_dec(meta->isa); |
631 | meta->isa = NULL; |
632 | } |
e1a479c5 |
633 | |
70cd14a1 |
634 | /* Inc the package generation, since our @ISA changed */ |
635 | meta->pkg_gen++; |
636 | |
e1a479c5 |
637 | /* Wipe the global method cache if this package |
638 | is UNIVERSAL or one of its parents */ |
dd69841b |
639 | |
640 | svp = hv_fetch(PL_isarev, stashname, stashname_len, 0); |
85fbaab2 |
641 | isarev = svp ? MUTABLE_HV(*svp) : NULL; |
dd69841b |
642 | |
643 | if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL")) |
644 | || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) { |
e1a479c5 |
645 | PL_sub_generation++; |
dd69841b |
646 | is_universal = TRUE; |
647 | } |
1e05feb3 |
648 | else { /* Wipe the local method cache otherwise */ |
dd69841b |
649 | meta->cache_gen++; |
1e05feb3 |
650 | is_universal = FALSE; |
651 | } |
e1a479c5 |
652 | |
653 | /* wipe next::method cache too */ |
654 | if(meta->mro_nextmethod) hv_clear(meta->mro_nextmethod); |
1e05feb3 |
655 | |
e1a479c5 |
656 | /* Iterate the isarev (classes that are our children), |
657 | wiping out their linearization and method caches */ |
dd69841b |
658 | if(isarev) { |
e1a479c5 |
659 | hv_iterinit(isarev); |
660 | while((iter = hv_iternext(isarev))) { |
ec49eb61 |
661 | I32 len; |
662 | const char* const revkey = hv_iterkey(iter, &len); |
663 | HV* revstash = gv_stashpvn(revkey, len, 0); |
bc2cbbac |
664 | struct mro_meta* revmeta; |
665 | |
666 | if(!revstash) continue; |
667 | revmeta = HvMROMETA(revstash); |
ad64d0ec |
668 | SvREFCNT_dec(MUTABLE_SV(revmeta->mro_linear_dfs)); |
e1a479c5 |
669 | revmeta->mro_linear_dfs = NULL; |
670 | revmeta->mro_linear_c3 = NULL; |
dd69841b |
671 | if(!is_universal) |
672 | revmeta->cache_gen++; |
e1a479c5 |
673 | if(revmeta->mro_nextmethod) |
674 | hv_clear(revmeta->mro_nextmethod); |
675 | } |
676 | } |
677 | |
1c908217 |
678 | /* Now iterate our MRO (parents), and do a few things: |
679 | 1) instantiate with the "fake" flag if they don't exist |
680 | 2) flag them as universal if we are universal |
681 | 3) Add everything from our isarev to their isarev |
682 | */ |
683 | |
684 | /* We're starting at the 2nd element, skipping ourselves here */ |
e1a479c5 |
685 | linear_mro = mro_get_linear_isa(stash); |
686 | svp = AvARRAY(linear_mro) + 1; |
687 | items = AvFILLp(linear_mro); |
1c908217 |
688 | |
e1a479c5 |
689 | while (items--) { |
690 | SV* const sv = *svp++; |
e1a479c5 |
691 | HV* mroisarev; |
692 | |
117b69ca |
693 | HE *he = hv_fetch_ent(PL_isarev, sv, TRUE, 0); |
694 | |
695 | /* That fetch should not fail. But if it had to create a new SV for |
4ea50411 |
696 | us, then will need to upgrade it to an HV (which sv_upgrade() can |
697 | now do for us. */ |
117b69ca |
698 | |
85fbaab2 |
699 | mroisarev = MUTABLE_HV(HeVAL(he)); |
e1a479c5 |
700 | |
ad64d0ec |
701 | SvUPGRADE(MUTABLE_SV(mroisarev), SVt_PVHV); |
117b69ca |
702 | |
25270bc0 |
703 | /* This hash only ever contains PL_sv_yes. Storing it over itself is |
704 | almost as cheap as calling hv_exists, so on aggregate we expect to |
705 | save time by not making two calls to the common HV code for the |
706 | case where it doesn't exist. */ |
707 | |
04fe65b0 |
708 | (void)hv_store(mroisarev, stashname, stashname_len, &PL_sv_yes, 0); |
e1a479c5 |
709 | |
710 | if(isarev) { |
711 | hv_iterinit(isarev); |
712 | while((iter = hv_iternext(isarev))) { |
dd69841b |
713 | I32 revkeylen; |
1e05feb3 |
714 | char* const revkey = hv_iterkey(iter, &revkeylen); |
04fe65b0 |
715 | (void)hv_store(mroisarev, revkey, revkeylen, &PL_sv_yes, 0); |
e1a479c5 |
716 | } |
717 | } |
718 | } |
719 | } |
720 | |
721 | /* |
722 | =for apidoc mro_method_changed_in |
723 | |
47c9dd14 |
724 | Invalidates method caching on any child classes |
725 | of the given stash, so that they might notice |
726 | the changes in this one. |
e1a479c5 |
727 | |
728 | Ideally, all instances of C<PL_sub_generation++> in |
dd69841b |
729 | perl source outside of C<mro.c> should be |
730 | replaced by calls to this. |
731 | |
732 | Perl automatically handles most of the common |
733 | ways a method might be redefined. However, there |
734 | are a few ways you could change a method in a stash |
735 | without the cache code noticing, in which case you |
736 | need to call this method afterwards: |
e1a479c5 |
737 | |
dd69841b |
738 | 1) Directly manipulating the stash HV entries from |
739 | XS code. |
e1a479c5 |
740 | |
dd69841b |
741 | 2) Assigning a reference to a readonly scalar |
742 | constant into a stash entry in order to create |
743 | a constant subroutine (like constant.pm |
744 | does). |
745 | |
746 | This same method is available from pure perl |
747 | via, C<mro::method_changed_in(classname)>. |
e1a479c5 |
748 | |
749 | =cut |
750 | */ |
751 | void |
752 | Perl_mro_method_changed_in(pTHX_ HV *stash) |
753 | { |
1e05feb3 |
754 | const char * const stashname = HvNAME_get(stash); |
755 | const STRLEN stashname_len = HvNAMELEN_get(stash); |
dd69841b |
756 | |
1e05feb3 |
757 | SV ** const svp = hv_fetch(PL_isarev, stashname, stashname_len, 0); |
85fbaab2 |
758 | HV * const isarev = svp ? MUTABLE_HV(*svp) : NULL; |
e1a479c5 |
759 | |
7918f24d |
760 | PERL_ARGS_ASSERT_MRO_METHOD_CHANGED_IN; |
761 | |
2c7f4b87 |
762 | if(!stashname) |
763 | Perl_croak(aTHX_ "Can't call mro_method_changed_in() on anonymous symbol table"); |
764 | |
70cd14a1 |
765 | /* Inc the package generation, since a local method changed */ |
766 | HvMROMETA(stash)->pkg_gen++; |
767 | |
e1a479c5 |
768 | /* If stash is UNIVERSAL, or one of UNIVERSAL's parents, |
769 | invalidate all method caches globally */ |
dd69841b |
770 | if((stashname_len == 9 && strEQ(stashname, "UNIVERSAL")) |
771 | || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) { |
e1a479c5 |
772 | PL_sub_generation++; |
773 | return; |
774 | } |
775 | |
776 | /* else, invalidate the method caches of all child classes, |
777 | but not itself */ |
dd69841b |
778 | if(isarev) { |
1e05feb3 |
779 | HE* iter; |
780 | |
e1a479c5 |
781 | hv_iterinit(isarev); |
782 | while((iter = hv_iternext(isarev))) { |
ec49eb61 |
783 | I32 len; |
784 | const char* const revkey = hv_iterkey(iter, &len); |
785 | HV* const revstash = gv_stashpvn(revkey, len, 0); |
bc2cbbac |
786 | struct mro_meta* mrometa; |
787 | |
788 | if(!revstash) continue; |
789 | mrometa = HvMROMETA(revstash); |
dd69841b |
790 | mrometa->cache_gen++; |
e1a479c5 |
791 | if(mrometa->mro_nextmethod) |
792 | hv_clear(mrometa->mro_nextmethod); |
793 | } |
794 | } |
795 | } |
796 | |
797 | /* These two are static helpers for next::method and friends, |
798 | and re-implement a bunch of the code from pp_caller() in |
799 | a more efficient manner for this particular usage. |
800 | */ |
801 | |
802 | STATIC I32 |
803 | __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) { |
804 | I32 i; |
805 | for (i = startingblock; i >= 0; i--) { |
806 | if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i; |
807 | } |
808 | return i; |
809 | } |
810 | |
e1a479c5 |
811 | #include "XSUB.h" |
812 | |
813 | XS(XS_mro_get_linear_isa); |
814 | XS(XS_mro_set_mro); |
815 | XS(XS_mro_get_mro); |
816 | XS(XS_mro_get_isarev); |
817 | XS(XS_mro_is_universal); |
c5860d66 |
818 | XS(XS_mro_invalidate_method_caches); |
e1a479c5 |
819 | XS(XS_mro_method_changed_in); |
70cd14a1 |
820 | XS(XS_mro_get_pkg_gen); |
f58cd386 |
821 | XS(XS_mro_nextcan); |
e1a479c5 |
822 | |
823 | void |
824 | Perl_boot_core_mro(pTHX) |
825 | { |
826 | dVAR; |
827 | static const char file[] = __FILE__; |
828 | |
829 | newXSproto("mro::get_linear_isa", XS_mro_get_linear_isa, file, "$;$"); |
830 | newXSproto("mro::set_mro", XS_mro_set_mro, file, "$$"); |
831 | newXSproto("mro::get_mro", XS_mro_get_mro, file, "$"); |
832 | newXSproto("mro::get_isarev", XS_mro_get_isarev, file, "$"); |
833 | newXSproto("mro::is_universal", XS_mro_is_universal, file, "$"); |
c5860d66 |
834 | newXSproto("mro::invalidate_all_method_caches", XS_mro_invalidate_method_caches, file, ""); |
e1a479c5 |
835 | newXSproto("mro::method_changed_in", XS_mro_method_changed_in, file, "$"); |
70cd14a1 |
836 | newXSproto("mro::get_pkg_gen", XS_mro_get_pkg_gen, file, "$"); |
f58cd386 |
837 | newXS("mro::_nextcan", XS_mro_nextcan, file); |
e1a479c5 |
838 | } |
839 | |
840 | XS(XS_mro_get_linear_isa) { |
841 | dVAR; |
842 | dXSARGS; |
843 | AV* RETVAL; |
844 | HV* class_stash; |
845 | SV* classname; |
846 | |
e1a479c5 |
847 | if(items < 1 || items > 2) |
afa74d42 |
848 | croak_xs_usage(cv, "classname [, type ]"); |
e1a479c5 |
849 | |
850 | classname = ST(0); |
851 | class_stash = gv_stashsv(classname, 0); |
e1a479c5 |
852 | |
70cd14a1 |
853 | if(!class_stash) { |
854 | /* No stash exists yet, give them just the classname */ |
855 | AV* isalin = newAV(); |
856 | av_push(isalin, newSVsv(classname)); |
ad64d0ec |
857 | ST(0) = sv_2mortal(newRV_noinc(MUTABLE_SV(isalin))); |
70cd14a1 |
858 | XSRETURN(1); |
859 | } |
860 | else if(items > 1) { |
1e05feb3 |
861 | const char* const which = SvPV_nolen(ST(1)); |
3d76853f |
862 | const struct mro_alg *const algo = S_get_mro_from_name(aTHX_ which); |
863 | if (!algo) |
864 | Perl_croak(aTHX_ "Invalid mro name: '%s'", which); |
84dccb35 |
865 | RETVAL = algo->resolve(aTHX_ class_stash, 0); |
e1a479c5 |
866 | } |
867 | else { |
868 | RETVAL = mro_get_linear_isa(class_stash); |
869 | } |
870 | |
ad64d0ec |
871 | ST(0) = newRV_inc(MUTABLE_SV(RETVAL)); |
e1a479c5 |
872 | sv_2mortal(ST(0)); |
873 | XSRETURN(1); |
874 | } |
875 | |
876 | XS(XS_mro_set_mro) |
877 | { |
878 | dVAR; |
879 | dXSARGS; |
880 | SV* classname; |
3d76853f |
881 | const char* whichstr; |
882 | const struct mro_alg *which; |
e1a479c5 |
883 | HV* class_stash; |
884 | struct mro_meta* meta; |
885 | |
e1a479c5 |
886 | if (items != 2) |
afa74d42 |
887 | croak_xs_usage(cv, "classname, type"); |
e1a479c5 |
888 | |
889 | classname = ST(0); |
890 | whichstr = SvPV_nolen(ST(1)); |
891 | class_stash = gv_stashsv(classname, GV_ADD); |
892 | if(!class_stash) Perl_croak(aTHX_ "Cannot create class: '%"SVf"'!", SVfARG(classname)); |
893 | meta = HvMROMETA(class_stash); |
894 | |
3d76853f |
895 | which = S_get_mro_from_name(aTHX_ whichstr); |
896 | if (!which) |
e1a479c5 |
897 | Perl_croak(aTHX_ "Invalid mro name: '%s'", whichstr); |
898 | |
899 | if(meta->mro_which != which) { |
900 | meta->mro_which = which; |
fa60396f |
901 | /* Scrub our cached pointer to the private data. */ |
902 | meta->mro_linear_c3 = NULL; |
e1a479c5 |
903 | /* Only affects local method cache, not |
904 | even child classes */ |
dd69841b |
905 | meta->cache_gen++; |
e1a479c5 |
906 | if(meta->mro_nextmethod) |
907 | hv_clear(meta->mro_nextmethod); |
908 | } |
909 | |
910 | XSRETURN_EMPTY; |
911 | } |
912 | |
913 | |
914 | XS(XS_mro_get_mro) |
915 | { |
916 | dVAR; |
917 | dXSARGS; |
918 | SV* classname; |
919 | HV* class_stash; |
e1a479c5 |
920 | |
e1a479c5 |
921 | if (items != 1) |
afa74d42 |
922 | croak_xs_usage(cv, "classname"); |
e1a479c5 |
923 | |
924 | classname = ST(0); |
925 | class_stash = gv_stashsv(classname, 0); |
e1a479c5 |
926 | |
3d76853f |
927 | ST(0) = sv_2mortal(newSVpv(class_stash |
928 | ? HvMROMETA(class_stash)->mro_which->name |
929 | : "dfs", 0)); |
e1a479c5 |
930 | XSRETURN(1); |
931 | } |
932 | |
933 | XS(XS_mro_get_isarev) |
934 | { |
935 | dVAR; |
936 | dXSARGS; |
937 | SV* classname; |
73968c7a |
938 | HE* he; |
e1a479c5 |
939 | HV* isarev; |
70cd14a1 |
940 | AV* ret_array; |
e1a479c5 |
941 | |
e1a479c5 |
942 | if (items != 1) |
afa74d42 |
943 | croak_xs_usage(cv, "classname"); |
e1a479c5 |
944 | |
945 | classname = ST(0); |
946 | |
e1a479c5 |
947 | SP -= items; |
dd69841b |
948 | |
70cd14a1 |
949 | |
73968c7a |
950 | he = hv_fetch_ent(PL_isarev, classname, 0, 0); |
85fbaab2 |
951 | isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL; |
70cd14a1 |
952 | |
953 | ret_array = newAV(); |
dd69841b |
954 | if(isarev) { |
e1a479c5 |
955 | HE* iter; |
956 | hv_iterinit(isarev); |
957 | while((iter = hv_iternext(isarev))) |
70cd14a1 |
958 | av_push(ret_array, newSVsv(hv_iterkeysv(iter))); |
e1a479c5 |
959 | } |
ad64d0ec |
960 | mXPUSHs(newRV_noinc(MUTABLE_SV(ret_array))); |
e1a479c5 |
961 | |
962 | PUTBACK; |
963 | return; |
964 | } |
965 | |
966 | XS(XS_mro_is_universal) |
967 | { |
968 | dVAR; |
969 | dXSARGS; |
970 | SV* classname; |
dd69841b |
971 | HV* isarev; |
70cd14a1 |
972 | char* classname_pv; |
973 | STRLEN classname_len; |
73968c7a |
974 | HE* he; |
e1a479c5 |
975 | |
e1a479c5 |
976 | if (items != 1) |
afa74d42 |
977 | croak_xs_usage(cv, "classname"); |
e1a479c5 |
978 | |
979 | classname = ST(0); |
e1a479c5 |
980 | |
cfff9797 |
981 | classname_pv = SvPV(classname,classname_len); |
dd69841b |
982 | |
73968c7a |
983 | he = hv_fetch_ent(PL_isarev, classname, 0, 0); |
85fbaab2 |
984 | isarev = he ? MUTABLE_HV(HeVAL(he)) : NULL; |
dd69841b |
985 | |
70cd14a1 |
986 | if((classname_len == 9 && strEQ(classname_pv, "UNIVERSAL")) |
dd69841b |
987 | || (isarev && hv_exists(isarev, "UNIVERSAL", 9))) |
9edc5bb8 |
988 | XSRETURN_YES; |
989 | else |
990 | XSRETURN_NO; |
e1a479c5 |
991 | } |
992 | |
c5860d66 |
993 | XS(XS_mro_invalidate_method_caches) |
e1a479c5 |
994 | { |
995 | dVAR; |
996 | dXSARGS; |
997 | |
e1a479c5 |
998 | if (items != 0) |
afa74d42 |
999 | croak_xs_usage(cv, ""); |
e1a479c5 |
1000 | |
1001 | PL_sub_generation++; |
1002 | |
1003 | XSRETURN_EMPTY; |
1004 | } |
1005 | |
e1a479c5 |
1006 | XS(XS_mro_method_changed_in) |
1007 | { |
1008 | dVAR; |
1009 | dXSARGS; |
1010 | SV* classname; |
1011 | HV* class_stash; |
1012 | |
e1a479c5 |
1013 | if(items != 1) |
afa74d42 |
1014 | croak_xs_usage(cv, "classname"); |
e1a479c5 |
1015 | |
1016 | classname = ST(0); |
1017 | |
1018 | class_stash = gv_stashsv(classname, 0); |
1019 | if(!class_stash) Perl_croak(aTHX_ "No such class: '%"SVf"'!", SVfARG(classname)); |
1020 | |
1021 | mro_method_changed_in(class_stash); |
1022 | |
1023 | XSRETURN_EMPTY; |
1024 | } |
1025 | |
70cd14a1 |
1026 | XS(XS_mro_get_pkg_gen) |
1027 | { |
1028 | dVAR; |
1029 | dXSARGS; |
1030 | SV* classname; |
1031 | HV* class_stash; |
1032 | |
70cd14a1 |
1033 | if(items != 1) |
afa74d42 |
1034 | croak_xs_usage(cv, "classname"); |
70cd14a1 |
1035 | |
1036 | classname = ST(0); |
1037 | |
1038 | class_stash = gv_stashsv(classname, 0); |
1039 | |
1040 | SP -= items; |
1041 | |
6e449a3a |
1042 | mXPUSHi(class_stash ? HvMROMETA(class_stash)->pkg_gen : 0); |
70cd14a1 |
1043 | |
1044 | PUTBACK; |
1045 | return; |
1046 | } |
1047 | |
f58cd386 |
1048 | XS(XS_mro_nextcan) |
e1a479c5 |
1049 | { |
1050 | dVAR; |
1051 | dXSARGS; |
f58cd386 |
1052 | SV* self = ST(0); |
1053 | const I32 throw_nomethod = SvIVX(ST(1)); |
bbd28cb9 |
1054 | register I32 cxix = cxstack_ix; |
f58cd386 |
1055 | register const PERL_CONTEXT *ccstack = cxstack; |
1056 | const PERL_SI *top_si = PL_curstackinfo; |
1057 | HV* selfstash; |
1058 | SV *stashname; |
1059 | const char *fq_subname; |
1060 | const char *subname; |
1061 | STRLEN stashname_len; |
1062 | STRLEN subname_len; |
1063 | SV* sv; |
1064 | GV** gvp; |
1065 | AV* linear_av; |
1066 | SV** linear_svp; |
1067 | const char *hvname; |
1068 | I32 entries; |
1069 | struct mro_meta* selfmeta; |
1070 | HV* nmcache; |
bbd28cb9 |
1071 | I32 i; |
e1a479c5 |
1072 | |
48fc4736 |
1073 | PERL_UNUSED_ARG(cv); |
1074 | |
f58cd386 |
1075 | SP -= items; |
1076 | |
1077 | if(sv_isobject(self)) |
1078 | selfstash = SvSTASH(SvRV(self)); |
1079 | else |
5fa9f951 |
1080 | selfstash = gv_stashsv(self, GV_ADD); |
f58cd386 |
1081 | |
1082 | assert(selfstash); |
1083 | |
1084 | hvname = HvNAME_get(selfstash); |
1085 | if (!hvname) |
1086 | Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup"); |
1087 | |
f58cd386 |
1088 | /* This block finds the contextually-enclosing fully-qualified subname, |
1089 | much like looking at (caller($i))[3] until you find a real sub that |
bbd28cb9 |
1090 | isn't ANON, etc (also skips over pureperl next::method, etc) */ |
1091 | for(i = 0; i < 2; i++) { |
1092 | cxix = __dopoptosub_at(ccstack, cxix); |
1093 | for (;;) { |
1094 | GV* cvgv; |
1095 | STRLEN fq_subname_len; |
1096 | |
1097 | /* we may be in a higher stacklevel, so dig down deeper */ |
1098 | while (cxix < 0) { |
1099 | if(top_si->si_type == PERLSI_MAIN) |
1100 | Perl_croak(aTHX_ "next::method/next::can/maybe::next::method must be used in method context"); |
1101 | top_si = top_si->si_prev; |
1102 | ccstack = top_si->si_cxstack; |
1103 | cxix = __dopoptosub_at(ccstack, top_si->si_cxix); |
1104 | } |
f58cd386 |
1105 | |
bbd28cb9 |
1106 | if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB |
1107 | || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) { |
1108 | cxix = __dopoptosub_at(ccstack, cxix - 1); |
1109 | continue; |
1110 | } |
e1a479c5 |
1111 | |
bbd28cb9 |
1112 | { |
1113 | const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1); |
1114 | if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) { |
1115 | if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) { |
1116 | cxix = dbcxix; |
1117 | continue; |
1118 | } |
f58cd386 |
1119 | } |
1120 | } |
f58cd386 |
1121 | |
bbd28cb9 |
1122 | cvgv = CvGV(ccstack[cxix].blk_sub.cv); |
f58cd386 |
1123 | |
bbd28cb9 |
1124 | if(!isGV(cvgv)) { |
1125 | cxix = __dopoptosub_at(ccstack, cxix - 1); |
1126 | continue; |
1127 | } |
f58cd386 |
1128 | |
bbd28cb9 |
1129 | /* we found a real sub here */ |
1130 | sv = sv_2mortal(newSV(0)); |
f58cd386 |
1131 | |
bbd28cb9 |
1132 | gv_efullname3(sv, cvgv, NULL); |
f58cd386 |
1133 | |
bbd28cb9 |
1134 | fq_subname = SvPVX(sv); |
1135 | fq_subname_len = SvCUR(sv); |
f58cd386 |
1136 | |
bbd28cb9 |
1137 | subname = strrchr(fq_subname, ':'); |
1138 | if(!subname) |
1139 | Perl_croak(aTHX_ "next::method/next::can/maybe::next::method cannot find enclosing method"); |
f58cd386 |
1140 | |
bbd28cb9 |
1141 | subname++; |
1142 | subname_len = fq_subname_len - (subname - fq_subname); |
1143 | if(subname_len == 8 && strEQ(subname, "__ANON__")) { |
1144 | cxix = __dopoptosub_at(ccstack, cxix - 1); |
1145 | continue; |
1146 | } |
1147 | break; |
f58cd386 |
1148 | } |
bbd28cb9 |
1149 | cxix--; |
e1a479c5 |
1150 | } |
f58cd386 |
1151 | |
1152 | /* If we made it to here, we found our context */ |
1153 | |
1154 | /* Initialize the next::method cache for this stash |
1155 | if necessary */ |
1156 | selfmeta = HvMROMETA(selfstash); |
1157 | if(!(nmcache = selfmeta->mro_nextmethod)) { |
1158 | nmcache = selfmeta->mro_nextmethod = newHV(); |
1159 | } |
1160 | else { /* Use the cached coderef if it exists */ |
1161 | HE* cache_entry = hv_fetch_ent(nmcache, sv, 0, 0); |
1162 | if (cache_entry) { |
1163 | SV* const val = HeVAL(cache_entry); |
1164 | if(val == &PL_sv_undef) { |
1165 | if(throw_nomethod) |
1166 | Perl_croak(aTHX_ "No next::method '%s' found for %s", subname, hvname); |
1167 | XSRETURN_EMPTY; |
1168 | } |
6e449a3a |
1169 | mXPUSHs(newRV_inc(val)); |
f58cd386 |
1170 | XSRETURN(1); |
1171 | } |
e1a479c5 |
1172 | } |
1173 | |
f58cd386 |
1174 | /* beyond here is just for cache misses, so perf isn't as critical */ |
e1a479c5 |
1175 | |
f58cd386 |
1176 | stashname_len = subname - fq_subname - 2; |
59cd0e26 |
1177 | stashname = newSVpvn_flags(fq_subname, stashname_len, SVs_TEMP); |
e1a479c5 |
1178 | |
f58cd386 |
1179 | linear_av = mro_get_linear_isa_c3(selfstash, 0); /* has ourselves at the top of the list */ |
e1a479c5 |
1180 | |
f58cd386 |
1181 | linear_svp = AvARRAY(linear_av); |
1182 | entries = AvFILLp(linear_av) + 1; |
e1a479c5 |
1183 | |
f58cd386 |
1184 | /* Walk down our MRO, skipping everything up |
1185 | to the contextually enclosing class */ |
1186 | while (entries--) { |
1187 | SV * const linear_sv = *linear_svp++; |
1188 | assert(linear_sv); |
1189 | if(sv_eq(linear_sv, stashname)) |
1190 | break; |
1191 | } |
e1a479c5 |
1192 | |
f58cd386 |
1193 | /* Now search the remainder of the MRO for the |
1194 | same method name as the contextually enclosing |
1195 | method */ |
1196 | if(entries > 0) { |
1197 | while (entries--) { |
1198 | SV * const linear_sv = *linear_svp++; |
1199 | HV* curstash; |
1200 | GV* candidate; |
1201 | CV* cand_cv; |
e1a479c5 |
1202 | |
f58cd386 |
1203 | assert(linear_sv); |
1204 | curstash = gv_stashsv(linear_sv, FALSE); |
1205 | |
1206 | if (!curstash) { |
1207 | if (ckWARN(WARN_SYNTAX)) |
1208 | Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Can't locate package %"SVf" for @%s::ISA", |
1209 | (void*)linear_sv, hvname); |
1210 | continue; |
1211 | } |
1212 | |
1213 | assert(curstash); |
1214 | |
1215 | gvp = (GV**)hv_fetch(curstash, subname, subname_len, 0); |
1216 | if (!gvp) continue; |
1217 | |
1218 | candidate = *gvp; |
1219 | assert(candidate); |
1220 | |
1221 | if (SvTYPE(candidate) != SVt_PVGV) |
1222 | gv_init(candidate, curstash, subname, subname_len, TRUE); |
1223 | |
1224 | /* Notably, we only look for real entries, not method cache |
1225 | entries, because in C3 the method cache of a parent is not |
1226 | valid for the child */ |
1227 | if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) { |
ad64d0ec |
1228 | SvREFCNT_inc_simple_void_NN(MUTABLE_SV(cand_cv)); |
1229 | (void)hv_store_ent(nmcache, newSVsv(sv), MUTABLE_SV(cand_cv), 0); |
1230 | mXPUSHs(newRV_inc(MUTABLE_SV(cand_cv))); |
f58cd386 |
1231 | XSRETURN(1); |
1232 | } |
1233 | } |
e1a479c5 |
1234 | } |
1235 | |
04fe65b0 |
1236 | (void)hv_store_ent(nmcache, newSVsv(sv), &PL_sv_undef, 0); |
f58cd386 |
1237 | if(throw_nomethod) |
1238 | Perl_croak(aTHX_ "No next::method '%s' found for %s", subname, hvname); |
1239 | XSRETURN_EMPTY; |
e1a479c5 |
1240 | } |
1241 | |
1242 | /* |
1243 | * Local variables: |
1244 | * c-indentation-style: bsd |
1245 | * c-basic-offset: 4 |
1246 | * indent-tabs-mode: t |
1247 | * End: |
1248 | * |
1249 | * ex: set ts=8 sts=4 sw=4 noet: |
1250 | */ |