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