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