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 /* We need (CHAR_BIT * sizeof(UV)) / 4 + 1 chars, but it's just better to take
92 #define OP2STR_BUF char buf[(CHAR_BIT * sizeof(UV)) / 2]
93 #define OP2STR(O) (sprintf(buf, "%"UVxf, PTR2UV(O)))
95 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
96 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
100 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
101 * In this case the linestr has temporarly changed, but the old buffer should
102 * still be alive somewhere. */
104 if (!PL_lex_inwhat) {
105 const char *pl_linestr = SvPVX_const(PL_linestr);
106 if (indirect_linestr != pl_linestr) {
107 hv_clear(indirect_map);
108 indirect_linestr = pl_linestr;
113 SvUPGRADE(val, SVt_PVIV);
114 SvUVX(val) = PTR2UV(src);
117 if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
120 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
121 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
125 if (indirect_linestr != SvPVX_const(PL_linestr))
128 val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
135 return INT2PTR(const char *, SvUVX(*val));
138 STATIC void indirect_map_delete(pTHX_ const OP *o) {
139 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
142 (void)hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
145 STATIC void indirect_map_clean_kids(pTHX_ const OP *o) {
146 #define indirect_map_clean_kids(O) indirect_map_clean_kids(aTHX_ (O))
147 if (o->op_flags & OPf_KIDS) {
148 const OP *kid = ((const UNOP *) o)->op_first;
149 for (; kid; kid = kid->op_sibling) {
150 indirect_map_clean_kids(kid);
151 indirect_map_delete(kid);
156 STATIC void indirect_map_clean(pTHX_ const OP *o) {
157 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
158 indirect_map_clean_kids(o);
159 indirect_map_delete(o);
162 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
163 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
165 const char *p = NULL, *r = SvPV_const(sv, len);
167 if (len >= 1 && *r == '$') {
180 p = strstr(p + 1, r);
186 /* ... ck_const ............................................................ */
188 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
190 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
191 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
193 if (indirect_hint()) {
195 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
196 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
202 /* ... ck_rv2sv ............................................................ */
204 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
206 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
207 if (indirect_hint()) {
208 OP *op = cUNOPo->op_first;
210 const char *name = NULL, *s;
212 OPCODE type = (OPCODE) op->op_type;
217 GV *gv = cGVOPx_gv(op);
223 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
224 SV *nsv = cSVOPx_sv(op);
225 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
226 name = SvPV_const(nsv, len);
232 sv = sv_2mortal(newSVpvn("$", 1));
233 sv_catpvn_nomg(sv, name, len);
234 s = indirect_find(sv, PL_oldbufptr);
235 if (!s) { /* If it failed, retry without the current stash */
236 const char *stash = HvNAME_get(PL_curstash);
237 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
239 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
240 || name[stashlen] != ':' || name[stashlen+1] != ':') {
241 /* Failed again ? Try to remove main */
244 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
245 || name[stashlen] != ':' || name[stashlen+1] != ':')
249 sv_setpvn(sv, "$", 1);
251 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
252 s = indirect_find(sv, PL_oldbufptr);
257 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
258 indirect_map_store(o, s, sv);
263 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
266 /* ... ck_padany ........................................................... */
268 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
270 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
271 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
273 if (indirect_hint()) {
275 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
277 while (s < t && isSPACE(*s)) ++s;
278 if (*s == '$' && ++s <= t) {
279 while (s < t && isSPACE(*s)) ++s;
280 while (s < t && isSPACE(*t)) --t;
281 sv = sv_2mortal(newSVpvn("$", 1));
282 sv_catpvn_nomg(sv, s, t - s + 1);
283 indirect_map_store(o, s, sv);
290 /* ... ck_method ........................................................... */
292 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
294 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
295 if (indirect_hint()) {
296 OP *op = cUNOPo->op_first;
298 const char *s = indirect_map_fetch(op, &sv);
301 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
303 sv = sv_mortalcopy(sv);
304 s = indirect_find(sv, PL_oldbufptr);
306 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
307 /* o may now be a method_named */
308 indirect_map_store(o, s, sv);
313 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
316 /* ... ck_entersub ......................................................... */
318 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
320 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
322 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
323 IV hint = indirect_hint();
325 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
328 const char *mpos, *opos;
329 SV *mnamesv, *onamesv;
335 lop = (LISTOP *) oop;
336 if (!(lop->op_flags & OPf_KIDS))
339 } while (oop->op_type != OP_PUSHMARK);
340 oop = oop->op_sibling;
343 if (mop->op_type == OP_METHOD)
344 mop = cUNOPx(mop)->op_first;
345 else if (mop->op_type != OP_METHOD_NAMED)
348 mpos = indirect_map_fetch(mop, &mnamesv);
352 opos = indirect_map_fetch(oop, &onamesv);
357 const char *mname = SvPV_nolen_const(mnamesv);
358 const char *oname = SvPV_nolen_const(onamesv);
360 croak(indirect_msg, mname, oname);
362 warn(indirect_msg, mname, oname);
366 indirect_map_clean(o);
372 STATIC U32 indirect_initialized = 0;
374 /* --- XS ------------------------------------------------------------------ */
376 MODULE = indirect PACKAGE = indirect
382 if (!indirect_initialized++) {
383 PERL_HASH(indirect_hash, "indirect", 8);
384 indirect_map = newHV();
385 indirect_old_ck_const = PL_check[OP_CONST];
386 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
387 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
388 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
389 indirect_old_ck_padany = PL_check[OP_PADANY];
390 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
391 indirect_old_ck_method = PL_check[OP_METHOD];
392 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
393 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
394 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);