Changelogging
[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
94 PERL_UNUSED_ARG(data); /* for moose-compat */
95
96 assert(metaobject);
97 assert(fmt);
98
99 va_start(args, fmt);
100 message = vnewSVpvf(fmt, &args);
101 va_end(args);
102
103 {
104 dSP;
105 PUSHMARK(SP);
106 EXTEND(SP, 4);
107
108 PUSHs(metaobject);
109 mPUSHs(message);
110
111 mPUSHs(newSVpvs("depth"));
112 mPUSHi(-1);
113
114 PUTBACK;
115
116 call_method("throw_error", G_VOID);
117 croak("throw_error() did not throw the error (%"SVf")", message);
118 }
119}
120
646c0371 121bool
122mouse_is_class_loaded(pTHX_ SV * const klass){
123 HV *stash;
124 GV** gvp;
125 HE* he;
126
127 if (!(SvPOKp(klass) && SvCUR(klass))) { /* XXX: SvPOK does not work with magical scalars */
128 return FALSE;
129 }
130
131 stash = gv_stashsv(klass, FALSE);
132 if (!stash) {
133 return FALSE;
134 }
135
136 if (( gvp = (GV**)hv_fetchs(stash, "VERSION", FALSE) )) {
137 if(isGV(*gvp) && GvSV(*gvp) && SvOK(GvSV(*gvp))){
138 return TRUE;
139 }
140 }
141
142 if (( gvp = (GV**)hv_fetchs(stash, "ISA", FALSE) )) {
143 if(isGV(*gvp) && GvAV(*gvp) && av_len(GvAV(*gvp)) != -1){
144 return TRUE;
145 }
146 }
147
148 hv_iterinit(stash);
149 while(( he = hv_iternext(stash) )){
150 GV* const gv = (GV*)HeVAL(he);
151
152 if(isGV(gv)){
153 if(GvCVu(gv)){
154 return TRUE;
155 }
156 }
157 else if(SvOK(gv)){
158 return TRUE;
159 }
160 }
161 return FALSE;
162}
163
164
0aad0266 165SV*
166mouse_call0 (pTHX_ SV* const self, SV* const method) {
646c0371 167 dSP;
168 SV *ret;
169
170 PUSHMARK(SP);
171 XPUSHs(self);
172 PUTBACK;
173
174 call_sv(method, G_SCALAR | G_METHOD);
175
176 SPAGAIN;
177 ret = POPs;
178 PUTBACK;
179
180 return ret;
181}
182
0aad0266 183SV*
184mouse_call1 (pTHX_ SV* const self, SV* const method, SV* const arg1) {
646c0371 185 dSP;
186 SV *ret;
187
188 PUSHMARK(SP);
189 EXTEND(SP, 2);
190 PUSHs(self);
191 PUSHs(arg1);
192 PUTBACK;
193
194 call_sv(method, G_SCALAR | G_METHOD);
195
196 SPAGAIN;
197 ret = POPs;
198 PUTBACK;
199
200 return ret;
201}
202
0aad0266 203int
204mouse_predicate_call(pTHX_ SV* const self, SV* const method) {
205 SV* const value = mcall0(self, method);
206 return SvTRUE(value);
207}
208
a39e9541 209SV*
aa2d2e2c 210mouse_get_metaclass(pTHX_ SV* metaclass_name){
fd168725 211 dMY_CXT;
212 HE* he;
a39e9541 213
aa2d2e2c 214 assert(metaclass_name);
fd168725 215 assert(MY_CXT.metas);
216
aa2d2e2c 217 if(IsObject(metaclass_name)){
a5c683f6 218 HV* const stash = SvSTASH(SvRV(metaclass_name));
aa2d2e2c 219
220 metaclass_name = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
221 sv_2mortal(metaclass_name);
222 }
223
fd168725 224 he = hv_fetch_ent(MY_CXT.metas, metaclass_name, FALSE, 0U);
a39e9541 225
fd168725 226 return he ? HeVAL(he) : &PL_sv_undef;
a39e9541 227}
228
646c0371 229MAGIC*
230mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags){
231 MAGIC* mg;
232
233 assert(sv != NULL);
234 for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
235 if(mg->mg_virtual == vtbl){
236 return mg;
237 }
238 }
239
240 if(flags & MOUSEf_DIE_ON_FAIL){
241 croak("mouse_mg_find: no MAGIC found for %"SVf, sv_2mortal(newRV_inc(sv)));
242 }
243 return NULL;
244}
245
a39e9541 246GV*
247mouse_stash_fetch(pTHX_ HV* const stash, const char* const name, I32 const namelen, I32 const create) {
248 GV** const gvp = (GV**)hv_fetch(stash, name, namelen, create);
249
250 if(gvp){
251 if(!isGV(*gvp)){
252 gv_init(*gvp, stash, name, namelen, GV_ADDMULTI);
253 }
254 return *gvp;
255 }
256 else{
257 return NULL;
258 }
259}
260
646c0371 261MODULE = Mouse::Util PACKAGE = Mouse::Util
262
263PROTOTYPES: DISABLE
264VERSIONCHECK: DISABLE
265
fd168725 266BOOT:
267{
268 MY_CXT_INIT;
269 MY_CXT.metas = NULL;
270}
271
272void
273__register_metaclass_storage(HV* metas, bool cloning)
274CODE:
275{
276 if(cloning){
277 MY_CXT_CLONE;
278 MY_CXT.metas = NULL;
279 }
280 {
281 dMY_CXT;
282 if(MY_CXT.metas) croak("Cannot set metaclass storage more than once");
283 MY_CXT.metas = metas;
284 SvREFCNT_inc_simple_void_NN(metas);
285 }
286}
287
646c0371 288bool
289is_class_loaded(SV* sv)
290
291void
292get_code_info(CV* code)
293PREINIT:
294 GV* gv;
295 HV* stash;
296PPCODE:
297 if((gv = CvGV(code)) && isGV(gv) && (stash = GvSTASH(gv))){
298 EXTEND(SP, 2);
299 mPUSHs(newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U));
300 mPUSHs(newSVpvn_share(GvNAME_get(gv), GvNAMELEN_get(gv), 0U));
301 }
302
303SV*
304get_code_package(CV* code)
305PREINIT:
306 HV* stash;
307CODE:
308 if(CvGV(code) && isGV(CvGV(code)) && (stash = GvSTASH(CvGV(code)))){
309 RETVAL = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
310 }
311 else{
312 RETVAL = &PL_sv_no;
313 }
314OUTPUT:
315 RETVAL
316
317CV*
318get_code_ref(SV* package, SV* name)
319CODE:
320{
321 HV* stash;
a39e9541 322 STRLEN name_len;
323 const char* name_pv;
324 GV* gv;
646c0371 325
326 if(!SvOK(package)){
327 croak("You must define a package name");
328 }
329 if(!SvOK(name)){
330 croak("You must define a subroutine name");
331 }
332
333 stash = gv_stashsv(package, FALSE);
334 if(!stash){
335 XSRETURN_UNDEF;
336 }
a39e9541 337
338 name_pv = SvPV_const(name, name_len);
339 gv = stash_fetch(stash, name_pv, name_len, FALSE);
340 RETVAL = gv ? GvCVu(gv) : NULL;
646c0371 341
342 if(!RETVAL){
343 XSRETURN_UNDEF;
344 }
345}
346OUTPUT:
347 RETVAL
348
349void
ebe91068 350generate_isa_predicate_for(SV* arg, SV* predicate_name = NULL)
351ALIAS:
352 generate_isa_predicate_for = 0
353 generate_can_predicate_for = 1
646c0371 354PPCODE:
355{
441964ce 356 const char* name_pv = NULL;
646c0371 357 CV* xsub;
358
ebe91068 359 SvGETMAGIC(arg);
441964ce 360
ebe91068 361 if(!SvOK(arg)){
362 croak("You must define %s", ix == 0 ? "a class name" : "method names");
646c0371 363 }
646c0371 364
441964ce 365 if(predicate_name){
366 SvGETMAGIC(predicate_name);
367 if(!SvOK(predicate_name)){
ebe91068 368 croak("You must define %s", "a predicate name");
441964ce 369 }
370 name_pv = SvPV_nolen_const(predicate_name);
646c0371 371 }
372
ebe91068 373 if(ix == 0){
374 xsub = mouse_generate_isa_predicate_for(aTHX_ arg, name_pv);
375 }
376 else{
377 xsub = mouse_generate_can_predicate_for(aTHX_ arg, name_pv);
378 }
441964ce 379
646c0371 380 if(predicate_name == NULL){ /* anonymous predicate */
381 XPUSHs( newRV_noinc((SV*)xsub) );
382 }
383}