1 /* This file is part of the indirect Perl module.
2 * See http://search.cpan.org/dist/indirect/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
15 # define SvPV_const SvPV
18 #ifndef SvPV_nolen_const
19 # define SvPV_nolen_const SvPV_nolen
23 # define SvPVX_const SvPVX
26 #ifndef sv_catpvn_nomg
27 # define sv_catpvn_nomg sv_catpvn
31 # define HvNAME_get(H) HvNAME(H)
35 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
38 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
40 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
41 # ifndef PL_lex_inwhat
42 # define PL_lex_inwhat PL_parser->lex_inwhat
45 # define PL_linestr PL_parser->linestr
48 # define PL_bufptr PL_parser->bufptr
51 # define PL_oldbufptr PL_parser->oldbufptr
54 # ifndef PL_lex_inwhat
55 # define PL_lex_inwhat PL_Ilex_inwhat
58 # define PL_linestr PL_Ilinestr
61 # define PL_bufptr PL_Ibufptr
64 # define PL_oldbufptr PL_Ioldbufptr
68 /* ... Hints ............................................................... */
70 STATIC U32 indirect_hash = 0;
72 STATIC IV indirect_hint(pTHX) {
73 #define indirect_hint() indirect_hint(aTHX)
75 #if I_HAS_PERL(5, 10, 0)
76 id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
78 __PACKAGE__, __PACKAGE_LEN__,
82 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
88 return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
91 /* ... op -> source position ............................................... */
93 #define PTABLE_NAME ptable_map
94 #define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
103 #define ptable_map_store(T, K, V) ptable_map_store(aTHX_ (T), (K), (V))
104 #define ptable_map_clear(T) ptable_map_clear(aTHX_ (T))
105 #define ptable_map_free(T) ptable_map_free(aTHX_ (T))
107 STATIC ptable *indirect_map = NULL;
109 STATIC const char *indirect_linestr = NULL;
111 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
112 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
115 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
116 * In this case the linestr has temporarly changed, but the old buffer should
117 * still be alive somewhere. */
119 if (!PL_lex_inwhat) {
120 const char *pl_linestr = SvPVX_const(PL_linestr);
121 if (indirect_linestr != pl_linestr) {
122 ptable_map_clear(indirect_map);
123 indirect_linestr = pl_linestr;
128 SvUPGRADE(val, SVt_PVIV);
129 SvUVX(val) = PTR2UV(src);
133 ptable_map_store(indirect_map, o, val);
136 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
137 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
140 if (indirect_linestr != SvPVX_const(PL_linestr))
143 val = ptable_fetch(indirect_map, o);
150 return INT2PTR(const char *, SvUVX(val));
153 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
154 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
156 const char *p = NULL, *r = SvPV_const(sv, len);
158 if (len >= 1 && *r == '$') {
171 p = strstr(p + 1, r);
177 /* ... ck_const ............................................................ */
179 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
181 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
182 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
184 if (indirect_hint()) {
186 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
187 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
193 /* ... ck_rv2sv ............................................................ */
195 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
197 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
198 if (indirect_hint()) {
199 OP *op = cUNOPo->op_first;
201 const char *name = NULL, *s;
203 OPCODE type = (OPCODE) op->op_type;
208 GV *gv = cGVOPx_gv(op);
214 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
215 SV *nsv = cSVOPx_sv(op);
216 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
217 name = SvPV_const(nsv, len);
223 sv = sv_2mortal(newSVpvn("$", 1));
224 sv_catpvn_nomg(sv, name, len);
225 s = indirect_find(sv, PL_oldbufptr);
226 if (!s) { /* If it failed, retry without the current stash */
227 const char *stash = HvNAME_get(PL_curstash);
228 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
230 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
231 || name[stashlen] != ':' || name[stashlen+1] != ':') {
232 /* Failed again ? Try to remove main */
235 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
236 || name[stashlen] != ':' || name[stashlen+1] != ':')
240 sv_setpvn(sv, "$", 1);
242 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
243 s = indirect_find(sv, PL_oldbufptr);
248 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
249 indirect_map_store(o, s, sv);
254 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
257 /* ... ck_padany ........................................................... */
259 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
261 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
262 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
264 if (indirect_hint()) {
266 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
268 while (s < t && isSPACE(*s)) ++s;
269 if (*s == '$' && ++s <= t) {
270 while (s < t && isSPACE(*s)) ++s;
271 while (s < t && isSPACE(*t)) --t;
272 sv = sv_2mortal(newSVpvn("$", 1));
273 sv_catpvn_nomg(sv, s, t - s + 1);
274 indirect_map_store(o, s, sv);
281 /* ... ck_method ........................................................... */
283 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
285 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
286 if (indirect_hint()) {
287 OP *op = cUNOPo->op_first;
289 const char *s = indirect_map_fetch(op, &sv);
292 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
294 sv = sv_mortalcopy(sv);
295 s = indirect_find(sv, PL_oldbufptr);
297 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
298 /* o may now be a method_named */
299 indirect_map_store(o, s, sv);
304 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
307 /* ... ck_entersub ......................................................... */
309 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
311 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
313 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
314 IV hint = indirect_hint();
316 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
319 const char *mpos, *opos;
320 SV *mnamesv, *onamesv;
326 lop = (LISTOP *) oop;
327 if (!(lop->op_flags & OPf_KIDS))
330 } while (oop->op_type != OP_PUSHMARK);
331 oop = oop->op_sibling;
334 if (mop->op_type == OP_METHOD)
335 mop = cUNOPx(mop)->op_first;
336 else if (mop->op_type != OP_METHOD_NAMED)
339 mpos = indirect_map_fetch(mop, &mnamesv);
343 opos = indirect_map_fetch(oop, &onamesv);
348 const char *mname = SvPV_nolen_const(mnamesv);
349 const char *oname = SvPV_nolen_const(onamesv);
351 croak(indirect_msg, mname, oname);
353 warn(indirect_msg, mname, oname);
361 STATIC U32 indirect_initialized = 0;
363 /* --- XS ------------------------------------------------------------------ */
365 MODULE = indirect PACKAGE = indirect
371 if (!indirect_initialized++) {
372 indirect_map = ptable_new();
374 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
376 indirect_old_ck_const = PL_check[OP_CONST];
377 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
378 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
379 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
380 indirect_old_ck_padany = PL_check[OP_PADANY];
381 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
382 indirect_old_ck_method = PL_check[OP_METHOD];
383 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
384 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
385 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);