]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Importing indirect-0.01.tar.gz
[perl/modules/indirect.git] / indirect.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 #ifndef SvPVX_const
7 # define SvPVX_const SvPVX
8 #endif
9
10 STATIC U32 indirect_initialized = 0;
11 STATIC U32 indirect_hash = 0;
12
13 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
14
15 STATIC HV *indirect_map = NULL;
16 STATIC const char *indirect_linestr = NULL;
17
18 STATIC UV indirect_hint(pTHX) {
19 #define indirect_hint() indirect_hint(aTHX)
20  SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
21                                          NULL,
22                                          "indirect", 8,
23                                          0,
24                                          indirect_hash);
25  return SvOK(id) ? SvUV(id) : 0;
26 }
27
28 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
29 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
30  char buf[32];
31  const char *pl_linestr;
32  SV *val;
33
34  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
35   * In this case the linestr has temporarly changed, but the old buffer should
36   * still be alive somewhere. */
37
38  if (!PL_parser->lex_inwhat) {
39   pl_linestr = SvPVX_const(PL_parser->linestr);
40   if (indirect_linestr != pl_linestr) {
41    hv_clear(indirect_map);
42    indirect_linestr = pl_linestr;
43   }
44  }
45
46  val = newSVsv(sv);
47  SvUPGRADE(val, SVt_PVIV);
48  SvUVX(val) = PTR2UV(src);
49  if (!hv_store(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), val, 0))
50   SvREFCNT_dec(val);
51 }
52
53 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV **name) {
54 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
55  char buf[32];
56  SV **val;
57
58  if (indirect_linestr != SvPVX(PL_parser->linestr))
59   return NULL;
60
61  val = hv_fetch(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), 0);
62  if (!val) {
63   *name = NULL;
64   return NULL;
65  }
66
67  *name = *val;
68  return INT2PTR(const char *, SvUVX(*val));
69 }
70
71 STATIC UV indirect_intuit(const char *meth, const char *obj) {
72  const char *s;
73  int indirect = 0, quotelike = 0;
74
75  for (s = meth; s < obj; ++s) {
76   switch (*s) {
77    case ',':
78    case '(':
79    case '=':
80    case '\'':
81    case '"':
82    case '`':
83     return 0;
84    case '-':
85     indirect = 1;
86     break;
87    case '>':
88     if (indirect)
89      return 0;
90     break;
91    case 'q':
92     indirect = 0;
93     if (quotelike == 1)
94      quotelike = 2;
95     break;
96    case 'w':
97    case 'r':
98    case 'x':
99     indirect = 0;
100     if (quotelike != 2)
101      quotelike = 0;
102     break;
103    default:
104     indirect = 0;
105     if (isSPACE(*s))
106      quotelike = 1;
107     else if (quotelike == 2 && !isALNUM(*s))
108      return 0;
109     else
110      quotelike = 0;
111   }
112  }
113
114  return 1;
115 }
116
117 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
118 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
119  STRLEN len;
120  const char *p = NULL, *r = SvPV_const(sv, len);
121
122  if (!len)
123   return s;
124
125  p = strstr(s, r);
126  while (p) {
127   p += len;
128   if (!isALNUM(*p))
129    break;
130   p = strstr(p + 1, r);
131  }
132
133  return p;
134 }
135
136 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
137
138 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
139  if (indirect_hint()) {
140   SV *sv = cSVOPo_sv;
141   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
142    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
143  }
144
145  return CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
146 }
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   SV *name = cSVOPx_sv(op);
154   if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
155    SV *sv = sv_2mortal(newSVpvn("$", 1));
156    sv_catsv(sv, name);
157    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
158   }
159  }
160
161  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
162 }
163
164 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
165
166 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
167  if (indirect_hint()) {
168   SV *sv;
169   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
170
171   while (s < t && isSPACE(*s)) ++s;
172   while (t > s && isSPACE(*t)) --t;
173   sv = sv_2mortal(newSVpvn(s, t - s + 1));
174
175   indirect_map_store(o, s, sv);
176  }
177
178  return CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
179 }
180
181 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
182
183 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
184  if (indirect_hint()) {
185   OP *op = cUNOPo->op_first;
186   SV *sv;
187   const char *s = indirect_map_fetch(op, &sv);
188   if (!s) {
189    sv = cSVOPx_sv(op);
190    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
191     goto done;
192    sv = sv_mortalcopy(sv);
193    s  = indirect_find(sv, PL_parser->oldbufptr);
194   }
195   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
196   /* o may now be a method_named */
197   indirect_map_store(o, s, sv);
198   return o;
199  }
200
201 done:
202  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
203 }
204
205 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
206
207 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
208  LISTOP *op;
209  OP *om, *oo;
210  UV hint = indirect_hint();
211
212  if (hint) {
213   const char *pm, *po;
214   SV *svm, *svo;
215   op = (LISTOP *) o;
216   while (op->op_type != OP_PUSHMARK)
217    op = (LISTOP *) op->op_first;
218   oo = op->op_sibling;
219   om = oo;
220   while (om->op_sibling)
221    om = om->op_sibling;
222   if (om->op_type == OP_METHOD)
223    om = cUNOPx(om)->op_first;
224   pm = indirect_map_fetch(om, &svm);
225   po = indirect_map_fetch(oo, &svo);
226   if (pm && po && pm < po && indirect_intuit(pm, po))
227    ((hint == 2) ? croak : warn)(indirect_msg, SvPV_nolen(svm), SvPV_nolen(svo));
228  }
229
230  return CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
231 }
232
233 MODULE = indirect      PACKAGE = indirect
234
235 PROTOTYPES: DISABLE
236
237 BOOT:
238 {
239  if (!indirect_initialized++) {
240   PERL_HASH(indirect_hash, "indirect", 8);
241   indirect_map = newHV();
242   indirect_old_ck_const    = PL_check[OP_CONST];
243   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
244   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
245   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
246   indirect_old_ck_padany   = PL_check[OP_PADANY];
247   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
248   indirect_old_ck_method   = PL_check[OP_METHOD];
249   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
250   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
251   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
252  }
253 }