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))))))
64 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
65 # ifndef PL_lex_inwhat
66 # define PL_lex_inwhat PL_parser->lex_inwhat
69 # define PL_linestr PL_parser->linestr
72 # define PL_bufptr PL_parser->bufptr
75 # define PL_oldbufptr PL_parser->oldbufptr
78 # ifndef PL_lex_inwhat
79 # define PL_lex_inwhat PL_Ilex_inwhat
82 # define PL_linestr PL_Ilinestr
85 # define PL_bufptr PL_Ibufptr
88 # define PL_oldbufptr PL_Ioldbufptr
92 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
93 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
96 /* ... Thread safety and multiplicity ...................................... */
98 /* Safe unless stated otherwise in Makefile.PL */
100 # define I_FORKSAFE 1
103 #ifndef I_MULTIPLICITY
104 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
105 # define I_MULTIPLICITY 1
107 # define I_MULTIPLICITY 0
110 #if I_MULTIPLICITY && !defined(tTHX)
111 # define tTHX PerlInterpreter*
114 #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))
115 # define I_THREADSAFE 1
116 # ifndef MY_CXT_CLONE
117 # define MY_CXT_CLONE \
119 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
120 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
121 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
124 # define I_THREADSAFE 0
126 # define dMY_CXT dNOOP
128 # define MY_CXT indirect_globaldata
130 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
132 # define MY_CXT_INIT NOOP
134 # define MY_CXT_CLONE NOOP
137 /* --- Helpers ------------------------------------------------------------- */
139 /* ... Thread-safe hints ................................................... */
141 #if I_WORKAROUND_REQUIRE_PROPAGATION
148 #define I_HINT_STRUCT 1
150 #define I_HINT_CODE(H) ((H)->code)
152 #define I_HINT_FREE(H) { \
153 indirect_hint_t *h = (H); \
154 SvREFCNT_dec(h->code); \
155 PerlMemShared_free(h); \
158 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
160 typedef SV indirect_hint_t;
162 #define I_HINT_STRUCT 0
164 #define I_HINT_CODE(H) (H)
166 #define I_HINT_FREE(H) SvREFCNT_dec(H);
168 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
172 #define PTABLE_NAME ptable_hints
173 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
182 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
183 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
185 #endif /* I_THREADSAFE */
187 /* Define the op->str ptable here because we need to be able to clean it during
195 } indirect_op_info_t;
197 #define PTABLE_NAME ptable
198 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
207 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
208 #define ptable_clear(T) ptable_clear(aTHX_ (T))
209 #define ptable_free(T) ptable_free(aTHX_ (T))
211 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
215 ptable *tbl; /* It really is a ptable_hints */
226 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
227 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
232 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
235 param.stashes = stashes;
237 param.proto_perl = owner;
239 dupsv = sv_dup(sv, ¶m);
243 SvREFCNT_dec(stashes);
246 return SvREFCNT_inc(dupsv);
249 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
251 indirect_hint_t *h1 = ent->val;
254 if (ud->owner == aTHX)
259 h2 = PerlMemShared_malloc(sizeof *h2);
260 h2->code = indirect_clone(h1->code, ud->owner);
261 SvREFCNT_inc(h2->code);
262 #if I_WORKAROUND_REQUIRE_PROPAGATION
263 h2->cxreq = h1->cxreq;
266 #else /* I_HINT_STRUCT */
268 h2 = indirect_clone(h1, ud->owner);
271 #endif /* !I_HINT_STRUCT */
273 ptable_hints_store(ud->tbl, ent->key, h2);
276 STATIC void indirect_thread_cleanup(pTHX_ void *);
278 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
284 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
288 PerlMemShared_free(level);
289 ptable_free(MY_CXT.map);
290 ptable_hints_free(MY_CXT.tbl);
294 #endif /* I_THREADSAFE */
296 #if I_WORKAROUND_REQUIRE_PROPAGATION
297 STATIC IV indirect_require_tag(pTHX) {
298 #define indirect_require_tag() indirect_require_tag(aTHX)
301 for (si = PL_curstackinfo; si; si = si->si_prev) {
304 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
305 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
307 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
314 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
316 STATIC SV *indirect_tag(pTHX_ SV *value) {
317 #define indirect_tag(V) indirect_tag(aTHX_ (V))
324 if (SvTYPE(value) >= SVt_PVCV) {
326 SvREFCNT_inc_simple_NN(code);
331 h = PerlMemShared_malloc(sizeof *h);
333 # if I_WORKAROUND_REQUIRE_PROPAGATION
334 h->cxreq = indirect_require_tag();
335 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
336 #else /* I_HINT_STRUCT */
338 #endif /* !I_HINT_STRUCT */
341 /* We only need for the key to be an unique tag for looking up the value later.
342 * Allocated memory provides convenient unique identifiers, so that's why we
343 * use the hint as the key itself. */
344 ptable_hints_store(MY_CXT.tbl, h, h);
345 #endif /* I_THREADSAFE */
347 return newSViv(PTR2IV(h));
350 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
351 #define indirect_detag(H) indirect_detag(aTHX_ (H))
355 if (!(hint && SvIOK(hint)))
358 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
360 h = ptable_fetch(MY_CXT.tbl, h);
361 #endif /* I_THREADSAFE */
363 #if I_WORKAROUND_REQUIRE_PROPAGATION
364 if (indirect_require_tag() != h->cxreq)
366 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
368 return I_HINT_CODE(h);
371 STATIC U32 indirect_hash = 0;
373 STATIC SV *indirect_hint(pTHX) {
374 #define indirect_hint() indirect_hint(aTHX)
380 #if I_HAS_PERL(5, 9, 5)
381 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
383 __PACKAGE__, __PACKAGE_LEN__,
388 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
395 return indirect_detag(hint);
398 /* ... op -> source position ............................................... */
400 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_t line) {
401 #define indirect_map_store(O, S, N, L) indirect_map_store(aTHX_ (O), (S), (N), (L))
402 indirect_op_info_t *oi;
407 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
408 * In this case the linestr has temporarly changed, but the old buffer should
409 * still be alive somewhere. */
411 if (!PL_lex_inwhat) {
412 const char *pl_linestr = SvPVX_const(PL_linestr);
413 if (MY_CXT.linestr != pl_linestr) {
414 ptable_clear(MY_CXT.map);
415 MY_CXT.linestr = pl_linestr;
419 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
420 Newx(oi, 1, indirect_op_info_t);
421 ptable_store(MY_CXT.map, o, oi);
427 s = SvPV_const(sv, len);
433 if (len > oi->size) {
435 Newx(oi->buf, len, char);
438 Copy(s, oi->buf, len, char);
445 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
446 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
447 const indirect_op_info_t *val;
450 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
453 return ptable_fetch(MY_CXT.map, o);
456 STATIC void indirect_map_delete(pTHX_ const OP *o) {
457 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
460 ptable_store(MY_CXT.map, o, NULL);
463 /* --- Check functions ----------------------------------------------------- */
465 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
466 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
468 const char *p = NULL, *r = SvPV_const(sv, len);
470 if (len >= 1 && *r == '$') {
483 p = strstr(p + 1, r);
489 /* ... ck_const ............................................................ */
491 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
493 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
494 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
496 if (indirect_hint()) {
498 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
499 const char *s = indirect_find(sv, PL_oldbufptr);
500 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
505 indirect_map_delete(o);
509 /* ... ck_rv2sv ............................................................ */
511 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
513 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
514 if (indirect_hint()) {
515 OP *op = cUNOPo->op_first;
517 const char *name = NULL, *s;
519 OPCODE type = (OPCODE) op->op_type;
524 GV *gv = cGVOPx_gv(op);
530 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
531 SV *nsv = cSVOPx_sv(op);
532 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
533 name = SvPV_const(nsv, len);
539 sv = sv_2mortal(newSVpvn("$", 1));
540 sv_catpvn_nomg(sv, name, len);
541 s = indirect_find(sv, PL_oldbufptr);
542 if (!s) { /* If it failed, retry without the current stash */
543 const char *stash = HvNAME_get(PL_curstash);
544 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
546 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
547 || name[stashlen] != ':' || name[stashlen+1] != ':') {
548 /* Failed again ? Try to remove main */
551 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
552 || name[stashlen] != ':' || name[stashlen+1] != ':')
556 sv_setpvn(sv, "$", 1);
558 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
559 s = indirect_find(sv, PL_oldbufptr);
564 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
565 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
570 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
572 indirect_map_delete(o);
576 /* ... ck_padany ........................................................... */
578 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
580 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
581 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
583 if (indirect_hint()) {
585 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
587 while (s < t && isSPACE(*s)) ++s;
588 if (*s == '$' && ++s <= t) {
589 while (s < t && isSPACE(*s)) ++s;
590 while (s < t && isSPACE(*t)) --t;
591 sv = sv_2mortal(newSVpvn("$", 1));
592 sv_catpvn_nomg(sv, s, t - s + 1);
593 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
598 indirect_map_delete(o);
602 /* ... ck_scope ............................................................ */
604 STATIC OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
605 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
607 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
608 OP *(*old_ck)(pTHX_ OP *) = 0;
610 switch (o->op_type) {
611 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
612 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
614 o = CALL_FPTR(old_ck)(aTHX_ o);
616 if (indirect_hint()) {
617 indirect_map_store(o, PL_oldbufptr, NULL, CopLINE(&PL_compiling));
621 indirect_map_delete(o);
625 /* We don't need to clean the map entries for leave ops because they can only
626 * be created by mutating from a lineseq. */
628 /* ... ck_method ........................................................... */
630 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
632 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
633 if (indirect_hint()) {
634 OP *op = cUNOPo->op_first;
635 const indirect_op_info_t *oi = indirect_map_fetch(op);
636 const char *s = NULL;
640 if (oi && (s = oi->pos)) {
641 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
642 line = oi->line; /* Keep the old line so that we really point to the first */
645 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
647 sv = sv_mortalcopy(sv);
648 s = indirect_find(sv, PL_oldbufptr);
649 line = CopLINE(&PL_compiling);
652 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
653 /* o may now be a method_named */
655 indirect_map_store(o, s, sv, line);
660 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
662 indirect_map_delete(o);
666 /* ... ck_entersub ......................................................... */
668 STATIC int indirect_is_indirect(const indirect_op_info_t *moi, const indirect_op_info_t *ooi) {
669 if (moi->pos > ooi->pos)
672 if (moi->pos == ooi->pos)
673 return moi->len == ooi->len && !memcmp(moi->buf, ooi->buf, moi->len);
678 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
680 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
681 SV *code = indirect_hint();
683 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
686 const indirect_op_info_t *moi, *ooi;
692 lop = (LISTOP *) oop;
693 if (!(lop->op_flags & OPf_KIDS))
696 } while (oop->op_type != OP_PUSHMARK);
697 oop = oop->op_sibling;
703 switch (oop->op_type) {
714 if (mop->op_type == OP_METHOD)
715 mop = cUNOPx(mop)->op_first;
716 else if (mop->op_type != OP_METHOD_NAMED)
719 moi = indirect_map_fetch(mop);
720 if (!(moi && moi->pos))
723 ooi = indirect_map_fetch(oop);
724 if (!(ooi && ooi->pos))
727 if (indirect_is_indirect(moi, ooi)) {
735 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
737 file = sv_mortalcopy(CopFILESV(&PL_compiling));
742 mPUSHp(ooi->buf, ooi->len);
743 mPUSHp(moi->buf, moi->len);
748 call_sv(code, G_VOID);
761 STATIC U32 indirect_initialized = 0;
763 STATIC void indirect_teardown(pTHX_ void *root) {
766 if (!indirect_initialized)
774 ptable_free(MY_CXT.map);
776 ptable_hints_free(MY_CXT.tbl);
779 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_old_ck_const);
780 indirect_old_ck_const = 0;
781 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
782 indirect_old_ck_rv2sv = 0;
783 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_old_ck_padany);
784 indirect_old_ck_padany = 0;
785 PL_check[OP_SCOPE] = MEMBER_TO_FPTR(indirect_old_ck_scope);
786 indirect_old_ck_scope = 0;
787 PL_check[OP_LINESEQ] = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
788 indirect_old_ck_lineseq = 0;
790 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_old_ck_method);
791 indirect_old_ck_method = 0;
792 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_old_ck_entersub);
793 indirect_old_ck_entersub = 0;
795 indirect_initialized = 0;
798 STATIC void indirect_setup(pTHX) {
799 #define indirect_setup() indirect_setup(aTHX)
800 if (indirect_initialized)
805 MY_CXT.tbl = ptable_new();
808 MY_CXT.map = ptable_new();
809 MY_CXT.linestr = NULL;
811 indirect_old_ck_const = PL_check[OP_CONST];
812 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
813 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
814 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
815 indirect_old_ck_padany = PL_check[OP_PADANY];
816 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
817 indirect_old_ck_scope = PL_check[OP_SCOPE];
818 PL_check[OP_SCOPE] = MEMBER_TO_FPTR(indirect_ck_scope);
819 indirect_old_ck_lineseq = PL_check[OP_LINESEQ];
820 PL_check[OP_LINESEQ] = MEMBER_TO_FPTR(indirect_ck_scope);
822 indirect_old_ck_method = PL_check[OP_METHOD];
823 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
824 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
825 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
828 call_atexit(indirect_teardown, aTHX);
830 call_atexit(indirect_teardown, NULL);
833 indirect_initialized = 1;
836 STATIC U32 indirect_booted = 0;
838 /* --- XS ------------------------------------------------------------------ */
840 MODULE = indirect PACKAGE = indirect
846 if (!indirect_booted++) {
849 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
851 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
852 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
853 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
871 ud.tbl = t = ptable_new();
872 ud.owner = MY_CXT.owner;
873 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
877 MY_CXT.map = ptable_new();
878 MY_CXT.linestr = NULL;
883 level = PerlMemShared_malloc(sizeof *level);
886 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
896 RETVAL = indirect_tag(value);