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 ---------------------------------------------- */
23 # define Newx(v, n, c) New(0, v, n, c)
27 # define SvPV_const SvPV
30 #ifndef SvPV_nolen_const
31 # define SvPV_nolen_const SvPV_nolen
35 # define SvPVX_const SvPVX
38 #ifndef SvREFCNT_inc_simple_NN
39 # define SvREFCNT_inc_simple_NN SvREFCNT_inc
42 #ifndef sv_catpvn_nomg
43 # define sv_catpvn_nomg sv_catpvn
47 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
51 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
55 # define HvNAME_get(H) HvNAME(H)
59 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
62 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
65 #if defined(ENTER_with_name) && !I_HAS_PERL(5, 11, 4)
66 # define ENTERn(N) ENTER_with_name(N)
68 # define ENTERn(N) ENTER
72 #if defined(LEAVE_with_name) && !I_HAS_PERL(5, 11, 4)
73 # define LEAVEn(N) LEAVE_with_name(N)
75 # define LEAVEn(N) LEAVE
78 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
79 # ifndef PL_lex_inwhat
80 # define PL_lex_inwhat PL_parser->lex_inwhat
83 # define PL_linestr PL_parser->linestr
86 # define PL_bufptr PL_parser->bufptr
89 # define PL_oldbufptr PL_parser->oldbufptr
92 # ifndef PL_lex_inwhat
93 # define PL_lex_inwhat PL_Ilex_inwhat
96 # define PL_linestr PL_Ilinestr
99 # define PL_bufptr PL_Ibufptr
101 # ifndef PL_oldbufptr
102 # define PL_oldbufptr PL_Ioldbufptr
106 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
107 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
110 /* ... Thread safety and multiplicity ...................................... */
112 /* Safe unless stated otherwise in Makefile.PL */
114 # define I_FORKSAFE 1
117 #ifndef I_MULTIPLICITY
118 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
119 # define I_MULTIPLICITY 1
121 # define I_MULTIPLICITY 0
124 #if I_MULTIPLICITY && !defined(tTHX)
125 # define tTHX PerlInterpreter*
128 #if I_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
129 # define I_THREADSAFE 1
130 # ifndef MY_CXT_CLONE
131 # define MY_CXT_CLONE \
133 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
134 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
135 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
138 # define I_THREADSAFE 0
140 # define dMY_CXT dNOOP
142 # define MY_CXT indirect_globaldata
144 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
146 # define MY_CXT_INIT NOOP
148 # define MY_CXT_CLONE NOOP
151 /* --- Helpers ------------------------------------------------------------- */
153 /* ... Thread-safe hints ................................................... */
155 #if I_WORKAROUND_REQUIRE_PROPAGATION
162 #define I_HINT_STRUCT 1
164 #define I_HINT_CODE(H) ((H)->code)
166 #define I_HINT_FREE(H) { \
167 indirect_hint_t *h = (H); \
168 SvREFCNT_dec(h->code); \
169 PerlMemShared_free(h); \
172 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
174 typedef SV indirect_hint_t;
176 #define I_HINT_STRUCT 0
178 #define I_HINT_CODE(H) (H)
180 #define I_HINT_FREE(H) SvREFCNT_dec(H);
182 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
186 #define PTABLE_NAME ptable_hints
187 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
196 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
197 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
199 #endif /* I_THREADSAFE */
201 /* Define the op->str ptable here because we need to be able to clean it during
209 } indirect_op_info_t;
211 #define PTABLE_NAME ptable
212 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
221 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
222 #define ptable_clear(T) ptable_clear(aTHX_ (T))
223 #define ptable_free(T) ptable_free(aTHX_ (T))
225 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
229 ptable *tbl; /* It really is a ptable_hints */
240 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
241 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
246 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
249 param.stashes = stashes;
251 param.proto_perl = owner;
253 dupsv = sv_dup(sv, ¶m);
257 SvREFCNT_dec(stashes);
260 return SvREFCNT_inc(dupsv);
263 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
265 indirect_hint_t *h1 = ent->val;
268 if (ud->owner == aTHX)
273 h2 = PerlMemShared_malloc(sizeof *h2);
274 h2->code = indirect_clone(h1->code, ud->owner);
275 SvREFCNT_inc(h2->code);
276 #if I_WORKAROUND_REQUIRE_PROPAGATION
277 h2->require_tag = PTR2IV(indirect_clone(INT2PTR(SV *, h1->require_tag),
281 #else /* I_HINT_STRUCT */
283 h2 = indirect_clone(h1, ud->owner);
286 #endif /* !I_HINT_STRUCT */
288 ptable_hints_store(ud->tbl, ent->key, h2);
291 STATIC void indirect_thread_cleanup(pTHX_ void *);
293 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
299 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
303 PerlMemShared_free(level);
304 ptable_free(MY_CXT.map);
305 ptable_hints_free(MY_CXT.tbl);
309 #endif /* I_THREADSAFE */
311 #if I_WORKAROUND_REQUIRE_PROPAGATION
312 STATIC IV indirect_require_tag(pTHX) {
313 #define indirect_require_tag() indirect_require_tag(aTHX)
314 const CV *cv, *outside;
319 /* If for some reason the pragma is operational at run-time, try to discover
320 * the current cv in use. */
323 for (si = PL_curstackinfo; si; si = si->si_prev) {
326 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
327 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
329 switch (CxTYPE(cx)) {
332 /* The propagation workaround is only needed up to 5.10.0 and at that
333 * time format and sub contexts were still identical. And even later the
334 * cv members offsets should have been kept the same. */
336 goto get_enclosing_cv;
338 cv = cx->blk_eval.cv;
339 goto get_enclosing_cv;
350 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
355 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
357 STATIC SV *indirect_tag(pTHX_ SV *value) {
358 #define indirect_tag(V) indirect_tag(aTHX_ (V))
365 if (SvTYPE(value) >= SVt_PVCV) {
367 SvREFCNT_inc_simple_NN(code);
372 h = PerlMemShared_malloc(sizeof *h);
374 # if I_WORKAROUND_REQUIRE_PROPAGATION
375 h->require_tag = indirect_require_tag();
376 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
377 #else /* I_HINT_STRUCT */
379 #endif /* !I_HINT_STRUCT */
382 /* We only need for the key to be an unique tag for looking up the value later.
383 * Allocated memory provides convenient unique identifiers, so that's why we
384 * use the hint as the key itself. */
385 ptable_hints_store(MY_CXT.tbl, h, h);
386 #endif /* I_THREADSAFE */
388 return newSViv(PTR2IV(h));
391 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
392 #define indirect_detag(H) indirect_detag(aTHX_ (H))
396 if (!(hint && SvIOK(hint)))
399 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
401 h = ptable_fetch(MY_CXT.tbl, h);
402 #endif /* I_THREADSAFE */
404 #if I_WORKAROUND_REQUIRE_PROPAGATION
405 if (indirect_require_tag() != h->require_tag)
407 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
409 return I_HINT_CODE(h);
412 STATIC U32 indirect_hash = 0;
414 STATIC SV *indirect_hint(pTHX) {
415 #define indirect_hint() indirect_hint(aTHX)
421 #if I_HAS_PERL(5, 9, 5)
422 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
424 __PACKAGE__, __PACKAGE_LEN__,
429 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
436 return indirect_detag(hint);
439 /* ... op -> source position ............................................... */
441 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_t line) {
442 #define indirect_map_store(O, S, N, L) indirect_map_store(aTHX_ (O), (S), (N), (L))
443 indirect_op_info_t *oi;
448 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
449 * In this case the linestr has temporarly changed, but the old buffer should
450 * still be alive somewhere. */
452 if (!PL_lex_inwhat) {
453 const char *pl_linestr = SvPVX_const(PL_linestr);
454 if (MY_CXT.linestr != pl_linestr) {
455 ptable_clear(MY_CXT.map);
456 MY_CXT.linestr = pl_linestr;
460 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
461 Newx(oi, 1, indirect_op_info_t);
462 ptable_store(MY_CXT.map, o, oi);
468 s = SvPV_const(sv, len);
474 if (len > oi->size) {
476 Newx(oi->buf, len, char);
479 Copy(s, oi->buf, len, char);
486 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
487 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
490 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
493 return ptable_fetch(MY_CXT.map, o);
496 STATIC void indirect_map_delete(pTHX_ const OP *o) {
497 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
500 ptable_store(MY_CXT.map, o, NULL);
503 /* --- Check functions ----------------------------------------------------- */
505 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
506 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
508 const char *p = NULL, *r = SvPV_const(sv, len);
510 if (len >= 1 && *r == '$') {
523 p = strstr(p + 1, r);
529 /* ... ck_const ............................................................ */
531 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
533 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
534 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
536 if (indirect_hint()) {
538 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
539 const char *s = indirect_find(sv, PL_oldbufptr);
540 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
545 indirect_map_delete(o);
549 /* ... ck_rv2sv ............................................................ */
551 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
553 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
554 if (indirect_hint()) {
555 OP *op = cUNOPo->op_first;
557 const char *name = NULL, *s;
559 OPCODE type = (OPCODE) op->op_type;
564 GV *gv = cGVOPx_gv(op);
570 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
571 SV *nsv = cSVOPx_sv(op);
572 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
573 name = SvPV_const(nsv, len);
579 sv = sv_2mortal(newSVpvn("$", 1));
580 sv_catpvn_nomg(sv, name, len);
581 s = indirect_find(sv, PL_oldbufptr);
582 if (!s) { /* If it failed, retry without the current stash */
583 const char *stash = HvNAME_get(PL_curstash);
584 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
586 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
587 || name[stashlen] != ':' || name[stashlen+1] != ':') {
588 /* Failed again ? Try to remove main */
591 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
592 || name[stashlen] != ':' || name[stashlen+1] != ':')
596 sv_setpvn(sv, "$", 1);
598 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
599 s = indirect_find(sv, PL_oldbufptr);
604 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
605 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
610 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
612 indirect_map_delete(o);
616 /* ... ck_padany ........................................................... */
618 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
620 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
621 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
623 if (indirect_hint()) {
625 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
627 while (s < t && isSPACE(*s)) ++s;
628 if (*s == '$' && ++s <= t) {
629 while (s < t && isSPACE(*s)) ++s;
630 while (s < t && isSPACE(*t)) --t;
631 sv = sv_2mortal(newSVpvn("$", 1));
632 sv_catpvn_nomg(sv, s, t - s + 1);
633 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
638 indirect_map_delete(o);
642 /* ... ck_scope ............................................................ */
644 STATIC OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
645 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
647 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
648 OP *(*old_ck)(pTHX_ OP *) = 0;
650 switch (o->op_type) {
651 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
652 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
654 o = CALL_FPTR(old_ck)(aTHX_ o);
656 if (indirect_hint()) {
657 indirect_map_store(o, PL_oldbufptr, NULL, CopLINE(&PL_compiling));
661 indirect_map_delete(o);
665 /* We don't need to clean the map entries for leave ops because they can only
666 * be created by mutating from a lineseq. */
668 /* ... ck_method ........................................................... */
670 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
672 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
673 if (indirect_hint()) {
674 OP *op = cUNOPo->op_first;
675 const indirect_op_info_t *oi = indirect_map_fetch(op);
676 const char *s = NULL;
680 if (oi && (s = oi->pos)) {
681 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
682 line = oi->line; /* Keep the old line so that we really point to the first */
685 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
687 sv = sv_mortalcopy(sv);
688 s = indirect_find(sv, PL_oldbufptr);
689 line = CopLINE(&PL_compiling);
692 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
693 /* o may now be a method_named */
695 indirect_map_store(o, s, sv, line);
700 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
702 indirect_map_delete(o);
706 /* ... ck_entersub ......................................................... */
708 STATIC int indirect_is_indirect(const indirect_op_info_t *moi, const indirect_op_info_t *ooi) {
709 if (moi->pos > ooi->pos)
712 if (moi->pos == ooi->pos)
713 return moi->len == ooi->len && !memcmp(moi->buf, ooi->buf, moi->len);
718 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
720 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
721 SV *code = indirect_hint();
723 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
726 const indirect_op_info_t *moi, *ooi;
732 lop = (LISTOP *) oop;
733 if (!(lop->op_flags & OPf_KIDS))
736 } while (oop->op_type != OP_PUSHMARK);
737 oop = oop->op_sibling;
743 switch (oop->op_type) {
754 if (mop->op_type == OP_METHOD)
755 mop = cUNOPx(mop)->op_first;
756 else if (mop->op_type != OP_METHOD_NAMED)
759 moi = indirect_map_fetch(mop);
760 if (!(moi && moi->pos))
763 ooi = indirect_map_fetch(oop);
764 if (!(ooi && ooi->pos))
767 if (indirect_is_indirect(moi, ooi)) {
775 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
777 file = sv_mortalcopy(CopFILESV(&PL_compiling));
782 mPUSHp(ooi->buf, ooi->len);
783 mPUSHp(moi->buf, moi->len);
788 call_sv(code, G_VOID);
801 STATIC U32 indirect_initialized = 0;
803 STATIC void indirect_teardown(pTHX_ void *root) {
806 if (!indirect_initialized)
814 ptable_free(MY_CXT.map);
816 ptable_hints_free(MY_CXT.tbl);
819 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_old_ck_const);
820 indirect_old_ck_const = 0;
821 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
822 indirect_old_ck_rv2sv = 0;
823 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_old_ck_padany);
824 indirect_old_ck_padany = 0;
825 PL_check[OP_SCOPE] = MEMBER_TO_FPTR(indirect_old_ck_scope);
826 indirect_old_ck_scope = 0;
827 PL_check[OP_LINESEQ] = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
828 indirect_old_ck_lineseq = 0;
830 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_old_ck_method);
831 indirect_old_ck_method = 0;
832 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_old_ck_entersub);
833 indirect_old_ck_entersub = 0;
835 indirect_initialized = 0;
838 STATIC void indirect_setup(pTHX) {
839 #define indirect_setup() indirect_setup(aTHX)
840 if (indirect_initialized)
845 MY_CXT.tbl = ptable_new();
848 MY_CXT.map = ptable_new();
849 MY_CXT.linestr = NULL;
851 indirect_old_ck_const = PL_check[OP_CONST];
852 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
853 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
854 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
855 indirect_old_ck_padany = PL_check[OP_PADANY];
856 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
857 indirect_old_ck_scope = PL_check[OP_SCOPE];
858 PL_check[OP_SCOPE] = MEMBER_TO_FPTR(indirect_ck_scope);
859 indirect_old_ck_lineseq = PL_check[OP_LINESEQ];
860 PL_check[OP_LINESEQ] = MEMBER_TO_FPTR(indirect_ck_scope);
862 indirect_old_ck_method = PL_check[OP_METHOD];
863 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
864 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
865 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
868 call_atexit(indirect_teardown, aTHX);
870 call_atexit(indirect_teardown, NULL);
873 indirect_initialized = 1;
876 STATIC U32 indirect_booted = 0;
878 /* --- XS ------------------------------------------------------------------ */
880 MODULE = indirect PACKAGE = indirect
886 if (!indirect_booted++) {
889 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
891 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
892 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
893 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
911 ud.tbl = t = ptable_new();
912 ud.owner = MY_CXT.owner;
913 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
917 MY_CXT.map = ptable_new();
918 MY_CXT.linestr = NULL;
923 level = PerlMemShared_malloc(sizeof *level);
926 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
936 RETVAL = indirect_tag(value);