File: LexAlias.xs

package info (click to toggle)
libdevel-lexalias-perl 0.05-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 76 kB
  • sloc: perl: 68; makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,464 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#ifndef PadARRAY
typedef AV PADNAMELIST;
typedef SV PADNAME;
# if PERL_VERSION < 8 || (PERL_VERSION == 8 && !PERL_SUBVERSION)
typedef AV PAD;
# endif
# define PadlistARRAY(pl)      ((PAD **)AvARRAY(pl))
# define PadlistNAMES(pl)      (*PadlistARRAY(pl))
# define PadnamelistARRAY(pnl) ((PADNAME **)AvARRAY(pnl))
# define PadnamelistMAX(pnl)   AvFILLp(pnl)
# define PadARRAY              AvARRAY
# define PadnamePV(pn)         (SvPOKp(pn) ? SvPVX(pn) : NULL)
#endif



/* cargo-culted from PadWalker */

MODULE = Devel::LexAlias                PACKAGE = Devel::LexAlias

void
_lexalias(SV* cv_ref, char *name, SV* new_rv)
  CODE:
{
    CV*          cv   = SvROK(cv_ref) ? (CV*) SvRV(cv_ref) : NULL;
    PADNAMELIST* padn = cv ? PadlistNAMES(CvPADLIST(cv)) : PL_comppad_name;
    PAD*         padv = cv ? PadlistARRAY(CvPADLIST(cv))[1] : PL_comppad;
    SV*          new_sv;
    I32          i;

    if (!SvROK(new_rv)) croak("ref is not a reference");
    new_sv = SvRV(new_rv);

    for (i = 0; i <= PadnamelistMAX(padn); ++i) {
        PADNAME* namesv = PadnamelistARRAY(padn)[i];
        char*    name_str;
        if (namesv && (name_str = PadnamePV(namesv))) {
            if (!strcmp(name, name_str)) {
                SvREFCNT_dec(PadARRAY(padv)[i]);
                PadARRAY(padv)[i] = new_sv;
                SvREFCNT_inc(new_sv);
                SvPADMY_on(new_sv);
            }
        }
    }
}