move XS file to root :p
[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
123                     /* this is basically shift(@seq) in void context */
124                     SvREFCNT_dec(*AvARRAY(seq));
125                     *AvARRAY(seq) = &PL_sv_undef;
126                     AvARRAY(seq) = AvARRAY(seq) + 1;
127                     AvMAX(seq)--;
128                     AvFILLp(seq)--;
129
130                     if(AvFILLp(seq) < 0) continue;
131                     svp = av_fetch(seq, 0, 0);
132                     seqhead = *svp;
133                     tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
134                     val = HeVAL(tail_entry);
135                     sv_dec(val);
136                 }
137             }
138             if(!cand) break;
139             if(!winner)
140                 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
141                     "merging failed on parent '%s'", stashname, SvPV_nolen(cand));
142         }
143     }
144
145     SvREADONLY_on(retval);
146     SvREFCNT_inc(retval); /* for cache storage */
147     SvREFCNT_inc(retval); /* for return */
148     hv_store(cache, stashname, stashname_len, (SV*)retval, 0);
149     return retval;
150 }
151
152 STATIC I32
153 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
154     I32 i;
155     for (i = startingblock; i >= 0; i--) {
156         if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
157     }
158     return i;
159 }
160
161 STATIC SV*
162 __nextcan(pTHX_ SV* self, I32 throw_nomethod)
163 {
164     register I32 cxix;
165     register const PERL_CONTEXT *ccstack = cxstack;
166     const PERL_SI *top_si = PL_curstackinfo;
167     HV* selfstash;
168     GV* cvgv;
169     SV *stashname;
170     const char *fq_subname;
171     const char *subname;
172     STRLEN fq_subname_len;
173     STRLEN stashname_len;
174     STRLEN subname_len;
175     SV* sv;
176     GV** gvp;
177     AV* linear_av;
178     SV** linear_svp;
179     SV* linear_sv;
180     HV* cstash;
181     GV* candidate = NULL;
182     CV* cand_cv = NULL;
183     const char *hvname;
184     I32 items;
185     HE* cache_entry;
186     SV* cachekey;
187
188     if(sv_isobject(self))
189         selfstash = SvSTASH(SvRV(self));
190     else
191         selfstash = gv_stashsv(self, 0);
192
193     assert(selfstash);
194
195     hvname = HvNAME(selfstash);
196     if (!hvname)
197         croak("Can't use anonymous symbol table for method lookup");
198
199     cxix = __dopoptosub_at(cxstack, cxstack_ix);
200
201     /* This block finds the contextually-enclosing fully-qualified subname,
202        much like looking at (caller($i))[3] until you find a real sub that
203        isn't ANON, etc */
204     for (;;) {
205         /* we may be in a higher stacklevel, so dig down deeper */
206         while (cxix < 0) {
207             if(top_si->si_type == PERLSI_MAIN)
208                 croak("next::method/next::can/maybe::next::method must be used in method context");
209             top_si = top_si->si_prev;
210             ccstack = top_si->si_cxstack;
211             cxix = __dopoptosub_at(ccstack, top_si->si_cxix);
212         }
213
214         if(CxTYPE((PERL_CONTEXT*)(&ccstack[cxix])) != CXt_SUB
215           || (PL_DBsub && GvCV(PL_DBsub) && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) {
216             cxix = __dopoptosub_at(ccstack, cxix - 1);
217             continue;
218         }
219
220         {
221             const I32 dbcxix = __dopoptosub_at(ccstack, cxix - 1);
222             if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub)) {
223                 if(CxTYPE((PERL_CONTEXT*)(&ccstack[dbcxix])) != CXt_SUB) {
224                     cxix = dbcxix;
225                     continue;
226                 }
227             }
228         }
229
230         cvgv = CvGV(ccstack[cxix].blk_sub.cv);
231
232         if(!isGV(cvgv)) {
233             cxix = __dopoptosub_at(ccstack, cxix - 1);
234             continue;
235         }
236
237         /* we found a real sub here */
238         sv = sv_2mortal(newSV(0));
239
240         gv_efullname3(sv, cvgv, NULL);
241
242         fq_subname = SvPVX(sv);
243         fq_subname_len = SvCUR(sv);
244
245         subname = strrchr(fq_subname, ':');
246         if(!subname)
247             croak("next::method/next::can/maybe::next::method cannot find enclosing method");
248
249         subname++;
250         subname_len = fq_subname_len - (subname - fq_subname);
251         if(subname_len == 8 && strEQ(subname, "__ANON__")) {
252             cxix = __dopoptosub_at(ccstack, cxix - 1);
253             continue;
254         }
255         break;
256     }
257
258     /* If we made it to here, we found our context */
259
260     /* cachekey = "objpkg|context::method::name" */
261     cachekey = sv_2mortal(newSVpv(hvname, 0));
262     sv_catpvn(cachekey, "|", 1);
263     sv_catsv(cachekey, sv);
264
265     if((cache_entry = hv_fetch_ent(nmcache, cachekey, 0, 0))) {
266         SV* val = HeVAL(cache_entry);
267         if(val == &PL_sv_undef) {
268             if(throw_nomethod)
269                 croak("No next::method '%s' found for %s", subname, hvname);
270             return &PL_sv_undef;
271         }
272         return SvREFCNT_inc(val);
273     }
274
275     /* beyond here is just for cache misses, so perf isn't as critical */
276
277     stashname_len = subname - fq_subname - 2;
278     stashname = sv_2mortal(newSVpvn(fq_subname, stashname_len));
279
280     linear_av = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(aTHX_ selfstash, NULL, 0));
281
282     linear_svp = AvARRAY(linear_av);
283     items = AvFILLp(linear_av) + 1;
284
285     while (items--) {
286         linear_sv = *linear_svp++;
287         assert(linear_sv);
288         if(sv_eq(linear_sv, stashname))
289             break;
290     }
291
292     if(items > 0) {
293         SV* sub_sv = sv_2mortal(newSVpv(subname, subname_len));
294         HV* cc3_mro = get_hv("Class::C3::MRO", 0);
295
296         while (items--) {
297             linear_sv = *linear_svp++;
298             assert(linear_sv);
299
300             if(cc3_mro) {
301                 HE* he_cc3_mro_class = hv_fetch_ent(cc3_mro, linear_sv, 0, 0);
302                 if(he_cc3_mro_class) {
303                     HV* cc3_mro_class = (HV*)SvRV(HeVAL(he_cc3_mro_class));
304                     SV** svp_cc3_mro_class_methods = hv_fetch(cc3_mro_class, "methods", 7, 0);
305                     if(svp_cc3_mro_class_methods) {
306                         HV* cc3_mro_class_methods = (HV*)SvRV(*svp_cc3_mro_class_methods);
307                         if(hv_exists_ent(cc3_mro_class_methods, sub_sv, 0))
308                             continue;
309                     }
310                 }
311             }
312
313             cstash = gv_stashsv(linear_sv, FALSE);
314
315             if (!cstash) {
316                 if (ckWARN(WARN_MISC))
317                     Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
318                         (void*)linear_sv, hvname);
319                 continue;
320             }
321
322             assert(cstash);
323
324             gvp = (GV**)hv_fetch(cstash, subname, subname_len, 0);
325             if (!gvp) continue;
326
327             candidate = *gvp;
328             assert(candidate);
329
330             if (SvTYPE(candidate) != SVt_PVGV)
331                 gv_init(candidate, cstash, subname, subname_len, TRUE);
332             if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
333                 SvREFCNT_inc((SV*)cand_cv);
334                 hv_store_ent(nmcache, newSVsv(cachekey), (SV*)cand_cv, 0);
335                 return (SV*)cand_cv;
336             }
337         }
338     }
339
340     hv_store_ent(nmcache, newSVsv(cachekey), &PL_sv_undef, 0);
341     if(throw_nomethod)
342         croak("No next::method '%s' found for %s", subname, hvname);
343     return &PL_sv_undef;
344 }
345
346 XS(XS_Class_C3_XS_calculateMRO);
347 XS(XS_Class_C3_XS_calculateMRO)
348 {
349 #ifdef dVAR
350     dVAR; dXSARGS;
351 #else
352     dXSARGS;
353 #endif
354
355     SV* classname;
356     HV* class_stash;
357     HV* cache = NULL;
358     AV* res;
359     I32 res_items;
360     I32 ret_items;
361     SV** res_ptr;
362
363     if(items < 1 || items > 2)
364         croak("Usage: calculateMRO(classname[, cache])");
365
366     classname = ST(0);
367     if(items == 2) cache = (HV*)SvRV(ST(1));
368
369     class_stash = gv_stashsv(classname, 0);
370     if(!class_stash) croak("No such class: '%s'!", SvPV_nolen(classname));
371
372     res = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(aTHX_ class_stash, cache, 0));
373
374     res_items = ret_items = AvFILLp(res) + 1;
375     res_ptr = AvARRAY(res);
376
377     SP -= items;
378
379     while(res_items--) {
380         SV* res_item = *res_ptr++;
381         XPUSHs(res_item);
382     }
383
384     PUTBACK;
385
386     return;
387 }
388
389 XS(XS_next_can);
390 XS(XS_next_can)
391 {
392 #ifdef dVAR
393     dVAR; dXSARGS;
394 #else
395     dXSARGS;
396 #endif
397
398     SV* self = ST(0);
399     SV* methcv = __nextcan(aTHX_ self, 0);
400
401     PERL_UNUSED_VAR(items);
402
403     if(methcv == &PL_sv_undef) {
404         ST(0) = &PL_sv_undef;
405     }
406     else {
407         ST(0) = sv_2mortal(newRV_inc(methcv));
408     }
409
410     XSRETURN(1);
411 }
412
413 XS(XS_next_method);
414 XS(XS_next_method)
415 {
416     dMARK;
417     dAX;
418     SV* self = ST(0);
419     SV* methcv = __nextcan(aTHX_ self, 1);
420
421     PL_markstack_ptr++;
422     call_sv(methcv, GIMME_V);
423 }
424
425 XS(XS_maybe_next_method);
426 XS(XS_maybe_next_method)
427 {
428     dMARK;
429     dAX;
430     SV* self = ST(0);
431     SV* methcv = __nextcan(aTHX_ self, 0);
432
433     if(methcv == &PL_sv_undef) {
434         ST(0) = &PL_sv_undef;
435         XSRETURN(1);
436     }
437
438     PL_markstack_ptr++;
439     call_sv(methcv, GIMME_V);
440 }
441
442 MODULE = Class::C3::XS  PACKAGE = Class::C3::XS
443
444 BOOT:
445     nmcache = get_hv("next::METHOD_CACHE", 1);
446     newXS("Class::C3::XS::calculateMRO", XS_Class_C3_XS_calculateMRO, __FILE__);
447     newXS("next::can", XS_next_can, __FILE__);
448     newXS("next::method", XS_next_method, __FILE__);
449     newXS("maybe::next::method", XS_maybe_next_method, __FILE__);