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);
124 mouse_must_defined(pTHX_ SV* const value, const char* const name) {
130 croak("You must define %s", name);
135 mouse_must_ref(pTHX_ SV* const value, const char* const name, svtype const t) {
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");
148 mouse_is_class_loaded(pTHX_ SV * const klass){
153 if (!(SvPOKp(klass) && SvCUR(klass))) { /* XXX: SvPOK does not work with magical scalars */
157 stash = gv_stashsv(klass, FALSE);
162 if (( gvp = (GV**)hv_fetchs(stash, "VERSION", FALSE) )) {
163 if(isGV(*gvp) && GvSV(*gvp) && SvOK(GvSV(*gvp))){
168 if (( gvp = (GV**)hv_fetchs(stash, "ISA", FALSE) )) {
169 if(isGV(*gvp) && GvAV(*gvp) && av_len(GvAV(*gvp)) != -1){
175 while(( he = hv_iternext(stash) )){
176 GV* const gv = (GV*)HeVAL(he);
179 if(GvCVu(gv)){ /* is GV and has CV */
180 hv_iterinit(stash); /* reset */
184 else if(SvOK(gv)){ /* is a stub or constant */
185 hv_iterinit(stash); /* reset */
194 mouse_call0 (pTHX_ SV* const self, SV* const method) {
202 call_sv(method, G_SCALAR | G_METHOD);
212 mouse_call1 (pTHX_ SV* const self, SV* const method, SV* const arg1) {
222 call_sv(method, G_SCALAR | G_METHOD);
232 mouse_predicate_call(pTHX_ SV* const self, SV* const method) {
233 return sv_true( mcall0(self, method) );
237 mouse_get_metaclass(pTHX_ SV* metaclass_name){
241 assert(metaclass_name);
242 assert(MY_CXT.metas);
244 if(IsObject(metaclass_name)){
245 HV* const stash = SvSTASH(SvRV(metaclass_name));
247 metaclass_name = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
248 sv_2mortal(metaclass_name);
251 he = hv_fetch_ent(MY_CXT.metas, metaclass_name, FALSE, 0U);
253 return he ? HeVAL(he) : &PL_sv_undef;
257 mouse_mg_find(pTHX_ SV* const sv, const MGVTBL* const vtbl, I32 const flags){
261 for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
262 if(mg->mg_virtual == vtbl){
267 if(flags & MOUSEf_DIE_ON_FAIL){
268 croak("mouse_mg_find: no MAGIC found for %"SVf, sv_2mortal(newRV_inc(sv)));
274 mouse_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);
279 gv_init(*gvp, stash, name, namelen, GV_ADDMULTI);
289 mouse_install_sub(pTHX_ GV* const gv, SV* const code_ref) {
293 assert(code_ref != NULL);
295 assert(IsCodeRef(code_ref));
297 if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
298 SvREFCNT_dec(GvCV(gv));
302 sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
304 /* name the CODE ref if it's anonymous */
305 cv = (CV*)SvRV(code_ref);
307 && CvGV(cv) /* a cv under construction has no gv */ ){
310 /* update %DB::sub to make NYTProf happy */
311 if((PL_perldb & (PERLDBf_SUBLINE|PERLDB_NAMEANON))
312 && PL_DBsub && (dbsub = GvHV(PL_DBsub))
314 /* see Perl_newATTRSUB() in op.c */
315 SV* const subname = sv_newmortal();
318 gv_efullname3(subname, CvGV(cv), NULL);
319 orig = hv_fetch_ent(dbsub, subname, FALSE, 0U);
321 gv_efullname3(subname, gv, NULL);
322 (void)hv_store_ent(dbsub, subname, HeVAL(orig), 0U);
323 SvREFCNT_inc_simple_void_NN(HeVAL(orig));
332 MODULE = Mouse::Util PACKAGE = Mouse::Util
335 VERSIONCHECK: DISABLE
344 __register_metaclass_storage(HV* metas, bool cloning)
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);
360 is_valid_class_name(SV* sv)
364 if(SvPOKp(sv) && SvCUR(sv) > 0){
367 for(i = 0; i < SvCUR(sv); i++){
368 char const c = SvPVX(sv)[i];
369 if(!(isALNUM(c) || c == ':')){
376 RETVAL = SvNIOKp(sv) ? TRUE : FALSE;
383 is_class_loaded(SV* sv)
386 get_code_info(CV* code)
391 if((gv = CvGV(code)) && isGV(gv) && (stash = GvSTASH(gv))){
393 mPUSHs(newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U));
394 mPUSHs(newSVpvn_share(GvNAME_get(gv), GvNAMELEN_get(gv), 0U));
398 get_code_package(CV* code)
402 if(CvGV(code) && isGV(CvGV(code)) && (stash = GvSTASH(CvGV(code)))){
403 RETVAL = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
412 get_code_ref(SV* package, SV* name)
420 must_defined(package, "a package name");
421 must_defined(name, "a subroutine name");
423 stash = gv_stashsv(package, FALSE);
428 name_pv = SvPV_const(name, name_len);
429 gv = stash_fetch(stash, name_pv, name_len, FALSE);
430 RETVAL = gv ? GvCVu(gv) : NULL;
440 generate_isa_predicate_for(SV* arg, SV* predicate_name = NULL)
442 generate_isa_predicate_for = 0
443 generate_can_predicate_for = 1
446 const char* name_pv = NULL;
449 must_defined(arg, ix == 0 ? "a class_name" : "method names");
452 must_defined(predicate_name, "a predicate name");
453 name_pv = SvPV_nolen_const(predicate_name);
457 xsub = mouse_generate_isa_predicate_for(aTHX_ arg, name_pv);
460 xsub = mouse_generate_can_predicate_for(aTHX_ arg, name_pv);
463 if(predicate_name == NULL){ /* anonymous predicate */
464 mXPUSHs( newRV_inc((SV*)xsub) );
468 # This xsub will redefine &Mouse::Util::install_subroutines()
470 install_subroutines(SV* into, ...)
476 must_defined(into, "a package name");
477 stash = gv_stashsv(into, TRUE);
479 if( ((items-1) % 2) != 0 ){
480 croak_xs_usage(cv, "into, name => coderef [, other_name, other_coderef ...]");
483 for(i = 1; i < items; i += 2) {
484 SV* const name = ST(i);
485 SV* const code = ST(i+1);
490 must_defined(name, "a subroutine name");
491 must_ref(code, "a CODE reference", SVt_PVCV);
493 pv = SvPV_const(name, len);
494 gv = stash_fetch(stash, pv, len, TRUE);
496 mouse_install_sub(aTHX_ gv, code);