]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Prefer SvPV_*_const functions
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #ifndef SvPV_const
10 # define SvPV_const SvPV
11 #endif
12
13 #ifndef SvPV_nolen_const
14 # define SvPV_nolen_const SvPV_nolen
15 #endif
16
17 #ifndef SvPVX_const
18 # define SvPVX_const SvPVX
19 #endif
20
21 /* ... Hints ............................................................... */
22
23 STATIC U32 indirect_hash = 0;
24
25 STATIC UV indirect_hint(pTHX) {
26 #define indirect_hint() indirect_hint(aTHX)
27  SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
28                                          NULL,
29                                          "indirect", 8,
30                                          0,
31                                          indirect_hash);
32  return SvOK(id) ? SvUV(id) : 0;
33 }
34
35 /* ... op -> source position ............................................... */
36
37 STATIC HV *indirect_map = NULL;
38 STATIC const char *indirect_linestr = NULL;
39
40 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
41 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
42  char buf[32];
43  const char *pl_linestr;
44  SV *val;
45
46  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
47   * In this case the linestr has temporarly changed, but the old buffer should
48   * still be alive somewhere. */
49
50  if (!PL_parser->lex_inwhat) {
51   pl_linestr = SvPVX_const(PL_parser->linestr);
52   if (indirect_linestr != pl_linestr) {
53    hv_clear(indirect_map);
54    indirect_linestr = pl_linestr;
55   }
56  }
57
58  val = newSVsv(sv);
59  SvUPGRADE(val, SVt_PVIV);
60  SvUVX(val) = PTR2UV(src);
61  if (!hv_store(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), val, 0))
62   SvREFCNT_dec(val);
63 }
64
65 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
66 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
67  char buf[32];
68  SV **val;
69
70  if (indirect_linestr != SvPVX_const(PL_parser->linestr))
71   return NULL;
72
73  val = hv_fetch(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), 0);
74  if (!val) {
75   *name = NULL;
76   return NULL;
77  }
78
79  *name = *val;
80  return INT2PTR(const char *, SvUVX(*val));
81 }
82
83 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
84 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
85  STRLEN len;
86  const char *p = NULL, *r = SvPV_const(sv, len);
87
88  if (!len)
89   return s;
90
91  p = strstr(s, r);
92  while (p) {
93   p += len;
94   if (!isALNUM(*p))
95    break;
96   p = strstr(p + 1, r);
97  }
98
99  return p;
100 }
101
102 /* ... ck_const ............................................................ */
103
104 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
105
106 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
107  if (indirect_hint()) {
108   SV *sv = cSVOPo_sv;
109   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
110    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
111  }
112
113  return CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
114 }
115
116 /* ... ck_rv2sv ............................................................ */
117
118 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
119
120 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
121  if (indirect_hint()) {
122   OP *op = cUNOPo->op_first;
123   SV *name = cSVOPx_sv(op);
124   if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
125    SV *sv = sv_2mortal(newSVpvn("$", 1));
126    sv_catsv(sv, name);
127    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
128   }
129  }
130
131  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
132 }
133
134 /* ... ck_padany ........................................................... */
135
136 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
137
138 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
139  if (indirect_hint()) {
140   SV *sv;
141   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
142
143   while (s < t && isSPACE(*s)) ++s;
144   while (t > s && isSPACE(*t)) --t;
145   sv = sv_2mortal(newSVpvn(s, t - s + 1));
146
147   indirect_map_store(o, s, sv);
148  }
149
150  return CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
151 }
152
153 /* ... ck_method ........................................................... */
154
155 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
156
157 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
158  if (indirect_hint()) {
159   OP *op = cUNOPo->op_first;
160   SV *sv;
161   const char *s = indirect_map_fetch(op, &sv);
162   if (!s) {
163    sv = cSVOPx_sv(op);
164    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
165     goto done;
166    sv = sv_mortalcopy(sv);
167    s  = indirect_find(sv, PL_parser->oldbufptr);
168   }
169   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
170   /* o may now be a method_named */
171   indirect_map_store(o, s, sv);
172   return o;
173  }
174
175 done:
176  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
177 }
178
179 /* ... ck_entersub ......................................................... */
180
181 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
182
183 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
184
185 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
186  LISTOP *op;
187  OP *om, *oo;
188  UV hint = indirect_hint();
189
190  if (hint) {
191   const char *pm, *po;
192   SV *svm, *svo;
193   op = (LISTOP *) o;
194   while (op->op_type != OP_PUSHMARK)
195    op = (LISTOP *) op->op_first;
196   oo = op->op_sibling;
197   om = oo;
198   while (om->op_sibling)
199    om = om->op_sibling;
200   if (om->op_type == OP_METHOD)
201    om = cUNOPx(om)->op_first;
202   else if (om->op_type != OP_METHOD_NAMED)
203    goto done;
204   pm = indirect_map_fetch(om, &svm);
205   po = indirect_map_fetch(oo, &svo);
206   if (pm && po && pm < po)
207    ((hint == 2) ? croak : warn)(indirect_msg, SvPV_nolen_const(svm),
208                                               SvPV_nolen_const(svo));
209  }
210
211 done:
212  return CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
213 }
214
215 STATIC U32 indirect_initialized = 0;
216
217 /* --- XS ------------------------------------------------------------------ */
218
219 MODULE = indirect      PACKAGE = indirect
220
221 PROTOTYPES: DISABLE
222
223 BOOT:
224 {
225  if (!indirect_initialized++) {
226   PERL_HASH(indirect_hash, "indirect", 8);
227   indirect_map = newHV();
228   indirect_old_ck_const    = PL_check[OP_CONST];
229   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
230   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
231   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
232   indirect_old_ck_padany   = PL_check[OP_PADANY];
233   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
234   indirect_old_ck_method   = PL_check[OP_METHOD];
235   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
236   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
237   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
238  }
239 }