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 #define PTABLE_NAME ptable_map
88 #define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
97 #define ptable_map_store(T, K, V) ptable_map_store(aTHX_ (T), (K), (V))
98 #define ptable_map_clear(T) ptable_map_clear(aTHX_ (T))
99 #define ptable_map_free(T) ptable_map_free(aTHX_ (T))
101 STATIC ptable *indirect_map = NULL;
103 STATIC const char *indirect_linestr = NULL;
105 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
106 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
109 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
110 * In this case the linestr has temporarly changed, but the old buffer should
111 * still be alive somewhere. */
113 if (!PL_lex_inwhat) {
114 const char *pl_linestr = SvPVX_const(PL_linestr);
115 if (indirect_linestr != pl_linestr) {
116 ptable_map_clear(indirect_map);
117 indirect_linestr = pl_linestr;
122 SvUPGRADE(val, SVt_PVIV);
123 SvUVX(val) = PTR2UV(src);
127 ptable_map_store(indirect_map, o, val);
130 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
131 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
134 if (indirect_linestr != SvPVX_const(PL_linestr))
137 val = ptable_fetch(indirect_map, o);
144 return INT2PTR(const char *, SvUVX(val));
147 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
148 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
150 const char *p = NULL, *r = SvPV_const(sv, len);
152 if (len >= 1 && *r == '$') {
165 p = strstr(p + 1, r);
171 /* ... ck_const ............................................................ */
173 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
175 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
176 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
178 if (indirect_hint()) {
180 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
181 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
187 /* ... ck_rv2sv ............................................................ */
189 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
191 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
192 if (indirect_hint()) {
193 OP *op = cUNOPo->op_first;
195 const char *name = NULL, *s;
197 OPCODE type = (OPCODE) op->op_type;
202 GV *gv = cGVOPx_gv(op);
208 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
209 SV *nsv = cSVOPx_sv(op);
210 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
211 name = SvPV_const(nsv, len);
217 sv = sv_2mortal(newSVpvn("$", 1));
218 sv_catpvn_nomg(sv, name, len);
219 s = indirect_find(sv, PL_oldbufptr);
220 if (!s) { /* If it failed, retry without the current stash */
221 const char *stash = HvNAME_get(PL_curstash);
222 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
224 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
225 || name[stashlen] != ':' || name[stashlen+1] != ':') {
226 /* Failed again ? Try to remove main */
229 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
230 || name[stashlen] != ':' || name[stashlen+1] != ':')
234 sv_setpvn(sv, "$", 1);
236 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
237 s = indirect_find(sv, PL_oldbufptr);
242 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
243 indirect_map_store(o, s, sv);
248 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
251 /* ... ck_padany ........................................................... */
253 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
255 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
256 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
258 if (indirect_hint()) {
260 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
262 while (s < t && isSPACE(*s)) ++s;
263 if (*s == '$' && ++s <= t) {
264 while (s < t && isSPACE(*s)) ++s;
265 while (s < t && isSPACE(*t)) --t;
266 sv = sv_2mortal(newSVpvn("$", 1));
267 sv_catpvn_nomg(sv, s, t - s + 1);
268 indirect_map_store(o, s, sv);
275 /* ... ck_method ........................................................... */
277 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
279 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
280 if (indirect_hint()) {
281 OP *op = cUNOPo->op_first;
283 const char *s = indirect_map_fetch(op, &sv);
286 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
288 sv = sv_mortalcopy(sv);
289 s = indirect_find(sv, PL_oldbufptr);
291 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
292 /* o may now be a method_named */
293 indirect_map_store(o, s, sv);
298 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
301 /* ... ck_entersub ......................................................... */
303 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
305 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
307 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
308 IV hint = indirect_hint();
310 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
313 const char *mpos, *opos;
314 SV *mnamesv, *onamesv;
320 lop = (LISTOP *) oop;
321 if (!(lop->op_flags & OPf_KIDS))
324 } while (oop->op_type != OP_PUSHMARK);
325 oop = oop->op_sibling;
328 if (mop->op_type == OP_METHOD)
329 mop = cUNOPx(mop)->op_first;
330 else if (mop->op_type != OP_METHOD_NAMED)
333 mpos = indirect_map_fetch(mop, &mnamesv);
337 opos = indirect_map_fetch(oop, &onamesv);
342 const char *mname = SvPV_nolen_const(mnamesv);
343 const char *oname = SvPV_nolen_const(onamesv);
345 croak(indirect_msg, mname, oname);
347 warn(indirect_msg, mname, oname);
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 = ptable_new();
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);