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
26 # define HvNAME_get(H) HvNAME(H)
30 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
33 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
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
40 # define PL_linestr PL_parser->linestr
43 # define PL_bufptr PL_parser->bufptr
46 # define PL_oldbufptr PL_parser->oldbufptr
49 # ifndef PL_lex_inwhat
50 # define PL_lex_inwhat PL_Ilex_inwhat
53 # define PL_linestr PL_Ilinestr
56 # define PL_bufptr PL_Ibufptr
59 # define PL_oldbufptr PL_Ioldbufptr
63 /* ... Hints ............................................................... */
65 STATIC U32 indirect_hash = 0;
67 STATIC IV indirect_hint(pTHX) {
68 #define indirect_hint() indirect_hint(aTHX)
70 #if I_HAS_PERL(5, 10, 0)
71 id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
77 SV **val = hv_fetch(GvHV(PL_hintgv), "indirect", 8, indirect_hash);
82 return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
85 /* ... op -> source position ............................................... */
87 STATIC HV *indirect_map = NULL;
88 STATIC const char *indirect_linestr = NULL;
90 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
92 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
93 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
95 const char *pl_linestr;
98 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
99 * In this case the linestr has temporarly changed, but the old buffer should
100 * still be alive somewhere. */
102 if (!PL_lex_inwhat) {
103 pl_linestr = SvPVX_const(PL_linestr);
104 if (indirect_linestr != pl_linestr) {
105 hv_clear(indirect_map);
106 indirect_linestr = pl_linestr;
111 SvUPGRADE(val, SVt_PVIV);
112 SvUVX(val) = PTR2UV(src);
115 if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
118 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
119 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
123 if (indirect_linestr != SvPVX_const(PL_linestr))
126 val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
133 return INT2PTR(const char *, SvUVX(*val));
136 STATIC void indirect_map_delete(pTHX_ const OP *o) {
137 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
140 hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
143 STATIC void indirect_map_clean_kids(pTHX_ const OP *o) {
144 #define indirect_map_clean_kids(O) indirect_map_clean_kids(aTHX_ (O))
145 if (o->op_flags & OPf_KIDS) {
146 const OP *kid = cUNOPo->op_first;
147 for (; kid; kid = kid->op_sibling) {
148 indirect_map_clean_kids(kid);
149 indirect_map_delete(kid);
154 STATIC void indirect_map_clean(pTHX_ const OP *o) {
155 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
156 indirect_map_clean_kids(o);
157 indirect_map_delete(o);
160 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
161 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
163 const char *p = NULL, *r = SvPV_const(sv, len);
165 if (len >= 1 && *r == '$') {
178 p = strstr(p + 1, r);
184 /* ... ck_const ............................................................ */
186 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
188 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
189 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
191 if (indirect_hint()) {
193 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
194 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
200 /* ... ck_rv2sv ............................................................ */
202 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
204 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
205 if (indirect_hint()) {
206 OP *op = cUNOPo->op_first;
208 const char *name = NULL, *s;
210 OPCODE type = op->op_type;
215 GV *gv = cGVOPx_gv(op);
221 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
222 SV *sv = cSVOPx_sv(op);
223 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
224 name = SvPV_const(sv, len);
230 sv = sv_2mortal(newSVpvn("$", 1));
231 sv_catpvn_nomg(sv, name, len);
232 s = indirect_find(sv, PL_oldbufptr);
233 if (!s) { /* If it failed, retry without the current stash */
234 const char *stash = HvNAME_get(PL_curstash);
235 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
237 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
238 || name[stashlen] != ':' || name[stashlen+1] != ':') {
239 /* Failed again ? Try to remove main */
242 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
243 || name[stashlen] != ':' || name[stashlen+1] != ':')
247 sv_setpvn(sv, "$", 1);
249 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
250 s = indirect_find(sv, PL_oldbufptr);
255 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
256 indirect_map_store(o, s, sv);
261 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
264 /* ... ck_padany ........................................................... */
266 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
268 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
269 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
271 if (indirect_hint()) {
273 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
275 while (s < t && isSPACE(*s)) ++s;
276 if (*s == '$' && ++s <= t) {
277 while (s < t && isSPACE(*s)) ++s;
278 while (s < t && isSPACE(*t)) --t;
279 sv = sv_2mortal(newSVpvn("$", 1));
280 sv_catpvn_nomg(sv, s, t - s + 1);
281 indirect_map_store(o, s, sv);
288 /* ... ck_method ........................................................... */
290 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
292 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
293 if (indirect_hint()) {
294 OP *op = cUNOPo->op_first;
296 const char *s = indirect_map_fetch(op, &sv);
299 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
301 sv = sv_mortalcopy(sv);
302 s = indirect_find(sv, PL_oldbufptr);
304 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
305 /* o may now be a method_named */
306 indirect_map_store(o, s, sv);
311 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
314 /* ... ck_entersub ......................................................... */
316 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
318 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
320 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
323 IV hint = indirect_hint();
325 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
333 if (!op->op_flags & OPf_KIDS)
336 } while (oo->op_type != OP_PUSHMARK);
339 if (om->op_type == OP_METHOD)
340 om = cUNOPx(om)->op_first;
341 else if (om->op_type != OP_METHOD_NAMED)
343 pm = indirect_map_fetch(om, &svm);
344 po = indirect_map_fetch(oo, &svo);
345 if (pm && po && pm < po) {
346 const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
348 croak(indirect_msg, psvm, psvo);
350 warn(indirect_msg, psvm, psvo);
353 indirect_map_clean(o);
359 STATIC U32 indirect_initialized = 0;
361 /* --- XS ------------------------------------------------------------------ */
363 MODULE = indirect PACKAGE = indirect
369 if (!indirect_initialized++) {
370 PERL_HASH(indirect_hash, "indirect", 8);
371 indirect_map = newHV();
372 indirect_old_ck_const = PL_check[OP_CONST];
373 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
374 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
375 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
376 indirect_old_ck_padany = PL_check[OP_PADANY];
377 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
378 indirect_old_ck_method = PL_check[OP_METHOD];
379 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
380 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
381 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);