]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Add support and tests for package variables
[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  if (!len)
117   return s;
118
119  p = strstr(s, r);
120  while (p) {
121   p += len;
122   if (!isALNUM(*p))
123    break;
124   p = strstr(p + 1, r);
125  }
126
127  return p;
128 }
129
130 /* ... ck_const ............................................................ */
131
132 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
133
134 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
135  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
136
137  if (indirect_hint()) {
138   SV *sv = cSVOPo_sv;
139   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
140    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
141  }
142
143  return o;
144 }
145
146 /* ... ck_rv2sv ............................................................ */
147
148 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
149
150 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
151  if (indirect_hint()) {
152   OP *op = cUNOPo->op_first;
153   const char *name = NULL;
154   STRLEN len;
155   switch (op->op_type) {
156    case OP_GV:
157    case OP_GVSV: {
158     GV *gv = cGVOPx_gv(op);
159     name = GvNAME(gv);
160     len  = GvNAMELEN(gv);
161     break;
162    }
163    default: {
164     SV *sv = cSVOPx_sv(op);
165     if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
166      name = SvPV_const(sv, len);
167     break;
168    }
169   }
170   if (name) {
171    SV *sv = sv_2mortal(newSVpvn("$", 1));
172    sv_catpvn_nomg(sv, name, len);
173    const char *s;
174    s = indirect_find(sv, PL_parser->oldbufptr);
175    if (!s) { /* If it failed, retry without the current stash */
176     const char *stash = HvNAME_get(PL_curstash);
177     STRLEN stashlen = HvNAMELEN_get(PL_curstash);
178     if ((len < stashlen + 2) || name != strstr(name, stash)) {
179      /* Failed again ? Try to remove main */
180      stash = "main";
181      stashlen = 4;
182      if ((len < stashlen + 2) || name != strstr(name, stash))
183       goto done;
184     }
185     sv_setpvn(sv, "$", 1);
186     if (name[stashlen] != ':' || name[stashlen+1] != ':')
187      goto done;
188     stashlen += 2;
189     sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
190     s = indirect_find(sv, PL_parser->oldbufptr);
191     if (!s)
192      goto done;
193    }
194    o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
195    indirect_map_store(o, s, sv);
196    return o;
197   }
198  }
199
200 done:
201  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
202 }
203
204 /* ... ck_padany ........................................................... */
205
206 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
207
208 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
209  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
210
211  if (indirect_hint()) {
212   SV *sv;
213   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
214
215   while (s < t && isSPACE(*s)) ++s;
216   while (t > s && isSPACE(*t)) --t;
217   sv = sv_2mortal(newSVpvn(s, t - s + 1));
218
219   indirect_map_store(o, s, sv);
220  }
221
222  return o;
223 }
224
225 /* ... ck_method ........................................................... */
226
227 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
228
229 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
230  if (indirect_hint()) {
231   OP *op = cUNOPo->op_first;
232   SV *sv;
233   const char *s = indirect_map_fetch(op, &sv);
234   if (!s) {
235    sv = cSVOPx_sv(op);
236    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
237     goto done;
238    sv = sv_mortalcopy(sv);
239    s  = indirect_find(sv, PL_parser->oldbufptr);
240   }
241   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
242   /* o may now be a method_named */
243   indirect_map_store(o, s, sv);
244   return o;
245  }
246
247 done:
248  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
249 }
250
251 /* ... ck_entersub ......................................................... */
252
253 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
254
255 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
256
257 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
258  LISTOP *op;
259  OP *om, *oo;
260  IV hint = indirect_hint();
261
262  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
263
264  if (hint) {
265   const char *pm, *po;
266   SV *svm, *svo;
267   oo = o;
268   do {
269    op = (LISTOP *) oo;
270    if (!op->op_flags & OPf_KIDS)
271     goto done;
272    oo = op->op_first;
273   } while (oo->op_type != OP_PUSHMARK);
274   oo = oo->op_sibling;
275   om = op->op_last;
276   if (om->op_type == OP_METHOD)
277    om = cUNOPx(om)->op_first;
278   else if (om->op_type != OP_METHOD_NAMED)
279    goto done;
280   pm = indirect_map_fetch(om, &svm);
281   po = indirect_map_fetch(oo, &svo);
282   if (pm && po && pm < po) {
283    const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
284    if (hint == 2)
285     croak(indirect_msg, psvm, psvo);
286    else
287     warn(indirect_msg, psvm, psvo);
288   }
289 done:
290   indirect_map_clean(o);
291  }
292
293  return o;
294 }
295
296 STATIC U32 indirect_initialized = 0;
297
298 /* --- XS ------------------------------------------------------------------ */
299
300 MODULE = indirect      PACKAGE = indirect
301
302 PROTOTYPES: DISABLE
303
304 BOOT:
305 {
306  if (!indirect_initialized++) {
307   PERL_HASH(indirect_hash, "indirect", 8);
308   indirect_map = newHV();
309   indirect_old_ck_const    = PL_check[OP_CONST];
310   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
311   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
312   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
313   indirect_old_ck_padany   = PL_check[OP_PADANY];
314   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
315   indirect_old_ck_method   = PL_check[OP_METHOD];
316   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
317   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
318   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
319  }
320 }