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