Reset stash iterators
[gitmo/Mouse.git] / xs-src / MouseUtil.xs
CommitLineData
646c0371 1#include "mouse.h"
2
fd168725 3#define MY_CXT_KEY "Mouse::Util::_guts" XS_VERSION
4typedef struct {
5 HV* metas;
6} my_cxt_t;
7START_MY_CXT
8
646c0371 9#define ISA_CACHE "::LINEALIZED_ISA_CACHE::"
10
11#ifdef no_mro_get_linear_isa
12AV*
13mouse_mro_get_linear_isa(pTHX_ HV* const stash){
6582d0e3 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{
cd658d1f 30 SvREFCNT_dec(isa);
31 GvAV(cachegv) = isa = newAV();
6582d0e3 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
80aa5731 53 if(IsArrayRef(avref)){
6582d0e3 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));
cd658d1f 74 return isa;
646c0371 75}
76#endif /* !no_mor_get_linear_isa */
77
78#ifdef DEBUGGING
79SV**
80mouse_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
88void
89mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const char* const fmt, ...){
90 dTHX;
91 va_list args;
92 SV* message;
93
646c0371 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);
077f2efd 104 EXTEND(SP, 6);
646c0371 105
106 PUSHs(metaobject);
107 mPUSHs(message);
108
431e4817 109 if(data){ /* extra arg, might be useful for debugging */
077f2efd 110 mPUSHs(newSVpvs("data"));
431e4817 111 PUSHs(data);
112 mPUSHs(newSVpvs("depth"));
113 mPUSHi(-1);
114 }
646c0371 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
d06d9266 123void
124mouse_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
134void
135mouse_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
646c0371 147bool
148mouse_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)){
fa4ac648 179 if(GvCVu(gv)){ /* is GV and has CV */
180 hv_iterinit(stash); /* reset */
646c0371 181 return TRUE;
182 }
183 }
fa4ac648 184 else if(SvOK(gv)){ /* is a stub or constant */
185 hv_iterinit(stash); /* reset */
646c0371 186 return TRUE;
187 }
188 }
189 return FALSE;
190}
191
192
0aad0266 193SV*
194mouse_call0 (pTHX_ SV* const self, SV* const method) {
646c0371 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
0aad0266 211SV*
212mouse_call1 (pTHX_ SV* const self, SV* const method, SV* const arg1) {
646c0371 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
0aad0266 231int
232mouse_predicate_call(pTHX_ SV* const self, SV* const method) {
6ad77996 233 return sv_true( mcall0(self, method) );
0aad0266 234}
235
a39e9541 236SV*
aa2d2e2c 237mouse_get_metaclass(pTHX_ SV* metaclass_name){
fd168725 238 dMY_CXT;
239 HE* he;
a39e9541 240
aa2d2e2c 241 assert(metaclass_name);
fd168725 242 assert(MY_CXT.metas);
243
aa2d2e2c 244 if(IsObject(metaclass_name)){
a5c683f6 245 HV* const stash = SvSTASH(SvRV(metaclass_name));
aa2d2e2c 246
247 metaclass_name = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
248 sv_2mortal(metaclass_name);
249 }
250
fd168725 251 he = hv_fetch_ent(MY_CXT.metas, metaclass_name, FALSE, 0U);
a39e9541 252
fd168725 253 return he ? HeVAL(he) : &PL_sv_undef;
a39e9541 254}
255
646c0371 256MAGIC*
257mouse_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
a39e9541 273GV*
274mouse_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
d67f600d 288void
289mouse_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 }
d06d9266 301
d67f600d 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
646c0371 332MODULE = Mouse::Util PACKAGE = Mouse::Util
333
334PROTOTYPES: DISABLE
335VERSIONCHECK: DISABLE
336
fd168725 337BOOT:
338{
339 MY_CXT_INIT;
340 MY_CXT.metas = NULL;
341}
342
343void
344__register_metaclass_storage(HV* metas, bool cloning)
345CODE:
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
646c0371 359bool
0ffc4183 360is_valid_class_name(SV* sv)
361CODE:
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}
379OUTPUT:
380 RETVAL
381
382bool
646c0371 383is_class_loaded(SV* sv)
384
385void
386get_code_info(CV* code)
387PREINIT:
388 GV* gv;
389 HV* stash;
390PPCODE:
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
397SV*
398get_code_package(CV* code)
399PREINIT:
400 HV* stash;
401CODE:
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 }
408OUTPUT:
409 RETVAL
410
411CV*
412get_code_ref(SV* package, SV* name)
413CODE:
414{
415 HV* stash;
a39e9541 416 STRLEN name_len;
417 const char* name_pv;
418 GV* gv;
646c0371 419
d06d9266 420 must_defined(package, "a package name");
421 must_defined(name, "a subroutine name");
646c0371 422
423 stash = gv_stashsv(package, FALSE);
424 if(!stash){
425 XSRETURN_UNDEF;
426 }
a39e9541 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;
646c0371 431
432 if(!RETVAL){
433 XSRETURN_UNDEF;
434 }
435}
436OUTPUT:
437 RETVAL
438
439void
ebe91068 440generate_isa_predicate_for(SV* arg, SV* predicate_name = NULL)
441ALIAS:
442 generate_isa_predicate_for = 0
443 generate_can_predicate_for = 1
646c0371 444PPCODE:
445{
441964ce 446 const char* name_pv = NULL;
646c0371 447 CV* xsub;
448
d06d9266 449 must_defined(arg, ix == 0 ? "a class_name" : "method names");
646c0371 450
441964ce 451 if(predicate_name){
d06d9266 452 must_defined(predicate_name, "a predicate name");
441964ce 453 name_pv = SvPV_nolen_const(predicate_name);
646c0371 454 }
455
ebe91068 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 }
441964ce 462
646c0371 463 if(predicate_name == NULL){ /* anonymous predicate */
73337709 464 mXPUSHs( newRV_inc((SV*)xsub) );
646c0371 465 }
466}
d67f600d 467
468# This xsub will redefine &Mouse::Util::install_subroutines()
469void
470install_subroutines(SV* into, ...)
471CODE:
472{
473 HV* stash;
474 I32 i;
475
d06d9266 476 must_defined(into, "a package name");
d67f600d 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
d06d9266 490 must_defined(name, "a subroutine name");
491 must_ref(code, "a CODE reference", SVt_PVCV);
d67f600d 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}