still working on cache stuff
[gitmo/Class-C3-XS.git] / lib / Class / C3 / 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 AV*
12 __mro_linear_isa_c3(pTHX_ HV* stash, HV* cache, I32 level)
13 {
14     AV* retval;
15     GV** gvp;
16     GV* gv;
17     AV* isa;
18     const char* stashname;
19     STRLEN stashname_len;
20
21     assert(stash);
22     assert(HvAUX(stash));
23
24     stashname = HvNAME(stash);
25     stashname_len = strlen(stashname);
26     if (!stashname)
27       Perl_croak(aTHX_
28                  "Can't linearize anonymous symbol table");
29
30     if (level > 100)
31         Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
32               stashname);
33
34     if(!cache) {
35         cache = sv_2mortal(newHV());
36     }
37     else {
38 /*
39         SV** cache_entry = hv_fetch(cache, stashname, stashname_len, 0);
40         if(cache_entry)
41             return (AV*)SvREFCNT_inc(*cache_entry);
42 */
43     }
44
45     /* not in cache, make a new one */
46
47     retval = (AV*)sv_2mortal((SV*)newAV());
48     av_push(retval, newSVpvn(stashname, stashname_len)); /* us first */
49
50     gvp = (GV**)hv_fetch(stash, "ISA", 3, FALSE);
51     isa = (gvp && (gv = *gvp) && gv != (GV*)&PL_sv_undef) ? GvAV(gv) : NULL;
52
53     if(isa && AvFILLp(isa) >= 0) {
54         SV** seqs_ptr;
55         I32 seqs_items;
56         HV* tails = (HV*)sv_2mortal((SV*)newHV());
57         AV* seqs = (AV*)sv_2mortal((SV*)newAV());
58         I32 items = AvFILLp(isa) + 1;
59         SV** isa_ptr = AvARRAY(isa);
60         while(items--) {
61             AV* isa_lin;
62             SV* isa_item = *isa_ptr++;
63             HV* isa_item_stash = gv_stashsv(isa_item, 0);
64             if(!isa_item_stash) {
65                 isa_lin = newAV();
66                 av_push(isa_lin, newSVsv(isa_item));
67             }
68             else {
69                 isa_lin = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(isa_item_stash, cache, level + 1)); /* recursion */
70             }
71             av_push(seqs, (SV*)av_make(AvFILLp(isa_lin)+1, AvARRAY(isa_lin)));
72         }
73         av_push(seqs, (SV*)av_make(AvFILLp(isa)+1, AvARRAY(isa)));
74
75         seqs_ptr = AvARRAY(seqs);
76         seqs_items = AvFILLp(seqs) + 1;
77         while(seqs_items--) {
78             AV* seq = (AV*)*seqs_ptr++;
79             I32 seq_items = AvFILLp(seq);
80             if(seq_items > 0) {
81                 SV** seq_ptr = AvARRAY(seq) + 1;
82                 while(seq_items--) {
83                     SV* seqitem = *seq_ptr++;
84                     HE* he = hv_fetch_ent(tails, seqitem, 0, 0);
85                     if(!he) {
86                         hv_store_ent(tails, seqitem, newSViv(1), 0);
87                     }
88                     else {
89                         SV* val = HeVAL(he);
90                         sv_inc(val);
91                     }
92                 }
93             }
94         }
95
96         while(1) {
97             SV* seqhead = NULL;
98             SV* cand = NULL;
99             SV* winner = NULL;
100             SV* val;
101             HE* tail_entry;
102             AV* seq;
103             SV** avptr = AvARRAY(seqs);
104             items = AvFILLp(seqs)+1;
105             while(items--) {
106                 SV** svp;
107                 seq = (AV*)*avptr++;
108                 if(AvFILLp(seq) < 0) continue;
109                 svp = av_fetch(seq, 0, 0);
110                 seqhead = *svp;
111                 if(!winner) {
112                     cand = seqhead;
113                     if((tail_entry = hv_fetch_ent(tails, cand, 0, 0))
114                        && (val = HeVAL(tail_entry))
115                        && (SvIVx(val) > 0))
116                            continue;
117                     winner = newSVsv(cand);
118                     av_push(retval, winner);
119                 }
120                 if(!sv_cmp(seqhead, winner)) {
121
122                     /* this is basically shift(@seq) in void context */
123                     SvREFCNT_dec(*AvARRAY(seq));
124                     *AvARRAY(seq) = &PL_sv_undef;
125                     AvARRAY(seq) = AvARRAY(seq) + 1;
126                     AvMAX(seq)--;
127                     AvFILLp(seq)--;
128
129                     if(AvFILLp(seq) < 0) continue;
130                     svp = av_fetch(seq, 0, 0);
131                     seqhead = *svp;
132                     tail_entry = hv_fetch_ent(tails, seqhead, 0, 0);
133                     val = HeVAL(tail_entry);
134                     sv_dec(val);
135                 }
136             }
137             if(!cand) break;
138             if(!winner)
139                 Perl_croak(aTHX_ "Inconsistent hierarchy during C3 merge of class '%s': "
140                     "merging failed on parent '%s'", stashname, SvPV_nolen(cand));
141         }
142     }
143
144     SvREADONLY_on(retval);
145     SvREFCNT_inc(retval); /* for cache storage */
146     SvREFCNT_inc(retval); /* for return */
147     /* hv_store(cache, stashname, stashname_len, (SV*)retval, 0); */
148     return retval;
149 }
150
151 STATIC I32
152 __dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock) {
153     I32 i;
154     for (i = startingblock; i >= 0; i--) {
155         if(CxTYPE((PERL_CONTEXT*)(&cxstk[i])) == CXt_SUB) return i;
156     }
157     return i;
158 }
159
160 STATIC SV*
161 __nextcan(pTHX_ SV* self, I32 throw_nomethod)
162 {
163     register I32 cxix;
164     register const PERL_CONTEXT *ccstack = cxstack;
165     const PERL_SI *top_si = PL_curstackinfo;
166     HV* selfstash;
167     GV* cvgv;
168     SV *stashname;
169     const char *fq_subname;
170     const char *subname;
171     STRLEN fq_subname_len;
172     STRLEN stashname_len;
173     STRLEN subname_len;
174     SV* sv;
175     GV** gvp;
176     AV* linear_av;
177     SV** linear_svp;
178     SV* linear_sv;
179     HV* cstash;
180     GV* candidate = NULL;
181     CV* cand_cv = NULL;
182     const char *hvname;
183     I32 items;
184     struct mro_meta* selfmeta;
185     HV* nmcache;
186     HE* cache_entry;
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     /* 
261     XXX check %next::METHOD_CACHE 
262
263     selfmeta = HvMROMETA(selfstash);
264     if(!(nmcache = selfmeta->mro_nextmethod)) {
265         nmcache = selfmeta->mro_nextmethod = newHV();
266     }
267
268     if((cache_entry = hv_fetch_ent(nmcache, sv, 0, 0))) {
269         SV* val = HeVAL(cache_entry);
270         if(val == &PL_sv_undef) {
271             if(throw_nomethod)
272                 croak("No next::method '%s' found for %s", subname, hvname);
273             return &PL_sv_undef;
274         }
275         return SvREFCNT_inc(val);
276     }
277     */
278
279     /* beyond here is just for cache misses, so perf isn't as critical */
280
281     stashname_len = subname - fq_subname - 2;
282     stashname = sv_2mortal(newSVpvn(fq_subname, stashname_len));
283
284     linear_av = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(selfstash, NULL, 0));
285
286     linear_svp = AvARRAY(linear_av);
287     items = AvFILLp(linear_av) + 1;
288
289     while (items--) {
290         linear_sv = *linear_svp++;
291         assert(linear_sv);
292         if(sv_eq(linear_sv, stashname))
293             break;
294     }
295
296     if(items > 0) {
297         SV* sub_sv = sv_2mortal(newSVpv(subname, subname_len));
298         HV* cc3_mro = get_hv("Class::C3::MRO", 0);
299
300         while (items--) {
301             linear_sv = *linear_svp++;
302             assert(linear_sv);
303
304             if(cc3_mro) {
305                 HE* he_cc3_mro_class = hv_fetch_ent(cc3_mro, linear_sv, 0, 0);
306                 if(he_cc3_mro_class) {
307                     HV* cc3_mro_class = (HV*)SvRV(HeVAL(he_cc3_mro_class));
308                     SV** svp_cc3_mro_class_methods = hv_fetch(cc3_mro_class, "methods", 7, 0);
309                     if(svp_cc3_mro_class_methods) {
310                         HV* cc3_mro_class_methods = (HV*)SvRV(*svp_cc3_mro_class_methods);
311                         if(hv_exists_ent(cc3_mro_class_methods, sub_sv, 0))
312                             continue;
313                     }
314                 }
315             }
316
317             cstash = gv_stashsv(linear_sv, FALSE);
318
319             if (!cstash) {
320                 if (ckWARN(WARN_MISC))
321                     Perl_warner(aTHX_ packWARN(WARN_MISC), "Can't locate package %"SVf" for @%s::ISA",
322                         (void*)linear_sv, hvname);
323                 continue;
324             }
325
326             assert(cstash);
327
328             gvp = (GV**)hv_fetch(cstash, subname, subname_len, 0);
329             if (!gvp) continue;
330
331             candidate = *gvp;
332             assert(candidate);
333
334             if (SvTYPE(candidate) != SVt_PVGV)
335                 gv_init(candidate, cstash, subname, subname_len, TRUE);
336             if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
337                 SvREFCNT_inc((SV*)cand_cv);
338                 /* XXX store result in cache */
339                 /* hv_store_ent(nmcache, newSVsv(sv), (SV*)cand_cv, 0); */
340                 return (SV*)cand_cv;
341             }
342         }
343     }
344
345     /* XXX store undef in cache */
346     /* hv_store_ent(nmcache, newSVsv(sv), &PL_sv_undef, 0); */
347     if(throw_nomethod)
348         croak("No next::method '%s' found for %s", subname, hvname);
349     return &PL_sv_undef;
350 }
351
352 XS(XS_Class_C3_XS_calculateMRO);
353 XS(XS_Class_C3_XS_calculateMRO)
354 {
355 #ifdef dVAR
356     dVAR; dXSARGS;
357 #else
358     dXSARGS;
359 #endif
360
361     SV* classname;
362     HV* class_stash;
363     HV* cache = NULL;
364     AV* res;
365     I32 res_items;
366     I32 ret_items;
367     SV** res_ptr;
368
369     if(items < 1 || items > 2)
370         croak("Usage: calculateMRO(classname[, cache])");
371
372     classname = ST(0);
373     if(items == 2) cache = (HV*)ST(1);
374
375     class_stash = gv_stashsv(classname, 0);
376     if(!class_stash) croak("No such class: '%s'!", SvPV_nolen(classname));
377
378     res = (AV*)sv_2mortal((SV*)__mro_linear_isa_c3(class_stash, cache, 0));
379
380     res_items = ret_items = AvFILLp(res) + 1;
381     res_ptr = AvARRAY(res);
382
383     SP -= items;
384
385     while(res_items--) {
386         SV* res_item = *res_ptr++;
387         XPUSHs(res_item);
388     }
389
390     PUTBACK;
391
392     return;
393 }
394
395 XS(XS_next_can);
396 XS(XS_next_can)
397 {
398 #ifdef dVAR
399     dVAR; dXSARGS;
400 #else
401     dXSARGS;
402 #endif
403
404     SV* self = ST(0);
405     SV* methcv = __nextcan(self, 0);
406
407     PERL_UNUSED_VAR(items);
408
409     if(methcv == &PL_sv_undef) {
410         ST(0) = &PL_sv_undef;
411     }
412     else {
413         ST(0) = sv_2mortal(newRV_inc(methcv));
414     }
415
416     XSRETURN(1);
417 }
418
419 XS(XS_next_method);
420 XS(XS_next_method)
421 {
422     dMARK;
423     dAX;
424     SV* self = ST(0);
425     SV* methcv = __nextcan(self, 1);
426
427     PL_markstack_ptr++;
428     call_sv(methcv, GIMME_V);
429 }
430
431 XS(XS_maybe_next_method);
432 XS(XS_maybe_next_method)
433 {
434     dMARK;
435     dAX;
436     SV* self = ST(0);
437     SV* methcv = __nextcan(self, 0);
438
439     if(methcv == &PL_sv_undef) {
440         ST(0) = &PL_sv_undef;
441         XSRETURN(1);
442     }
443
444     PL_markstack_ptr++;
445     call_sv(methcv, GIMME_V);
446 }
447
448 MODULE = Class::C3::XS  PACKAGE = Class::C3::XS
449
450 BOOT:
451     newXS("Class::C3::XS::calculateMRO", XS_Class_C3_XS_calculateMRO, __FILE__);
452     newXS("next::can", XS_next_can, __FILE__);
453     newXS("next::method", XS_next_method, __FILE__);
454     newXS("maybe::next::method", XS_maybe_next_method, __FILE__);