]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Better logic for finding oo and om in ck_entersub
[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 IV 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 (id && SvOK(id) && SvIOK(id)) ? SvIV(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  SvIOK_on(val);
62  SvIsUV_on(val);
63  if (!hv_store(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), val, 0))
64   SvREFCNT_dec(val);
65 }
66
67 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
68 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
69  char buf[32];
70  SV **val;
71
72  if (indirect_linestr != SvPVX_const(PL_parser->linestr))
73   return NULL;
74
75  val = hv_fetch(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), 0);
76  if (!val) {
77   *name = NULL;
78   return NULL;
79  }
80
81  *name = *val;
82  return INT2PTR(const char *, SvUVX(*val));
83 }
84
85 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
86 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
87  STRLEN len;
88  const char *p = NULL, *r = SvPV_const(sv, len);
89
90  if (!len)
91   return s;
92
93  p = strstr(s, r);
94  while (p) {
95   p += len;
96   if (!isALNUM(*p))
97    break;
98   p = strstr(p + 1, r);
99  }
100
101  return p;
102 }
103
104 /* ... ck_const ............................................................ */
105
106 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
107
108 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
109  if (indirect_hint()) {
110   SV *sv = cSVOPo_sv;
111   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
112    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
113  }
114
115  return CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
116 }
117
118 /* ... ck_rv2sv ............................................................ */
119
120 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
121
122 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
123  if (indirect_hint()) {
124   OP *op = cUNOPo->op_first;
125   SV *name = cSVOPx_sv(op);
126   if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
127    SV *sv = sv_2mortal(newSVpvn("$", 1));
128    sv_catsv(sv, name);
129    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
130   }
131  }
132
133  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
134 }
135
136 /* ... ck_padany ........................................................... */
137
138 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
139
140 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
141  if (indirect_hint()) {
142   SV *sv;
143   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
144
145   while (s < t && isSPACE(*s)) ++s;
146   while (t > s && isSPACE(*t)) --t;
147   sv = sv_2mortal(newSVpvn(s, t - s + 1));
148
149   indirect_map_store(o, s, sv);
150  }
151
152  return CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
153 }
154
155 /* ... ck_method ........................................................... */
156
157 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
158
159 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
160  if (indirect_hint()) {
161   OP *op = cUNOPo->op_first;
162   SV *sv;
163   const char *s = indirect_map_fetch(op, &sv);
164   if (!s) {
165    sv = cSVOPx_sv(op);
166    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
167     goto done;
168    sv = sv_mortalcopy(sv);
169    s  = indirect_find(sv, PL_parser->oldbufptr);
170   }
171   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
172   /* o may now be a method_named */
173   indirect_map_store(o, s, sv);
174   return o;
175  }
176
177 done:
178  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
179 }
180
181 /* ... ck_entersub ......................................................... */
182
183 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
184
185 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
186
187 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
188  LISTOP *op;
189  OP *om, *oo;
190  IV hint = indirect_hint();
191
192  if (hint) {
193   const char *pm, *po;
194   SV *svm, *svo;
195   oo = o;
196   do {
197    op = (LISTOP *) oo;
198    if (!op->op_flags & OPf_KIDS)
199     goto done;
200    oo = op->op_first;
201   } while (oo->op_type != OP_PUSHMARK);
202   oo = oo->op_sibling;
203   om = op->op_last;
204   if (om->op_type == OP_METHOD)
205    om = cUNOPx(om)->op_first;
206   else if (om->op_type != OP_METHOD_NAMED)
207    goto done;
208   pm = indirect_map_fetch(om, &svm);
209   po = indirect_map_fetch(oo, &svo);
210   if (pm && po && pm < po) {
211    const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
212    if (hint == 2)
213     croak(indirect_msg, psvm, psvo);
214    else
215     warn(indirect_msg, psvm, psvo);
216   }
217  }
218
219 done:
220  return CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
221 }
222
223 STATIC U32 indirect_initialized = 0;
224
225 /* --- XS ------------------------------------------------------------------ */
226
227 MODULE = indirect      PACKAGE = indirect
228
229 PROTOTYPES: DISABLE
230
231 BOOT:
232 {
233  if (!indirect_initialized++) {
234   PERL_HASH(indirect_hash, "indirect", 8);
235   indirect_map = newHV();
236   indirect_old_ck_const    = PL_check[OP_CONST];
237   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
238   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
239   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
240   indirect_old_ck_padany   = PL_check[OP_PADANY];
241   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
242   indirect_old_ck_method   = PL_check[OP_METHOD];
243   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
244   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
245   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
246  }
247 }