Reset stash iterators
[gitmo/Mouse.git] / xs-src / MouseUtil.xs
1 #include "mouse.h"
2
3 #define MY_CXT_KEY "Mouse::Util::_guts" XS_VERSION
4 typedef struct {
5     HV* metas;
6 } my_cxt_t;
7 START_MY_CXT
8
9 #define ISA_CACHE "::LINEALIZED_ISA_CACHE::"
10
11 #ifdef no_mro_get_linear_isa
12 AV*
13 mouse_mro_get_linear_isa(pTHX_ HV* const stash){
14     GV* const cachegv = *(GV**)hv_fetchs(stash, ISA_CACHE, TRUE);
15     AV* isa;
16     SV* gen;
17     CV* get_linear_isa;
18
19     if(!isGV(cachegv))
20         gv_init(cachegv, stash, ISA_CACHE, sizeof(ISA_CACHE)-1, TRUE);
21
22     isa = GvAVn(cachegv);
23     gen = GvSVn(cachegv);
24
25
26     if(SvIOK(gen) && SvIVX(gen) == (IV)mro_get_pkg_gen(stash)){
27         return isa; /* returns the cache if available */
28     }
29     else{
30         SvREFCNT_dec(isa);
31         GvAV(cachegv) = isa = newAV();
32     }
33
34     get_linear_isa = get_cv("Mouse::Util::get_linear_isa", TRUE);
35
36     {
37         SV* avref;
38         dSP;
39
40         ENTER;
41         SAVETMPS;
42
43         PUSHMARK(SP);
44         mXPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
45         PUTBACK;
46
47         call_sv((SV*)get_linear_isa, G_SCALAR);
48
49         SPAGAIN;
50         avref = POPs;
51         PUTBACK;
52
53         if(IsArrayRef(avref)){
54             AV* const av  = (AV*)SvRV(avref);
55             I32 const len = AvFILLp(av) + 1;
56             I32 i;
57
58             for(i = 0; i < len; i++){
59                 HV* const stash = gv_stashsv(AvARRAY(av)[i], FALSE);
60                 if(stash)
61                     av_push(isa, newSVpv(HvNAME(stash), 0));
62             }
63             SvREADONLY_on(isa);
64         }
65         else{
66             Perl_croak(aTHX_ "Mouse:Util::get_linear_isa() didn't return an ARRAY reference");
67         }
68
69         FREETMPS;
70         LEAVE;
71     }
72
73     sv_setiv(gen, (IV)mro_get_pkg_gen(stash));
74     return isa;
75 }
76 #endif /* !no_mor_get_linear_isa */
77
78 #ifdef DEBUGGING
79 SV**
80 mouse_av_at_safe(pTHX_ AV* const av, I32 const ix){
81     assert(av);
82     assert(SvTYPE(av) == SVt_PVAV);
83     assert(AvMAX(av) >= ix);
84     return &AvARRAY(av)[ix];
85 }
86 #endif
87
88 void
89 mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const char* const fmt, ...){
90     dTHX;
91     va_list args;
92     SV* message;
93
94     assert(metaobject);
95     assert(fmt);
96
97     va_start(args, fmt);
98     message = vnewSVpvf(fmt, &args);
99     va_end(args);
100
101     {
102         dSP;
103         PUSHMARK(SP);
104         EXTEND(SP, 6);
105
106         PUSHs(metaobject);
107         mPUSHs(message);
108
109         if(data){ /* extra arg, might be useful for debugging */
110             mPUSHs(newSVpvs("data"));
111             PUSHs(data);
112             mPUSHs(newSVpvs("depth"));
113             mPUSHi(-1);
114         }
115
116         PUTBACK;
117
118         call_method("throw_error", G_VOID);
119         croak("throw_error() did not throw the error (%"SVf")", message);
120     }
121 }
122
123 void
124 mouse_must_defined(pTHX_ SV* const value, const char* const name) {
125     assert(value);
126     assert(name);
127
128     SvGETMAGIC(value);
129     if(!SvOK(value)){
130         croak("You must define %s", name);
131     }
132 }
133
134 void
135 mouse_must_ref(pTHX_ SV* const value, const char* const name, svtype const t) {
136     assert(value);
137     assert(name);
138
139     SvGETMAGIC(value);
140     if(!(SvROK(value) && (t == SVt_NULL || SvTYPE(SvRV(value)) == t))) {
141         croak("You must pass %s, not %s",
142             name, SvOK(value) ? SvPV_nolen(value) : "undef");
143     }
144 }
145
146
147 bool
148 mouse_is_class_loaded(pTHX_ SV * const klass){
149     HV *stash;
150     GV** gvp;
151     HE* he;
152
153     if (!(SvPOKp(klass) && SvCUR(klass))) { /* XXX: SvPOK does not work with magical scalars */
154         return FALSE;
155     }
156
157     stash = gv_stashsv(klass, FALSE);
158     if (!stash) {
159         return FALSE;
160     }
161
162     if (( gvp = (GV**)hv_fetchs(stash, "VERSION", FALSE) )) {
163         if(isGV(*gvp) && GvSV(*gvp) && SvOK(GvSV(*gvp))){
164             return TRUE;
165         }
166     }
167
168     if (( gvp = (GV**)hv_fetchs(stash, "ISA", FALSE) )) {
169         if(isGV(*gvp) && GvAV(*gvp) && av_len(GvAV(*gvp)) != -1){
170             return TRUE;
171         }
172     }
173
174     hv_iterinit(stash);
175     while(( he = hv_iternext(stash) )){
176         GV* const gv = (GV*)HeVAL(he);
177
178         if(isGV(gv)){
179             if(GvCVu(gv)){ /* is GV and has CV */
180                 hv_iterinit(stash); /* reset */
181                 return TRUE;
182             }
183         }
184         else if(SvOK(gv)){ /* is a stub or constant */
185             hv_iterinit(stash); /* reset */
186             return TRUE;
187         }
188     }
189     return FALSE;
190 }
191
192
193 SV*
194 mouse_call0 (pTHX_ SV* const self, SV* const method) {
195     dSP;
196     SV *ret;
197
198     PUSHMARK(SP);
199     XPUSHs(self);
200     PUTBACK;
201
202     call_sv(method, G_SCALAR | G_METHOD);
203
204     SPAGAIN;
205     ret = POPs;
206     PUTBACK;
207
208     return ret;
209 }
210
211 SV*
212 mouse_call1 (pTHX_ SV* const self, SV* const method, SV* const arg1) {
213     dSP;
214     SV *ret;
215
216     PUSHMARK(SP);
217     EXTEND(SP, 2);
218     PUSHs(self);
219     PUSHs(arg1);
220     PUTBACK;
221
222     call_sv(method, G_SCALAR | G_METHOD);
223
224     SPAGAIN;
225     ret = POPs;
226     PUTBACK;
227
228     return ret;
229 }
230
231 int
232 mouse_predicate_call(pTHX_ SV* const self, SV* const method) {
233     return sv_true( mcall0(self, method) );
234 }
235
236 SV*
237 mouse_get_metaclass(pTHX_ SV* metaclass_name){
238     dMY_CXT;
239     HE* he;
240
241     assert(metaclass_name);
242     assert(MY_CXT.metas);
243
244     if(IsObject(metaclass_name)){
245         HV* const stash = SvSTASH(SvRV(metaclass_name));
246
247         metaclass_name = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
248         sv_2mortal(metaclass_name);
249     }
250
251     he = hv_fetch_ent(MY_CXT.metas, metaclass_name, FALSE, 0U);
252
253     return he ? HeVAL(he) : &PL_sv_undef;
254 }
255
256 MAGIC*
257 mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags){
258     MAGIC* mg;
259
260     assert(sv != NULL);
261     for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
262         if(mg->mg_virtual == vtbl){
263             return mg;
264         }
265     }
266
267     if(flags & MOUSEf_DIE_ON_FAIL){
268         croak("mouse_mg_find: no MAGIC found for %"SVf, sv_2mortal(newRV_inc(sv)));
269     }
270     return NULL;
271 }
272
273 GV*
274 mouse_stash_fetch(pTHX_ HV* const stash, const char* const name, I32 const namelen, I32 const create) {
275     GV** const gvp = (GV**)hv_fetch(stash, name, namelen, create);
276
277     if(gvp){
278         if(!isGV(*gvp)){
279             gv_init(*gvp, stash, name, namelen, GV_ADDMULTI);
280         }
281         return *gvp;
282     }
283     else{
284         return NULL;
285     }
286 }
287
288 void
289 mouse_install_sub(pTHX_ GV* const gv, SV* const code_ref) {
290     CV* cv;
291
292     assert(gv != NULL);
293     assert(code_ref != NULL);
294     assert(isGV(gv));
295     assert(IsCodeRef(code_ref));
296
297     if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
298         SvREFCNT_dec(GvCV(gv));
299         GvCV(gv) = NULL;
300     }
301
302     sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
303
304     /* name the CODE ref if it's anonymous */
305     cv = (CV*)SvRV(code_ref);
306     if(CvANON(cv)
307         && CvGV(cv) /* a cv under construction has no gv */ ){
308         HV* dbsub;
309
310         /* update %DB::sub to make NYTProf happy */
311         if((PL_perldb & (PERLDBf_SUBLINE|PERLDB_NAMEANON))
312             && PL_DBsub && (dbsub = GvHV(PL_DBsub))
313         ){
314             /* see Perl_newATTRSUB() in op.c */
315             SV* const subname = sv_newmortal();
316             HE* orig;
317
318             gv_efullname3(subname, CvGV(cv), NULL);
319             orig = hv_fetch_ent(dbsub, subname, FALSE, 0U);
320             if(orig){
321                 gv_efullname3(subname, gv, NULL);
322                 (void)hv_store_ent(dbsub, subname, HeVAL(orig), 0U);
323                 SvREFCNT_inc_simple_void_NN(HeVAL(orig));
324             }
325         }
326
327         CvGV(cv) = gv;
328         CvANON_off(cv);
329     }
330 }
331
332 MODULE = Mouse::Util  PACKAGE = Mouse::Util
333
334 PROTOTYPES:   DISABLE
335 VERSIONCHECK: DISABLE
336
337 BOOT:
338 {
339     MY_CXT_INIT;
340     MY_CXT.metas = NULL;
341 }
342
343 void
344 __register_metaclass_storage(HV* metas, bool cloning)
345 CODE:
346 {
347     if(cloning){
348         MY_CXT_CLONE;
349         MY_CXT.metas = NULL;
350     }
351     {
352         dMY_CXT;
353         if(MY_CXT.metas) croak("Cannot set metaclass storage more than once");
354         MY_CXT.metas = metas;
355         SvREFCNT_inc_simple_void_NN(metas);
356     }
357 }
358
359 bool
360 is_valid_class_name(SV* sv)
361 CODE:
362 {
363     SvGETMAGIC(sv);
364     if(SvPOKp(sv) && SvCUR(sv) > 0){
365         UV i;
366         RETVAL = TRUE;
367         for(i = 0; i < SvCUR(sv); i++){
368             char const c = SvPVX(sv)[i];
369             if(!(isALNUM(c) || c == ':')){
370                 RETVAL = FALSE;
371                 break;
372             }
373         }
374     }
375     else{
376         RETVAL = SvNIOKp(sv) ? TRUE : FALSE;
377     }
378 }
379 OUTPUT:
380     RETVAL
381
382 bool
383 is_class_loaded(SV* sv)
384
385 void
386 get_code_info(CV* code)
387 PREINIT:
388     GV* gv;
389     HV* stash;
390 PPCODE:
391     if((gv = CvGV(code)) && isGV(gv) && (stash = GvSTASH(gv))){
392         EXTEND(SP, 2);
393         mPUSHs(newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U));
394         mPUSHs(newSVpvn_share(GvNAME_get(gv), GvNAMELEN_get(gv), 0U));
395     }
396
397 SV*
398 get_code_package(CV* code)
399 PREINIT:
400     HV* stash;
401 CODE:
402     if(CvGV(code) && isGV(CvGV(code)) && (stash = GvSTASH(CvGV(code)))){
403         RETVAL = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
404     }
405     else{
406         RETVAL = &PL_sv_no;
407     }
408 OUTPUT:
409     RETVAL
410
411 CV*
412 get_code_ref(SV* package, SV* name)
413 CODE:
414 {
415     HV* stash;
416     STRLEN name_len;
417     const char* name_pv;
418     GV* gv;
419
420     must_defined(package, "a package name");
421     must_defined(name,    "a subroutine name");
422
423     stash = gv_stashsv(package, FALSE);
424     if(!stash){
425         XSRETURN_UNDEF;
426     }
427
428     name_pv = SvPV_const(name, name_len);
429     gv = stash_fetch(stash, name_pv, name_len, FALSE);
430     RETVAL = gv ? GvCVu(gv) : NULL;
431
432     if(!RETVAL){
433         XSRETURN_UNDEF;
434     }
435 }
436 OUTPUT:
437     RETVAL
438
439 void
440 generate_isa_predicate_for(SV* arg, SV* predicate_name = NULL)
441 ALIAS:
442     generate_isa_predicate_for = 0
443     generate_can_predicate_for = 1
444 PPCODE:
445 {
446     const char* name_pv = NULL;
447     CV* xsub;
448
449     must_defined(arg, ix == 0 ? "a class_name" : "method names");
450
451     if(predicate_name){
452         must_defined(predicate_name, "a predicate name");
453         name_pv = SvPV_nolen_const(predicate_name);
454     }
455
456     if(ix == 0){
457         xsub = mouse_generate_isa_predicate_for(aTHX_ arg, name_pv);
458     }
459     else{
460         xsub = mouse_generate_can_predicate_for(aTHX_ arg, name_pv);
461     }
462
463     if(predicate_name == NULL){ /* anonymous predicate */
464         mXPUSHs( newRV_inc((SV*)xsub) );
465     }
466 }
467
468 # This xsub will redefine &Mouse::Util::install_subroutines()
469 void
470 install_subroutines(SV* into, ...)
471 CODE:
472 {
473     HV* stash;
474     I32 i;
475
476     must_defined(into, "a package name");
477     stash = gv_stashsv(into, TRUE);
478
479     if( ((items-1) % 2) != 0 ){
480         croak_xs_usage(cv, "into, name => coderef [, other_name, other_coderef ...]");
481     }
482
483     for(i = 1; i < items; i += 2) {
484         SV* const name = ST(i);
485         SV* const code = ST(i+1);
486         STRLEN len;
487         const char* pv;
488         GV* gv;
489
490         must_defined(name, "a subroutine name");
491         must_ref(code, "a CODE reference", SVt_PVCV);
492
493         pv = SvPV_const(name, len);
494         gv = stash_fetch(stash, pv, len, TRUE);
495
496         mouse_install_sub(aTHX_ gv, code);
497     }
498 }