Commit | Line | Data |
646c0371 |
1 | #include "mouse.h" |
2 | |
fd168725 |
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 | |
646c0371 |
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){ |
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 |
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 | |
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 | |
5433f845 |
123 | static I32 |
124 | S_dopoptosub(pTHX_ I32 const startingblock) |
125 | { |
126 | const PERL_CONTEXT* const cxstk = cxstack; |
127 | I32 i; |
128 | for (i = startingblock; i >= 0; i--) { |
129 | const PERL_CONTEXT* const cx = &cxstk[i]; |
130 | |
131 | switch (CxTYPE(cx)) { |
132 | case CXt_EVAL: |
133 | case CXt_SUB: |
134 | case CXt_FORMAT: |
135 | return i; |
136 | } |
137 | } |
138 | return i; |
139 | } |
140 | |
b3cd4c14 |
141 | /* workaround RT #69939 */ |
142 | I32 |
143 | mouse_call_sv_safe(pTHX_ SV* const sv, I32 const flags) { |
5433f845 |
144 | const PERL_CONTEXT* const cx = &cxstack[S_dopoptosub(aTHX_ cxstack_ix)]; |
b3cd4c14 |
145 | assert( (flags & G_EVAL) == 0 ); |
5433f845 |
146 | |
147 | //warn("cx_type=0x%02x PL_eval=0x%02x (%"SVf")", (unsigned)cx->cx_type, (unsigned)PL_in_eval, sv); |
148 | if(!(cx->cx_type & CXp_TRYBLOCK)) { |
b3cd4c14 |
149 | I32 count; |
9a87d68a |
150 | //SAVESPTR(ERRSV); |
151 | //ERRSV = sv_newmortal(); |
b3cd4c14 |
152 | |
153 | count = Perl_call_sv(aTHX_ sv, flags | G_EVAL); |
154 | |
155 | if(sv_true(ERRSV)){ |
156 | croak(NULL); /* rethrow */ |
157 | } |
158 | return count; |
159 | } |
160 | else { |
161 | return Perl_call_sv(aTHX_ sv, flags); |
162 | } |
163 | } |
164 | |
d06d9266 |
165 | void |
166 | mouse_must_defined(pTHX_ SV* const value, const char* const name) { |
167 | assert(value); |
168 | assert(name); |
169 | |
170 | SvGETMAGIC(value); |
171 | if(!SvOK(value)){ |
172 | croak("You must define %s", name); |
173 | } |
174 | } |
175 | |
176 | void |
177 | mouse_must_ref(pTHX_ SV* const value, const char* const name, svtype const t) { |
178 | assert(value); |
179 | assert(name); |
180 | |
181 | SvGETMAGIC(value); |
182 | if(!(SvROK(value) && (t == SVt_NULL || SvTYPE(SvRV(value)) == t))) { |
183 | croak("You must pass %s, not %s", |
184 | name, SvOK(value) ? SvPV_nolen(value) : "undef"); |
185 | } |
186 | } |
187 | |
188 | |
646c0371 |
189 | bool |
190 | mouse_is_class_loaded(pTHX_ SV * const klass){ |
191 | HV *stash; |
192 | GV** gvp; |
193 | HE* he; |
194 | |
195 | if (!(SvPOKp(klass) && SvCUR(klass))) { /* XXX: SvPOK does not work with magical scalars */ |
196 | return FALSE; |
197 | } |
198 | |
199 | stash = gv_stashsv(klass, FALSE); |
200 | if (!stash) { |
201 | return FALSE; |
202 | } |
203 | |
204 | if (( gvp = (GV**)hv_fetchs(stash, "VERSION", FALSE) )) { |
205 | if(isGV(*gvp) && GvSV(*gvp) && SvOK(GvSV(*gvp))){ |
206 | return TRUE; |
207 | } |
208 | } |
209 | |
210 | if (( gvp = (GV**)hv_fetchs(stash, "ISA", FALSE) )) { |
211 | if(isGV(*gvp) && GvAV(*gvp) && av_len(GvAV(*gvp)) != -1){ |
212 | return TRUE; |
213 | } |
214 | } |
215 | |
216 | hv_iterinit(stash); |
217 | while(( he = hv_iternext(stash) )){ |
218 | GV* const gv = (GV*)HeVAL(he); |
219 | |
220 | if(isGV(gv)){ |
fa4ac648 |
221 | if(GvCVu(gv)){ /* is GV and has CV */ |
222 | hv_iterinit(stash); /* reset */ |
646c0371 |
223 | return TRUE; |
224 | } |
225 | } |
fa4ac648 |
226 | else if(SvOK(gv)){ /* is a stub or constant */ |
227 | hv_iterinit(stash); /* reset */ |
646c0371 |
228 | return TRUE; |
229 | } |
230 | } |
231 | return FALSE; |
232 | } |
233 | |
234 | |
0aad0266 |
235 | SV* |
236 | mouse_call0 (pTHX_ SV* const self, SV* const method) { |
646c0371 |
237 | dSP; |
238 | SV *ret; |
239 | |
240 | PUSHMARK(SP); |
241 | XPUSHs(self); |
242 | PUTBACK; |
243 | |
b3cd4c14 |
244 | call_sv_safe(method, G_SCALAR | G_METHOD); |
646c0371 |
245 | |
246 | SPAGAIN; |
247 | ret = POPs; |
248 | PUTBACK; |
249 | |
250 | return ret; |
251 | } |
252 | |
0aad0266 |
253 | SV* |
254 | mouse_call1 (pTHX_ SV* const self, SV* const method, SV* const arg1) { |
646c0371 |
255 | dSP; |
256 | SV *ret; |
257 | |
258 | PUSHMARK(SP); |
259 | EXTEND(SP, 2); |
260 | PUSHs(self); |
261 | PUSHs(arg1); |
262 | PUTBACK; |
263 | |
b3cd4c14 |
264 | call_sv_safe(method, G_SCALAR | G_METHOD); |
646c0371 |
265 | |
266 | SPAGAIN; |
267 | ret = POPs; |
268 | PUTBACK; |
269 | |
270 | return ret; |
271 | } |
272 | |
0aad0266 |
273 | int |
274 | mouse_predicate_call(pTHX_ SV* const self, SV* const method) { |
6ad77996 |
275 | return sv_true( mcall0(self, method) ); |
0aad0266 |
276 | } |
277 | |
a39e9541 |
278 | SV* |
aa2d2e2c |
279 | mouse_get_metaclass(pTHX_ SV* metaclass_name){ |
fd168725 |
280 | dMY_CXT; |
281 | HE* he; |
a39e9541 |
282 | |
aa2d2e2c |
283 | assert(metaclass_name); |
fd168725 |
284 | assert(MY_CXT.metas); |
285 | |
aa2d2e2c |
286 | if(IsObject(metaclass_name)){ |
a5c683f6 |
287 | HV* const stash = SvSTASH(SvRV(metaclass_name)); |
aa2d2e2c |
288 | |
289 | metaclass_name = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U); |
290 | sv_2mortal(metaclass_name); |
291 | } |
292 | |
fd168725 |
293 | he = hv_fetch_ent(MY_CXT.metas, metaclass_name, FALSE, 0U); |
a39e9541 |
294 | |
fd168725 |
295 | return he ? HeVAL(he) : &PL_sv_undef; |
a39e9541 |
296 | } |
297 | |
646c0371 |
298 | MAGIC* |
299 | mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags){ |
300 | MAGIC* mg; |
301 | |
302 | assert(sv != NULL); |
303 | for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){ |
304 | if(mg->mg_virtual == vtbl){ |
305 | return mg; |
306 | } |
307 | } |
308 | |
309 | if(flags & MOUSEf_DIE_ON_FAIL){ |
310 | croak("mouse_mg_find: no MAGIC found for %"SVf, sv_2mortal(newRV_inc(sv))); |
311 | } |
312 | return NULL; |
313 | } |
314 | |
a39e9541 |
315 | GV* |
316 | mouse_stash_fetch(pTHX_ HV* const stash, const char* const name, I32 const namelen, I32 const create) { |
317 | GV** const gvp = (GV**)hv_fetch(stash, name, namelen, create); |
318 | |
319 | if(gvp){ |
320 | if(!isGV(*gvp)){ |
321 | gv_init(*gvp, stash, name, namelen, GV_ADDMULTI); |
322 | } |
323 | return *gvp; |
324 | } |
325 | else{ |
326 | return NULL; |
327 | } |
328 | } |
329 | |
d67f600d |
330 | void |
331 | mouse_install_sub(pTHX_ GV* const gv, SV* const code_ref) { |
332 | CV* cv; |
333 | |
334 | assert(gv != NULL); |
335 | assert(code_ref != NULL); |
336 | assert(isGV(gv)); |
337 | assert(IsCodeRef(code_ref)); |
338 | |
339 | if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */ |
340 | SvREFCNT_dec(GvCV(gv)); |
341 | GvCV(gv) = NULL; |
342 | } |
d06d9266 |
343 | |
d67f600d |
344 | sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */ |
345 | |
346 | /* name the CODE ref if it's anonymous */ |
347 | cv = (CV*)SvRV(code_ref); |
348 | if(CvANON(cv) |
349 | && CvGV(cv) /* a cv under construction has no gv */ ){ |
350 | HV* dbsub; |
351 | |
352 | /* update %DB::sub to make NYTProf happy */ |
353 | if((PL_perldb & (PERLDBf_SUBLINE|PERLDB_NAMEANON)) |
354 | && PL_DBsub && (dbsub = GvHV(PL_DBsub)) |
355 | ){ |
356 | /* see Perl_newATTRSUB() in op.c */ |
357 | SV* const subname = sv_newmortal(); |
358 | HE* orig; |
359 | |
360 | gv_efullname3(subname, CvGV(cv), NULL); |
361 | orig = hv_fetch_ent(dbsub, subname, FALSE, 0U); |
362 | if(orig){ |
363 | gv_efullname3(subname, gv, NULL); |
364 | (void)hv_store_ent(dbsub, subname, HeVAL(orig), 0U); |
365 | SvREFCNT_inc_simple_void_NN(HeVAL(orig)); |
366 | } |
367 | } |
368 | |
369 | CvGV(cv) = gv; |
370 | CvANON_off(cv); |
371 | } |
372 | } |
373 | |
646c0371 |
374 | MODULE = Mouse::Util PACKAGE = Mouse::Util |
375 | |
376 | PROTOTYPES: DISABLE |
377 | VERSIONCHECK: DISABLE |
378 | |
fd168725 |
379 | BOOT: |
380 | { |
381 | MY_CXT_INIT; |
382 | MY_CXT.metas = NULL; |
383 | } |
384 | |
385 | void |
386 | __register_metaclass_storage(HV* metas, bool cloning) |
387 | CODE: |
388 | { |
389 | if(cloning){ |
390 | MY_CXT_CLONE; |
391 | MY_CXT.metas = NULL; |
392 | } |
393 | { |
394 | dMY_CXT; |
25ba356e |
395 | if(MY_CXT.metas && ckWARN(WARN_REDEFINE)){ |
396 | Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Metaclass storage more than once"); |
397 | } |
fd168725 |
398 | MY_CXT.metas = metas; |
399 | SvREFCNT_inc_simple_void_NN(metas); |
400 | } |
401 | } |
402 | |
646c0371 |
403 | bool |
0ffc4183 |
404 | is_valid_class_name(SV* sv) |
405 | CODE: |
406 | { |
407 | SvGETMAGIC(sv); |
408 | if(SvPOKp(sv) && SvCUR(sv) > 0){ |
409 | UV i; |
410 | RETVAL = TRUE; |
411 | for(i = 0; i < SvCUR(sv); i++){ |
412 | char const c = SvPVX(sv)[i]; |
413 | if(!(isALNUM(c) || c == ':')){ |
414 | RETVAL = FALSE; |
415 | break; |
416 | } |
417 | } |
418 | } |
419 | else{ |
420 | RETVAL = SvNIOKp(sv) ? TRUE : FALSE; |
421 | } |
422 | } |
423 | OUTPUT: |
424 | RETVAL |
425 | |
426 | bool |
646c0371 |
427 | is_class_loaded(SV* sv) |
428 | |
429 | void |
430 | get_code_info(CV* code) |
431 | PREINIT: |
432 | GV* gv; |
433 | HV* stash; |
434 | PPCODE: |
435 | if((gv = CvGV(code)) && isGV(gv) && (stash = GvSTASH(gv))){ |
436 | EXTEND(SP, 2); |
437 | mPUSHs(newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U)); |
438 | mPUSHs(newSVpvn_share(GvNAME_get(gv), GvNAMELEN_get(gv), 0U)); |
439 | } |
440 | |
441 | SV* |
442 | get_code_package(CV* code) |
443 | PREINIT: |
444 | HV* stash; |
445 | CODE: |
446 | if(CvGV(code) && isGV(CvGV(code)) && (stash = GvSTASH(CvGV(code)))){ |
447 | RETVAL = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U); |
448 | } |
449 | else{ |
450 | RETVAL = &PL_sv_no; |
451 | } |
452 | OUTPUT: |
453 | RETVAL |
454 | |
455 | CV* |
456 | get_code_ref(SV* package, SV* name) |
457 | CODE: |
458 | { |
459 | HV* stash; |
a39e9541 |
460 | STRLEN name_len; |
461 | const char* name_pv; |
462 | GV* gv; |
646c0371 |
463 | |
d06d9266 |
464 | must_defined(package, "a package name"); |
465 | must_defined(name, "a subroutine name"); |
646c0371 |
466 | |
467 | stash = gv_stashsv(package, FALSE); |
468 | if(!stash){ |
469 | XSRETURN_UNDEF; |
470 | } |
a39e9541 |
471 | |
472 | name_pv = SvPV_const(name, name_len); |
473 | gv = stash_fetch(stash, name_pv, name_len, FALSE); |
474 | RETVAL = gv ? GvCVu(gv) : NULL; |
646c0371 |
475 | |
476 | if(!RETVAL){ |
477 | XSRETURN_UNDEF; |
478 | } |
479 | } |
480 | OUTPUT: |
481 | RETVAL |
482 | |
483 | void |
ebe91068 |
484 | generate_isa_predicate_for(SV* arg, SV* predicate_name = NULL) |
485 | ALIAS: |
486 | generate_isa_predicate_for = 0 |
487 | generate_can_predicate_for = 1 |
646c0371 |
488 | PPCODE: |
489 | { |
441964ce |
490 | const char* name_pv = NULL; |
646c0371 |
491 | CV* xsub; |
492 | |
d06d9266 |
493 | must_defined(arg, ix == 0 ? "a class_name" : "method names"); |
646c0371 |
494 | |
441964ce |
495 | if(predicate_name){ |
d06d9266 |
496 | must_defined(predicate_name, "a predicate name"); |
441964ce |
497 | name_pv = SvPV_nolen_const(predicate_name); |
646c0371 |
498 | } |
499 | |
ebe91068 |
500 | if(ix == 0){ |
501 | xsub = mouse_generate_isa_predicate_for(aTHX_ arg, name_pv); |
502 | } |
503 | else{ |
504 | xsub = mouse_generate_can_predicate_for(aTHX_ arg, name_pv); |
505 | } |
441964ce |
506 | |
646c0371 |
507 | if(predicate_name == NULL){ /* anonymous predicate */ |
73337709 |
508 | mXPUSHs( newRV_inc((SV*)xsub) ); |
646c0371 |
509 | } |
510 | } |
d67f600d |
511 | |
512 | # This xsub will redefine &Mouse::Util::install_subroutines() |
513 | void |
514 | install_subroutines(SV* into, ...) |
515 | CODE: |
516 | { |
517 | HV* stash; |
518 | I32 i; |
519 | |
d06d9266 |
520 | must_defined(into, "a package name"); |
d67f600d |
521 | stash = gv_stashsv(into, TRUE); |
522 | |
523 | if( ((items-1) % 2) != 0 ){ |
524 | croak_xs_usage(cv, "into, name => coderef [, other_name, other_coderef ...]"); |
525 | } |
526 | |
527 | for(i = 1; i < items; i += 2) { |
528 | SV* const name = ST(i); |
529 | SV* const code = ST(i+1); |
530 | STRLEN len; |
531 | const char* pv; |
532 | GV* gv; |
533 | |
d06d9266 |
534 | must_defined(name, "a subroutine name"); |
535 | must_ref(code, "a CODE reference", SVt_PVCV); |
d67f600d |
536 | |
537 | pv = SvPV_const(name, len); |
538 | gv = stash_fetch(stash, pv, len, TRUE); |
539 | |
540 | mouse_install_sub(aTHX_ gv, code); |
541 | } |
542 | } |