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(pTHX_ const OP *o) {
144 #define indirect_map_clean(O) indirect_map_clean(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_delete(kid);
149 indirect_map_clean(kid);
152 indirect_map_delete(o);
156 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
157 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
159 const char *p = NULL, *r = SvPV_const(sv, len);
161 if (len >= 1 && *r == '$') {
174 p = strstr(p + 1, r);
180 /* ... ck_const ............................................................ */
182 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
184 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
185 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
187 if (indirect_hint()) {
189 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
190 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
196 /* ... ck_rv2sv ............................................................ */
198 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
200 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
201 if (indirect_hint()) {
202 OP *op = cUNOPo->op_first;
204 const char *name = NULL, *s;
206 OPCODE type = op->op_type;
211 GV *gv = cGVOPx_gv(op);
217 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
218 SV *sv = cSVOPx_sv(op);
219 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
220 name = SvPV_const(sv, len);
226 sv = sv_2mortal(newSVpvn("$", 1));
227 sv_catpvn_nomg(sv, name, len);
228 s = indirect_find(sv, PL_oldbufptr);
229 if (!s) { /* If it failed, retry without the current stash */
230 const char *stash = HvNAME_get(PL_curstash);
231 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
233 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
234 || name[stashlen] != ':' || name[stashlen+1] != ':') {
235 /* Failed again ? Try to remove main */
238 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
239 || name[stashlen] != ':' || name[stashlen+1] != ':')
243 sv_setpvn(sv, "$", 1);
245 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
246 s = indirect_find(sv, PL_oldbufptr);
251 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
252 indirect_map_store(o, s, sv);
257 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
260 /* ... ck_padany ........................................................... */
262 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
264 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
265 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
267 if (indirect_hint()) {
269 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
271 while (s < t && isSPACE(*s)) ++s;
272 if (*s == '$' && ++s <= t) {
273 while (s < t && isSPACE(*s)) ++s;
274 while (s < t && isSPACE(*t)) --t;
275 sv = sv_2mortal(newSVpvn("$", 1));
276 sv_catpvn_nomg(sv, s, t - s + 1);
277 indirect_map_store(o, s, sv);
284 /* ... ck_method ........................................................... */
286 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
288 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
289 if (indirect_hint()) {
290 OP *op = cUNOPo->op_first;
292 const char *s = indirect_map_fetch(op, &sv);
295 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
297 sv = sv_mortalcopy(sv);
298 s = indirect_find(sv, PL_oldbufptr);
300 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
301 /* o may now be a method_named */
302 indirect_map_store(o, s, sv);
307 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
310 /* ... ck_entersub ......................................................... */
312 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
314 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
316 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
319 IV hint = indirect_hint();
321 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
329 if (!op->op_flags & OPf_KIDS)
332 } while (oo->op_type != OP_PUSHMARK);
335 if (om->op_type == OP_METHOD)
336 om = cUNOPx(om)->op_first;
337 else if (om->op_type != OP_METHOD_NAMED)
339 pm = indirect_map_fetch(om, &svm);
340 po = indirect_map_fetch(oo, &svo);
341 if (pm && po && pm < po) {
342 const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
344 croak(indirect_msg, psvm, psvo);
346 warn(indirect_msg, psvm, psvo);
349 indirect_map_clean(o);
355 STATIC U32 indirect_initialized = 0;
357 /* --- XS ------------------------------------------------------------------ */
359 MODULE = indirect PACKAGE = indirect
365 if (!indirect_initialized++) {
366 PERL_HASH(indirect_hash, "indirect", 8);
367 indirect_map = newHV();
368 indirect_old_ck_const = PL_check[OP_CONST];
369 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
370 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
371 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
372 indirect_old_ck_padany = PL_check[OP_PADANY];
373 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
374 indirect_old_ck_method = PL_check[OP_METHOD];
375 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
376 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
377 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);