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 IV 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 (id && SvOK(id) && SvIOK(id)) ? SvIV(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);
63 if (!hv_store(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), val, 0))
67 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
68 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
72 if (indirect_linestr != SvPVX_const(PL_parser->linestr))
75 val = hv_fetch(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), 0);
82 return INT2PTR(const char *, SvUVX(*val));
85 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
86 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
88 const char *p = NULL, *r = SvPV_const(sv, len);
104 /* ... ck_const ............................................................ */
106 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
108 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
109 if (indirect_hint()) {
111 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
112 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
115 return CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
118 /* ... ck_rv2sv ............................................................ */
120 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
122 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
123 if (indirect_hint()) {
124 OP *op = cUNOPo->op_first;
125 SV *name = cSVOPx_sv(op);
126 if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
127 SV *sv = sv_2mortal(newSVpvn("$", 1));
129 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
133 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
136 /* ... ck_padany ........................................................... */
138 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
140 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
141 if (indirect_hint()) {
143 const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
145 while (s < t && isSPACE(*s)) ++s;
146 while (t > s && isSPACE(*t)) --t;
147 sv = sv_2mortal(newSVpvn(s, t - s + 1));
149 indirect_map_store(o, s, sv);
152 return CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
155 /* ... ck_method ........................................................... */
157 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
159 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
160 if (indirect_hint()) {
161 OP *op = cUNOPo->op_first;
163 const char *s = indirect_map_fetch(op, &sv);
166 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
168 sv = sv_mortalcopy(sv);
169 s = indirect_find(sv, PL_parser->oldbufptr);
171 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
172 /* o may now be a method_named */
173 indirect_map_store(o, s, sv);
178 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
181 /* ... ck_entersub ......................................................... */
183 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
185 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
187 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
190 IV hint = indirect_hint();
198 if (!op->op_flags & OPf_KIDS)
201 } while (oo->op_type != OP_PUSHMARK);
204 if (om->op_type == OP_METHOD)
205 om = cUNOPx(om)->op_first;
206 else if (om->op_type != OP_METHOD_NAMED)
208 pm = indirect_map_fetch(om, &svm);
209 po = indirect_map_fetch(oo, &svo);
210 if (pm && po && pm < po) {
211 const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
213 croak(indirect_msg, psvm, psvo);
215 warn(indirect_msg, psvm, psvo);
220 return CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
223 STATIC U32 indirect_initialized = 0;
225 /* --- XS ------------------------------------------------------------------ */
227 MODULE = indirect PACKAGE = indirect
233 if (!indirect_initialized++) {
234 PERL_HASH(indirect_hash, "indirect", 8);
235 indirect_map = newHV();
236 indirect_old_ck_const = PL_check[OP_CONST];
237 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
238 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
239 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
240 indirect_old_ck_padany = PL_check[OP_PADANY];
241 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
242 indirect_old_ck_method = PL_check[OP_METHOD];
243 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
244 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
245 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);