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