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