]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Support for perl 5.8
[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(pTHX_ const OP *o) {
144 #define indirect_map_clean(O) indirect_map_clean(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_delete(kid);
149    indirect_map_clean(kid);
150   }
151  } else {
152   indirect_map_delete(o);
153  }
154 }
155
156 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
157 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
158  STRLEN len;
159  const char *p = NULL, *r = SvPV_const(sv, len);
160
161  if (len >= 1 && *r == '$') {
162   ++r;
163   --len;
164   s = strchr(s, '$');
165   if (!s)
166    return NULL;
167  }
168
169  p = strstr(s, r);
170  while (p) {
171   p += len;
172   if (!isALNUM(*p))
173    break;
174   p = strstr(p + 1, r);
175  }
176
177  return p;
178 }
179
180 /* ... ck_const ............................................................ */
181
182 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
183
184 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
185  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
186
187  if (indirect_hint()) {
188   SV *sv = cSVOPo_sv;
189   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
190    indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
191  }
192
193  return o;
194 }
195
196 /* ... ck_rv2sv ............................................................ */
197
198 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
199
200 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
201  if (indirect_hint()) {
202   OP *op = cUNOPo->op_first;
203   SV *sv;
204   const char *name = NULL, *s;
205   STRLEN len;
206
207   switch (op->op_type) {
208    case OP_GV:
209    case OP_GVSV: {
210     GV *gv = cGVOPx_gv(op);
211     name = GvNAME(gv);
212     len  = GvNAMELEN(gv);
213     break;
214    }
215    default: {
216     SV *sv = cSVOPx_sv(op);
217     if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
218      name = SvPV_const(sv, len);
219     break;
220    }
221   }
222   if (!name)
223    goto done;
224
225   sv = sv_2mortal(newSVpvn("$", 1));
226   sv_catpvn_nomg(sv, name, len);
227   s = indirect_find(sv, PL_oldbufptr);
228   if (!s) { /* If it failed, retry without the current stash */
229    const char *stash = HvNAME_get(PL_curstash);
230    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
231
232    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
233        || name[stashlen] != ':' || name[stashlen+1] != ':') {
234     /* Failed again ? Try to remove main */
235     stash = "main";
236     stashlen = 4;
237     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
238         || name[stashlen] != ':' || name[stashlen+1] != ':')
239      goto done;
240    }
241
242    sv_setpvn(sv, "$", 1);
243    stashlen += 2;
244    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
245    s = indirect_find(sv, PL_oldbufptr);
246    if (!s)
247     goto done;
248   }
249
250   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
251   indirect_map_store(o, s, sv);
252   return o;
253  }
254
255 done:
256  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
257 }
258
259 /* ... ck_padany ........................................................... */
260
261 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
262
263 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
264  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
265
266  if (indirect_hint()) {
267   SV *sv;
268   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
269
270   while (s < t && isSPACE(*s)) ++s;
271   if (*s == '$' && ++s <= t) {
272    while (s < t && isSPACE(*s)) ++s;
273    while (s < t && isSPACE(*t)) --t;
274    sv = sv_2mortal(newSVpvn("$", 1));
275    sv_catpvn_nomg(sv, s, t - s + 1);
276    indirect_map_store(o, s, sv);
277   }
278  }
279
280  return o;
281 }
282
283 /* ... ck_method ........................................................... */
284
285 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
286
287 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
288  if (indirect_hint()) {
289   OP *op = cUNOPo->op_first;
290   SV *sv;
291   const char *s = indirect_map_fetch(op, &sv);
292   if (!s) {
293    sv = cSVOPx_sv(op);
294    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
295     goto done;
296    sv = sv_mortalcopy(sv);
297    s  = indirect_find(sv, PL_oldbufptr);
298   }
299   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
300   /* o may now be a method_named */
301   indirect_map_store(o, s, sv);
302   return o;
303  }
304
305 done:
306  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
307 }
308
309 /* ... ck_entersub ......................................................... */
310
311 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
312
313 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
314
315 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
316  LISTOP *op;
317  OP *om, *oo;
318  IV hint = indirect_hint();
319
320  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
321
322  if (hint) {
323   const char *pm, *po;
324   SV *svm, *svo;
325   oo = o;
326   do {
327    op = (LISTOP *) oo;
328    if (!op->op_flags & OPf_KIDS)
329     goto done;
330    oo = op->op_first;
331   } while (oo->op_type != OP_PUSHMARK);
332   oo = oo->op_sibling;
333   om = op->op_last;
334   if (om->op_type == OP_METHOD)
335    om = cUNOPx(om)->op_first;
336   else if (om->op_type != OP_METHOD_NAMED)
337    goto done;
338   pm = indirect_map_fetch(om, &svm);
339   po = indirect_map_fetch(oo, &svo);
340   if (pm && po && pm < po) {
341    const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
342    if (hint == 2)
343     croak(indirect_msg, psvm, psvo);
344    else
345     warn(indirect_msg, psvm, psvo);
346   }
347 done:
348   indirect_map_clean(o);
349  }
350
351  return o;
352 }
353
354 STATIC U32 indirect_initialized = 0;
355
356 /* --- XS ------------------------------------------------------------------ */
357
358 MODULE = indirect      PACKAGE = indirect
359
360 PROTOTYPES: DISABLE
361
362 BOOT:
363 {
364  if (!indirect_initialized++) {
365   PERL_HASH(indirect_hash, "indirect", 8);
366   indirect_map = newHV();
367   indirect_old_ck_const    = PL_check[OP_CONST];
368   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
369   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
370   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
371   indirect_old_ck_padany   = PL_check[OP_PADANY];
372   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
373   indirect_old_ck_method   = PL_check[OP_METHOD];
374   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
375   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
376   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
377  }
378 }