]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Tighten the scope of pl_linestr in indirect_map_store()
[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 /* We need (CHAR_BIT * sizeof(UV)) / 4 + 1 chars, but it's just better to take
91  * a power of two */
92 #define OP2STR_BUF char buf[(CHAR_BIT * sizeof(UV)) / 2]
93 #define OP2STR(O)  (sprintf(buf, "%"UVxf, PTR2UV(O)))
94
95 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
96 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
97  OP2STR_BUF;
98  SV *val;
99
100  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
101   * In this case the linestr has temporarly changed, but the old buffer should
102   * still be alive somewhere. */
103
104  if (!PL_lex_inwhat) {
105   const char *pl_linestr = SvPVX_const(PL_linestr);
106   if (indirect_linestr != pl_linestr) {
107    hv_clear(indirect_map);
108    indirect_linestr = pl_linestr;
109   }
110  }
111
112  val = newSVsv(sv);
113  SvUPGRADE(val, SVt_PVIV);
114  SvUVX(val) = PTR2UV(src);
115  SvIOK_on(val);
116  SvIsUV_on(val);
117  if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
118 }
119
120 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
121 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
122  OP2STR_BUF;
123  SV **val;
124
125  if (indirect_linestr != SvPVX_const(PL_linestr))
126   return NULL;
127
128  val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
129  if (!val) {
130   *name = NULL;
131   return NULL;
132  }
133
134  *name = *val;
135  return INT2PTR(const char *, SvUVX(*val));
136 }
137
138 STATIC void indirect_map_delete(pTHX_ const OP *o) {
139 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
140  OP2STR_BUF;
141
142  (void)hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
143 }
144
145 STATIC void indirect_map_clean_kids(pTHX_ const OP *o) {
146 #define indirect_map_clean_kids(O) indirect_map_clean_kids(aTHX_ (O))
147  if (o->op_flags & OPf_KIDS) {
148   const OP *kid = ((const UNOP *) o)->op_first;
149   for (; kid; kid = kid->op_sibling) {
150    indirect_map_clean_kids(kid);
151    indirect_map_delete(kid);
152   }
153  }
154 }
155
156 STATIC void indirect_map_clean(pTHX_ const OP *o) {
157 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
158  indirect_map_clean_kids(o);
159  indirect_map_delete(o);
160 }
161
162 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
163 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
164  STRLEN len;
165  const char *p = NULL, *r = SvPV_const(sv, len);
166
167  if (len >= 1 && *r == '$') {
168   ++r;
169   --len;
170   s = strchr(s, '$');
171   if (!s)
172    return NULL;
173  }
174
175  p = strstr(s, r);
176  while (p) {
177   p += len;
178   if (!isALNUM(*p))
179    break;
180   p = strstr(p + 1, r);
181  }
182
183  return p;
184 }
185
186 /* ... ck_const ............................................................ */
187
188 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
189
190 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
191  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
192
193  if (indirect_hint()) {
194   SV *sv = cSVOPo_sv;
195   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
196    indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
197  }
198
199  return o;
200 }
201
202 /* ... ck_rv2sv ............................................................ */
203
204 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
205
206 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
207  if (indirect_hint()) {
208   OP *op = cUNOPo->op_first;
209   SV *sv;
210   const char *name = NULL, *s;
211   STRLEN len;
212   OPCODE type = (OPCODE) op->op_type;
213
214   switch (type) {
215    case OP_GV:
216    case OP_GVSV: {
217     GV *gv = cGVOPx_gv(op);
218     name = GvNAME(gv);
219     len  = GvNAMELEN(gv);
220     break;
221    }
222    default:
223     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
224      SV *nsv = cSVOPx_sv(op);
225      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
226       name = SvPV_const(nsv, len);
227     }
228   }
229   if (!name)
230    goto done;
231
232   sv = sv_2mortal(newSVpvn("$", 1));
233   sv_catpvn_nomg(sv, name, len);
234   s = indirect_find(sv, PL_oldbufptr);
235   if (!s) { /* If it failed, retry without the current stash */
236    const char *stash = HvNAME_get(PL_curstash);
237    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
238
239    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
240        || name[stashlen] != ':' || name[stashlen+1] != ':') {
241     /* Failed again ? Try to remove main */
242     stash = "main";
243     stashlen = 4;
244     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
245         || name[stashlen] != ':' || name[stashlen+1] != ':')
246      goto done;
247    }
248
249    sv_setpvn(sv, "$", 1);
250    stashlen += 2;
251    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
252    s = indirect_find(sv, PL_oldbufptr);
253    if (!s)
254     goto done;
255   }
256
257   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
258   indirect_map_store(o, s, sv);
259   return o;
260  }
261
262 done:
263  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
264 }
265
266 /* ... ck_padany ........................................................... */
267
268 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
269
270 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
271  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
272
273  if (indirect_hint()) {
274   SV *sv;
275   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
276
277   while (s < t && isSPACE(*s)) ++s;
278   if (*s == '$' && ++s <= t) {
279    while (s < t && isSPACE(*s)) ++s;
280    while (s < t && isSPACE(*t)) --t;
281    sv = sv_2mortal(newSVpvn("$", 1));
282    sv_catpvn_nomg(sv, s, t - s + 1);
283    indirect_map_store(o, s, sv);
284   }
285  }
286
287  return o;
288 }
289
290 /* ... ck_method ........................................................... */
291
292 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
293
294 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
295  if (indirect_hint()) {
296   OP *op = cUNOPo->op_first;
297   SV *sv;
298   const char *s = indirect_map_fetch(op, &sv);
299   if (!s) {
300    sv = cSVOPx_sv(op);
301    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
302     goto done;
303    sv = sv_mortalcopy(sv);
304    s  = indirect_find(sv, PL_oldbufptr);
305   }
306   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
307   /* o may now be a method_named */
308   indirect_map_store(o, s, sv);
309   return o;
310  }
311
312 done:
313  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
314 }
315
316 /* ... ck_entersub ......................................................... */
317
318 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
319
320 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
321
322 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
323  IV hint = indirect_hint();
324
325  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
326
327  if (hint) {
328   const char *mpos, *opos;
329   SV *mnamesv, *onamesv;
330   OP *mop, *oop;
331   LISTOP *lop;
332
333   oop = o;
334   do {
335    lop = (LISTOP *) oop;
336    if (!(lop->op_flags & OPf_KIDS))
337     goto done;
338    oop = lop->op_first;
339   } while (oop->op_type != OP_PUSHMARK);
340   oop = oop->op_sibling;
341   mop = lop->op_last;
342
343   if (mop->op_type == OP_METHOD)
344    mop = cUNOPx(mop)->op_first;
345   else if (mop->op_type != OP_METHOD_NAMED)
346    goto done;
347
348   mpos = indirect_map_fetch(mop, &mnamesv);
349   if (!mpos)
350    goto done;
351
352   opos = indirect_map_fetch(oop, &onamesv);
353   if (!opos)
354    goto done;
355
356   if (mpos < opos) {
357    const char *mname = SvPV_nolen_const(mnamesv);
358    const char *oname = SvPV_nolen_const(onamesv);
359    if (hint == 2)
360     croak(indirect_msg, mname, oname);
361    else
362     warn(indirect_msg, mname, oname);
363   }
364
365 done:
366   indirect_map_clean(o);
367  }
368
369  return o;
370 }
371
372 STATIC U32 indirect_initialized = 0;
373
374 /* --- XS ------------------------------------------------------------------ */
375
376 MODULE = indirect      PACKAGE = indirect
377
378 PROTOTYPES: DISABLE
379
380 BOOT:
381 {
382  if (!indirect_initialized++) {
383   PERL_HASH(indirect_hash, "indirect", 8);
384   indirect_map = newHV();
385   indirect_old_ck_const    = PL_check[OP_CONST];
386   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
387   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
388   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
389   indirect_old_ck_padany   = PL_check[OP_PADANY];
390   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
391   indirect_old_ck_method   = PL_check[OP_METHOD];
392   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
393   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
394   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
395  }
396 }