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 #ifndef sv_catpvn_nomg
22 # define sv_catpvn_nomg sv_catpvn
25 /* ... Hints ............................................................... */
27 STATIC U32 indirect_hash = 0;
29 STATIC IV indirect_hint(pTHX) {
30 #define indirect_hint() indirect_hint(aTHX)
31 SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
36 return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
39 /* ... op -> source position ............................................... */
41 STATIC HV *indirect_map = NULL;
42 STATIC const char *indirect_linestr = NULL;
44 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
46 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
47 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
49 const char *pl_linestr;
52 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
53 * In this case the linestr has temporarly changed, but the old buffer should
54 * still be alive somewhere. */
56 if (!PL_parser->lex_inwhat) {
57 pl_linestr = SvPVX_const(PL_parser->linestr);
58 if (indirect_linestr != pl_linestr) {
59 hv_clear(indirect_map);
60 indirect_linestr = pl_linestr;
65 SvUPGRADE(val, SVt_PVIV);
66 SvUVX(val) = PTR2UV(src);
69 if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
72 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
73 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
77 if (indirect_linestr != SvPVX_const(PL_parser->linestr))
80 val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
87 return INT2PTR(const char *, SvUVX(*val));
90 STATIC void indirect_map_delete(pTHX_ const OP *o) {
91 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
94 hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
97 STATIC void indirect_map_clean(pTHX_ const OP *o) {
98 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
99 if (o->op_flags & OPf_KIDS) {
100 const OP *kid = cUNOPo->op_first;
101 for (; kid; kid = kid->op_sibling) {
102 indirect_map_delete(kid);
103 indirect_map_clean(kid);
106 indirect_map_delete(o);
110 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
111 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
113 const char *p = NULL, *r = SvPV_const(sv, len);
115 if (len >= 1 && *r == '$') {
128 p = strstr(p + 1, r);
134 /* ... ck_const ............................................................ */
136 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
138 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
139 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
141 if (indirect_hint()) {
143 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
144 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
150 /* ... ck_rv2sv ............................................................ */
152 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
154 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
155 if (indirect_hint()) {
156 OP *op = cUNOPo->op_first;
158 const char *name = NULL, *s;
161 switch (op->op_type) {
164 GV *gv = cGVOPx_gv(op);
170 SV *sv = cSVOPx_sv(op);
171 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
172 name = SvPV_const(sv, len);
179 sv = sv_2mortal(newSVpvn("$", 1));
180 sv_catpvn_nomg(sv, name, len);
181 s = indirect_find(sv, PL_parser->oldbufptr);
182 if (!s) { /* If it failed, retry without the current stash */
183 const char *stash = HvNAME_get(PL_curstash);
184 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
186 if ((len < stashlen + 2) || name != strstr(name, stash)) {
187 /* Failed again ? Try to remove main */
190 if ((len < stashlen + 2) || name != strstr(name, stash))
193 if (name[stashlen] != ':' || name[stashlen+1] != ':')
196 sv_setpvn(sv, "$", 1);
198 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
199 s = indirect_find(sv, PL_parser->oldbufptr);
204 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
205 indirect_map_store(o, s, sv);
210 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
213 /* ... ck_padany ........................................................... */
215 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
217 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
218 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
220 if (indirect_hint()) {
222 const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
224 while (s < t && isSPACE(*s)) ++s;
225 if (*s == '$' && ++s <= t) {
226 while (s < t && isSPACE(*s)) ++s;
227 while (s < t && isSPACE(*t)) --t;
228 sv = sv_2mortal(newSVpvn("$", 1));
229 sv_catpvn_nomg(sv, s, t - s + 1);
230 indirect_map_store(o, s, sv);
237 /* ... ck_method ........................................................... */
239 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
241 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
242 if (indirect_hint()) {
243 OP *op = cUNOPo->op_first;
245 const char *s = indirect_map_fetch(op, &sv);
248 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
250 sv = sv_mortalcopy(sv);
251 s = indirect_find(sv, PL_parser->oldbufptr);
253 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
254 /* o may now be a method_named */
255 indirect_map_store(o, s, sv);
260 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
263 /* ... ck_entersub ......................................................... */
265 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
267 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
269 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
272 IV hint = indirect_hint();
274 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
282 if (!op->op_flags & OPf_KIDS)
285 } while (oo->op_type != OP_PUSHMARK);
288 if (om->op_type == OP_METHOD)
289 om = cUNOPx(om)->op_first;
290 else if (om->op_type != OP_METHOD_NAMED)
292 pm = indirect_map_fetch(om, &svm);
293 po = indirect_map_fetch(oo, &svo);
294 if (pm && po && pm < po) {
295 const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
297 croak(indirect_msg, psvm, psvo);
299 warn(indirect_msg, psvm, psvo);
302 indirect_map_clean(o);
308 STATIC U32 indirect_initialized = 0;
310 /* --- XS ------------------------------------------------------------------ */
312 MODULE = indirect PACKAGE = indirect
318 if (!indirect_initialized++) {
319 PERL_HASH(indirect_hash, "indirect", 8);
320 indirect_map = newHV();
321 indirect_old_ck_const = PL_check[OP_CONST];
322 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
323 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
324 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
325 indirect_old_ck_padany = PL_check[OP_PADANY];
326 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
327 indirect_old_ck_method = PL_check[OP_METHOD];
328 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
329 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
330 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);