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 #ifndef ENTER_with_name
63 # define ENTER_with_name(N) ENTER
66 #ifndef LEAVE_with_name
67 # define LEAVE_with_name(N) LEAVE
70 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
72 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
73 # ifndef PL_lex_inwhat
74 # define PL_lex_inwhat PL_parser->lex_inwhat
77 # define PL_linestr PL_parser->linestr
80 # define PL_bufptr PL_parser->bufptr
83 # define PL_oldbufptr PL_parser->oldbufptr
86 # ifndef PL_lex_inwhat
87 # define PL_lex_inwhat PL_Ilex_inwhat
90 # define PL_linestr PL_Ilinestr
93 # define PL_bufptr PL_Ibufptr
96 # define PL_oldbufptr PL_Ioldbufptr
100 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
101 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
104 /* ... Thread safety and multiplicity ...................................... */
106 /* Safe unless stated otherwise in Makefile.PL */
108 # define I_FORKSAFE 1
111 #ifndef I_MULTIPLICITY
112 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
113 # define I_MULTIPLICITY 1
115 # define I_MULTIPLICITY 0
118 #if I_MULTIPLICITY && !defined(tTHX)
119 # define tTHX PerlInterpreter*
122 #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))
123 # define I_THREADSAFE 1
124 # ifndef MY_CXT_CLONE
125 # define MY_CXT_CLONE \
127 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
128 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
129 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
132 # define I_THREADSAFE 0
134 # define dMY_CXT dNOOP
136 # define MY_CXT indirect_globaldata
138 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
140 # define MY_CXT_INIT NOOP
142 # define MY_CXT_CLONE NOOP
145 /* --- Helpers ------------------------------------------------------------- */
147 /* ... Thread-safe hints ................................................... */
149 #if I_WORKAROUND_REQUIRE_PROPAGATION
156 #define I_HINT_STRUCT 1
158 #define I_HINT_CODE(H) ((H)->code)
160 #define I_HINT_FREE(H) { \
161 indirect_hint_t *h = (H); \
162 SvREFCNT_dec(h->code); \
163 PerlMemShared_free(h); \
166 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
168 typedef SV indirect_hint_t;
170 #define I_HINT_STRUCT 0
172 #define I_HINT_CODE(H) (H)
174 #define I_HINT_FREE(H) SvREFCNT_dec(H);
176 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
180 #define PTABLE_NAME ptable_hints
181 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
190 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
191 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
193 #endif /* I_THREADSAFE */
195 /* Define the op->str ptable here because we need to be able to clean it during
203 } indirect_op_info_t;
205 #define PTABLE_NAME ptable
206 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
215 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
216 #define ptable_clear(T) ptable_clear(aTHX_ (T))
217 #define ptable_free(T) ptable_free(aTHX_ (T))
219 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
223 ptable *tbl; /* It really is a ptable_hints */
234 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
235 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
240 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
243 param.stashes = stashes;
245 param.proto_perl = owner;
247 dupsv = sv_dup(sv, ¶m);
251 SvREFCNT_dec(stashes);
254 return SvREFCNT_inc(dupsv);
257 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
259 indirect_hint_t *h1 = ent->val;
262 if (ud->owner == aTHX)
267 h2 = PerlMemShared_malloc(sizeof *h2);
268 h2->code = indirect_clone(h1->code, ud->owner);
269 SvREFCNT_inc(h2->code);
270 #if I_WORKAROUND_REQUIRE_PROPAGATION
271 h2->cxreq = h1->cxreq;
274 #else /* I_HINT_STRUCT */
276 h2 = indirect_clone(h1, ud->owner);
279 #endif /* !I_HINT_STRUCT */
281 ptable_hints_store(ud->tbl, ent->key, h2);
284 STATIC void indirect_thread_cleanup(pTHX_ void *);
286 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
292 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
296 PerlMemShared_free(level);
297 ptable_free(MY_CXT.map);
298 ptable_hints_free(MY_CXT.tbl);
302 #endif /* I_THREADSAFE */
304 #if I_WORKAROUND_REQUIRE_PROPAGATION
305 STATIC IV indirect_require_tag(pTHX) {
306 #define indirect_require_tag() indirect_require_tag(aTHX)
309 for (si = PL_curstackinfo; si; si = si->si_prev) {
312 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
313 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
315 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
322 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
324 STATIC SV *indirect_tag(pTHX_ SV *value) {
325 #define indirect_tag(V) indirect_tag(aTHX_ (V))
332 if (SvTYPE(value) >= SVt_PVCV) {
334 SvREFCNT_inc_simple_NN(code);
339 h = PerlMemShared_malloc(sizeof *h);
341 # if I_WORKAROUND_REQUIRE_PROPAGATION
342 h->cxreq = indirect_require_tag();
343 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
344 #else /* I_HINT_STRUCT */
346 #endif /* !I_HINT_STRUCT */
349 /* We only need for the key to be an unique tag for looking up the value later.
350 * Allocated memory provides convenient unique identifiers, so that's why we
351 * use the hint as the key itself. */
352 ptable_hints_store(MY_CXT.tbl, h, h);
353 #endif /* I_THREADSAFE */
355 return newSViv(PTR2IV(h));
358 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
359 #define indirect_detag(H) indirect_detag(aTHX_ (H))
363 if (!(hint && SvIOK(hint)))
366 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
368 h = ptable_fetch(MY_CXT.tbl, h);
369 #endif /* I_THREADSAFE */
371 #if I_WORKAROUND_REQUIRE_PROPAGATION
372 if (indirect_require_tag() != h->cxreq)
374 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
376 return I_HINT_CODE(h);
379 STATIC U32 indirect_hash = 0;
381 STATIC SV *indirect_hint(pTHX) {
382 #define indirect_hint() indirect_hint(aTHX)
388 #if I_HAS_PERL(5, 9, 5)
389 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
391 __PACKAGE__, __PACKAGE_LEN__,
396 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
403 return indirect_detag(hint);
406 /* ... op -> source position ............................................... */
408 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_t line) {
409 #define indirect_map_store(O, S, N, L) indirect_map_store(aTHX_ (O), (S), (N), (L))
410 indirect_op_info_t *oi;
415 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
416 * In this case the linestr has temporarly changed, but the old buffer should
417 * still be alive somewhere. */
419 if (!PL_lex_inwhat) {
420 const char *pl_linestr = SvPVX_const(PL_linestr);
421 if (MY_CXT.linestr != pl_linestr) {
422 ptable_clear(MY_CXT.map);
423 MY_CXT.linestr = pl_linestr;
427 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
428 Newx(oi, 1, indirect_op_info_t);
429 ptable_store(MY_CXT.map, o, oi);
435 s = SvPV_const(sv, len);
441 if (len > oi->size) {
443 Newx(oi->buf, len, char);
446 Copy(s, oi->buf, len, char);
453 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
454 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
455 const indirect_op_info_t *val;
458 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
461 return ptable_fetch(MY_CXT.map, o);
464 STATIC void indirect_map_delete(pTHX_ const OP *o) {
465 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
468 ptable_store(MY_CXT.map, o, NULL);
471 /* --- Check functions ----------------------------------------------------- */
473 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
474 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
476 const char *p = NULL, *r = SvPV_const(sv, len);
478 if (len >= 1 && *r == '$') {
491 p = strstr(p + 1, r);
497 /* ... ck_const ............................................................ */
499 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
501 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
502 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
504 if (indirect_hint()) {
506 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
507 const char *s = indirect_find(sv, PL_oldbufptr);
508 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
513 indirect_map_delete(o);
517 /* ... ck_rv2sv ............................................................ */
519 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
521 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
522 if (indirect_hint()) {
523 OP *op = cUNOPo->op_first;
525 const char *name = NULL, *s;
527 OPCODE type = (OPCODE) op->op_type;
532 GV *gv = cGVOPx_gv(op);
538 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
539 SV *nsv = cSVOPx_sv(op);
540 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
541 name = SvPV_const(nsv, len);
547 sv = sv_2mortal(newSVpvn("$", 1));
548 sv_catpvn_nomg(sv, name, len);
549 s = indirect_find(sv, PL_oldbufptr);
550 if (!s) { /* If it failed, retry without the current stash */
551 const char *stash = HvNAME_get(PL_curstash);
552 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
554 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
555 || name[stashlen] != ':' || name[stashlen+1] != ':') {
556 /* Failed again ? Try to remove main */
559 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
560 || name[stashlen] != ':' || name[stashlen+1] != ':')
564 sv_setpvn(sv, "$", 1);
566 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
567 s = indirect_find(sv, PL_oldbufptr);
572 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
573 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
578 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
580 indirect_map_delete(o);
584 /* ... ck_padany ........................................................... */
586 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
588 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
589 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
591 if (indirect_hint()) {
593 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
595 while (s < t && isSPACE(*s)) ++s;
596 if (*s == '$' && ++s <= t) {
597 while (s < t && isSPACE(*s)) ++s;
598 while (s < t && isSPACE(*t)) --t;
599 sv = sv_2mortal(newSVpvn("$", 1));
600 sv_catpvn_nomg(sv, s, t - s + 1);
601 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
606 indirect_map_delete(o);
610 /* ... ck_scope ............................................................ */
612 STATIC OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
613 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
615 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
616 OP *(*old_ck)(pTHX_ OP *) = 0;
618 switch (o->op_type) {
619 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
620 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
622 o = CALL_FPTR(old_ck)(aTHX_ o);
624 if (indirect_hint()) {
625 indirect_map_store(o, PL_oldbufptr, NULL, CopLINE(&PL_compiling));
629 indirect_map_delete(o);
633 /* We don't need to clean the map entries for leave ops because they can only
634 * be created by mutating from a lineseq. */
636 /* ... ck_method ........................................................... */
638 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
640 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
641 if (indirect_hint()) {
642 OP *op = cUNOPo->op_first;
643 const indirect_op_info_t *oi = indirect_map_fetch(op);
644 const char *s = NULL;
648 if (oi && (s = oi->pos)) {
649 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
650 line = oi->line; /* Keep the old line so that we really point to the first */
653 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
655 sv = sv_mortalcopy(sv);
656 s = indirect_find(sv, PL_oldbufptr);
657 line = CopLINE(&PL_compiling);
660 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
661 /* o may now be a method_named */
663 indirect_map_store(o, s, sv, line);
668 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
670 indirect_map_delete(o);
674 /* ... ck_entersub ......................................................... */
676 STATIC int indirect_is_indirect(const indirect_op_info_t *moi, const indirect_op_info_t *ooi) {
677 if (moi->pos > ooi->pos)
680 if (moi->pos == ooi->pos)
681 return moi->len == ooi->len && !memcmp(moi->buf, ooi->buf, moi->len);
686 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
688 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
689 SV *code = indirect_hint();
691 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
694 const indirect_op_info_t *moi, *ooi;
700 lop = (LISTOP *) oop;
701 if (!(lop->op_flags & OPf_KIDS))
704 } while (oop->op_type != OP_PUSHMARK);
705 oop = oop->op_sibling;
711 switch (oop->op_type) {
722 if (mop->op_type == OP_METHOD)
723 mop = cUNOPx(mop)->op_first;
724 else if (mop->op_type != OP_METHOD_NAMED)
727 moi = indirect_map_fetch(mop);
728 if (!(moi && moi->pos))
731 ooi = indirect_map_fetch(oop);
732 if (!(ooi && ooi->pos))
735 if (indirect_is_indirect(moi, ooi)) {
743 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
745 file = sv_mortalcopy(CopFILESV(&PL_compiling));
750 mPUSHp(ooi->buf, ooi->len);
751 mPUSHp(moi->buf, moi->len);
756 call_sv(code, G_VOID);
769 STATIC U32 indirect_initialized = 0;
771 STATIC void indirect_teardown(pTHX_ void *root) {
774 if (!indirect_initialized)
782 ptable_free(MY_CXT.map);
784 ptable_hints_free(MY_CXT.tbl);
787 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_old_ck_const);
788 indirect_old_ck_const = 0;
789 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
790 indirect_old_ck_rv2sv = 0;
791 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_old_ck_padany);
792 indirect_old_ck_padany = 0;
793 PL_check[OP_SCOPE] = MEMBER_TO_FPTR(indirect_old_ck_scope);
794 indirect_old_ck_scope = 0;
795 PL_check[OP_LINESEQ] = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
796 indirect_old_ck_lineseq = 0;
798 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_old_ck_method);
799 indirect_old_ck_method = 0;
800 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_old_ck_entersub);
801 indirect_old_ck_entersub = 0;
803 indirect_initialized = 0;
806 STATIC void indirect_setup(pTHX) {
807 #define indirect_setup() indirect_setup(aTHX)
808 if (indirect_initialized)
813 MY_CXT.tbl = ptable_new();
816 MY_CXT.map = ptable_new();
817 MY_CXT.linestr = NULL;
819 indirect_old_ck_const = PL_check[OP_CONST];
820 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
821 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
822 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
823 indirect_old_ck_padany = PL_check[OP_PADANY];
824 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
825 indirect_old_ck_scope = PL_check[OP_SCOPE];
826 PL_check[OP_SCOPE] = MEMBER_TO_FPTR(indirect_ck_scope);
827 indirect_old_ck_lineseq = PL_check[OP_LINESEQ];
828 PL_check[OP_LINESEQ] = MEMBER_TO_FPTR(indirect_ck_scope);
830 indirect_old_ck_method = PL_check[OP_METHOD];
831 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
832 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
833 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
836 call_atexit(indirect_teardown, aTHX);
838 call_atexit(indirect_teardown, NULL);
841 indirect_initialized = 1;
844 STATIC U32 indirect_booted = 0;
846 /* --- XS ------------------------------------------------------------------ */
848 MODULE = indirect PACKAGE = indirect
854 if (!indirect_booted++) {
857 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
859 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
860 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
861 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
879 ud.tbl = t = ptable_new();
880 ud.owner = MY_CXT.owner;
881 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
885 MY_CXT.map = ptable_new();
886 MY_CXT.linestr = NULL;
891 level = PerlMemShared_malloc(sizeof *level);
893 LEAVE_with_name("sub");
894 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
895 ENTER_with_name("sub");
904 RETVAL = indirect_tag(value);