}
}
+void
+mouse_must_defined(pTHX_ SV* const value, const char* const name) {
+ assert(value);
+ assert(name);
+
+ SvGETMAGIC(value);
+ if(!SvOK(value)){
+ croak("You must define %s", name);
+ }
+}
+
+void
+mouse_must_ref(pTHX_ SV* const value, const char* const name, svtype const t) {
+ assert(value);
+ assert(name);
+
+ SvGETMAGIC(value);
+ if(!(SvROK(value) && (t == SVt_NULL || SvTYPE(SvRV(value)) == t))) {
+ croak("You must pass %s, not %s",
+ name, SvOK(value) ? SvPV_nolen(value) : "undef");
+ }
+}
+
+
bool
mouse_is_class_loaded(pTHX_ SV * const klass){
HV *stash;
GV* const gv = (GV*)HeVAL(he);
if(isGV(gv)){
- if(GvCVu(gv)){
+ if(GvCVu(gv)){ /* is GV and has CV */
+ hv_iterinit(stash); /* reset */
return TRUE;
}
}
- else if(SvOK(gv)){
+ else if(SvOK(gv)){ /* is a stub or constant */
+ hv_iterinit(stash); /* reset */
return TRUE;
}
}
}
}
+void
+mouse_install_sub(pTHX_ GV* const gv, SV* const code_ref) {
+ CV* cv;
+
+ assert(gv != NULL);
+ assert(code_ref != NULL);
+ assert(isGV(gv));
+ assert(IsCodeRef(code_ref));
+
+ if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
+ SvREFCNT_dec(GvCV(gv));
+ GvCV(gv) = NULL;
+ }
+
+ sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
+
+ /* name the CODE ref if it's anonymous */
+ cv = (CV*)SvRV(code_ref);
+ if(CvANON(cv)
+ && CvGV(cv) /* a cv under construction has no gv */ ){
+ HV* dbsub;
+
+ /* update %DB::sub to make NYTProf happy */
+ if((PL_perldb & (PERLDBf_SUBLINE|PERLDB_NAMEANON))
+ && PL_DBsub && (dbsub = GvHV(PL_DBsub))
+ ){
+ /* see Perl_newATTRSUB() in op.c */
+ SV* const subname = sv_newmortal();
+ HE* orig;
+
+ gv_efullname3(subname, CvGV(cv), NULL);
+ orig = hv_fetch_ent(dbsub, subname, FALSE, 0U);
+ if(orig){
+ gv_efullname3(subname, gv, NULL);
+ (void)hv_store_ent(dbsub, subname, HeVAL(orig), 0U);
+ SvREFCNT_inc_simple_void_NN(HeVAL(orig));
+ }
+ }
+
+ CvGV(cv) = gv;
+ CvANON_off(cv);
+ }
+}
+
MODULE = Mouse::Util PACKAGE = Mouse::Util
PROTOTYPES: DISABLE
const char* name_pv;
GV* gv;
- if(!SvOK(package)){
- croak("You must define a package name");
- }
- if(!SvOK(name)){
- croak("You must define a subroutine name");
- }
+ must_defined(package, "a package name");
+ must_defined(name, "a subroutine name");
stash = gv_stashsv(package, FALSE);
if(!stash){
const char* name_pv = NULL;
CV* xsub;
- SvGETMAGIC(arg);
-
- if(!SvOK(arg)){
- croak("You must define %s", ix == 0 ? "a class name" : "method names");
- }
+ must_defined(arg, ix == 0 ? "a class_name" : "method names");
if(predicate_name){
- SvGETMAGIC(predicate_name);
- if(!SvOK(predicate_name)){
- croak("You must define %s", "a predicate name");
- }
+ must_defined(predicate_name, "a predicate name");
name_pv = SvPV_nolen_const(predicate_name);
}
mXPUSHs( newRV_inc((SV*)xsub) );
}
}
+
+# This xsub will redefine &Mouse::Util::install_subroutines()
+void
+install_subroutines(SV* into, ...)
+CODE:
+{
+ HV* stash;
+ I32 i;
+
+ must_defined(into, "a package name");
+ stash = gv_stashsv(into, TRUE);
+
+ if( ((items-1) % 2) != 0 ){
+ croak_xs_usage(cv, "into, name => coderef [, other_name, other_coderef ...]");
+ }
+
+ for(i = 1; i < items; i += 2) {
+ SV* const name = ST(i);
+ SV* const code = ST(i+1);
+ STRLEN len;
+ const char* pv;
+ GV* gv;
+
+ must_defined(name, "a subroutine name");
+ must_ref(code, "a CODE reference", SVt_PVCV);
+
+ pv = SvPV_const(name, len);
+ gv = stash_fetch(stash, pv, len, TRUE);
+
+ mouse_install_sub(aTHX_ gv, code);
+ }
+}