1 /* This file is part of the indirect Perl module.
2 * See http://search.cpan.org/dist/indirect/ */
4 #define PERL_NO_GET_CONTEXT
10 # define SvPV_const SvPV
13 #ifndef SvPV_nolen_const
14 # define SvPV_nolen_const SvPV_nolen
18 # define SvPVX_const SvPVX
21 /* ... Hints ............................................................... */
23 STATIC U32 indirect_hash = 0;
25 STATIC UV indirect_hint(pTHX) {
26 #define indirect_hint() indirect_hint(aTHX)
27 SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
32 return SvOK(id) ? SvUV(id) : 0;
35 /* ... op -> source position ............................................... */
37 STATIC HV *indirect_map = NULL;
38 STATIC const char *indirect_linestr = NULL;
40 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
41 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
43 const char *pl_linestr;
46 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
47 * In this case the linestr has temporarly changed, but the old buffer should
48 * still be alive somewhere. */
50 if (!PL_parser->lex_inwhat) {
51 pl_linestr = SvPVX_const(PL_parser->linestr);
52 if (indirect_linestr != pl_linestr) {
53 hv_clear(indirect_map);
54 indirect_linestr = pl_linestr;
59 SvUPGRADE(val, SVt_PVIV);
60 SvUVX(val) = PTR2UV(src);
61 if (!hv_store(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), val, 0))
65 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
66 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
70 if (indirect_linestr != SvPVX_const(PL_parser->linestr))
73 val = hv_fetch(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), 0);
80 return INT2PTR(const char *, SvUVX(*val));
83 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
84 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
86 const char *p = NULL, *r = SvPV_const(sv, len);
102 /* ... ck_const ............................................................ */
104 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
106 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
107 if (indirect_hint()) {
109 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
110 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
113 return CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
116 /* ... ck_rv2sv ............................................................ */
118 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
120 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
121 if (indirect_hint()) {
122 OP *op = cUNOPo->op_first;
123 SV *name = cSVOPx_sv(op);
124 if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
125 SV *sv = sv_2mortal(newSVpvn("$", 1));
127 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
131 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
134 /* ... ck_padany ........................................................... */
136 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
138 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
139 if (indirect_hint()) {
141 const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
143 while (s < t && isSPACE(*s)) ++s;
144 while (t > s && isSPACE(*t)) --t;
145 sv = sv_2mortal(newSVpvn(s, t - s + 1));
147 indirect_map_store(o, s, sv);
150 return CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
153 /* ... ck_method ........................................................... */
155 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
157 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
158 if (indirect_hint()) {
159 OP *op = cUNOPo->op_first;
161 const char *s = indirect_map_fetch(op, &sv);
164 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
166 sv = sv_mortalcopy(sv);
167 s = indirect_find(sv, PL_parser->oldbufptr);
169 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
170 /* o may now be a method_named */
171 indirect_map_store(o, s, sv);
176 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
179 /* ... ck_entersub ......................................................... */
181 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
183 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
185 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
188 UV hint = indirect_hint();
194 while (op->op_type != OP_PUSHMARK)
195 op = (LISTOP *) op->op_first;
198 while (om->op_sibling)
200 if (om->op_type == OP_METHOD)
201 om = cUNOPx(om)->op_first;
202 else if (om->op_type != OP_METHOD_NAMED)
204 pm = indirect_map_fetch(om, &svm);
205 po = indirect_map_fetch(oo, &svo);
206 if (pm && po && pm < po)
207 ((hint == 2) ? croak : warn)(indirect_msg, SvPV_nolen_const(svm),
208 SvPV_nolen_const(svo));
212 return CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
215 STATIC U32 indirect_initialized = 0;
217 /* --- XS ------------------------------------------------------------------ */
219 MODULE = indirect PACKAGE = indirect
225 if (!indirect_initialized++) {
226 PERL_HASH(indirect_hash, "indirect", 8);
227 indirect_map = newHV();
228 indirect_old_ck_const = PL_check[OP_CONST];
229 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
230 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
231 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
232 indirect_old_ck_padany = PL_check[OP_PADANY];
233 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
234 indirect_old_ck_method = PL_check[OP_METHOD];
235 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
236 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
237 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);