0.01_03 fixes
[gitmo/Class-C3-XS.git] / XS.xs
1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 /* Most of this code is backported from the bleadperl patch's
7    mro.c, and then modified to work with Class::C3's
8    internals.
9 */
10
11 /* This is %next::METHOD_CACHE */
12 STATIC HV* nmcache;
13
14 AV*
15 __mro_linear_isa_c3(pTHX_ HV* stash, HV* cache, I32 level)
16 {
17     AV* retval;
18     GV** gvp;
19     GV* gv;
20     AV* isa;
21     const char* stashname;
22     STRLEN stashname_len;
23
24     assert(stash);
25     assert(HvAUX(stash));
26
27     stashname = HvNAME(stash);
28     stashname_len = strlen(stashname);
29     if (!stashname)
30       Perl_croak(aTHX_
31                  "Can't linearize anonymous symbol table");
32
33     if (level > 100)
34         Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
35               stashname);
36
37     if(!cache) {
38         cache = (HV*)sv_2mortal((SV*)newHV());
39     }
40     else {
41         SV** cache_entry = hv_fetch(cache, stashname, stashname_len, 0);
42         if(cache_entry)
43             return (AV*)SvREFCNT_inc(*cache_entry);
44     }
45
46     /* not in cache, make a new one */
47
48     retval = (AV*)sv_2mortal((SV*)newAV());
49     av_push(retval, newSVpvn(stashname, stashname_len)); /* us first */
50
51     gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
52     isa = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : NULL;
53
54     if(isa && AvFILLp(isa) >= 0) {
55         SV** seqs_ptr;
56         I32 seqs_items;
57         HV* tails = (HV*)sv_2mortal((SV*)newHV());
58         AV* seqs = (AV*)sv_2mortal((SV*)newAV());
59         I32 items = AvFILLp(isa) + 1;
60         SV** isa_ptr = AvARRAY(isa);
61         while(items--) {
62             AV* isa_lin;
63             SV* isa_item = *isa_ptr++;
64             HV* isa_item_stash = gv_stashsv(isa_item, 0);
65             if(!isa_item_stash) {
66                 isa_lin = newAV();
67                 av_push(isa_lin, newSVsv(isa_item));
68             }
69             else {
70                 isa_lin = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(aTHX_ isa_item_stash, cache, level + 1)); /* recursion */
71             }
72             av_push(seqs, (SV*)av_make(AvFILLp(isa_lin)+1, AvARRAY(isa_lin)));
73         }
74         av_push(seqs, (SV*)av_make(AvFILLp(isa)+1, AvARRAY(isa)));
75
76         seqs_ptr = AvARRAY(seqs);
77         seqs_items = AvFILLp(seqs) + 1;
78         while(seqs_items--) {
79             AV* seq = (AV*)*seqs_ptr++;
80             I32 seq_items = AvFILLp(seq);
81             if(seq_items > 0) {
82                 SV** seq_ptr = AvARRAY(seq) + 1;
83                 while(seq_items--) {
84                     SV* seqitem = *seq_ptr++;
85                     HE* he = hv_fetch_ent(tails, seqitem, 0, 0);
86                     if(!he) {
87                         hv_store_ent(tails, seqitem, newSViv(1), 0);
88                     }
89                     else {
90                         SV* val = HeVAL(he);
91                         sv_inc(val);
92                     }
93                 }
94             }
95         }
96
97         while(1) {
98             SV* seqhead = NULL;
99             SV* cand = NULL;
100             SV* winner = NULL;
101             SV* val;
102             HE* tail_entry;
103             AV* seq;
104             SV** avptr = AvARRAY(seqs);
105             items = AvFILLp(seqs)+1;
106             while(items--) {
107                 SV** svp;
108                 seq = (AV*)*avptr++;
109                 if(AvFILLp(seq) < 0) continue;
110                 svp = av_fetch(seq, 0, 0);
111                 seqhead = *svp;
112                 if(!winner) {
113                     cand = seqhead;
114                     if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
115                        && (val = HeVAL(tail_entry))
116                        && (SvIVx(val) > 0))
117                            continue;
118                     winner = newSVsv(cand);
119                     av_push(retval, winner);
120                 }
121                 if(!sv_cmp(seqhead, winner)) {
122                     sv_2mortal(av_shift(seq));
123                     if(AvFILLp(seq) < 0) continue;
124                     svp = av_fetch(seq, 0, 0);
125                     seqhead = *svp;
126                     tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
127                     val = HeVAL(tail_entry);
128                     sv_dec(val);
129                 }
130             }
131             if(!cand) break;
132             if(!winner)
133                 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
134                     "merging failed on parent '%s'", stashname, SvPV_nolen(cand));
135         }
136     }
137
138     SvREADONLY_on(retval);
139     SvREFCNT_inc(retval); /* for cache storage */
140     SvREFCNT_inc(retval); /* for return */
141     hv_store(cache, stashname, stashname_len, (SV*)retval, 0);
142     return retval;
143 }
144
145 STATIC I32
146 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
147     I32 i;
148     for (i = startingblock; i >= 0; i--) {
149         if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
150     }
151     return i;
152 }
153
154 STATIC SV*
155 __nextcan(pTHX_ SV* self, I32 throw_nomethod)
156 {
157     register I32 cxix;
158     register const PERL_CONTEXT *ccstack = cxstack;
159     const PERL_SI *top_si = PL_curstackinfo;
160     HV* selfstash;
161     GV* cvgv;
162     SV *stashname;
163     const char *fq_subname;
164     const char *subname;
165     STRLEN fq_subname_len;
166     STRLEN stashname_len;
167     STRLEN subname_len;
168     SV* sv;
169     GV** gvp;
170     AV* linear_av;
171     SV** linear_svp;
172     SV* linear_sv;
173     HV* cstash;
174     GV* candidate = NULL;
175     CV* cand_cv = NULL;
176     const char *hvname;
177     I32 items;
178     HE* cache_entry;
179     SV* cachekey;
180
181     if(sv_isobject(self))
182         selfstash = SvSTASH(SvRV(self));
183     else
184         selfstash = gv_stashsv(self, 0);
185
186     assert(selfstash);
187
188     hvname = HvNAME(selfstash);
189     if (!hvname)
190         croak("Can't use anonymous symbol table for method lookup");
191
192     cxix = __dopoptosub_at(cxstack, cxstack_ix);
193
194     /* This block finds the contextually-enclosing fully-qualified subname,
195        much like looking at (caller($i))[3] until you find a real sub that
196        isn't ANON, etc */
197     for (;;) {
198         /* we may be in a higher stacklevel, so dig down deeper */
199         while (cxix < 0) {
200             if(top_si->si_type == PERLSI_MAIN)
201                 croak("next::method/next::can/maybe::next::method must be used in method context");
202             top_si = top_si->si_prev;
203             ccstack = top_si->si_cxstack;
204             cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
205         }
206
207         if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
208           || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
209             cxix = __dopoptosub_at(ccstack, cxix - 1);
210             continue;
211         }
212
213         {
214             const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
215             if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
216                 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
217                     cxix = dbcxix;
218                     continue;
219                 }
220             }
221         }
222
223         cvgv = CvGV(ccstack[cxix].blk_sub.cv);
224
225         if(!isGV(cvgv)) {
226             cxix = __dopoptosub_at(ccstack, cxix - 1);
227             continue;
228         }
229
230         /* we found a real sub here */
231         sv = sv_2mortal(newSV(0));
232
233         gv_efullname3(sv, cvgv, NULL);
234
235         fq_subname = SvPVX(sv);
236         fq_subname_len = SvCUR(sv);
237
238         subname = strrchr(fq_subname, ':');
239         if(!subname)
240             croak("next::method/next::can/maybe::next::method cannot find enclosing method");
241
242         subname++;
243         subname_len = fq_subname_len - (subname - fq_subname);
244         if(subname_len == 8 && strEQ(subname, "__ANON__")) {
245             cxix = __dopoptosub_at(ccstack, cxix - 1);
246             continue;
247         }
248         break;
249     }
250
251     /* If we made it to here, we found our context */
252
253     /* cachekey = "objpkg|context::method::name" */
254     cachekey = sv_2mortal(newSVpv(hvname, 0));
255     sv_catpvn(cachekey, "|", 1);
256     sv_catsv(cachekey, sv);
257
258     if((cache_entry = hv_fetch_ent(nmcache, cachekey, 0, 0))) {
259         SV* val = HeVAL(cache_entry);
260         if(val == &PL_sv_undef) {
261             if(throw_nomethod)
262                 croak("No next::method '%s' found for %s", subname, hvname);
263             return &PL_sv_undef;
264         }
265         return SvREFCNT_inc(val);
266     }
267
268     /* beyond here is just for cache misses, so perf isn't as critical */
269
270     stashname_len = subname - fq_subname - 2;
271     stashname = sv_2mortal(newSVpvn(fq_subname, stashname_len));
272
273     linear_av = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(aTHX_ selfstash, NULL, 0));
274
275     linear_svp = AvARRAY(linear_av);
276     items = AvFILLp(linear_av) + 1;
277
278     while (items--) {
279         linear_sv = *linear_svp++;
280         assert(linear_sv);
281         if(sv_eq(linear_sv, stashname))
282             break;
283     }
284
285     if(items > 0) {
286         SV* sub_sv = sv_2mortal(newSVpv(subname, subname_len));
287         HV* cc3_mro = get_hv("Class::C3::MRO", 0);
288
289         while (items--) {
290             linear_sv = *linear_svp++;
291             assert(linear_sv);
292
293             if(cc3_mro) {
294                 HE* he_cc3_mro_class = hv_fetch_ent(cc3_mro, linear_sv, 0, 0);
295                 if(he_cc3_mro_class) {
296                     HV* cc3_mro_class = (HV*)SvRV(HeVAL(he_cc3_mro_class));
297                     SV** svp_cc3_mro_class_methods = hv_fetch(cc3_mro_class, "methods", 7, 0);
298                     if(svp_cc3_mro_class_methods) {
299                         HV* cc3_mro_class_methods = (HV*)SvRV(*svp_cc3_mro_class_methods);
300                         if(hv_exists_ent(cc3_mro_class_methods, sub_sv, 0))
301                             continue;
302                     }
303                 }
304             }
305
306             cstash = gv_stashsv(linear_sv, FALSE);
307
308             if (!cstash) {
309                 if (ckWARN(WARN_MISC))
310                     Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
311                         (void*)linear_sv, hvname);
312                 continue;
313             }
314
315             assert(cstash);
316
317             gvp = (GV**)hv_fetch(cstash, subname, subname_len, 0);
318             if (!gvp) continue;
319
320             candidate = *gvp;
321             assert(candidate);
322
323             if (SvTYPE(candidate) != SVt_PVGV)
324                 gv_init(candidate, cstash, subname, subname_len, TRUE);
325             if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
326                 SvREFCNT_inc((SV*)cand_cv);
327                 hv_store_ent(nmcache, newSVsv(cachekey), (SV*)cand_cv, 0);
328                 return (SV*)cand_cv;
329             }
330         }
331     }
332
333     hv_store_ent(nmcache, newSVsv(cachekey), &PL_sv_undef, 0);
334     if(throw_nomethod)
335         croak("No next::method '%s' found for %s", subname, hvname);
336     return &PL_sv_undef;
337 }
338
339 XS(XS_Class_C3_XS_calculateMRO);
340 XS(XS_Class_C3_XS_calculateMRO)
341 {
342 #ifdef dVAR
343     dVAR; dXSARGS;
344 #else
345     dXSARGS;
346 #endif
347
348     SV* classname;
349     HV* class_stash;
350     HV* cache = NULL;
351     AV* res;
352     I32 res_items;
353     I32 ret_items;
354     SV** res_ptr;
355
356     if(items < 1 || items > 2)
357         croak("Usage: calculateMRO(classname[, cache])");
358
359     classname = ST(0);
360     if(items == 2) cache = (HV*)SvRV(ST(1));
361
362     class_stash = gv_stashsv(classname, 0);
363     if(!class_stash) croak("No such class: '%s'!", SvPV_nolen(classname));
364
365     res = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(aTHX_ class_stash, cache, 0));
366
367     res_items = ret_items = AvFILLp(res) + 1;
368     res_ptr = AvARRAY(res);
369
370     SP -= items;
371
372     while(res_items--) {
373         SV* res_item = *res_ptr++;
374         XPUSHs(res_item);
375     }
376
377     PUTBACK;
378
379     return;
380 }
381
382 XS(XS_next_can);
383 XS(XS_next_can)
384 {
385 #ifdef dVAR
386     dVAR; dXSARGS;
387 #else
388     dXSARGS;
389 #endif
390
391     SV* self = ST(0);
392     SV* methcv = __nextcan(aTHX_ self, 0);
393
394     PERL_UNUSED_VAR(items);
395
396     if(methcv == &PL_sv_undef) {
397         ST(0) = &PL_sv_undef;
398     }
399     else {
400         ST(0) = sv_2mortal(newRV_inc(methcv));
401     }
402
403     XSRETURN(1);
404 }
405
406 XS(XS_next_method);
407 XS(XS_next_method)
408 {
409     dMARK;
410     dAX;
411     SV* self = ST(0);
412     SV* methcv = __nextcan(aTHX_ self, 1);
413
414     PL_markstack_ptr++;
415     call_sv(methcv, GIMME_V);
416 }
417
418 XS(XS_maybe_next_method);
419 XS(XS_maybe_next_method)
420 {
421     dMARK;
422     dAX;
423     SV* self = ST(0);
424     SV* methcv = __nextcan(aTHX_ self, 0);
425
426     if(methcv == &PL_sv_undef) {
427         ST(0) = &PL_sv_undef;
428         XSRETURN(1);
429     }
430
431     PL_markstack_ptr++;
432     call_sv(methcv, GIMME_V);
433 }
434
435 MODULE = Class::C3::XS  PACKAGE = Class::C3::XS
436
437 BOOT:
438     nmcache = get_hv("next::METHOD_CACHE", 1);
439     newXS("Class::C3::XS::calculateMRO", XS_Class_C3_XS_calculateMRO, __FILE__);
440     newXS("next::can", XS_next_can, __FILE__);
441     newXS("next::method", XS_next_method, __FILE__);
442     newXS("maybe::next::method", XS_maybe_next_method, __FILE__);