]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Cleanup indirect_map_clean() recursion
[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 #ifndef sv_catpvn_nomg
22 # define sv_catpvn_nomg sv_catpvn
23 #endif
24
25 #ifndef HvNAME_get
26 # define HvNAME_get(H) HvNAME(H)
27 #endif
28
29 #ifndef HvNAMELEN_get
30 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
31 #endif
32
33 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
34
35 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
36 # ifndef PL_lex_inwhat
37 #  define PL_lex_inwhat PL_parser->lex_inwhat
38 # endif
39 # ifndef PL_linestr
40 #  define PL_linestr PL_parser->linestr
41 # endif
42 # ifndef PL_bufptr
43 #  define PL_bufptr PL_parser->bufptr
44 # endif
45 # ifndef PL_oldbufptr
46 #  define PL_oldbufptr PL_parser->oldbufptr
47 # endif
48 #else
49 # ifndef PL_lex_inwhat
50 #  define PL_lex_inwhat PL_Ilex_inwhat
51 # endif
52 # ifndef PL_linestr
53 #  define PL_linestr PL_Ilinestr
54 # endif
55 # ifndef PL_bufptr
56 #  define PL_bufptr PL_Ibufptr
57 # endif
58 # ifndef PL_oldbufptr
59 #  define PL_oldbufptr PL_Ioldbufptr
60 # endif
61 #endif
62
63 /* ... Hints ............................................................... */
64
65 STATIC U32 indirect_hash = 0;
66
67 STATIC IV indirect_hint(pTHX) {
68 #define indirect_hint() indirect_hint(aTHX)
69  SV *id;
70 #if I_HAS_PERL(5, 10, 0)
71  id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
72                                      NULL,
73                                      "indirect", 8,
74                                      0,
75                                      indirect_hash);
76 #else
77  SV **val = hv_fetch(GvHV(PL_hintgv), "indirect", 8, indirect_hash);
78  if (!val)
79   return 0;
80  id = *val;
81 #endif
82  return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
83 }
84
85 /* ... op -> source position ............................................... */
86
87 STATIC HV *indirect_map = NULL;
88 STATIC const char *indirect_linestr = NULL;
89
90 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
91
92 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
93 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
94  char buf[32];
95  const char *pl_linestr;
96  SV *val;
97
98  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
99   * In this case the linestr has temporarly changed, but the old buffer should
100   * still be alive somewhere. */
101
102  if (!PL_lex_inwhat) {
103   pl_linestr = SvPVX_const(PL_linestr);
104   if (indirect_linestr != pl_linestr) {
105    hv_clear(indirect_map);
106    indirect_linestr = pl_linestr;
107   }
108  }
109
110  val = newSVsv(sv);
111  SvUPGRADE(val, SVt_PVIV);
112  SvUVX(val) = PTR2UV(src);
113  SvIOK_on(val);
114  SvIsUV_on(val);
115  if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
116 }
117
118 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
119 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
120  char buf[32];
121  SV **val;
122
123  if (indirect_linestr != SvPVX_const(PL_linestr))
124   return NULL;
125
126  val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
127  if (!val) {
128   *name = NULL;
129   return NULL;
130  }
131
132  *name = *val;
133  return INT2PTR(const char *, SvUVX(*val));
134 }
135
136 STATIC void indirect_map_delete(pTHX_ const OP *o) {
137 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
138  char buf[32];
139
140  hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
141 }
142
143 STATIC void indirect_map_clean_kids(pTHX_ const OP *o) {
144 #define indirect_map_clean_kids(O) indirect_map_clean_kids(aTHX_ (O))
145  if (o->op_flags & OPf_KIDS) {
146   const OP *kid = cUNOPo->op_first;
147   for (; kid; kid = kid->op_sibling) {
148    indirect_map_clean_kids(kid);
149    indirect_map_delete(kid);
150   }
151  }
152 }
153
154 STATIC void indirect_map_clean(pTHX_ const OP *o) {
155 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
156  indirect_map_clean_kids(o);
157  indirect_map_delete(o);
158 }
159
160 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
161 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
162  STRLEN len;
163  const char *p = NULL, *r = SvPV_const(sv, len);
164
165  if (len >= 1 && *r == '$') {
166   ++r;
167   --len;
168   s = strchr(s, '$');
169   if (!s)
170    return NULL;
171  }
172
173  p = strstr(s, r);
174  while (p) {
175   p += len;
176   if (!isALNUM(*p))
177    break;
178   p = strstr(p + 1, r);
179  }
180
181  return p;
182 }
183
184 /* ... ck_const ............................................................ */
185
186 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
187
188 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
189  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
190
191  if (indirect_hint()) {
192   SV *sv = cSVOPo_sv;
193   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
194    indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
195  }
196
197  return o;
198 }
199
200 /* ... ck_rv2sv ............................................................ */
201
202 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
203
204 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
205  if (indirect_hint()) {
206   OP *op = cUNOPo->op_first;
207   SV *sv;
208   const char *name = NULL, *s;
209   STRLEN len;
210   OPCODE type = op->op_type;
211
212   switch (type) {
213    case OP_GV:
214    case OP_GVSV: {
215     GV *gv = cGVOPx_gv(op);
216     name = GvNAME(gv);
217     len  = GvNAMELEN(gv);
218     break;
219    }
220    default:
221     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
222      SV *sv = cSVOPx_sv(op);
223      if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
224       name = SvPV_const(sv, len);
225     }
226   }
227   if (!name)
228    goto done;
229
230   sv = sv_2mortal(newSVpvn("$", 1));
231   sv_catpvn_nomg(sv, name, len);
232   s = indirect_find(sv, PL_oldbufptr);
233   if (!s) { /* If it failed, retry without the current stash */
234    const char *stash = HvNAME_get(PL_curstash);
235    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
236
237    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
238        || name[stashlen] != ':' || name[stashlen+1] != ':') {
239     /* Failed again ? Try to remove main */
240     stash = "main";
241     stashlen = 4;
242     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
243         || name[stashlen] != ':' || name[stashlen+1] != ':')
244      goto done;
245    }
246
247    sv_setpvn(sv, "$", 1);
248    stashlen += 2;
249    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
250    s = indirect_find(sv, PL_oldbufptr);
251    if (!s)
252     goto done;
253   }
254
255   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
256   indirect_map_store(o, s, sv);
257   return o;
258  }
259
260 done:
261  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
262 }
263
264 /* ... ck_padany ........................................................... */
265
266 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
267
268 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
269  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
270
271  if (indirect_hint()) {
272   SV *sv;
273   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
274
275   while (s < t && isSPACE(*s)) ++s;
276   if (*s == '$' && ++s <= t) {
277    while (s < t && isSPACE(*s)) ++s;
278    while (s < t && isSPACE(*t)) --t;
279    sv = sv_2mortal(newSVpvn("$", 1));
280    sv_catpvn_nomg(sv, s, t - s + 1);
281    indirect_map_store(o, s, sv);
282   }
283  }
284
285  return o;
286 }
287
288 /* ... ck_method ........................................................... */
289
290 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
291
292 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
293  if (indirect_hint()) {
294   OP *op = cUNOPo->op_first;
295   SV *sv;
296   const char *s = indirect_map_fetch(op, &sv);
297   if (!s) {
298    sv = cSVOPx_sv(op);
299    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
300     goto done;
301    sv = sv_mortalcopy(sv);
302    s  = indirect_find(sv, PL_oldbufptr);
303   }
304   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
305   /* o may now be a method_named */
306   indirect_map_store(o, s, sv);
307   return o;
308  }
309
310 done:
311  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
312 }
313
314 /* ... ck_entersub ......................................................... */
315
316 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
317
318 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
319
320 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
321  LISTOP *op;
322  OP *om, *oo;
323  IV hint = indirect_hint();
324
325  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
326
327  if (hint) {
328   const char *pm, *po;
329   SV *svm, *svo;
330   oo = o;
331   do {
332    op = (LISTOP *) oo;
333    if (!op->op_flags & OPf_KIDS)
334     goto done;
335    oo = op->op_first;
336   } while (oo->op_type != OP_PUSHMARK);
337   oo = oo->op_sibling;
338   om = op->op_last;
339   if (om->op_type == OP_METHOD)
340    om = cUNOPx(om)->op_first;
341   else if (om->op_type != OP_METHOD_NAMED)
342    goto done;
343   pm = indirect_map_fetch(om, &svm);
344   po = indirect_map_fetch(oo, &svo);
345   if (pm && po && pm < po) {
346    const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
347    if (hint == 2)
348     croak(indirect_msg, psvm, psvo);
349    else
350     warn(indirect_msg, psvm, psvo);
351   }
352 done:
353   indirect_map_clean(o);
354  }
355
356  return o;
357 }
358
359 STATIC U32 indirect_initialized = 0;
360
361 /* --- XS ------------------------------------------------------------------ */
362
363 MODULE = indirect      PACKAGE = indirect
364
365 PROTOTYPES: DISABLE
366
367 BOOT:
368 {
369  if (!indirect_initialized++) {
370   PERL_HASH(indirect_hash, "indirect", 8);
371   indirect_map = newHV();
372   indirect_old_ck_const    = PL_check[OP_CONST];
373   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
374   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
375   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
376   indirect_old_ck_padany   = PL_check[OP_PADANY];
377   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
378   indirect_old_ck_method   = PL_check[OP_METHOD];
379   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
380   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
381   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
382  }
383 }