X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=Name.xs;h=f2c790341015a4fe571ae80ec08ce536f128a051;hb=fa213968c7910aab4fde2b0fac110d9eb7546fa5;hp=6a4dc320a81a997e0efb20d7c134e89172527964;hpb=bbd0130659727a45e7f214cb8086cddbaecd5dec;p=p5sagit%2FSub-Name.git diff --git a/Name.xs b/Name.xs index 6a4dc32..f2c7903 100644 --- a/Name.xs +++ b/Name.xs @@ -1,6 +1,5 @@ -/* $Id: Name.xs,v 1.5 2004/08/18 13:21:44 xmath Exp $ - * Copyright (C) 2004 Matthijs van Duin. All rights reserved. - * This program is free software; you can redistribute it and/or modify +/* Copyright (C) 2004, 2008 Matthijs van Duin. All rights reserved. + * This program is free software; you can redistribute it and/or modify * it under the same terms as Perl itself. */ @@ -10,6 +9,15 @@ static MGVTBL subname_vtbl; +#ifndef PERL_MAGIC_ext +# define PERL_MAGIC_ext '~' +#endif + +#ifndef SvMAGIC_set +#define SvMAGIC_set(sv, val) (SvMAGIC(sv) = (val)) +#endif + + MODULE = Sub::Name PACKAGE = Sub::Name PROTOTYPES: DISABLE @@ -23,6 +31,7 @@ subname(name, sub) GV *gv; HV *stash = CopSTASH(PL_curcop); char *s, *end = NULL, saved; + MAGIC *mg; PPCODE: if (!SvROK(sub) && SvGMAGICAL(sub)) mg_get(sub); @@ -33,7 +42,8 @@ subname(name, sub) else if (!SvOK(sub)) croak(PL_no_usym, "a subroutine"); else if (PL_op->op_private & HINT_STRICT_REFS) - croak(PL_no_symref, SvPV_nolen(sub), "a subroutine"); + croak("Can't use string (\"%.32s\") as %s ref while \"strict refs\" in use", + SvPV_nolen(sub), "a subroutine"); else if ((gv = gv_fetchpv(SvPV_nolen(sub), FALSE, SVt_PVCV))) cv = GvCVu(gv); if (!cv) @@ -56,24 +66,24 @@ subname(name, sub) } gv = (GV *) newSV(0); gv_init(gv, stash, name, s - name, TRUE); -#ifndef USE_5005THREADS - if (CvPADLIST(cv)) { - /* cheap way to refcount the gv */ - av_store((AV *) AvARRAY(CvPADLIST(cv))[0], 0, (SV *) gv); - } -#endif - else { - /* expensive way to refcount the gv */ - MAGIC *mg = SvMAGIC(cv); - while (mg && mg->mg_virtual != &subname_vtbl) - mg = mg->mg_moremagic; - if (!mg) - mg = sv_magicext((SV *) cv, NULL, PERL_MAGIC_ext, - &subname_vtbl, NULL, 0); - if (mg->mg_flags & MGf_REFCOUNTED) - SvREFCNT_dec(mg->mg_obj); - mg->mg_flags |= MGf_REFCOUNTED; - mg->mg_obj = (SV *) gv; + + mg = SvMAGIC(cv); + while (mg && mg->mg_virtual != &subname_vtbl) + mg = mg->mg_moremagic; + if (!mg) { + Newz(702, mg, 1, MAGIC); + mg->mg_moremagic = SvMAGIC(cv); + mg->mg_type = PERL_MAGIC_ext; + mg->mg_virtual = &subname_vtbl; + SvMAGIC_set(cv, mg); } + if (mg->mg_flags & MGf_REFCOUNTED) + SvREFCNT_dec(mg->mg_obj); + mg->mg_flags |= MGf_REFCOUNTED; + mg->mg_obj = (SV *) gv; +#ifndef CvGV_set CvGV(cv) = gv; +#else + CvGV_set(cv, gv); +#endif PUSHs(sub);