3 #define MY_CXT_KEY "Mouse::Util::_guts" XS_VERSION
9 #define ISA_CACHE "::LINEALIZED_ISA_CACHE::"
11 #ifdef no_mro_get_linear_isa
13 mouse_mro_get_linear_isa(pTHX_ HV* const stash){
14 GV* const cachegv = *(GV**)hv_fetchs(stash, ISA_CACHE, TRUE);
20 gv_init(cachegv, stash, ISA_CACHE, sizeof(ISA_CACHE)-1, TRUE);
26 if(SvIOK(gen) && SvIVX(gen) == (IV)mro_get_pkg_gen(stash)){
27 return isa; /* returns the cache if available */
31 GvAV(cachegv) = isa = newAV();
34 get_linear_isa = get_cv("Mouse::Util::get_linear_isa", TRUE);
44 mXPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
47 call_sv((SV*)get_linear_isa, G_SCALAR);
53 if(IsArrayRef(avref)){
54 AV* const av = (AV*)SvRV(avref);
55 I32 const len = AvFILLp(av) + 1;
58 for(i = 0; i < len; i++){
59 HV* const stash = gv_stashsv(AvARRAY(av)[i], FALSE);
61 av_push(isa, newSVpv(HvNAME(stash), 0));
66 Perl_croak(aTHX_ "Mouse:Util::get_linear_isa() didn't return an ARRAY reference");
73 sv_setiv(gen, (IV)mro_get_pkg_gen(stash));
76 #endif /* !no_mor_get_linear_isa */
80 mouse_av_at_safe(pTHX_ AV* const av, I32 const ix){
82 assert(SvTYPE(av) == SVt_PVAV);
83 assert(AvMAX(av) >= ix);
84 return &AvARRAY(av)[ix];
89 mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const char* const fmt, ...){
98 message = vnewSVpvf(fmt, &args);
109 if(data){ /* extra arg, might be useful for debugging */
110 mPUSHs(newSVpvs("data"));
112 mPUSHs(newSVpvs("depth"));
118 call_method("throw_error", G_VOID);
119 croak("throw_error() did not throw the error (%"SVf")", message);
123 /* workaround RT #69939 */
125 mouse_call_sv_safe(pTHX_ SV* const sv, I32 const flags) {
126 const PERL_CONTEXT* const cx = &cxstack[cxstack_ix];
127 assert( (flags & G_EVAL) == 0 );
128 //warn("%d 0x%x 0x%x", (int)cx->cx_type, (int)cx->cx_type, (int)PL_in_eval);
129 if(!(cx->cx_type & (CXt_EVAL|CXp_TRYBLOCK))) {
132 //ERRSV = sv_newmortal();
134 count = Perl_call_sv(aTHX_ sv, flags | G_EVAL);
137 croak(NULL); /* rethrow */
142 return Perl_call_sv(aTHX_ sv, flags);
147 mouse_must_defined(pTHX_ SV* const value, const char* const name) {
153 croak("You must define %s", name);
158 mouse_must_ref(pTHX_ SV* const value, const char* const name, svtype const t) {
163 if(!(SvROK(value) && (t == SVt_NULL || SvTYPE(SvRV(value)) == t))) {
164 croak("You must pass %s, not %s",
165 name, SvOK(value) ? SvPV_nolen(value) : "undef");
171 mouse_is_class_loaded(pTHX_ SV * const klass){
176 if (!(SvPOKp(klass) && SvCUR(klass))) { /* XXX: SvPOK does not work with magical scalars */
180 stash = gv_stashsv(klass, FALSE);
185 if (( gvp = (GV**)hv_fetchs(stash, "VERSION", FALSE) )) {
186 if(isGV(*gvp) && GvSV(*gvp) && SvOK(GvSV(*gvp))){
191 if (( gvp = (GV**)hv_fetchs(stash, "ISA", FALSE) )) {
192 if(isGV(*gvp) && GvAV(*gvp) && av_len(GvAV(*gvp)) != -1){
198 while(( he = hv_iternext(stash) )){
199 GV* const gv = (GV*)HeVAL(he);
202 if(GvCVu(gv)){ /* is GV and has CV */
203 hv_iterinit(stash); /* reset */
207 else if(SvOK(gv)){ /* is a stub or constant */
208 hv_iterinit(stash); /* reset */
217 mouse_call0 (pTHX_ SV* const self, SV* const method) {
225 call_sv_safe(method, G_SCALAR | G_METHOD);
235 mouse_call1 (pTHX_ SV* const self, SV* const method, SV* const arg1) {
245 call_sv_safe(method, G_SCALAR | G_METHOD);
255 mouse_predicate_call(pTHX_ SV* const self, SV* const method) {
256 return sv_true( mcall0(self, method) );
260 mouse_get_metaclass(pTHX_ SV* metaclass_name){
264 assert(metaclass_name);
265 assert(MY_CXT.metas);
267 if(IsObject(metaclass_name)){
268 HV* const stash = SvSTASH(SvRV(metaclass_name));
270 metaclass_name = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
271 sv_2mortal(metaclass_name);
274 he = hv_fetch_ent(MY_CXT.metas, metaclass_name, FALSE, 0U);
276 return he ? HeVAL(he) : &PL_sv_undef;
280 mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags){
284 for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
285 if(mg->mg_virtual == vtbl){
290 if(flags & MOUSEf_DIE_ON_FAIL){
291 croak("mouse_mg_find: no MAGIC found for %"SVf, sv_2mortal(newRV_inc(sv)));
297 mouse_stash_fetch(pTHX_ HV* const stash, const char* const name, I32 const namelen, I32 const create) {
298 GV** const gvp = (GV**)hv_fetch(stash, name, namelen, create);
302 gv_init(*gvp, stash, name, namelen, GV_ADDMULTI);
312 mouse_install_sub(pTHX_ GV* const gv, SV* const code_ref) {
316 assert(code_ref != NULL);
318 assert(IsCodeRef(code_ref));
320 if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
321 SvREFCNT_dec(GvCV(gv));
325 sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
327 /* name the CODE ref if it's anonymous */
328 cv = (CV*)SvRV(code_ref);
330 && CvGV(cv) /* a cv under construction has no gv */ ){
333 /* update %DB::sub to make NYTProf happy */
334 if((PL_perldb & (PERLDBf_SUBLINE|PERLDB_NAMEANON))
335 && PL_DBsub && (dbsub = GvHV(PL_DBsub))
337 /* see Perl_newATTRSUB() in op.c */
338 SV* const subname = sv_newmortal();
341 gv_efullname3(subname, CvGV(cv), NULL);
342 orig = hv_fetch_ent(dbsub, subname, FALSE, 0U);
344 gv_efullname3(subname, gv, NULL);
345 (void)hv_store_ent(dbsub, subname, HeVAL(orig), 0U);
346 SvREFCNT_inc_simple_void_NN(HeVAL(orig));
355 MODULE = Mouse::Util PACKAGE = Mouse::Util
358 VERSIONCHECK: DISABLE
367 __register_metaclass_storage(HV* metas, bool cloning)
376 if(MY_CXT.metas && ckWARN(WARN_REDEFINE)){
377 Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Metaclass storage more than once");
379 MY_CXT.metas = metas;
380 SvREFCNT_inc_simple_void_NN(metas);
385 is_valid_class_name(SV* sv)
389 if(SvPOKp(sv) && SvCUR(sv) > 0){
392 for(i = 0; i < SvCUR(sv); i++){
393 char const c = SvPVX(sv)[i];
394 if(!(isALNUM(c) || c == ':')){
401 RETVAL = SvNIOKp(sv) ? TRUE : FALSE;
408 is_class_loaded(SV* sv)
411 get_code_info(CV* code)
416 if((gv = CvGV(code)) && isGV(gv) && (stash = GvSTASH(gv))){
418 mPUSHs(newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U));
419 mPUSHs(newSVpvn_share(GvNAME_get(gv), GvNAMELEN_get(gv), 0U));
423 get_code_package(CV* code)
427 if(CvGV(code) && isGV(CvGV(code)) && (stash = GvSTASH(CvGV(code)))){
428 RETVAL = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
437 get_code_ref(SV* package, SV* name)
445 must_defined(package, "a package name");
446 must_defined(name, "a subroutine name");
448 stash = gv_stashsv(package, FALSE);
453 name_pv = SvPV_const(name, name_len);
454 gv = stash_fetch(stash, name_pv, name_len, FALSE);
455 RETVAL = gv ? GvCVu(gv) : NULL;
465 generate_isa_predicate_for(SV* arg, SV* predicate_name = NULL)
467 generate_isa_predicate_for = 0
468 generate_can_predicate_for = 1
471 const char* name_pv = NULL;
474 must_defined(arg, ix == 0 ? "a class_name" : "method names");
477 must_defined(predicate_name, "a predicate name");
478 name_pv = SvPV_nolen_const(predicate_name);
482 xsub = mouse_generate_isa_predicate_for(aTHX_ arg, name_pv);
485 xsub = mouse_generate_can_predicate_for(aTHX_ arg, name_pv);
488 if(predicate_name == NULL){ /* anonymous predicate */
489 mXPUSHs( newRV_inc((SV*)xsub) );
493 # This xsub will redefine &Mouse::Util::install_subroutines()
495 install_subroutines(SV* into, ...)
501 must_defined(into, "a package name");
502 stash = gv_stashsv(into, TRUE);
504 if( ((items-1) % 2) != 0 ){
505 croak_xs_usage(cv, "into, name => coderef [, other_name, other_coderef ...]");
508 for(i = 1; i < items; i += 2) {
509 SV* const name = ST(i);
510 SV* const code = ST(i+1);
515 must_defined(name, "a subroutine name");
516 must_ref(code, "a CODE reference", SVt_PVCV);
518 pv = SvPV_const(name, len);
519 gv = stash_fetch(stash, pv, len, TRUE);
521 mouse_install_sub(aTHX_ gv, code);