Commit | Line | Data |
646c0371 |
1 | #include "mouse.h" |
2 | |
3 | #define ISA_CACHE "::LINEALIZED_ISA_CACHE::" |
4 | |
5 | #ifdef no_mro_get_linear_isa |
6 | AV* |
7 | mouse_mro_get_linear_isa(pTHX_ HV* const stash){ |
6582d0e3 |
8 | GV* const cachegv = *(GV**)hv_fetchs(stash, ISA_CACHE, TRUE); |
9 | AV* isa; |
10 | SV* gen; |
11 | CV* get_linear_isa; |
12 | |
13 | if(!isGV(cachegv)) |
14 | gv_init(cachegv, stash, ISA_CACHE, sizeof(ISA_CACHE)-1, TRUE); |
15 | |
16 | isa = GvAVn(cachegv); |
17 | gen = GvSVn(cachegv); |
18 | |
19 | |
20 | if(SvIOK(gen) && SvIVX(gen) == (IV)mro_get_pkg_gen(stash)){ |
21 | return isa; /* returns the cache if available */ |
22 | } |
23 | else{ |
24 | SvREADONLY_off(isa); |
25 | av_clear(isa); |
26 | } |
27 | |
28 | get_linear_isa = get_cv("Mouse::Util::get_linear_isa", TRUE); |
29 | |
30 | { |
31 | SV* avref; |
32 | dSP; |
33 | |
34 | ENTER; |
35 | SAVETMPS; |
36 | |
37 | PUSHMARK(SP); |
38 | mXPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash)); |
39 | PUTBACK; |
40 | |
41 | call_sv((SV*)get_linear_isa, G_SCALAR); |
42 | |
43 | SPAGAIN; |
44 | avref = POPs; |
45 | PUTBACK; |
46 | |
80aa5731 |
47 | if(IsArrayRef(avref)){ |
6582d0e3 |
48 | AV* const av = (AV*)SvRV(avref); |
49 | I32 const len = AvFILLp(av) + 1; |
50 | I32 i; |
51 | |
52 | for(i = 0; i < len; i++){ |
53 | HV* const stash = gv_stashsv(AvARRAY(av)[i], FALSE); |
54 | if(stash) |
55 | av_push(isa, newSVpv(HvNAME(stash), 0)); |
56 | } |
57 | SvREADONLY_on(isa); |
58 | } |
59 | else{ |
60 | Perl_croak(aTHX_ "Mouse:Util::get_linear_isa() didn't return an ARRAY reference"); |
61 | } |
62 | |
63 | FREETMPS; |
64 | LEAVE; |
65 | } |
66 | |
67 | sv_setiv(gen, (IV)mro_get_pkg_gen(stash)); |
68 | return GvAV(cachegv); |
646c0371 |
69 | } |
70 | #endif /* !no_mor_get_linear_isa */ |
71 | |
72 | #ifdef DEBUGGING |
73 | SV** |
74 | mouse_av_at_safe(pTHX_ AV* const av, I32 const ix){ |
75 | assert(av); |
76 | assert(SvTYPE(av) == SVt_PVAV); |
77 | assert(AvMAX(av) >= ix); |
78 | return &AvARRAY(av)[ix]; |
79 | } |
80 | #endif |
81 | |
82 | void |
83 | mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const char* const fmt, ...){ |
84 | dTHX; |
85 | va_list args; |
86 | SV* message; |
87 | |
88 | PERL_UNUSED_ARG(data); /* for moose-compat */ |
89 | |
90 | assert(metaobject); |
91 | assert(fmt); |
92 | |
93 | va_start(args, fmt); |
94 | message = vnewSVpvf(fmt, &args); |
95 | va_end(args); |
96 | |
97 | { |
98 | dSP; |
99 | PUSHMARK(SP); |
100 | EXTEND(SP, 4); |
101 | |
102 | PUSHs(metaobject); |
103 | mPUSHs(message); |
104 | |
105 | mPUSHs(newSVpvs("depth")); |
106 | mPUSHi(-1); |
107 | |
108 | PUTBACK; |
109 | |
110 | call_method("throw_error", G_VOID); |
111 | croak("throw_error() did not throw the error (%"SVf")", message); |
112 | } |
113 | } |
114 | |
646c0371 |
115 | bool |
116 | mouse_is_class_loaded(pTHX_ SV * const klass){ |
117 | HV *stash; |
118 | GV** gvp; |
119 | HE* he; |
120 | |
121 | if (!(SvPOKp(klass) && SvCUR(klass))) { /* XXX: SvPOK does not work with magical scalars */ |
122 | return FALSE; |
123 | } |
124 | |
125 | stash = gv_stashsv(klass, FALSE); |
126 | if (!stash) { |
127 | return FALSE; |
128 | } |
129 | |
130 | if (( gvp = (GV**)hv_fetchs(stash, "VERSION", FALSE) )) { |
131 | if(isGV(*gvp) && GvSV(*gvp) && SvOK(GvSV(*gvp))){ |
132 | return TRUE; |
133 | } |
134 | } |
135 | |
136 | if (( gvp = (GV**)hv_fetchs(stash, "ISA", FALSE) )) { |
137 | if(isGV(*gvp) && GvAV(*gvp) && av_len(GvAV(*gvp)) != -1){ |
138 | return TRUE; |
139 | } |
140 | } |
141 | |
142 | hv_iterinit(stash); |
143 | while(( he = hv_iternext(stash) )){ |
144 | GV* const gv = (GV*)HeVAL(he); |
145 | |
146 | if(isGV(gv)){ |
147 | if(GvCVu(gv)){ |
148 | return TRUE; |
149 | } |
150 | } |
151 | else if(SvOK(gv)){ |
152 | return TRUE; |
153 | } |
154 | } |
155 | return FALSE; |
156 | } |
157 | |
158 | |
159 | SV * |
160 | mouse_call0 (pTHX_ SV *const self, SV *const method) |
161 | { |
162 | dSP; |
163 | SV *ret; |
164 | |
165 | PUSHMARK(SP); |
166 | XPUSHs(self); |
167 | PUTBACK; |
168 | |
169 | call_sv(method, G_SCALAR | G_METHOD); |
170 | |
171 | SPAGAIN; |
172 | ret = POPs; |
173 | PUTBACK; |
174 | |
175 | return ret; |
176 | } |
177 | |
178 | SV * |
179 | mouse_call1 (pTHX_ SV *const self, SV *const method, SV* const arg1) |
180 | { |
181 | dSP; |
182 | SV *ret; |
183 | |
184 | PUSHMARK(SP); |
185 | EXTEND(SP, 2); |
186 | PUSHs(self); |
187 | PUSHs(arg1); |
188 | PUTBACK; |
189 | |
190 | call_sv(method, G_SCALAR | G_METHOD); |
191 | |
192 | SPAGAIN; |
193 | ret = POPs; |
194 | PUTBACK; |
195 | |
196 | return ret; |
197 | } |
198 | |
199 | MAGIC* |
200 | mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags){ |
201 | MAGIC* mg; |
202 | |
203 | assert(sv != NULL); |
204 | for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){ |
205 | if(mg->mg_virtual == vtbl){ |
206 | return mg; |
207 | } |
208 | } |
209 | |
210 | if(flags & MOUSEf_DIE_ON_FAIL){ |
211 | croak("mouse_mg_find: no MAGIC found for %"SVf, sv_2mortal(newRV_inc(sv))); |
212 | } |
213 | return NULL; |
214 | } |
215 | |
216 | MODULE = Mouse::Util PACKAGE = Mouse::Util |
217 | |
218 | PROTOTYPES: DISABLE |
219 | VERSIONCHECK: DISABLE |
220 | |
221 | bool |
222 | is_class_loaded(SV* sv) |
223 | |
224 | void |
225 | get_code_info(CV* code) |
226 | PREINIT: |
227 | GV* gv; |
228 | HV* stash; |
229 | PPCODE: |
230 | if((gv = CvGV(code)) && isGV(gv) && (stash = GvSTASH(gv))){ |
231 | EXTEND(SP, 2); |
232 | mPUSHs(newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U)); |
233 | mPUSHs(newSVpvn_share(GvNAME_get(gv), GvNAMELEN_get(gv), 0U)); |
234 | } |
235 | |
236 | SV* |
237 | get_code_package(CV* code) |
238 | PREINIT: |
239 | HV* stash; |
240 | CODE: |
241 | if(CvGV(code) && isGV(CvGV(code)) && (stash = GvSTASH(CvGV(code)))){ |
242 | RETVAL = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U); |
243 | } |
244 | else{ |
245 | RETVAL = &PL_sv_no; |
246 | } |
247 | OUTPUT: |
248 | RETVAL |
249 | |
250 | CV* |
251 | get_code_ref(SV* package, SV* name) |
252 | CODE: |
253 | { |
254 | HV* stash; |
255 | HE* he; |
256 | |
257 | if(!SvOK(package)){ |
258 | croak("You must define a package name"); |
259 | } |
260 | if(!SvOK(name)){ |
261 | croak("You must define a subroutine name"); |
262 | } |
263 | |
264 | stash = gv_stashsv(package, FALSE); |
265 | if(!stash){ |
266 | XSRETURN_UNDEF; |
267 | } |
268 | he = hv_fetch_ent(stash, name, FALSE, 0U); |
269 | if(he){ |
270 | GV* const gv = (GV*)hv_iterval(stash, he); |
271 | if(!isGV(gv)){ /* special constant or stub */ |
272 | STRLEN len; |
273 | const char* const pv = SvPV_const(name, len); |
274 | gv_init(gv, stash, pv, len, GV_ADDMULTI); |
275 | } |
276 | RETVAL = GvCVu(gv); |
277 | } |
278 | else{ |
279 | RETVAL = NULL; |
280 | } |
281 | |
282 | if(!RETVAL){ |
283 | XSRETURN_UNDEF; |
284 | } |
285 | } |
286 | OUTPUT: |
287 | RETVAL |
288 | |
289 | void |
441964ce |
290 | generate_isa_predicate_for(SV* klass, SV* predicate_name = NULL) |
646c0371 |
291 | PPCODE: |
292 | { |
441964ce |
293 | const char* name_pv = NULL; |
646c0371 |
294 | CV* xsub; |
295 | |
441964ce |
296 | SvGETMAGIC(klass); |
297 | |
646c0371 |
298 | if(!SvOK(klass)){ |
441964ce |
299 | croak("You must define a class name"); |
646c0371 |
300 | } |
646c0371 |
301 | |
441964ce |
302 | if(predicate_name){ |
303 | SvGETMAGIC(predicate_name); |
304 | if(!SvOK(predicate_name)){ |
305 | croak("You must define a predicate_name"); |
306 | } |
307 | name_pv = SvPV_nolen_const(predicate_name); |
646c0371 |
308 | } |
309 | |
672bde7c |
310 | xsub = mouse_generate_isa_predicate_for(aTHX_ klass, name_pv); |
441964ce |
311 | |
646c0371 |
312 | if(predicate_name == NULL){ /* anonymous predicate */ |
313 | XPUSHs( newRV_noinc((SV*)xsub) ); |
314 | } |
315 | } |